07

September
2012

initrd versus initramfs

As Wikipedia nicely describes, initrd (initial ramdisk) is a scheme for loading a temporary file system into memory in the boot process of the Linux kernel. Initrd and initramfs refer to slightly different methods of achieving this. Both are commonly used to make preparations before the real root file system can be mounted, but there is a difference.

Initrd is a fixed-size block device, which requires to be 'formatted' by a filesystem such as ext2. It sits on /dev/ram0 by default, and cannot be enlarged or shortened.

On the other hand, initramfs is a cpio archive which is simply unpacked during boot to ramfs memory. This memory is of dynamic size and thus can be shortened or enlarged as needed.

So initramfs seems better, right? Not necessarily. The main problem is that it doesn't support pivot_root. What pivot_root does? It basically 'switches' from initrd to the new root (unioned data structures mounted from CD/USB), but initramfs somehow is not capable of that. It seems there was some circular reference and kernel developers disabled pivot_root for initramfs at all. And without pivot_root we cant transpose the current root with the new one. But I want to keep using initramfs.

Currently I'm able to (almost) accomplish the goal by mounting union over tmpfs, and then rbind mounting the tmpfs over a subdirectory in union. Seems strange, but works :) The only part which is left are the initramfs tools (busybox etc), I'd like to have those accessible as well in the chrooted environment. This will probably require copying, which I'm trying to avoid, so I'll keep digging ...

User comments
jm 2012-09-07 11:30

It will suffer some file redundancy, but what about including those tools in pre-squashed bundle ?

Tomas M 2012-09-07 20:18

The main reason I want to keep the tools is to use them during shutdown when all squashed bundles are unmounted. So it can't be unmounted with the others squashed bundles.

The user 'fanthom' actually warned me about this and it seems he was right - most likely it's not possible to cleanly shutdown the system if its started from initramfs. This is no problem for RAM-only Slax, but is a big problem if it's started from a writable drive such as USB.

So actually we have to find a way how to do clean shutdown from initramfs, or else switch back to initrd.

jm 2012-09-07 23:39

Just an idea, not tested or seen anywhere.
It is probably unnecessary complicated, but what about separate squashed bundle loaded from small enough ramX drive, instead of bundle loaded usb? That ram drive, as I understand it, has no need to be actually unmounted by shutdown script(if it is possible the script to be instructed, to skip unmount for that kind of device or filesystem), and in that case, this drive will be created especially for that purpose - to hold those same tools for shutdown. Unfortunately this will occupy some RAM space. I suspect that with zram that you listed as your solution for ram drives, it will be unnecessary to use squashfs compression, but then again, copying will be in order and you don't like it :(.