How to Create a Ramdisk in Linux

50

A ramdisk is a volatile storage space defined in the RAM memory. Using this feature increases file processing performance ten times over the best SSD hard disks. Implementing a ramdisk is very advantageous for users whose tasks require significant amounts of hardware resources. In addition, media editors and gamers can enjoy this implementation.

A ramdisk is a volatile space, all information stored in it will be lost if the device is turned off or reboots.

In Linux, ramdisks can be created using the command mount and the filesystems tmpfs and ramfs. This tutorial shows how to create a ramdisk in Linux using both of them.

Tmpfs and Ramfs:

Tmpfs: Tmpfs is a temporary file system stored in the RAM memory (and/or swap memory). By specifying this file system with the argument -t of the command mount, you can assign limited memory resources to a temporary file system. As a result, applications stored in this filesystem will perform several times faster than they would on conventional storage devices, including cssd devices.

Ramfs: Ramfs is similar to Tmpfs, but the user can’t ensure a limit, and the allocated resource grows dynamically. If the user doesn’t control the ramfs consumption, ramfs will keep using all the memory until hanging or crashing the system.

Tmpfs vs. Ramfs: There is no notable difference between the performance of tmpfs and its predecessor ramfs. The reason behind ramfs being replaced by tmpfs is the unlimited RAM consumption risk by ramfs which may lead to a system crash.

Another advantage of tmpfs over ramfs is the ability to use the swap space while ramfs are limited to hardware memory.

How to Create a Ramdisk in Linux Using Tmpfs:

First, let’s see the free memory we can use before creating a tmpfs mount point. To check the available ram and swap, you can use the command free. To print the results in gigabytes, you can add the argument –giga, as shown in the example below:

As you can see in the output above, I have two physical GB and two on the swap space.

Now, create a mount point under the directory /mnt using the command mkdir as shown in the example below. The mount point name choice is arbitrary. If you are going to use the ramdisk for a specific application, you can name the mount point after it. In the example below I call it /mnt/tmp:

Now you can create the ramdisk using the mount command. The following example shows how to create a ramdisk using tmpfs on 2GB Ram/Swap, on the mount point /mnt/tmp.
The -t (type) argument allows to specify the file system (in this case, tmpfs). The -o (options) argument is used to define the space for the ramdisk.

mount -t tmpfs -o size=2g tmpfs /mnt/tmp

The ramdisk was created under /mnt/tmp.

SSD vs. Tmpfs:

I copied an Ubuntu image from a user’s home directory to the root directory in the following screenshot.

Using the command time to display timing, you can see the copying process took 0:55.290s

time cp /home/linuxhint/Downloads/ubuntu-20.04.1-desktop-amd64.iso .

In the following screenshot, you can see how copying the same Ubuntu iso image to the ramdisk takes 0:9.424s:

time cp ubuntu-20.04.1-desktop-amd64.iso /mnt/tmp/

As you can see, the difference is titanic, and the ramdisk is very advantageous for tasks with large amounts of file writing.

To remove the ramdisk, just unmount it by running the following command and replacing tmp with your mount point:

Creating a Ramdisk in Linux Using Ramfs:

The procedure to create a ramdisk using ramfs is the same as with tmpfs. The following command will create a dynamic ramdisk on the mount point /mnt/tmp.

mount -t ramfs -o size=4 ramfs /mnt/tmp

Tmpfs vs. Ramfs:

Now let’s test the ramfs performance against tmpfs, and let’s see what happens when each ramdisk type reaches the defined limit.

In the first example, I will create a 2GB ramdisk using tmpfs, and I will try to copy a bigger iso inside:

mount -t tmpfs -o size=2g tmpfs /mnt/tmp
time cp ubuntu-20.04.1-desktop-amd64.iso /mnt/tmp

As you can see, the cp returned an error because the ramdisk space isn’t enough for the iso image. I only assigned 2GB for the ramdisk.

Now, see what happens when I do the same procedure using ramdisk:

mount -t ramfs -o size=2g ramfs /mnt/tmp
time cp ubuntu-20.04.1-desktop-amd64.iso /mnt/tmp

As you can see, the ramfs kept writing into /mnt/tmp even though I have defined a 2GB limit. This is ramfs disadvantage because it may hang a system by consuming all its RAM memory. On the contrary, tmpfs is limited to the memory amount we define.

You can also see in the output that the copying task was done within 0:9.624s, almost the same performance shown by tmpfs in the test against SSD.

Note: The same iso image was used.

Conclusion

Creating a ramdisk is a one-minute process with significant benefits for any user who needs to process big files. The reading and writing speed increase exponentially over the best hard disks in the market. Portable software can be executed from a ramdisk, though changes won’t be persistent. This implementation is being highly appreciated by media editors whose tasks require long periods of media conversion.

Using ramfs may be risky if the system runs out of resources. That’s why tmpfs became the first method.

I hope this tutorial to create a ramdisk in Linux was useful. Keep following Linux Hint for more Linux tips and tutorials.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More