📜 ⬆️ ⬇️

Linux file system entirely on tmpfs - speed without compromise

Prehistory


It so happened that for five years my ntfs partition with the Windows operating system is located on the ramdisk. It was decided not by hardware, but in a purely software way, accessible on any PC with a sufficient amount of RAM: a ramdisk is created using the grub4dos bootloader, and Windows recognizes it using the firadisk driver.

However, until recently, I did not know how to implement this for Linux. No, of course, there are a huge number of Linux LiveCDs that are loaded into memory using the kernel options toram, copy2ram, etc., but this is not exactly that. First, these are compressed file systems, usually squashfs, so any reading from them is accompanied by unpacking overhead, which is detrimental to performance. Secondly, it is a rather complicated cascade mount system (since squashfs is a read-only system, and for the operation of the OS, a record is needed), but I wanted as simple as possible, which can “take and transform like that” Linux is bootable entirely in ram.

Below I will describe this method, which was successfully tested. For the experiments was taken the most distinguished distribution of Linux - Debian.

I used the network installation, from the very minimum set:
http://ftp.nl.debian.org/debian/dists/unstable/main/installer-amd64/current/images/netboot/debian-installer/amd64/linux
http://ftp.nl.debian.org/debian/dists/unstable/main/installer-amd64/current/images/netboot/debian-installer/amd64/initrd.gz
')
But since the installation of Debian is not the subject of this article, I will not describe it in detail.

Such a choice is generally dictated by the fact that there is never a lot of RAM and there is no intention of keeping something huge like KDE in it. After installing the programs necessary for the work on the hard disk, one and a half gigabyte was used. Installation was carried out in one section, without section swap. The RAM on the computer is 16 gigabytes.

Actually, the way


1. In the / usr / share / initramfs-tools / scripts / local file, comment out the line:
checkfs $ {ROOT} root
and line:
mount $ {roflag} -t $ {FSTYPE} $ {ROOTFLAGS} $ {ROOT} $ {rootmnt}
and immediately after it we insert the following text:

mkdir / ramboottmp
mount $ {roflag} -t $ {FSTYPE} $ {ROOTFLAGS} $ {ROOT} / ramboottmp
mount -t tmpfs -o size = 100% none $ {rootmnt}
cd $ {rootmnt}
tar -zxf /ramboottmp/ram.tar.gz
umount / ramboottmp

2. Run the mkinitramfs -o /initrd-ram.img command
and after it has completed, we will return the / usr / share / initramfs-tools / scripts / local file to its original state.

3. In the / etc / fstab file we comment out the line describing the mounting of the root partition / and insert the following line:
none / tmpfs defaults 0 0

4. Download some other Linux from the LiveCD to completely get rid of the operating system under test,
and archive the entire partition with its file system:
cd / mnt / first && busybox tar -czf /mnt/work/ram.tar.gz *
after the end, we will return the / etc / fstab file to its original state.

5. As a result, we have got Linux, consisting of only three files:
Kernel, initrd-ram.img and ram.tar.gz. The location of ram.tar.gz is specified in the root = kernel parameter in the grub bootloader menu:
title Linux in RAM
kernel / vmlinuz root = / dev / sdb1
initrd /initrd-ram.img

This is all instruction. Required Comments:
- we will comment out checkfs because there is no such fsck to check tmpfs, did not write it;
- we use busybox tar to create an archive instead of a simple tar, because there is no simple tar in the initrd, we will unpack our archive exactly busybox, and there is such a bug that it will not be able to unpack;
- the asterisk in the command line is not terrible, since there are usually no hidden files and folders in the root, and they are archived in directories.
- / mnt / first is the mounted partition with the OS under test, and / mnt / work / is the partition for the archive.

How it works?


We made a special initrd, which, when loaded, creates a root file system like tmpfs (this is all salt, since it is located in RAM), then looks at the root = section specified in the option, takes an archive file whose name is hardcoded (ram. tar.gz), and unpacks from it the whole FS tree to this tmpfs.

So FS is in memory.

Moreover, tmpfs has the advantageous differences from ramdisks (including the one I use for Windows) - it is not a block device, but a file system, it takes up space in the memory exactly as long as the files are occupied, and dynamically increases, if you install something, new files, and reduced if you uninstall software, delete files. The remaining memory is available for the operating system, programs. And Linux understands that it is ALREADY memory and does not need to be cached. Wonderful thing!

Benefits


Yes, of course, caching in modern OS partially solves the problem of poor performance of disk devices, but it still takes time to first read the file from the disk, and it can also be unloaded from the cache at any time and then it will take time to reread it. Placing the entire OS in memory is an uncompromising decision, guaranteeing the highest possible speed of reading and writing its files. The simplest test using dd demonstrates 3 gigabytes per second for sequential reading and 2 gigabytes per second for sequential writing:

dd if = / dev / zero of = / test bs = 1M count = 500
524288000 bytes (524 MB) copied, 0.268589 s, 2.0 GB / s

dd if = / test of = / dev / null bs = 1M count = 500
524288000 bytes (524 MB) copied, 0.167294 s, 3.1 GB / s

This is about 30 times faster than HDD, and 8 times faster than SSD.

An advanced test using fio demonstrates iops 349059 with random reading and complete latency of 0.29 microseconds (the latency is two to three (decimal) ORDERS less than the SSD):

randread 4K fio

In work


The output of the free command in a typical working situation:

total used free shared buffers cached
Mem: 16469572 3236968 13232604 2075372 65552 2687436

Immediately after loading about 2 gigabytes of memory is used, of which 1.5 is occupied by the file system. With 16 gigabytes of RAM, there is plenty of room for installing even large applications like LibreOffice or Blender. The size of the ram.tar.gz file is approximately half a half, which allows storing it, the kernel and the initrd on any small flash drive or on a CD. Hard disk may not be at all. Such a system is not killed. But the main thing is, of course, the speed of work.

In conclusion, a thirty-second screencast on the actual speed of launching applications in such a system. No, this is not the opening of applications from the tray, this is the launch of programs from the media, which in this case is tmpfs:

Source: https://habr.com/ru/post/253759/


All Articles