Course Info / Syllabus

Labs

Lecture Topics

Exam Reviews

Source Code & Other Resources


Operating Systems Concepts & Design

How the Root Filesystem is Mounted

The process of loading the root file system (/) is a coordinated handoff between the hardware, the bootloader, and a temporary mini-operating system called the initramfs.

Because the kernel cannot read the hard drive until it has the proper drivers—and the drivers are usually stored on the hard drive—Linux uses a “bootstrap” method to bridge the gap.


1. The Kernel and the Initramfs

Once the bootloader (usually GRUB) loads the Linux kernel into memory, it also loads a compressed archive called the initramfs (Initial RAM File System).


2. Finding the “Real” Root

Inside the initramfs, a script (often named init) runs. Its job is to find the physical partition that contains the actual / file system. It uses parameters passed from the bootloader, such as:

The kernel uses its built-in drivers or the modules loaded from the initramfs to “see” the hardware devices and identify the correct UUID.


3. Mounting the Superblock

Once the device is identified (e.g., /dev/sda1), the kernel performs a mount operation:

  1. Read the Superblock: The kernel reads the “magic block” at the start of the partition to identify the filesystem type (Ext4, XFS, etc.) and its geometry.
  2. Verify Integrity: It checks the “clean” flag in the superblock. If the system crashed previously, it might trigger a filesystem check (fsck) at this stage.
  3. Root Inode: The kernel locates Inode #2 (the standard inode for the root directory /) to begin mapping the file tree.

4. The “Switch Root” (The Great Handoff)

This is the most critical step. The system is currently running off the RAM-based initramfs, but it needs to transition to the disk-based filesystem.


5. Starting Init (systemd)

Now that the real / is mounted, the kernel looks for the first user-space program to run, typically located at /sbin/init (which is usually a symbolic link to systemd).

Systemd then takes over, reading /etc/fstab to mount all other filesystems (like /home or /var) and starting the background services (daemons) that make the system functional.


Summary of the “Chain of Trust”

Component Responsibility
Bootloader Loads Kernel and Initramfs into RAM.
Initramfs Provides the drivers to “talk” to the hard drive.
Kernel Reads the Superblock and mounts the physical partition.
switch_root Swaps the temporary RAM-disk for the real disk.
systemd Starts the OS services from the real disk.

When is fstab used?

While the kernel and the bootloader handle the “magic” of mounting the root (/) filesystem, /etc/fstab (the Filesystem Table) comes into play immediately afterward during the User Space initialization.

Think of it as a two-stage process: the kernel gets the “foundation” (the root) ready, and then systemd (or your init system) uses fstab to build the rest of the “house.”


1. The Timing: Post-Switch Root

The system cannot read /etc/fstab until the root filesystem is already mounted.

  1. Kernel/Initramfs: Mounts / (the root) using the root= parameter from the bootloader.
  2. The Handoff: The system performs the switch_root to the real disk.
  3. The Read: Once the real disk is /, the init process (systemd) finally has access to the file path /etc/fstab.

2. How systemd Uses fstab

Modern Linux systems use systemd, which doesn’t just “read” the fstab file; it converts it.


3. The Mounting Sequence

Once the generators have finished, the system starts the local-fs.target. This is the point in the boot sequence where:

  1. Integrity Check: The system looks at the pass column (the 6th field) in fstab to see if it needs to run fsck on those partitions.
  2. Mounting: It executes the mount commands for all entries that do not have the noauto option.
  3. Remote Filesystems: Later in the boot process, after the network is up, it reaches the remote-fs.target and mounts things like NFS or SMB shares defined in fstab.

4. What happens if fstab is wrong?

Because fstab is processed so early, errors can be critical:


Summary Table: / vs. fstab

Feature Root (/) Mounting fstab Mounting
Triggered By Kernel / Initramfs systemd (Init System)
Source of Info Bootloader (GRUB) cmdline /etc/fstab file
Target Only the Root partition /home, /var, /tmp, swap, etc.
Error Result Kernel Panic (VFS: Unable to mount) Emergency Mode / Maintenance Shell

Making entries in /etc/fstab

Creating an entry in /etc/fstab requires a specific six-field syntax. Each field tells the system exactly how to handle the device during the boot process.

Before you start, you need the UUID (Universally Unique Identifier) of the partition. While you can use device names like /dev/sdb1, these can change if you plug in a new drive. UUIDs are permanent.

Step 1: Get the Partition UUID

Run the following command to find the UUID and the filesystem type (TYPE) of your target partition:

lsblk -f

Take note of the UUID string (e.g., 550e8400-e29b-41d4-a716-446655440000) and the FSTYPE (e.g., ext4).


Step 2: The fstab Syntax

An entry consists of six fields separated by tabs or spaces:

Field Purpose Example
1. Device The UUID or device path UUID=1234-abcd-...
2. Mount Point Where the folder lives in / /mnt/backup
3. Filesystem The format of the partition ext4
4. Options Mount behavior (read/write, etc.) defaults
5. Dump Used by backup tools (rarely used) 0
6. Pass Order for fsck disk check 2

Step 3: Common Options (Field 4)

While defaults works for most cases, you can customize behavior:


Step 4: Adding the Entry

  1. Create the mount point: sudo mkdir -p /mnt/my_data
  2. Open fstab: sudo nano /etc/fstab
  3. Add your line at the bottom: ```text UUID=your-uuid-here /mnt/my_data ext4 defaults,nofail 0 2


4. **Save and Exit:** (In Nano, press `Ctrl+O`, `Enter`, then `Ctrl+X`).

---

### Step 5: Test Without Rebooting (Critical)

**Never reboot immediately after editing fstab.** If there is a typo, your system might fail to boot.

Run this command to test the configuration:

```bash
sudo mount -a

Viewing initramfs and the Boot Loader (GRUB)

To view these components, you have to peel back the layers of the boot process. One lives as a compressed archive on your /boot partition, and the other is a configuration file that dictates what you see on your monitor before Linux even starts.


1. Viewing the Initramfs

The initramfs is usually a compressed CPIO archive located in /boot. You cannot simply cat it; you need to decompress it or use a specialized tool to peek inside.

The Easy Way: lsinitramfs

Most Debian/Ubuntu-based systems include a utility that lists the contents without manual decompression.

```

The Manual Way (Extracting)

If you want to actually read the scripts inside, you can extract it to a temporary folder:

  1. Create a workspace: mkdir /tmp/initrd && cd /tmp/initrd
  2. Extract: unmkinitramfs /boot/initrd.img-$(uname -r) .
  3. Explore: You can now cd into the folders and use cat to read the init script that handles the “switch root” process.

2. Viewing the Bootloader Menu (GRUB)

The “menu” you see at boot is generated by GRUB. While the active configuration is a complex script, the human-readable source is much easier to digest.

The “Source” Configuration

On modern Linux systems, you should look at /etc/default/grub. This file controls the menu timeout, the default OS, and kernel parameters (like root=UUID...).

The “Active” Compiled Script

The actual file GRUB reads at boot is /boot/grub/grub.cfg.


3. Testing Changes Safely

If you decide to modify your fstab or your GRUB configuration, you can verify your work before a reboot “breaks” the system:


Summary Checklist

Component Primary Location Command to View
Initramfs /boot/initrd.img-... lsinitramfs
GRUB Settings /etc/default/grub cat
GRUB Menu Logic /boot/grub/grub.cfg less