Ora

How Does the Linux Boot Process Work?

Published in Linux Boot Process 6 mins read

The Linux boot process is a meticulously orchestrated sequence of events that transforms a powered-off machine into a fully functional operating system. This journey involves multiple stages, each with specific responsibilities, ensuring that hardware is initialized, the kernel is loaded, and essential system services are started before a user can interact with the system.

The entire process can be broken down into six distinct stages:

The Six Stages of Linux Boot

The booting of a Linux system is a methodical progression through several critical phases, each building upon the last to bring the operating system to life.

Stage 1: BIOS (Basic Input/Output System)

The first step in the boot process begins with the BIOS, a firmware embedded on a chip on the computer's motherboard.

  • Power-On Self-Test (POST): Immediately after power is supplied, the BIOS performs a POST. This diagnostic test checks essential hardware components like the CPU, memory, and graphics card to ensure they are functioning correctly. Any critical errors at this stage typically result in audible beeps or error messages.
  • Hardware Initialization: The BIOS initializes basic hardware, including disk controllers and USB ports, setting up the foundational environment for the operating system to take over.
  • Boot Device Selection: Based on a predefined boot order (e.g., hard drive, CD/DVD, USB), the BIOS searches for a bootable device. Once found, it hands over control to the boot loader located on that device.

Stage 2: MBR (Master Boot Record)

Once a bootable device is identified, the BIOS transfers control to the Master Boot Record (MBR).

  • Location: The MBR is the first 512-byte sector on the bootable hard drive (typically /dev/sda or /dev/vda).
  • Contents: It contains three crucial components:
    • Primary Boot Loader: A tiny piece of executable code (446 bytes) whose sole purpose is to locate and load the next stage of the boot loader.
    • Partition Table: Information about how the disk is partitioned (64 bytes).
    • Magic Number: A signature (2 bytes) that identifies the MBR as valid.
  • Execution: The MBR's primary boot loader then scans the partition table to find the active (bootable) partition and loads its volume boot record, which in turn points to the GRUB boot loader.

Stage 3: GRUB (GRand Unified Bootloader)

GRUB (most commonly GRUB2 in modern systems) is the sophisticated boot loader that takes over from the MBR's primary boot loader.

  • Functionality: GRUB is highly configurable and offers significant advantages:
    • It can boot multiple operating systems (multi-boot scenarios).
    • It provides a boot menu, allowing users to select different kernel versions or operating systems.
    • It understands various filesystem types, allowing it to locate the kernel image.
  • Loading the Kernel: GRUB's primary task is to load the Linux kernel image (often located in /boot) and the initramfs (initial RAM filesystem) into memory. The initramfs is a temporary root filesystem that contains necessary modules and drivers to access the real root filesystem.
  • Configuration: Its configuration is typically found in /boot/grub/grub.cfg.

Stage 4: Kernel

After GRUB successfully loads the kernel and initramfs into memory, the Linux Kernel takes control of the boot process.

  • Self-Decompression: If the kernel image is compressed (which is common), it first decompresses itself.
  • Hardware Detection and Initialization: Using the modules provided by initramfs, the kernel detects and initializes essential hardware components and their respective drivers (e.g., storage controllers, network interfaces).
  • Mounting Root Filesystem: Once the necessary drivers are loaded, the kernel mounts the actual root filesystem (/) based on the parameters passed to it by GRUB.
  • Init Process Execution: Finally, the kernel hands over control to the init program, which is always the first process executed by the kernel (Process ID 1 or PID 1).

Stage 5: Init (Initialization)

The Init process is the mother of all processes, responsible for bringing the system to a fully operational state.

  • Parent Process (PID 1): The init process is the first user-space process and the direct or indirect parent of every other process on the system.
  • System Initialization: It reads its configuration files to determine which services and processes need to be started.
    • SysVinit (Older): Reads /etc/inittab to determine the default runlevel and execute scripts within /etc/rc.d/ or /etc/init.d/.
    • Systemd (Modern): Widely adopted in contemporary distributions, systemd uses "targets" instead of runlevels and manages services through unit files (e.g., .service, .target files). It offers parallel service startup for faster booting.
  • Transition to User Space: This stage marks the transition from kernel-space operations to user-space, where applications and services run.

Stage 6: RunLevel Programs

The final stage involves init (or systemd) executing programs based on the configured runlevel (SysVinit) or target (systemd), which define the system's operational state.

  • Runlevels (SysVinit):
    • 0: Halt (shut down)
    • 1: Single-user mode (maintenance)
    • 2: Multi-user mode, no networking (rarely used)
    • 3: Multi-user mode, command-line (server default)
    • 4: Unused
    • 5: Multi-user mode, graphical (desktop default)
    • 6: Reboot
  • Targets (Systemd): Modern systemd systems use more descriptive "targets" like multi-user.target (analogous to runlevel 3) or graphical.target (analogous to runlevel 5).
  • Service Startup: Scripts (or unit files) corresponding to the desired runlevel/target are executed to start various system services (e.g., networking, logging, web servers, desktop environments). These services are typically located in directories like /etc/rcX.d/ (SysVinit) or managed directly by systemctl (systemd).
  • Login Prompt: Once all necessary services for the chosen runlevel/target are started, the system presents a login prompt, signaling that the boot process is complete and the system is ready for user interaction.

The following table summarizes the stages of the Linux boot process:

Stage Description Key Action
1. BIOS Firmware that initializes hardware and finds a bootable device. Performs POST, initializes hardware, finds MBR.
2. MBR First sector of the bootable disk containing primary boot loader and partition table. Loads the next stage of the boot loader (e.g., GRUB).
3. GRUB The boot loader that presents boot options and loads the kernel. Loads Linux kernel and initramfs into memory.
4. Kernel The core of the operating system that manages hardware and processes. Initializes hardware, mounts root filesystem, starts init (PID 1).
5. Init The first process in user space, responsible for system initialization. Reads configuration, starts core system services.
6. RunLevel Programs Services and programs launched based on the system's desired operational state. Starts all necessary services for user login, presents login prompt.