28

I was making some changes to /etc/fstab, when this chicken and egg question occurred to me - if /etc/fstab contains the instructions for mounting the file systems, including the root partition, then how does the OS read that file in the first place?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
sashoalm
  • 5,760
  • 11
  • 32
  • 47

2 Answers2

22

When the boot loader calls the kernel it passes it a parameter called root. So once the kernel finished initializing it will continue by mounting the given root partition to / and then calling /sbin/init (unless this has been overriden by other parameters).

Then the init process starts the rest of the system by loading all services that are defined to be started in your default runlevel.

Depending on your configuration and on the init system that you use, there can be multiple other steps between the ones that I mentioned. Currently the most popular init systems on Linux are SysVInit (the traditional one), Upstart and Systemd. You can find more details about the boot process in this wikipedia article.

Here is a simplified example of my Grub config. The important part to answer your question is on the second to last line, there is a root=/dev/sda3:

menuentry 'Gentoo GNU/Linux' --class gentoo --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-40864544-2d0f-471a-ab67-edd7e4754dae' {
    set root='hd0,msdos1'
    echo    'Loading Linux 3.12.6-gentoo-c2 ...'
    linux   /kernel-3.12.6-gentoo-c2 root=/dev/sda3 ro  
}

In many configurations the kernel mounts / in read-only mode and all the rest of the options are set to the defaults. In /etc/fstab you might specify file system parameters which would then be applied once init remounts it.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
replay
  • 8,483
  • 1
  • 26
  • 31
  • 5
    The root partition is initially mounted read-only by the kernel. An `init` process then mounts the things in `/etc/fstab` according to the parameters there, which usually means re-mounting the root partition read-write. – goldilocks Jan 04 '14 at 14:20
  • 1
    Also kernel can be compiled with hardcoded routines that looks for some special files on the active partition that is not mounted already. FreeBSD loader works that way. – Kondybas Jan 04 '14 at 14:37
  • @Kondybas LILO on Linux is also hardcoded to load the kernel at some fixed block numbers – phuclv Jun 16 '17 at 04:12
  • 1
    Depending on the stage of init, `/` is technically the initramfs, whereas the root filesystem is mounted at `${rootmnt}`. `${rootmnt}` is moved to `/` only after init, therefore init is actually reading `${rootmnt}/etc/fstab` rather than `/etc/fstab`. – DustWolf Sep 06 '20 at 11:51
  • @DustWolf, on my system with systemd `/` is pointing to root of "main" drive partition **before** `init` is started. – Martian2020 Oct 28 '21 at 14:50
  • An initramfs is not always required. It is possible to boot directly into the rootfs's init if the root= kernel parameter is set. An initramfs (and initrd= parameter) is usually needed if additional preparation is needed before rootfs's init can be called (for example, if the rootfs is encrypted and needs to be decrypted before rootfs's init can be accessed). – nishanthshanmugham Dec 28 '21 at 22:55
4

An entry in fstab is needed, if you want to specify some non-default mount options. However, nowadays with systemd, a correct kernel device and fstype in fstab are unncesessary. You can replace the root entry with something like:

#UUID=8f74237d-b689-4beb-9d1f-f60b426c9969 /            ext4        rw,relatime,data=ordered    0 1
dummy /             auto        rw,relatime,data=ordered,debug  0 1

and the mount options are still honored by systemd.

You can use any bad device name, e.g. /dev/sdz1, except for bad UUID. With a bad UUID the message will be printed at boot: Failed to start Remount Root and Kernel File Systems, but the system boots anyway.

basin
  • 1,931
  • 4
  • 20
  • 36