Detailed Explanation of the Linux Boot Process and Key Interview Points

Many people can recite the rough steps of “BIOS -> GRUB -> Kernel -> init”, but when it comes to digging deeper, they often falter. Below, I will break down the Linux boot process in detail and highlight those easily overlooked but crucial core knowledge points, which are key to standing out in interviews.

Overview of the Linux Boot Process (Six Stages)

A complete Linux boot process can be clearly divided into the following six stages. We will start from a more macro perspective:

  1. Power-On Self-Test (POST)
  2. Boot Loader (Boot Loader)
  3. Kernel Initialization (Kernel Initialization)
  4. Initial RAM Disk (initrd / initramfs)
  5. Systemd / Init Process Initialization (System Daemon)
  6. Establish Terminal and User Login

Next, we will detail each stage one by one.

Stage One: Power-On Self-Test (POST)

  • Core Task: This is a hardware-level process managed by the BIOS or UEFI firmware on the motherboard. It checks whether key hardware components such as the CPU, memory, hard disk, and graphics card are functioning properly.
  • Interview Highlight:
    • BIOS vs. UEFI: This is the most important distinction. Traditional BIOS uses the MBR partition format, while modern UEFI uses the GPT partition format and requires a special EFI System Partition (ESP) to store the boot loader.UEFI boots faster, supports larger hard drives, and has higher security (supports Secure Boot).
    • Commonly Overlooked Point:After the POST is completed, the BIOS/UEFI will search for a bootable device according to the preset boot order (e.g., USB -> Hard Disk -> Network). Once found, it will read the first sector (512 bytes), which for hard disks is the Master Boot Record (MBR).

