Linux file system completely on tmpfs - speed without compromise

Background


It so happened that for five years my ntfs section with the Windows operating system has been located on ramdisk. This was decided not by hardware, but by purely software means, accessible on any PC with enough RAM: the ramdisk is created using the grub4dos bootloader, and Windows recognizes it using the firadisk driver.

However, until recently, I did not know a way to implement this for Linux. No, of course, there are a huge number of Linux LiveCDs that are loaded into memory using the toram, copy2ram, etc. kernel options, but this is not quite right. Firstly, these are compressed file systems, usually squashfs, so any reading from them is accompanied by unpacking overhead, which is detrimental to performance. Secondly, it’s a rather complicated cascade mount system (since squashfs is a read-only system, and for the OS to function, you need a record), but I wanted the simplest way possible, which can be “taken and turned like this” on any installed on the hard drive Linux bootable entirely in RAM.

Below I will describe a method that has been successfully tested. For experiments, the most deserved Linux distribution, Debian, was taken.

I used a 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 installing Debian is not the subject of this article, it is not necessary to describe it in detail will.

This choice is generally dictated by the fact that there is never a lot of RAM and it was not supposed to hold something huge like KDE in it. After installing the necessary programs for work on the hard disk, one and a half gigabytes was taken. Installation was carried out in one partition, without a swap partition. RAM on the computer installed 16 gigabytes.

Actually, the way


1. In the file / usr / share / initramfs-tools / scripts / local, comment out the line:
checkfs $ {ROOT} root
and the 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 command mkinitramfs -o /initrd-ram.img
and after it works , return the file / usr / share / initramfs-tools / scripts / local to its original state.

3. In the file / etc / fstab, 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 LiveCD to completely get rid of the tested operating system,
and archive the entire section with its file system:
cd / mnt / first && busybox tar -czf / mnt / work / ram .tar.gz *
after the end, we will return the file / etc / fstab to its original state.

5. As a result, we 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 the whole instruction. Required Comments:
- checkfs comment because there is no such fsck for checking tmpfs, did not write it;
- we use busybox tar to create an archive instead of a simple tar due to the fact that initrd does not have a simple tar, busybox will unpack our archive, and there is such a bug that it cannot unpack;
- an asterisk on the command line is not scary, since usually there are 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 placing the archive.

How it works?


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

So the FS is in memory.

Moreover, tmpfs has favorable differences from ramdisks (including the one I use for Windows) - it is not a block device, but a file system, it takes up as much memory space as files take, and it will dynamically increase if you install something, write new files, and decreases if you uninstall software, delete files. The remaining memory is available for operating systems and programs. And Linux understands that it is ALREADY memory and it does not need to be cached. Wonderful thing!

Benefits


Yes, of course, caching in modern operating systems partially solves the problem of low performance of disk devices, but it still takes time for the first reading of the file from disk, and it can also be unloaded from the cache at any time and then it will take time to read it again. Placing the entire OS in memory is an uncompromising solution that guarantees the highest possible speed of reading and writing its files. The simplest test with dd shows 3 gigabytes per second for sequential reads and 2 gigabytes per second for sequential writes:

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 an HDD, and 8 times faster than an SSD.

An advanced test with fio shows iops 349059 with random reading and a complete latency of 0.29 microseconds (latency is two to three (decimal) ORDER less than that of 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

Right after loading, about 2 gigabytes of memory are used, of which 1.5 is occupied by the file system. With 16 gigabytes of RAM, there is plenty of room to install even large applications like LibreOffice or Blender. The file size ram.tar.gz is about half a gig, which allows you to store it, the kernel and initrd on any small flash drive or CD. There may not be a hard drive at all. Such a system is indestructible. 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 opening applications from the tray, this is launching programs from the media, which in this case is tmpfs:


Also popular now: