Linux: Create RAM Disk Filesystem

Objective: Create a Linux filesystem or partition using RAM for extremely fast access.

RAM disk (ramfs) is a temporary file storage facility on many Unix / Linux operating systems. It is intended to appear as a mounted file system, but stored in volatile memory instead of a persistent storage device. RAM drive appears as a virtual disk drive and hosts a disk file system. It is most often used when you require extremely fast IO speed. One of its uses is to provide storage space for web cache, eg. Nginx Microcaching.

Create RAM disk block device

Before using a ram disk, check for the existence of the ram block devices.

If you do not see any ram block devices then you will need to create a new ram block device and change the ownership permissions. To create a new /dev/ram0 block device, use the following syntax.

Now, create a directory to mount the RAM disk. Let’s use /mnt/ramdisk.

Mount RAM Disk Block Device

One downside of ramfs is that it will grow dynamically and you can keep writing data into it until you fill up all memory. So, you need to make sure that the processes that writes data to the ramfs partition only use a fixed amount of RAM space. Confusing? Let’s look at an example.

Let’s mount /dev/ram0 to /mnt/ramdisk and specify the filesystem size to 4MB. The filesystem in use is ramfs. Take note that the maxsize parameter is of no use actually.

Now, let’s now try to create a 16MB file under /mnt/ramdisk directory.

Do you see the problem? We specified a size of 4MB and a maxsize of 4MB, but the filesystem grew to 16MB without any issues. To force the disk usage of the ramfs device to stay within the specified limit, we will need to change the way we format and mount the device.

Format the RAM device using mkfs (default is ext2 partition) and mount it. A journaling filesystem like ext3 or ext4 is not required.

Let’s try to create a 16MB file on this partition using dd.

This time, dd stopped after writing 4MB of data. This mounting approach is safer.

Mount RAM Disk During Boot

If you want to mount the RAM disk during boot time, you can enter the details in /etc/fstab file. This approach is only applicable if you are are mounting the device using ramfs filesystem. This means that you cannot specify the size limit of the filesystem. To mount a RAM disk with an initial size of 4MB, add the following line to the /etc/fstab file.

If you want to limit the filesystem size, you cannot define the mount point in /etc/fstab file. Instead, you will need to append the following shell code to the /etc/rc.local file. The code below will create a 4MB RAM disk filesystem and mount it under /mnt/ramdisk.

RAMFS and TMPFS difference

tmpfs is supported by the Linux kernel from version 2.4. tmpfs (also known as shmfs) is based on ramfs code and is used during bootup and also uses the page cache, but unlike ramfs it supports swapping out less-used pages to swap space as well as filesystem size and inode limits to prevent out of memory situations (defaulting to half of physical RAM and half the number of RAM pages, respectively). ramfs, in contrast, does not make use of swap.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }