In Linux distributions there are two file systems that allow you to create a file system based on a part of RAM and that can be mounted and used as a normal directory, writing or read from any regular partition. But since writing/reading is made directly from RAM, the speed will be higher, even a few times higher than a normal SSD.
What is a RAM disk?
Basically, a RAM-based file system is something that creates storage in memory as if it were a partition on a disk – it’s called a RAM disk. Note that RAM is volatile and data is lost at system restart or after a crash.
The most important benefit of RAM drives is their speed – even over 10 times faster than SSDs. These very fast storage types are ideal for applications that need speed and fast cache access. Repeat: Data written in this type of file system is NOT persistent, so keep in mind that it is lost on reboot – not even a backup can restore it.
ramfs versus tmpfs
The two RAM-based filesystems are ramfs and tmpfs. ramfs is an older version and has been replaced everywhere by tmpfs. Basicaly, the two file systems do the same, with a few minor differences:
• ramfs grow dynamically – which means we need to make sure that written data does not exceed the physical memory of the system. If we have a system with 8 GB of RAM and we have created a 4 GB ramfs file system mounted in /mnt/ram. When the total size of the data written here reaches 4 GB, we can continue to write data here – the system will not stop writing data when it reaches 4GB. However, when it reaches 8GB (maximum physical memory), the system may be blocked because there is no more space in RAM to hold data;
• tmpfs do not grow dynamically – it will not be allowed on this type of file system to write a quantity of data larger than the size specified when assembling it. So, we do not have to worry that we will have no RAM in the system – the only error we can get is the one that announces that there is no disk space anymore;
• tmpfs uses swap memory – if the system is out of RAM (for reasons other than the tmpfs partition), the files in the tmpfs partition will also be written to the swap (if it exists) and will be read from the disk when accessed, like any data that uses swap;
• ramfs does not use swap memory.
Since these 2 file systems are volatile, we need to make periodic disk scratches if we want to keep some data processed here.
Create and mount tmpfs
Being the most used RAM based file system at present, the example I will give you will refer strictly to tmpfs (mounting the ramfs will be done in the same way).
• we create a directory (a mount point) for the future tmpfs
# mkdir /mnt/tmp
• we actually mount some of the RAM (1 GB in the example below) at the mount point above using mount command (mount -t [TYPE] -o size=[SIZE] [FSTYPE] [MOUNTPOINT])
# mount -t tmpfs -o size=1G tmpfs /mnt/tmp
• then we use findmnt command (or mount) to see all mount points in the system:
# findmnt /mnt/tmp tmpfs tmpfs rw,relatime,seclabel,size=1048576k
• with the df -h command we can find out more about this partition:
# df -h /mnt/tmp
Filesystem Size Used Avail Use% Mounted on
tmpfs 1.0G 0 1.0G 0% /mnt/tmp
From this moment we can start writing and reading files on this new partition.
The tmpfs file system can be mounted automatically at the boot by adding an entry to the /etc/fstab file like this example:
tmpfs /mnt/tmp tmpfs nodev,nosuid,noexec,nodiratime,size=1024M 0 0