1

The iniramfs is like a small os which mount root file system and hand over control to systemd. So, how does initramfs work internally? Is there any scripts executed to mount the root in linux?

  • like this wiki.gentoo.org/wiki/Custom_Initramfs#Init (read the intro for explanation) – frostschutz Oct 10 at 11:48
  • 1
    This is too broad. Basically, if initramfs is used, it becomes the very first filesystem mounted (the / of the init mount namespace). Then kernel would execute the init program in it (from specific path), which is the PID=1 of the init pid namespace. As such, the useespace of the OS begins. Mounting the real on-disk root filesystem and pivot_root to it is just a common user-defined behavior of most linux OS, but it's not a must(not a definite behavior of the kernel), because initramfs can be fully customized. – 炸鱼薯条德里克 Oct 10 at 11:57
3

After that initrd/initramfs (only historical difference) is extracted and mounted, it is the script /init that is run. Mostly it only mounts the real root with help of the modules from the initramfs.

And then it has to pivot/switch root to that new partition, a thing that is only possible as pid 1.

Here is a minimal /init script:

mkdir /newroot
mount /dev/sda2 /newroot
mount --move /sys /newroot
mount --move /dev /newroot
mount --move /proc /newroot

switch_root /newroot /bin/bash

(or /sbin/init for a normal continuation)

The thing is the kernel cannot mount /dev/sda2 without the necessary modules built in. Having the modules on sda2 does not help: egg-and-hen problem.

So the kernel gets a cpio archive with a root filesystem containing modules and scripts. The kernel simply hands over to /init and is done with it's own startup.


That gentoo link starts with:

initramfs is a root filesystem that is embedded into the kernel...

What should that mean?


This is a minimal kernel command line:

vmlinuz root=/dev/sdaX

telling the boot loader to load kernel "vmlinuz" and telling the kernel to mount /dev/sdaX.

And this:

vmlinuz initrd=initramfs.img root=/dev/sdaX

makes the boot loader also load an initrd, which the kernel will find and mount as root, and tells the (default) /init script to pivot/switch to /dev/sdaX (and then run default /sbin/init).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.