Stage Two: Boot Loader (Boot Loader)

  • Core Task: The MBR is very small (the first 446 bytes are boot code), and its main function is to load and execute the second stage boot loader, such as GRUB2.
  • Detailed Process (Using GRUB2 as an Example):
  1. First Stage of GRUB in MBR (boot.img):The first 446 bytes of the MBR. Its sole mission is to load the initial part of the core code that follows the MBR (usually located in the sector immediately after the MBR, called core.img).
  2. Second Stage of GRUB (core.img):The core.img contains the necessary drivers to access the file system (e.g., drivers for ext4, xfs). This allows GRUB to read the configuration and modules in the /boot/grub2/ directory.
  3. Loading Modules and Configuration Files:The core.img will load the necessary modules and then parse the /boot/grub2/grub.cfg configuration file, displaying an optional boot menu on the screen.
  4. Loading Kernel and initramfs: After the user selects (or times out to default), GRUB will load the kernel image (vmlinuz) and initial memory disk image (initramfs) into memory from the /boot/ directory.
  • Interview Highlight:
      • Why are two stages needed? Because the MBR is too small to hold the complete GRUB and file system drivers. The core.img serves as a “bridge” that allows GRUB to understand the /boot partition (which may be in ext4 format) and load the actual configuration and kernel.
      • When is initramfs loaded? It is loaded into memory by GRUB along with the kernel, not found by the kernel itself. This is often confused.
      • It is best not to manually edit the grub.cfg file, but to generate it using the grub2-mkconfig command based on the templates in /etc/default/grub and /etc/grub.d/.

    Stage Three: Kernel Initialization (Kernel Initialization)

    • Core Task: The kernel image in memory takes control. The kernel first unpacks itself and then performs the following initializations:
      • Initialize hardware: Detect CPU, memory, etc.
      • Set up memory management and scheduler.
      • Load built-in (compiled into the kernel) drivers.
    • Interview Highlight:
      • At this point, the kernel does not yet have the ability to access the root file system(/) ! This is because the root file system may be located on complex hardware (such as RAID, LVM, or network storage NFS), or may require special drivers (for example, the ext4 driver may not be built-in but exist as a module). This is the fundamental reason for the existence of the next stage, initramfs.

    Stage Four: Initial RAM Disk (initrd / initramfs)

    • Core Task: This is a temporary root file system used before the real root file system is mounted. It is an archive file in cpio format, loaded into memory.
    • Detailed Process:
    1. The kernel mounts the initramfs in memory as a temporary root file system.
    2. Execute the initialization script in initramfs (usually /init). The task of this script is:
    • Load the necessary kernel modules required to access the real root file system (such as ext4 driver, LVM driver, RAID driver, network drivers, etc.).
    • Prepare the real root device (for example, activate the LVM volume group).
  • The script will finally execute the switch_root operation, cleaning up the initramfs environment and mounting the real root file system to /, then jumping to the real root file system.
  • Interview Highlight (Core of the Core!):
      • Why is initramfs needed? Because the kernel may not have all the drivers required to access the root partition directly, and these drivers are stored as modules in the root partition itself. This creates a “chicken and egg” problem.initramfs solves this problem by containing the necessary tools and modules.
      • initrd vs initramfs:initrd is the older block device-based method, while initramfs is a superior memory file system-based method. Now it generally refers to initramfs.
      • How to view or modify the contents of initramfs?

    bash

    # View contents

    lsinitrd /boot/initramfs-$(uname -r).img | less

    # or

    mkdir /tmp/initrd && cd /tmp/initrd

    zcat /boot/initramfs-$(uname -r).img | cpio -idmv

      • If the initramfs is corrupted or missing the drivers required for the root partition, the system will fail to boot and hang with an error like “Kernel panic – not syncing: VFS: Unable to mount root fs”.

    Stage Five: Systemd / Init Process Initialization

    • Core Task: After the switch_root, the kernel will attempt to execute the first user space process on the root file system, which is PID=1 process. In modern mainstream distributions (RHEL 7/8/9, Ubuntu 15.04+, CentOS 7+, etc.), this process is systemd. (Older systems may use SysV init).
    • Detailed Process (Systemd):
    1. systemd starts as PID=1: It is the parent process of all other processes.
    2. Resolve Default Target:systemd will read the symbolic link of /etc/systemd/system/default.target (usually pointing to graphical.target or multi-user.target), which defines the run level the system should enter.
    3. Start Services in Parallel:One of the major advantages of systemd is parallel startup, which starts units defined in /usr/lib/systemd/system/ and /etc/systemd/system/ based on dependencies, such as sshd.service, network.service, etc.
    4. Execute Local Initialization Scripts: For compatibility, it usually also executes /etc/rc.local (if it exists and is executable).
  • Interview Highlight:
      • Importance of PID 1:PID 1 process is extremely important; it manages all other processes (orphaned processes are adopted by it), and if it crashes, the kernel will trigger a panic.
      • Systemd vs SysV init: Be able to articulate the differences.Systemd is parallel startup, dependency management, using target instead of run levels, unified log management (journald), and simple configuration (unit files).
      • How to switch targets? systemctl isolate multi-user.target or init 3 (compatibility method).

    Stage Six: Establish Terminal and User Login

    • Core Task: When systemd starts the getty service, the getty process will display the login: prompt on the terminal (tty1-tty6).
    • Detailed Process:
    1. The user inputs their username.
    2. getty will call the login program to verify the password.
    3. Upon successful verification, the login program will start the user-specified shell (defined in /etc/passwd, such as /bin/bash).
    4. After the shell starts, it will execute the corresponding initialization scripts (such as /etc/profile, ~/.bash_profile).
    5. Finally, the user obtains an interactive command line interface. If the graphical interface (graphical.target) is started, a graphical login manager (such as GDM, LightDM) will be displayed, and after logging in, the desktop environment will start.

    Interview Summary and Common Questions

    1. Can you detail the entire process from pressing the power button to the login prompt?
    • Answer according to the six stages above, emphasizing the differences between BIOS/UEFI, the role of the two stages of GRUB, the core value of initramfs, and the role of systemd.
  • What is the purpose of initramfs? Why is it needed?
    • Standard Answer: To solve the dependency problem of the kernel accessing the root file system. The kernel may not have the drivers for the device where the root file system resides (such as LVM, RAID, NFS), and these drivers are stored as modules in the root file system. initramfs serves as a temporary root file system that loads these modules in advance, allowing the kernel to mount the real root file system.
  • If the system hangs after booting, displaying “Give root password for maintenance” or similar errors, what could be the problem?
    • This is usually a problem with Stage Four or Stage Five.
    • Possible Reasons:
      • /etc/fstab file misconfiguration (especially if the UUID of the root file system is incorrect).
      • initramfs image does not contain the drivers required for the root partition (for example, if the root partition is on LVM, but the initramfs does not have the LVM driver). The solution is to regenerate the initramfs in rescue mode:dracut -f.
      • The root file system itself is corrupted (needs fsck repair).
  • What is the PID 1 process? What is its role?
    • Modern Systems: It is systemd.
    • Role: Initializes the system, manages all system services (start, stop, restart), manages dependencies, provides logging, manages mount points, etc. It is the ancestor of all user space processes.
  • What to do if GRUB is corrupted?
    • Describe how to enter rescue mode and reinstall GRUB. For example:

    bash

    # In a Live CD environment

    chroot /mnt/sysimage # Switch to the original system’s root directory

    grub2-install /dev/sda # Install to the disk’s MBR

    grub2-mkconfig -o /boot/grub2/grub.cfg #Regenerate the configuration file

    Final Advice: Understand the overall flow and deeply grasp 1-2 key points (such as initramfs), which will make you confident in answering this question in interviews. Good luck with your interview!

    #linux#system administration

    Leave a Comment