
RAMDisks in Linux

Today we will discuss the topic of keeping files, especially temporary ones, in the memory of the operating system. This is a rarely discussed topic, however, which can significantly increase the efficiency and / or security of some solutions. Most word administrators RAMDisk, often written simply as ramdisk, rightly associated with files initramfswhich are placed in the […]
Today we will discuss the topic of keeping files, especially temporary ones, in the memory of the operating system. This is a rarely discussed topic, however, which can significantly increase the efficiency and / or security of some solutions.
Most word administrators RAMDisk, often written simply as ramdisk, rightly associated with files initramfswhich are placed in the boot directory / partition.
[Alex@Citadel ~]$ ll /boot/initramfs-* | head -2 -rw-------. 1 root root 73562106 Nov 14 2018 /boot/initramfs-0-rescue-d83e252f8d4a4e838692e59dbf0d7a9f.img -rw-------. 1 root root 33280664 Nov 23 20:06 /boot/initramfs-3.10.0-1062.4.3.el7.x86_64.img
These files are mounted as a file system in memory, i.e. de facto (RAMDisk), and contain the software necessary for further system startup during its boot. There is, among others, a stripped down init (in newer versions of Enterprise Linux stripped down version of systemd). After the system is prepared, the program is called switch_root
which means, among other things, that the existing (initramfs) RAMDisk is released from memory (recursively deleted) and the system works in the new main directory tree.
In this article, however, we will look at the concept of RAMDisk in a much broader perspective. Finally, we will also carry out a short benchmark.
At the end of the introduction, it is worth mentioning that the concept of RAMDisk itself is written in at least 4 ways:
- RAMDisk (this way of saving can be found e.g. on the AMD website)
- RAM Disk (this way of saving can be found e.g. on Wikipedia)
- ramdisk (this way of writing is naturally used in commands, but also appears in documentation and in articles)
- tmpfs – this entry appears in articles on strictly linux / unix systems (e.g. BSD) and is both a collective name for the RAMDisk idea and one of its implementations.
I will use the name RAMDisk to keep the article read.
What is RAMDisk?
As I mentioned, RAMDisk is a file system stored in memory. Most Linux users have unknowingly several RAMDisks running on their system.
[Alex@Citadel ~]$ df -h | grep tmpfs devtmpfs 7.7G 0 7.7G 0% /dev tmpfs 7.8G 107M 7.7G 2% /dev/shm tmpfs 7.8G 2.4M 7.8G 1% /run tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup tmpfs 1.6G 44K 1.6G 1% /run/user/1000
All these RAMDisks were created automatically without the user’s participation. What’s more, they don’t have their entries in fstab. More about automatically created RAMDisks can be found later in the article.
Advantages of using RAMDisk:
- speed – is the fastest file interface we can have
- flexibility – creating RAMDisk does not require any allocation. So you can create overcommit, i.e. report and share more resources than you actually have. In the example above, we have RAMDiski for a total of about 32 GB, although the system has 16 GB of RAM
- data temporality – after turning off the computer or unmounting the RAMDisk (and overwriting or deleting data) is irretrievably lost.
Disadvantages of using RAMDisk:
- use of „expensive ” RAM. If processes in the system do not consume a significant part of memory, RAMDisk can speed up the operation of selected operations and processes. Please note that Linux works best when it has the ability to store data in RAM. Taking this memory to use RAMDisk can finally slow down the system
- data temporariness – limits the number of applications to data from which we are reconciled with any sudden loss.
As a curiosity, it should be noted that some configurations of unix systems, especially popular in the case of Solaris, mount by default /tmp
as tmpfs. As a result, in theory, they don’t need additional work to delete temporary files. However, this is done at the expense of using RAM and potentially filling the system’s memory (both RAM and swap) with unnecessary temporary files.
Types of RAMDisks on Linux
RAMDyski in Linux can be divided at least due to:
- implementations (type)
- creation and destiny.
First, let me discuss RAMDisk implementations in Linux:
- RAMDisk type
ramfs
. It is a relatively simple mechanism that creates a synthetic file system. It uses the fact that the kernel uses cache / caching mechanisms when reading or writing a file. Files and directories saved toramfs
they also enter their data into cache (or dentry cache for directories), but they do not save their data on the device. This approach eliminates, inter alia, the use of a file system and the need to preallocate resources. His ills include:- inability to control size
- writeability until memory is exhausted physical
- memory pages used by ramfs cannot be stored in swap memory
- in the absence of memory in the system, OOM Killer (Out-Of-Memory) will be started, but the memory used by RAMDisk will not be released until it is unmounted
- for the above-mentioned reasons, it should not be used by disadvantaged users;
- RAMDisk type
tmpfs
tmpfs was created on the basis of ramfs and is its more extensive version. Using tmpfs, you get, among others, control over the maximum size and the ability to switch from physical memory (RAM) to extended memory (swap). Thanks to this, the system can better manage memory taking into account real needs. However, if we care about increased security and that data from RAMDisk have not been saved to disk using the swap mechanism, do not use it - RAMDisk type
ramdisk
. The RAMDisk type is no longer used. Creates a synthetic block device. Uses a real file system and is inefficient. It requires the preallocation of some resources, and in addition duplicates many operations. He was pushed out by ramfs - RAMDisk type
cramfs
andsquashfs
. The last type of RAMDisks are compressed RAMDiski, allowing only for reading (read-only) data contained therein. They allow free access to compressed data that is decompressed „on flight ”. Due to the difficulty of adding new data to an already compressed image / file, they are used only for reading. For example, squashfs is used, among others, in „live ” images of Linux distributions, distribution for OpenWRT routers or Google Chromecast.
The next proposed division of RAMDisks is the division by the user creating RAMDisk with its use.
1. Ramdiski created automatically by the system:
- shm – / dev / shm implements shared memory ideas. This space is used, among others, by system calls from the family shm_open and shm_unlinkwhich are part of the POSIX standard
- devtmpfs – RAMDisk, in which Linux creates, among others, device files
- cgroups – RAMDisk created for cgroup version 1. It has individual cgroup controllers.
2. user-created RAMDisk:
- RAMDisk created for processes that intensively use I / O operations, while data held in RAMDisk do not have significant value. As a rule, a type RAMDisk is used
tmpfs
. An example of use is compiling a program or building packages / packages. Thanks to the use of RAMDisk, the building process can be even an order of magnitude faster. If the computer loses power, we do not incur any losses, as these processes are easily repeatable - RAMDisks used to keep secrets. As a rule, ramfs type RAMDisk is used. After turning off the computer, the secrets contained in the RAMDisk (e.g. GPG key, certificate key, ssh keys, bitcoin wallet key) cease to be available.
Use /dev/shm
RAMDisk /dev/shm can be used in any way. By default, it does not require any additional permissions, although restrictions are imposed on secured systems. In the example below, a soft link is created in the home directory softlink), which is then used to store information in RAMDisk.
[User@SpaceStation ~]$ ln -s /dev/shm/ my_ramdisk [User@SpaceStation ~]$ df -h | grep shm tmpfs 7.8G 86M 7.7G 2% /dev/shm [User@SpaceStation ~]$ dd -if=/dev/zero -of=my_ramdisk/4GB_PLIK -count=1024 -bs=4M dd: invalid option -- 'i' Try 'dd --help' for more information. [User@SpaceStation ~]$ dd if=/dev/zero of=my_ramdisk/4GB_PLIK count=1024 bs=4M 1024+0 records in 1024+0 records out 4294967296 bytes (4.3 GB) copied, 1.06584 s, 4.0 GB/s [Alex@SpaceStation ~]$ df -h | grep shm tmpfs 7.8G 4.1G 3.7G 53% /dev/shm
It should also be remembered that the RAMDisk used in this way allows reading data by other program users by default (after all, it is shared memory).
Creating RAMDisk tmpfs
tmpfs
is an advanced RAMDisk mechanism allowing, among others:
- restrictions on the size, number of inode entries or memory blocks
- setting default access rights and user
- setting the memory allocation policy due to NUMA.
In the example below, a 4GB file was saved on 6 GB RAMDisk:
[root@Normandy ~]# mount -t tmpfs tmpfs /mnt -o size=6G,uid=1000,gid=1000 [root@Normandy ~]# free -m total used free shared buff/cache available Mem: 15777 2287 8922 329 4567 12848 Swap: 7935 0 7935 [root@Normandy ~]# dd if=/dev/zero of=/mnt/4GB_zero bs=4M count=1024 1024+0 records in 1024+0 records out 4294967296 bytes (4.3 GB) copied, 1.34219 s, 3.2 GB/s [root@Normandy ~]# free -m total used free shared buff/cache available Mem: 15777 2285 4818 4426 8673 8753 Swap: 7935 0 7935
The location occupied by the file is reported in the „buff/cache ” column. In the case of a loaded system, some memory pages could be saved to the swap memory.
Both command df
how and mount
returns information about RAMDisk.
[root @ Normandy ~] # df -h | grep mnt tmpfs 6.0G 4.0G 2.0G 67% /mnt [root @ Normandy ~] # mount | grep mnt tmpfs on /mnt type tmpfs (rw, relatime, seclabel, size = 6291456k, uid = 1000, gid = 1000)
In order to free up memory space, which results in the loss of data on RAMDisk, it should simply be unmounted. It is worth remembering that some data, if it is in the swap memory, can still be saved to disk. For performance reasons, the kernel does not overwrite the data of the released memory pages.
[root @ SpaceStation mnt] # umount /mnt/ [root @ Normandy ~] # free -m total used free shared buff/cache available Mem: 15777 3248 7663 481 4865 11736 Swap: 7935 0 7935
Creating RAMDisk ramfs
Because ramfs doesn’t have no options mounting, creating a ramfs RAMDisk is as easy as:
mount -t ramfs ramfs /point/of/mount
In the example below, a 4GB file was saved on RAMDisk:
[root @ SpaceStation mnt] # mount -t ramfs ramfs /mnt [root @ SpaceStation mnt] # free -m total used free shared buff/cache available Mem: 15815 3580 6381 4198 5854 7703 Swap: 7999 1326 6673 [root @ SpaceStation mnt] # dd if = /dev/null of=/mnt/4GB_zero bs = 4M count = 1024 1024 + 0 records in 1024 + 0 records out 4294967296 bytes (4.3 GB) copied, 0.913067 s, 4.7 GB/s [root @ SpaceStation mnt] # free -m total used free shared buff/cache available Mem: 15815 3580 2275 4198 9959 7703 Swap: 7999 1326 6673
As you can see, the place taken by the 4 GB file was reported in the „buff / cache ” column. This is in line with the ramfs description that it uses the kernel caching mechanism, but without saving data to the physical device.
Then note that the ramfs RAMDisk is not included in the output of the command df -h
.
[root@SpaceStation mnt]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 7.8G 0 7.8G 0% /dev tmpfs 7.8G 4.2G 3.7G 54% /dev/shm tmpfs 7.8G 18M 7.8G 1% /run tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup /dev/dm-2 230G 184G 46G 81% / /dev/nvme0n1p2 1014M 398M 617M 40% /boot /dev/nvme0n1p1 200M 9.9M 190M 5% /boot/efi tmpfs 1.6G 48K 1.6G 1% /run/user/1000
Fortunately, the command mount
includes RAMDiski ramfs.
[root @ SpaceStation ~] # mount | grep ramfs none on / mnt type ramfs (rw, relatime)
Similarly to tmpfs, in order to free up memory space, which results in the loss of data on RAMDisk, it should simply be unmounted.
[root @ SpaceStation mnt] # umount /mnt/ [root @ SpaceStation mnt] # free -m total used free shared buff/cache available Mem: 15815 3583 6377 4198 5855 7700 Swap: 7999 1325 6674
At the very end it is worth mentioning that the RAMDisk created in this way can only be used by the root user by default.
Creating your own RAMDisk at system startup
To create RAMDisk automatically when the system starts, enter it in the / etc / fstab file.
Sample entry creating 18 GB RAMDisk type tmpfs:
tmpfs /mnt tmpfs size = 18G
Short comparison of RAMDisk speed with NVMe disks
The following comparison was created on two local machines that have the following parameters:
Host 1 (SpaceShip) | Host 2 (SpaceStation) | |
---|---|---|
Computer | Thinkpad T480 | There was a station to play the author. |
CPU | Intel (R) Core (TM) i7-8550U | Intel (R) Core (TM) i7-6700K |
Memory: | 2 x Samsung 8GB M471A1K43CB1-CRC DDR4-2400 | 2 x G.SKILL 16GB Ripjams V DDR4 3200MHz |
Disk: | Samsung SSD 970 EVO 500GB | SAMSUNG PM961 MZVLB256HAHQ-000L7 |
Kernel: | 5.5.0-1.el7.elrepo.x86_64 | 3.10.0-1062.12.1.el7.x86_64 |
System: | EuroLinux 7 | EuroLinux 7 |
As you can see, the differences in hosts are significant. In addition, Host 2 has a much weaker disk, which, worse, is largely full (SSDs usually lose over 80% of their fill). However, it has a faster processor and memory. To the detriment of Host 1 is the fact that the disks working in the ThinkPads from the T480 series are connected only by two PCIe lines. Attention should also be paid to differences in kernel versions.
For testing, I used the gobonnie program, which is a reimplementation of the bonnie program. I would like to point out that this test should not be considered as an interpretation. This is not a detailed case study with recommendations. Finally, it is worth mentioning that the system kernels were not significantly tuned.
Disk tests were carried out using the following command:
./gobonniego -dir /mnt/ -size 32.0 -runs 3
A 32 GB sample selection is not accidental. This is because it is twice the operating memory, which reduces the impact of the file storage mechanism by the kernel.
For RAMDisk tests, a 9GB RAMDisk type tmpfs was created mounted in /mnt
. Then the command was called gobonnie
with the following parameters:
./gobonniego -dir /mnt -size 8.0 -runs 3
The average results are as follows:
Host 1 (SpaceShip) | Host 2 (SpaceStation) | |
---|---|---|
Average write speed (Disk) | 382 MB / s | 163 MB / s |
Average reading speed (Disk) | 1570 MB / s | 1036 MB / s |
Average number of IOPS (Disk) | 98245 | 46478 |
Average write speed (RAMDisk – tmpfs) | 16268 MB / s | 15328 MB / s |
Average reading speed (RAMDisk – tmpfs) | 22094 MB / s | 22018 MB / s |
Average IOPS (RAMDisk – tmpfs) | 2023803 | 1929915 |
As you can see:
- recording can be up to 100 times faster
- the reading speed is on average about 10-15 times higher
- the amount of IOPS in this case is 20 to 40 times higher.
End
As usual, I would like to thank you for the time spent reading this article. I hope you have learned a lot of new information about RAMDisks. The more that they can really speed up the work of selected sentences, and their idea is simply interesting.
Bibliography
man 8 mount
https://www.kernel.org/doc/Documentation/filesystems/tmpfs.txt
https://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt
https://lwn.net/Articles/330985/
http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html