There may be instances where you need to include the fastest possible storage you can find on a server. In some cases, the best route to that is by making use of a ramdisk. Effectively, a ramdisk takes a portion of your system memory and uses it as a disk drive. This method of storage is considerably faster than standard hard disk storage, so it is a great tool for when you need blistering speed on a specific app.
Ramdisks, of course, come with a serious caveat. Should you lose power (or shut down the machine), whatever you’re working on could be lost. Because of that, it is important to do a regular backup of the directory used for your ramdisk (more on that in a bit).
With that said, let’s create a ramdisk. I’ll be working with Ubuntu 16.04, but this will work on nearly any distribution.
Creating the ramdisk directory
The first thing you must do is create a folder that will be used to mount the ramdisk. I’ll create the folder /media/ramdisk. To do that, open up a terminal window and issue the command:
sudo mkdir -p /media/ramdisk
You can name that folder whatever you like and place it anywhere on the directory structure. I like /media because it is the same location other drives will be mounted into by default.
Mounting the ramdisk
Now we actually mount the newly created directory to a temporary storage area (one that will use RAM as opposed to hard drive space). This is accomplished with the following command:
sudo mount -t tmpfs -o size=2048M tmpfs /media/ramdisk
You can adjust both the size and the mount point to fit your needs. In the above example, I have mounted 2GB of RAM to be used as a temporary file system to /media/ramdisk. That mounted directory can now be used at your discretion.
When you’re done using the ramdisk, you can unmount it with the command:
sudo umount /media/ramdisk
Automounting the ramdisk
What if you want to have the ramdisk automatically created at boot? This can be done with the help of /etc/fstab. Open up that file and add the following (edit to suit your needs):
none /media/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=2048M 0 0
Save and close that file. You can test the newly modified /etc/fstab file with the command mount -a. If you receive no warnings, you’re good to go.
Backup that ramdisk data
Because we’re dealing with non-persistent memory, you’re going to want to set up a regular backup. You could create a very simple bash script with the following contents:
âcp -ru /media/ramdisk /BACKUP/PATH
Where /BACKUP/PATH is a path to a location to house the backup of /media/ramdisk. Save and close that file (we’ll name it /root/ramdisk_backup.sh). Give the backup script executable permissions with the command chmod u+x ramdisk_backup.sh. Next we must create a crontab entry. Issue the command sudo crontab -e and then add the following:
*/15 * * * * /root/ramdisk_backup.sh
The above crontab entry will backup your ramdisk data every fifteen minutes. Now, should you lose power or have to reboot the machine, you won’t lose data.
Use it wisely
How you use your ramdisk is up to you. Make sure to use this type of non-persistent storage wisely. The last thing you want is to depend upon it, only to lose precious data, thanks to a black out. If used wisely, a ramdisk can be a serious benefit to your data center servers. If used poorly, well, I’m sure you know how that story ends.
Subscribe to the Data Insider Newsletter
Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more. Delivered Mondays and Thursdays