Initramfs and fstab – Does Initramfs Use /etc/fstab?

bootfstabinitramfs

The way I understand it, initramfs is responsible for loading the "real" root filesystem.

Now, there are two places where we define that root. First we put an entry in /etc/fstab. Second, we put the device on the kernel boot commands e.g. root=/dev/sda1.

Which one does initramfs use to determine where is the root filesystem? If it uses the root kernel parameter, why do we have an entry in /etc/fstab? The second option, (it reads /etc/fstab), is quite illogical because the /etc/fstab file is on the very root device that initramfs is trying to mount in the first place.

Very confusing stuff.

Best Answer

As you stated, the purpose of initramfs is to get the "real" root filesystem mounted (it can do other things too, but this is the common task).

Without an initramfs, the kernel will normally mount a partition up as read-only and then pass control over to /sbin/init. An initramfs just takes over this task from the kernel, usually when the root filesystem isn't a normal partition (mdraid, lvm, encrypted, etc).

Now, aside from the background on initramfs, your /etc/fstab resides on your root filesystem. As such, when initramfs is launched, that root filesystem isn't there, and so it can't get to the fstab (chicken and egg problem).
Instead we have to pass a parameter into the kernel boot arguments for the initramfs to use. Normally this is something like root=/dev/sdX. However it might also do something to automatically figure out where your root device is, and so there's no parameter at all. Since it's just software (generally a script), it can really do anything it wants for mounting the root device.

Now, as stated earlier, the kernel will mount the real root as read-only. The initramfs should do exactly this. Once the initramfs is done, the system proceeds booting exactly as if there were no initramfs at all, and /sbin/init starts up. This init then starts all your normal boot scripts, and it's the job of one of these scripts to read /etc/fstab, switch root to read-write, and mount all your other filesystems.

Related Question