Linux – How does the Linux or Unix “ / ” get mounted during bootup

filesystemslinuxmount

Devices can be mounted to a path. For example "/dev/sda1" can be mounted to "/home/user". What I don't understand is how and where the " / " is mounted during boot.
Any help explaining?

Best Answer

During the boot of a Unix system, the kernel does a few things that it doesn't do during normal operation. One of these things is to mount a filesystem on the directory /; this is quite different from normal mounting operations since the mounting is not triggered of a mount system call, and the target directory is not an existing directory. Another thing is to execute a program as PID 1, which is different from normal operation since this creates a process without duplicating an existing process.

The way this “magic” mounting of the root directory is very different in different Unix variants. The kernel chooses what device to mount based on configuration parameters that may be specified in a variety of ways: compile-time configuration, runtime configuration in the kernel image, runtime configuration in some predefined memory location, command line parameters, … To find how it works on your machine, you would need to look at the documentation of your Unix variant, and find how your machine is configured.

To give an idea of how it works, here's an overview of how a modern Linux kernel operates. This is not the simplest example because Linux has a lot of history and varied use cases.

  • Linux can start with a “special” filesystem attached to the path /, which consists of files stored in RAM. This special filesystem is called initramfs; it's an instance of the rootfs filesystem type. The initramfs is populated by content either passed by the bootloader through an architecture-dependent protocol, compiled directly into the kernel image that is loaded into memory by the bootloader.
  • Alternatively, Linux can mount a device to / that is part of a restricted (but large) set of volume types that are recognized by the initialization code in the kernel. Such device types include any filesystem on common types of partitions on common types of disks (anything vaguely SCSI-like, including ATA, USB, etc.), as well as RAM disks and NFS mounts.
  • Depending on which path was taken, the initial root filesystem may later be shadowed or replaced by another. Shadowing is what happens with an initramfs, and that's how most desktop and server systems operate (embedded systems, on the other hand, often have a hard-coded root filesystem). Replacement is what happens with an initrd, which is a particular kind of RAM disk. The job of the initramfs or initrd is to load the drivers that provide the “real” root filesystem that will get used in normal operation.
Related Question