RAM disk is a virtual disk drive that’s mounted in your RAM. And because it’s located in RAM (Random Access Memory), the main benefit is its staggering speed. In situations like video rendering or editing and gaming etc, by having your files put inside a RAM disk can greatly speed things up, provided that you have a large amount of RAM.
RAM disks can also be used not only to deliver better performance but to offload (read/write requests) the main storage device, thus prolonging its life as well (specially in a server environment). But for the average computer user having a RAM disk might not make a huge difference, still, if you frequently do things like compress large number of files etc, then you too can benefit by having a ram disk because if the files are in RAM, they can processed quite fast.
Anyhow, for whatever reasons, if you’re in need of a ram disk managing software for GNU/Linux, then this guide is intended for you. For setting things up, you’ll have to use the command-line a bit, but I assure you that this is extremely easy, if you follow my instructions.
You don’t have to be a ‘linux expert’ to do any of this. If you know how to run a command with administrative privileges and know how to copy and paste commands from a webpage to your terminal window, then you already have all the qualifications!. If all you need a RAM disk, then you don’t even have to follow all the steps either.
The ‘Basic Setup‘ should work any on GNU/Linux distribution, however, the steps laid out in the ‘Enhancing the Functionality‘ will only work on Ubuntu 15.04 & later versions (I’ve tested this on Ubuntu 15.10), though, with minor changes (or maybe without any changes at all) they should work on other newer distributions, as long as they use systemd (a core part of ‘Linux’ that manages applications that run in the background, aka ‘daemons’).
Step 1: Let’s a create folder for the RAM drive. I’ve chosen to create it in the ‘/mnt’ directory, but you can create it anywhere you like (if you do that however, then make sure to change the folder path accordingly). I’ll call this folder ‘RAM_disk’ which will be mounted into the RAM. To create the folder, open up the terminal and enter the below command (When asked, enter the root or the administrator’s password):
sudo mkdir /mnt/RAM_disk
Step 2: Now let’s open the ‘/etc/fstab’ configuration file (this file contains all the file systems that should be mounted upon the operating system’s boot-up) using the default text editor of Ubuntu (it’s called Gedit), but first, let’s make a backup using the below command:
sudo cp /etc/fstab /etc/fstab.bak
Now use the below command to open the original file:
sudo gedit /etc/fstab
Step 3: Now let’s add an entry to the end of this configuration file so that the ‘/mnt/RAM_disk’ folder is mounted every time the operating system boots. Copy the below code and paste it in a new line, at the end of the ‘/etc/fstab’ configuration file (shown below):
tmpfs /mnt/RAM_disk tmpfs rw,size=1G,x-gvfs-show 0 0
Using this code, we can easily define the desired size of the RAM disk, and as you might have guessed, I’ve highlighted that section of the code in Red (size=1G – Note that there’s no space between the number ‘1‘ and the letter ‘G‘). Here I’ve set the size to 1 GB. If you want to set it to 2 GB, then change ‘1’ to ‘2’, as simple as that. If you want to define it in Megabytes, say you want it to be 500 Megabytes, then the code should be: size=500M , where the ‘G‘ is replaced by ‘M‘ (and the number too). Again, pretty intuitive right?
P.S: After making your changes, make sure to save the changes, before closing the text editor.
Step 4: Now enter the below command to mount the newly created ram disk:
sudo mount -a
As soon as you run this command, if it doesn’t popup automatically, open your file manager and you should see on its side panel, ‘RAM_disk’ has an entry which you can use to open it just like any other mounted media. Well’ that’s it!.
Enhancing the functionality…
Now, one of the main disadvantages of a RAM disk is that it can only hold data temporarily, in the sense that, if a power failure occurs or if you shutdown or reboot the computer, the data of the RAM disk gets lost, permanently.
Luckily, this can easily be overcome (to a certain degree) by making the operating system save the data on the RAM disk to your actual hard disk, before shutting down or rebooting, and also making it restore the data when booting-up (this step though is to make the user’s life easy). Unfortunately, this will slow down the boot-up speed of your operating system (depending on the size of the data that needs to be loaded to the RAM drive), though not necessarily the shutdown speed.
That’s because the utility that copies the data (the one I’ve used here) is a quite smart one as once it’s done loading the data to the RAM drive (even if it’s as big as couple of Gigabytes) during system boot-up, when shutting down it only saves files & folders that have been changed. In other words, even if you had a RAM disk filled with big files, if no changes had occurred to the files, then the utility (unlike the conventional file copy tools) will not save any data to the hard disk!. But if lots of files have been altered (say a few hundreds of Megabytes of new data has been written into them), then obviously it will affect the shutdown speed negatively.
Anyhow, if you don’t have a UPS or a battery, even that method of only saving the data on the RAM drive to the hard disk when shutting down won’t provide much protection against a sudden power failure, if the data in the RAM disk gets changed quite often. In such a situation, as a countermeasure, we can make the OS save the RAM disk’s data to the hard disk, once every minute.
This too however, as explained above, can be costly because writing a big chuck of data (again, assuming such changes have occurred) to your hard disk once every minute is going to negatively affect the responsiveness of the operating system.
I honestly don’t think most users would ever need to go to this extreme, thus if all you want is to load the data to the RAM drive when booting and then save it back when shutting down, then you can follow from ‘Step 1‘ to ‘Step 5‘ below, and skip the rest.
So if you’re ready, let’s begin.
Step 1: Let’s first create a folder where the RAM disk’s data gets saved to. I’ll create it in the same ‘/mnt/’ directory as earlier, but again, you can create this anywhere you like:
sudo mkdir /mnt/RAM_disk_backup
Step 2: Now just like we previously did, let’s give the your preferred user the ability to read, write and execute the content of this folder. Enter the below command for that (remember to replace ‘gayan‘ with the proper username):
sudo setfacl -m user:gayan:rwx /mnt/RAM_disk_backup
Step 3: Now let’s create a configuration file containing simple instructions telling the operating system to save data from the RAM disk when turning off (and rebooting) and also to restore it back when booting-up. We’ll call the file ‘ram_disk_load_save.service’ and we’ll create it in ‘/lib/systemd/system/’ (mandatory location) folder.
To make it happen, open a terminal window and enter the below command (again, when asked, enter the administrator’s or the ‘root’ password):
sudo gedit /lib/systemd/system/ram_disk_load_save.service
This should open up the text editor.
Step 4: Now copy the whole code below, and paste it into the text editor window (make sure to replace ‘gayan‘ with the correct username):
Description=A script that save and restore the data on the RAM disk
ExecStart=/usr/bin/rsync -ar /mnt/RAM_disk_backup/ /mnt/RAM_disk/
ExecStop=/usr/bin/rsync -ar /mnt/RAM_disk/ /mnt/RAM_disk_backup/
Then click on the ‘Save’ button to save the changes and exit the text editor.
Step 5: Now let’s make the OS load this configuration file every time it boots-up so these instructions get executed. For that, enter the below command:
sudo systemctl enable ram_disk_load_save.service
Now simply reboot the computer for the change to take effect. And as mentioned above, you can stop here if you like, though, if required, we can further enhance the integrity of your data on the RAM disk by making the OS save those data every minute, and please keep in mind the drawback I mentioned.
Step 6: For this task we’re going to use a built-in task scheduler utility of GNU/Linux called ‘cron’. ‘cron’ includes a sub program called ‘crontab’ that enables users to easily add (& edit) new entries to its scheduled tasks list. So if ready, open a new terminal window and enter the below command to add a new entry for the RAM disk:
Now you’ll be asked (as shown below) which editor you prefer to use, simply hit the Enter key.
Step 7: Then scroll-down (use the Down-Arrow on your keyboard) and on a new line, copy and paste the below code:
* * * * * rsync -ar /mnt/RAM_disk/ /mnt/RAM_disk_backup/
Once done, press Ctrl + O keys and when asked hit Enter, and then again press Ctrl + X keys to exit.
Well, now you’re done!, good luck.