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.
1 2 3 4 5 |
$ ls -l /dev/ram* brw-rw---- 1 root disk 1, 0 Feb 8 2015 /dev/ram0 brw-rw---- 1 root disk 1, 1 Feb 8 2015 /dev/ram1 brw-rw---- 1 root disk 1, 2 Feb 8 2015 /dev/ram2 brw-rw---- 1 root disk 1, 3 Feb 8 2015 /dev/ram3 |
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.
1 2 |
# mknod -m 660 /dev/ram0 b 1 0 # chown root:disk /dev/ram0 |
Now, create a directory to mount the RAM disk. Let’s use /mnt/ramdisk
.
1 |
# mkdir -p /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.
1 |
# mount -t ramfs -o size=4M,maxsize=4M /dev/ram0 /mnt/ramdisk |
Now, let’s now try to create a 16MB file under /mnt/ramdisk
directory.
1 2 3 4 5 6 7 |
# dd if=/dev/zero of=/mnt/ramdisk/ddout.bin bs=1M count=16 16+0 records in 16+0 records out 16777216 bytes (17 MB) copied, 0.00970789 s, 1.7 GB/s # ls -l /mnt/ramdisk total 16384 -rw-r--r-- 1 root root 16777216 Feb 7 21:11 ddout.bin |
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.
1 2 |
# mkfs /dev/ram0 4M # mount /dev/ram0 /mnt/ramdisk |
Let’s try to create a 16MB file on this partition using dd
.
1 2 3 4 5 |
dd if=/dev/zero of=ddout.bin bs=1M count=16 dd: writing `ddout.bin': No space left on device 4+0 records in 3+0 records out 4009984 bytes (4.0 MB) copied, 0.00967182 s, 415 MB/s |
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.
1 |
/dev/ram0 /mnt/ramdisk ramfs size=4M |
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
.
1 2 3 |
mkfs -t ext2 -q /dev/ram0 4M [ ! -d /mnt/ramdisk ] && mkdir -p /mnt/ramdisk mount /dev/ram0 /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.