How to Create a Ramdisk in Linux?

Learn via video courses
Topics Covered

A ramdisk is a kind of storage space that is defined in RAM and provides high-speed file processing. It provides a significant performance boost, often surpassing even the fastest SSD hard disks. A Linux ramdisk can be quite useful for individuals who perform resource-intensive tasks. It is particularly useful for media editors and gamers. Since ramdisks are volatile storage space, all data stored on them is erased when the device is powered off or restarted. In Linux, ramdisks can be created by using the mount command and either the tmpfs or ramfs file systems.

Tmpfs:

Tmpfs is a temporary file system that permits the creation of temporary storage spaces in RAM. It is especially useful for handling temporary files and directories, offering quick access and excellent performance for read and write operations. Tmpfs allows you to establish size limitations and numerous mount options for customization. By using the -t option with the mount command, you can dedicate a certain amount of memory resources to establish this file system.

Ramfs:

Ramfs is also a temporary file system that resides in RAM. It is similar to Tmpfs, except that the user cannot set a limit and the allocated resource grows dynamically. If the user fails to control ramfs usage, ramfs will consume all available memory until the system hangs or crashes.

Let us start the creation of a Linux ramdisk. The following steps can be used not only for Ubuntu 16.04 but also for almost any Linux distribution.

Creating Directory

To create a Linux ramdisk, you need to first create a directory where the ramdisk will be mounted. In this case, let us create the folder /media/ramdisk. Open a terminal window and type the following command to do this:

creating directory

You can name the folder whatever you want and put it anywhere in the directory structure. I like /media since it is a similar location where other disks will be mounted by default.

Mounting and AutoMounting

To mount the newly created directory to a temporary storage area (one that will use RAM rather than hard drive space), use the following command:

mounting

The size and mount point can be adjusted according to your requirements. In the above example, we have allocated 2GB of RAM to create a temporary file system and mounted it to /media/ramdisk. After that, you are free to use the mounted directory as needed.

When you're done utilizing the Linux ramdisk, use the following command to unmount it:

unmounting

You can use the /etc/fstab file to automate the creation of the Linux ramdisk on boot. Open the /etc/fstab file and add the following entry:

After adding the entry to the /etc/fstab file, save and close the file. To test the modified /etc/fstab file, use the `mount -a command. If you don't get any warnings, you're good to proceed.

Data Backup

Since we are utilizing non-persistent memory, it is important to set up regular backups. A simple bash script can be created with the following content:

In the above script, "/BACKUP/PATH" denotes the path where the backup of "/media/ramdisk" will be saved. After finishing the script, save and close the file, naming it "/root/ramdisk_backup.sh". Use the command "chmod u+x ramdisk_backup.sh" to make the backup script executable. Next, create a crontab entry by running "sudo crontab -e" and add the following entry:

The above crontab entry ensures that your ramdisk data is backed up every fifteen minutes. This means that your data will remain safe even if the power goes off or the machine reboots.

How to Use Linux Ramdisk?

To use a Linux ramdisk, go through the following steps:

  • Create a directory to serve as the mount point for the ramdisk, e.g., mkdir /media/ramdisk.
  • Mount the ramdisk with mount -t tmpfs -o size=SIZE tmpfs /media/ramdisk. Replace "SIZE" with the required ramdisk size (for example, 1G for 1 gigabyte).
  • The ramdisk is now mounted and ready for usage. To perform file actions, access it like you would any other directory.
  • Keep in mind that ramdisk data are stored in volatile memory and will be lost if the system reboots or the power goes off.
  • If you need to keep the data on the ramdisk, set up adequate backup mechanisms.
  • When you're done using ramdisk, run the command umount /media/ramdisk to unmount it.

The usage of a Linux ramdisk is entirely dependent on your choice and requirements. When utilized correctly, ramdisks may provide considerable benefits to your data center servers. However, if overused or improperly handled, the impact might be negative, possibly leading to data loss. It's important to remember that using a ramdisk consumes system memory, so keep an eye on your system's available memory and allocate an appropriate size for the ramdisk.

Conclusion

  • A Linux ramdisk is a kind of storage space that is defined in RAM and provides high-speed file processing.
  • Tmpfs is a temporary file system that is especially useful for handling temporary files and directories, offering quick access and excellent performance for read and write operations.
  • Ramfs is similar to Tmpfs in that the user cannot set a limit and the allocated resource grows dynamically.
  • Ramdisks in Linux can be created with the mount command and either the tmpfs or ramfs file systems.
  • The /etc/fstab file can be used to automate the creation of the Linux ramdisk on boot.
  • It’s important to remember that using a ramdisk consumes system memory, so keep an eye on your system’s available memory and allocate an appropriate size for the Linux ramdisk.