1. Overview
In this tutorial, we’ll learn how to place our files into RAM in Linux. Specifically, we’ll learn how to mount the RAM filesystem onto a directory to speed up I/O performance.
2. RAM Filesystem in Linux
In computing, random access memory (RAM) is a volatile memory and offers a much faster read-write speed than nonvolatile memory such as a hard disk drive (HDD) or solid-state drive (SSD). One downside of RAM is that the RAM will not persist the data across reboots.
Despite not being able to persist data permanently, the faster read-write speed offered by RAM is helpful for significantly improving the runtime of applications that do a lot of I/O. Therefore, it’s common to see cases where files are being placed onto a RAM disk to improve the performance of the application. For example, putting source code onto a RAM disk during the build phase will usually speed up the build time, given that there are a lot of I/O activities going on.
In Linux, there are two different types of RAM filesystems available: ramfs and tmpfs.
3. ramfs and tmpfs
Both ramfs and tmpfs are RAM-based file systems. In other words, data stored on both file systems will be on volatile memory and, therefore, will not persist across reboot.
On the other hand, there are several differences between ramfs and tmpfs. On a high level, ramfs are based on the kernel caching system’s mechanism, whereas tmpfs is a newer implementation that builds on top of ramfs to include more features. As a result, people tend to use tmpfs more than ramfs nowadays.
Additionally, the ramfs cannot be limited in size and will happily use up all the available memory. In contrast, the tmpfs offer a size limit configuration to impose a size limit on the directory. This feature of tmpfs is considered an enhancement over ramfs as it provides better system stability by ensuring no one process can take up the entire RAM.
Besides that, ramfs is not eligible for swap, whereas tmpfs may use swap space when the size grows. Depending on use cases, the fact that tmpfs can be swapped might not be desirable since swapping involves nonvolatile disk I/O. As a result, it regresses the overall I/O performance back to the disk-based memory.
Finally, the ramfs does not accept permissions bits parameters such as uid and gid. On the other hand, tmpfs accepts permissions parameters such as uid and gid to configure the permissions bits on the mounted directory.
3. Mounting ramfs to Directory Using mount Command
In most cases, we should prefer the tmpfs as it is the newer implementation and offers more configuration options. However, if we wanted a pure RAM filesystem that does not involve swap space at all, we should still use the ramfs.
Before we proceed, we must keep in mind that because no limit can be imposed on the ramfs, we must exercise caution to prevent the size of the mount from growing uncontrollably. If we fail to do that, we might risk starving critical processes from RAM resources and crashing the system.
3.1. Creating a ramfs Mount
To create a ramfs mount, we can run the command mount ramfs with the -t ramfs option followed by the target directory.
Let’s mount the ramfs onto the /ram-dir directory:
$ sudo mount ramfs -t ramfs /ram-dir
Note that the ramfs filesystem does not accept any other parameters, such as permission bits and size limits. Passing the options to the mount command will not result in any error, but the ramfs will simply ignore them.
Subsequently, any content we put into the /ram-dir will live in the physical RAM. One obvious effect we can observe right away is that the read-write speed on the /ram-dir will significantly increase. To perform the comparison, let’s move 1GB of data into a normal directory and time it:
$ dd bs=1M count=1024 if=/dev/zero of=/control/test conv=fdatasync
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.4594 s, 310 MB/s
The script above uses the dd command to create a 1GB file from /dev/zero. Then, it moves the file into the /control directory. We see that the write speed is 310 MB per second, which is what we can expect from an SSD.
Now let’s run the same command, but this time, we write the 1GB file into our ramfs mounted /ram-dir directory:
$ dd bs=1M count=1024 if=/dev/zero of=/ram-dir/test conv=fdatasync
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.244371 s, 4.4 GB/s
From the result, we can observe that the writing speed of the ramfs mount is a staggering 4.4GB per second, ten times the write speed of SSD!
4. Mounting tmpfs to Directory Using mount Command
To create a tmpfs mount, we use the command mount tmpfs followed by the option -t tmpfs. For instance, we can mount the tmpfs onto the /ram-dir directory as such:
$ sudo mount tmpfs -t tmpfs /ram-dir
Contrary to the ramfs, tmpfs offers several configurations we can tweak to impose size limits and set permission bits on the mount. Let’s see how we can configure the mount using the -o flag in the subsequent sections.
4.1. Limiting tmpfs Size
The tmpfs mount takes the -o size option to limit the size of the mount. For instance, we can limit our tmpfs to 100 Kb by specifying the option as -o size=100k:
$ sudo mount tmpfs -t tmpfs -o size=100k /ram-dir
The size parameters accept the suffixes k, m, and G, which stand for kilobytes, megabytes, and gigabytes, respectively.
To see the limit in action, we can try to move a file that’s larger than 100kb into the /ram-dir using the mv command:
$ ls -lh
total 704K
-rw-r--r-- 1 bob bob 703K Jan 21 07:07 700kbfile.txt
$ mv 700kbfile.txt /ram-dir/700kbfile.txt
mv: error writing 'ram-dir/700kbfile.txt': No space left on device
As expected, the mv command fails with “no space left on device” because the file we are trying to move has exceeded the size we’ve allocated for the mount.
4.2. Setting the UID and GID of Mount
The tmpfs mount also supports setting the permission bits on the mount through the -o uid and -o gid parameters. For instance, we can mount the ramfs onto the ram-disk directory and set its uid and gid to bob:
$ sudo mount tmpfs -t tmpfs -o uid=bob -o gid=bob /ram-disk
ls -hl
total 0
drwxrwxrwt 2 bob bob 40 Jan 21 08:00 ram-disk
Without the options, the default uid and gid will be set to the root user instead.
5. Conclusion
In this tutorial, we’ve briefly introduced the RAM filesystem in Linux. Particularly, we’ve highlighted the non-persistence nature of the RAM filesystem as well as the I/O speed boost it offers. Then, we’ve looked at the comparison between the ramfs and the tmpfs RAM filesystem.
Furthermore, we saw how we could mount the ramfs onto the directory using the mount command in Linux. Finally, we’ve repeated the same demonstration with the tmpfs.