Boot Process: BIOS → GRUB → Kernel → systemd
Chapter 2: Boot Process: BIOS → GRUB → Kernel → systemd
Section titled “Chapter 2: Boot Process: BIOS → GRUB → Kernel → systemd”Learning Objectives
Section titled “Learning Objectives”By the end of this chapter, you will:
- Understand every stage of the Linux boot process in precise detail
- Know how to diagnose boot failures at each stage
- Understand how to customize the bootloader and kernel parameters
- Know how systemd target units map to traditional runlevels
- Be able to create production systemd services correctly
Prerequisites
Section titled “Prerequisites”- Chapter 1: Linux Architecture & Kernel Overview
- Basic familiarity with Linux filesystem structure
What is this? (Beginner On-Ramp)
Section titled “What is this? (Beginner On-Ramp)”The boot process is the sequence of events that happens from the moment you press the power button until you get a login prompt. It’s like waking up: first your brain turns on (BIOS/UEFI), then you load your core memories (Kernel), and finally you start your daily routines (systemd and services).
2.1 Overview: The Complete Boot Flow
Section titled “2.1 Overview: The Complete Boot Flow” ┌─────────────────────────────────────────────────────────────────┐ │ POWER ON │ └──────────────────────────┬──────────────────────────────────────┘ │ ┌──────────────────────────▼──────────────────────────────────────┐ │ STAGE 1: FIRMWARE │ │ BIOS or UEFI │ │ │ │ • CPU starts at fixed address (0xFFFFFFF0 for x86) │ │ • POST (Power-On Self Test): tests RAM, CPU, devices │ │ • Finds boot device (NVMe, SATA, USB, network) │ │ • BIOS: loads first 512 bytes (MBR) from boot device │ │ • UEFI: reads EFI System Partition (ESP), runs .efi file │ │ │ └──────────────────────────┬──────────────────────────────────────┘ │ ┌──────────────────────────▼──────────────────────────────────────┐ │ STAGE 2: BOOTLOADER │ │ GRUB2 (most Linux systems) │ │ │ │ • GRUB loaded by BIOS/UEFI │ │ • Reads /boot/grub/grub.cfg │ │ • Shows boot menu (if configured) │ │ • Loads kernel (vmlinuz) into RAM │ │ • Loads initramfs/initrd into RAM │ │ • Passes kernel command line parameters │ │ • Hands control to kernel │ │ │ └──────────────────────────┬──────────────────────────────────────┘ │ ┌──────────────────────────▼──────────────────────────────────────┐ │ STAGE 3: KERNEL INITIALIZATION │ │ │ │ • Kernel decompresses itself │ │ • Sets up CPU, memory, early hardware │ │ • Mounts initramfs as temporary root filesystem │ │ • Loads essential modules (disk drivers, filesystem drivers) │ │ • Mounts real root filesystem │ │ • Executes /sbin/init (PID 1) — now systemd in modern Linux │ │ │ └──────────────────────────┬──────────────────────────────────────┘ │ ┌──────────────────────────▼──────────────────────────────────────┐ │ STAGE 4: SYSTEMD (PID 1) │ │ │ │ • Reads /etc/systemd/system/ and /lib/systemd/system/ │ │ • Activates default.target (usually multi-user.target) │ │ • Starts units in dependency order (parallel where possible) │ │ • Launches all configured services, sockets, timers │ │ • System is now operational │ │ │ └──────────────────────────┬──────────────────────────────────────┘ │ ┌──────────────────────────▼──────────────────────────────────────┐ │ SYSTEM OPERATIONAL │ │ Login prompts, services running, applications available │ └─────────────────────────────────────────────────────────────────┘2.2 Stage 1: BIOS vs UEFI
Section titled “2.2 Stage 1: BIOS vs UEFI”Traditional BIOS
Section titled “Traditional BIOS”BIOS (Basic Input/Output System) is the legacy firmware from the 1970s–2000s.
BIOS Boot Sequence ────────────────── 1. CPU starts at address 0xFFFFFFF0 (Reset Vector) 2. BIOS code runs from ROM 3. POST: counts RAM, tests hardware 4. Reads boot device order from CMOS settings 5. Reads first 446 bytes of MBR (Master Boot Record) 6. The 446-byte MBR code is GRUB Stage 1 7. GRUB Stage 1 loads GRUB Stage 2 from disk 8. GRUB Stage 2 reads grub.cfg, shows menu, loads kernel
MBR Layout (512 bytes): ┌─────────────────────────────────────────────────┐ │ Bootstrap Code (446 bytes) │ Partition Table │ Magic │ │ GRUB Stage 1 │ (64 bytes, 4 x │ 0xAA55│ │ │ 16-byte entries)│ │ └─────────────────────────────────────────────────┘ 0 446 510 512Modern UEFI
Section titled “Modern UEFI”UEFI (Unified Extensible Firmware Interface) replaced BIOS for most modern hardware.
UEFI Boot Sequence ────────────────── 1. Firmware runs from UEFI ROM 2. POST and hardware initialization 3. UEFI reads NVRAM for boot entries 4. Finds EFI System Partition (ESP) — fat32 formatted 5. Loads /EFI/ubuntu/grubx64.efi (or similar) 6. GRUB EFI binary takes over
UEFI Advantages: ✓ Supports disks >2TB (GPT partition table) ✓ Secure Boot (cryptographic verification of bootloader) ✓ Faster boot (no need for MBR stage 1/2) ✓ Network boot (PXE) built-in ✓ 64-bit mode from the start ✓ Driver model (network, disk access in firmware)
ESP Layout: /boot/efi/ ├── EFI/ │ ├── ubuntu/ │ │ ├── grubx64.efi # GRUB EFI binary │ │ └── shimx64.efi # Secure Boot shim │ ├── BOOT/ │ │ └── BOOTX64.EFI # Fallback bootloader │ └── Microsoft/ # Windows bootloader (if dual boot) └── ...# Check if system uses UEFI or BIOS[ -d /sys/firmware/efi ] && echo "UEFI" || echo "BIOS"
# View UEFI boot entriesefibootmgr -v
# View EFI System Partition contentsls /boot/efi/EFI/
# Check Secure Boot statusmokutil --sb-state2.3 Stage 2: GRUB2 Deep Dive
Section titled “2.3 Stage 2: GRUB2 Deep Dive”GRUB (GRand Unified Bootloader) is what most Linux systems use. GRUB2 is the current version.
GRUB Configuration
Section titled “GRUB Configuration”# Main GRUB config (auto-generated, DO NOT edit directly)cat /boot/grub/grub.cfg
# User-editable GRUB defaultscat /etc/default/grub
# Directory for custom GRUB scriptsls /etc/grub.d/
# After editing /etc/default/grub, regenerate:sudo update-grub # Debian/Ubuntusudo grub2-mkconfig -o /boot/grub2/grub.cfg # RHEL/CentOS
# Useful /etc/default/grub settings:GRUB_TIMEOUT=5 # Seconds to show menuGRUB_DEFAULT=0 # Default menu entry (0=first)GRUB_CMDLINE_LINUX="quiet splash" # Kernel parameters for all entriesGRUB_CMDLINE_LINUX_DEFAULT="" # Params for default entry onlyCritical Kernel Parameters (Passed by GRUB)
Section titled “Critical Kernel Parameters (Passed by GRUB)”These parameters fundamentally change kernel behavior:
# View current kernel parameters (what was used at boot)cat /proc/cmdline# Example output:# BOOT_IMAGE=/vmlinuz-6.8.0-45-generic root=UUID=abc123 ro quiet splash
# Common kernel parameters:# root= — Root device (can be UUID, /dev/sda1, LABEL=)# ro — Mount root read-only initially# quiet — Suppress non-critical kernel messages# splash — Show graphical splash screen# init= — Override init process (use init=/bin/bash for recovery)# single — Boot into single-user mode (runlevel 1)# mem= — Limit available RAM (testing purposes)# maxcpus= — Limit CPU count# nomodeset — Disable GPU kernel module (use for video troubleshooting)# systemd.unit=— Override systemd target (systemd.unit=rescue.target)# selinux=0 — Disable SELinux (DANGEROUS, for emergency only)# apparmor=0 — Disable AppArmorProduction GRUB Hardening
Section titled “Production GRUB Hardening”# /etc/default/grub production settingsGRUB_TIMEOUT=1 # Short timeout (reduce attack window)GRUB_TIMEOUT_STYLE=menu # Show menu briefly
# Set GRUB password to prevent unauthorized boot modification# This prevents someone with physical access from booting into single-user modegrub-mkpasswd-pbkdf2# Enter password, get PBKDF2 hash
# Add to /etc/grub.d/40_custom:set superusers="admin"password_pbkdf2 admin grub.pbkdf2.sha512.10000.<HASH>
# Restrict menu entry to superusers only# (in grub.cfg, add --unrestricted for entries users can use without password)2.4 Stage 3: Kernel Initialization
Section titled “2.4 Stage 3: Kernel Initialization”After GRUB hands control to the kernel, a complex initialization sequence occurs.
The initramfs/initrd
Section titled “The initramfs/initrd”initramfs (initial RAM filesystem) is a temporary root filesystem loaded into RAM before the real root filesystem is mounted.
Why it exists: The kernel needs to mount the real root filesystem, but that might require drivers that aren’t compiled in (e.g., NVMe driver, LVM, LUKS encryption, RAID). initramfs contains these drivers.
The initramfs Problem and Solution ────────────────────────────────────
Problem: Kernel needs to mount /dev/nvme0n1p1, but the NVMe driver is a loadable module. Modules live on the root filesystem. The root filesystem needs the NVMe driver to access. → Chicken-and-egg problem!
Solution: initramfs 1. GRUB loads kernel + initramfs into RAM 2. Kernel mounts initramfs as temporary / 3. initramfs contains the NVMe module (built-in) 4. initramfs script loads NVMe module 5. Now kernel can see /dev/nvme0n1p1 6. Real root filesystem mounted 7. initramfs unmounted, real / takes over 8. PID 1 (systemd) starts from real filesystem
Contents of initramfs: ┌────────────────────────────────┐ │ /bin/sh (busybox or dash) │ │ /lib/modules/ (needed mods) │ │ /scripts/ (boot scripts) │ │ /sbin/init → systemd │ │ cryptsetup (for LUKS) │ │ lvm2 tools (for LVM) │ │ mdadm (for software RAID) │ └────────────────────────────────┘# View initramfs contentslsinitramfs /boot/initrd.img-$(uname -r)
# Or on RHEL/CentOSlsinitrd /boot/initramfs-$(uname -r).img
# Rebuild initramfs (needed after adding kernel modules)update-initramfs -u # Debian/Ubuntudracut --force # RHEL/CentOS/Fedora
# Extract initramfs for debuggingmkdir /tmp/initramfs && cd /tmp/initramfszcat /boot/initrd.img-$(uname -r) | cpio -idmv# Or with newer zstd compression:zstd -d /boot/initrd.img-$(uname -r) -o /tmp/initrd.cpiocpio -idmv < /tmp/initrd.cpioKernel Startup Sequence (Internal)
Section titled “Kernel Startup Sequence (Internal)” Kernel Startup (simplified) ──────────────────────────── 1. head.S / head_64.S — Architecture-specific assembly Sets up page tables, GDT, IDT 2. start_kernel() — First C function in kernel 3. setup_arch() — CPU detection, memory map parsing 4. mm_init() — Memory management initialization 5. sched_init() — Scheduler initialization 6. vfs_caches_init() — VFS and dcache initialization 7. rest_init() — Creates kernel threads ├── kernel_thread(kthreadd) — Parent of all kernel threads └── kernel_thread(kernel_init) — Will become PID 1 8. kernel_init() ├── do_initcalls() — Calls all __init functions │ (driver initialization) ├── mount_root() — Mounts initramfs ├── prepare_namespace() — Mounts real root filesystem └── run_init_process("/sbin/init") — Executes systemd2.5 Stage 4: systemd — PID 1 Deep Dive
Section titled “2.5 Stage 4: systemd — PID 1 Deep Dive”Once the kernel executes /sbin/init (which is systemd on modern Linux), the user-space boot begins.
systemd Architecture
Section titled “systemd Architecture” systemd is the parent of all user-space processes ─────────────────────────────────────────────────
PID 1: systemd ├── journald (log collection) ├── udevd (device management) ├── networkd (networking) ├── resolved (DNS) ├── logind (login sessions) ├── dbus-daemon (D-Bus message bus) ├── nginx.service (your web server) ├── postgresql.service (your database) ├── sshd.service (SSH daemon) └── ... (all other services)systemd Units
Section titled “systemd Units”Every resource systemd manages is a unit. Units are defined in unit files:
| Unit Type | Extension | Purpose |
|---|---|---|
| Service | .service | Daemons and processes |
| Socket | .socket | Socket-activated services |
| Timer | .timer | Scheduled jobs (cron replacement) |
| Target | .target | Groups of units (like runlevels) |
| Mount | .mount | Filesystem mounts |
| Device | .device | Hardware devices |
| Path | .path | File/directory monitoring |
| Slice | .slice | Resource control groups |
systemd Targets (Replacing Runlevels)
Section titled “systemd Targets (Replacing Runlevels)” Traditional Runlevels → systemd Targets ───────────────────────────────────────── Runlevel 0 → poweroff.target Runlevel 1 → rescue.target Runlevel 2 → multi-user.target Runlevel 3 → multi-user.target Runlevel 5 → graphical.target Runlevel 6 → reboot.target
Target Dependency Chain: ┌──────────────────────────────────────────┐ │ graphical.target │ │ └── multi-user.target │ │ └── basic.target │ │ └── sysinit.target │ │ └── local-fs.target │ └── network.target └──────────────────────────────────────────┘# View current target (runlevel)systemctl get-default
# Change default targetsystemctl set-default multi-user.target
# Switch to a different target (immediately)systemctl isolate rescue.target
# List all targetssystemctl list-units --type=target
# View which units are in a targetsystemctl list-dependencies multi-user.targetWriting a Production systemd Service
Section titled “Writing a Production systemd Service”A properly written service unit is essential for production reliability. Here’s a complete example:
[Unit]Description=My Production ApplicationDocumentation=https://docs.myapp.com# Hard dependencies (must be active before this starts)Requires=network-online.targetAfter=network-online.target postgresql.service
# Soft dependencies (start after if available, but don't fail if not)Wants=redis.serviceAfter=redis.service
[Service]Type=notify # App signals systemd when ready (most correct)# Other types: simple (default), forking, oneshot, dbus, idle
# Run as non-root userUser=myappGroup=myappWorkingDirectory=/opt/myapp
# EnvironmentEnvironment=NODE_ENV=productionEnvironmentFile=/etc/myapp/env # Load secrets from file
# The actual commandExecStart=/opt/myapp/bin/server --port 8080ExecReload=/bin/kill -HUP $MAINPID # Reload without restartExecStop=/bin/kill -TERM $MAINPID # Graceful shutdown
# Restart policyRestart=always # Always restart on failureRestartSec=5 # Wait 5 seconds before restartingStartLimitInterval=60 # Time window for start limitStartLimitBurst=5 # Max 5 restarts in 60 seconds
# Resource limitsLimitNOFILE=65536 # Max open file descriptorsLimitNPROC=4096 # Max processes/threadsMemoryMax=2G # Max RAM (OOM kill if exceeded)CPUQuota=200% # Max 2 CPU cores
# Security hardeningNoNewPrivileges=yes # Cannot gain new privilegesPrivateTmp=yes # Isolated /tmpProtectSystem=strict # Read-only /usr, /boot, /etcProtectHome=yes # No access to /homeReadWritePaths=/var/lib/myapp # Allow write to specific dirCapabilityBoundingSet= # Drop all capabilitiesAmbientCapabilities= # No ambient capabilities
# TimeoutsTimeoutStartSec=30 # Fail if not ready within 30sTimeoutStopSec=30 # Force kill after 30s
[Install]WantedBy=multi-user.target# Deploy the servicesudo systemctl daemon-reload # Reload unit filessudo systemctl enable myapp # Enable on bootsudo systemctl start myapp # Start nowsudo systemctl status myapp # Check statusjournalctl -u myapp -f # Follow logs2.6 Boot Performance Analysis
Section titled “2.6 Boot Performance Analysis”# Measure boot time breakdownsystemd-analyze # Total boot timesystemd-analyze blame # Which units took longestsystemd-analyze critical-chain # Critical path of bootsystemd-analyze plot > boot.svg # SVG visualization
# Example output of systemd-analyze blame:# 23.543s NetworkManager-wait-online.service# 8.281s snapd.service# 4.371s dev-sda1.device# 3.246s fwupd.service# 2.841s accounts-daemon.service
# Example: Reduce boot time by masking slow units# NetworkManager-wait-online delays boot by waiting for network# On servers, use networkd's systemd-networkd-wait-online insteadsudo systemctl mask NetworkManager-wait-online.service2.7 Boot Troubleshooting
Section titled “2.7 Boot Troubleshooting”Scenario 1: System Won’t Boot — Emergency Shell
Section titled “Scenario 1: System Won’t Boot — Emergency Shell” GRUB menu → Edit boot entry → Add to kernel line: systemd.unit=emergency.target or init=/bin/bash# In emergency shell:# Mount root filesystem read-writemount -o remount,rw /
# Check filesystem errorsfsck /dev/sda1
# View systemd journal from previous failed bootjournalctl -b -1 --priority=3 # Errors from last boot
# Check what failedsystemctl --failedsystemctl status <failed-unit>Scenario 2: Service Fails to Start
Section titled “Scenario 2: Service Fails to Start”# Check service statussystemctl status nginx.service
# View full logs for the servicejournalctl -u nginx.service --no-pager -n 100
# View kernel messages around service startjournalctl -k -b | grep -A 5 "nginx"
# Check if a dependency failedsystemctl list-dependencies nginx.servicesystemd-analyze critical-chain nginx.service
# Debug service startup interactively# (starts with output to terminal)systemd-run --unit=test-nginx -t /usr/sbin/nginx -g "daemon off;"Scenario 3: Slow Boot Diagnosed in Production
Section titled “Scenario 3: Slow Boot Diagnosed in Production”# The culprit: NetworkManager-wait-online.service waiting 90ssystemd-analyze blame | head -5# 90.000s NetworkManager-wait-online.service
# Fix for servers that don't need GUI networking:# Option 1: Mask the wait servicesudo systemctl mask NetworkManager-wait-online.service
# Option 2: Configure network properly with networkdsudo systemctl enable --now systemd-networkdsudo systemctl enable systemd-networkd-wait-online
# Verify fixsystemd-analyze# Before: Startup finished in 103.342s# After: Startup finished in 8.234s2.8 Real-World Production Scenarios
Section titled “2.8 Real-World Production Scenarios”Scenario: Recovering a Server with Corrupted GRUB
Section titled “Scenario: Recovering a Server with Corrupted GRUB”# Boot from live ISO or rescue image# Step 1: Mount the broken system's partitionsmount /dev/sda2 /mnt # Root partitionmount /dev/sda1 /mnt/boot # Boot partitionmount --bind /dev /mnt/devmount --bind /proc /mnt/procmount --bind /sys /mnt/sys
# Step 2: chroot into broken systemchroot /mnt /bin/bash
# Step 3: Reinstall GRUBgrub-install /dev/sda # BIOS# or for UEFI:mount /dev/sda1 /boot/efigrub-install --target=x86_64-efi --efi-directory=/boot/efi
# Step 4: Regenerate GRUB configupdate-grub # Ubuntu/Debiangrub2-mkconfig -o /boot/grub2/grub.cfg # RHEL/CentOS
# Step 5: Exit and rebootexitumount -R /mntrebootScenario: Production System with Slow initramfs
Section titled “Scenario: Production System with Slow initramfs”An on-call engineer reports that a fleet of servers takes 3+ minutes to boot after a kernel update. Investigation:
# Boot with early debug output# Add to kernel parameters: rd.debug rd.log# In GRUB: press 'e' and add to kernel line
# Check initramfs boot logjournalctl -b | grep "dracut\|initramfs" | head -40
# The problem: RAID assembly timeout# udev waited 30s for each of 6 RAID members# Fix: Add rdblacklist=megaraid_sas to GRUB params# and set GRUB_CMDLINE_LINUX="rd.timeout=60"
# Or specify the root device explicitly to skip discovery# GRUB_CMDLINE_LINUX="root=UUID=abc123-def456 rd.timeout=30"2.9 Best Practices
Section titled “2.9 Best Practices”| Practice | Why | Implementation |
|---|---|---|
| Set short GRUB timeout | Faster boot, less attack window | GRUB_TIMEOUT=1 |
| Password-protect GRUB | Prevent single-user mode bypass | grub-mkpasswd-pbkdf2 |
| Use Type=notify for services | Accurate dependency tracking | systemd knows when service is truly ready |
| Set resource limits in service | Prevent runaway processes | MemoryMax=, CPUQuota= |
| Enable Watchdog | Auto-restart hung services | WatchdogSec=30 in [Service] |
Use PrivateTmp=yes | Isolate /tmp per service | Security hardening |
Set Restart=always | Auto-recovery from crashes | Never lose a service silently |
2.10 Interview Questions
Section titled “2.10 Interview Questions”Q1: Explain the entire Linux boot process from power-on to login prompt.
Answer: (1) Firmware (BIOS/UEFI): CPU starts at reset vector, runs POST, selects boot device. BIOS loads MBR (512 bytes); UEFI reads the EFI System Partition and loads a
.efibinary. (2) GRUB2: Readsgrub.cfg, shows boot menu, loads kernel (vmlinuz) andinitramfsinto RAM, passes kernel parameters, hands over execution. (3) Kernel initialization: Decompresses itself, initializes CPU/memory/drivers, mounts initramfs as temporary root, loads any needed modules (LUKS, LVM, NVMe), mounts real root filesystem, executes/sbin/init(PID 1). (4) systemd: Reads unit files, resolves dependencies, starts units in parallel up todefault.target, brings up all services. Login prompt appears whengetty@tty1.serviceorsshd.serviceis ready.
Q2: What is initramfs and why is it needed?
Answer: initramfs (initial RAM filesystem) is a small compressed filesystem packed alongside the kernel by the bootloader. It’s needed to solve a chicken-and-egg problem: the kernel needs drivers (e.g., for NVMe, LVM, or LUKS encryption) to mount the real root filesystem, but those drivers are stored as loadable modules on the root filesystem. initramfs contains a minimal set of tools and modules. The kernel mounts it first, runs its startup scripts to load required drivers and potentially unlock encrypted drives, then mounts the real root filesystem and switches over to it.
Q3: What is the difference between Requires= and Wants= in a systemd unit file?
Answer:
Requires=is a hard dependency — if the required unit fails to start, this unit will also fail.Wants=is a soft dependency — if the wanted unit fails, this unit continues starting anyway. In production, useWants=for optional dependencies (like a cache) andRequires=for mandatory ones (like a database). Always pair withAfter=to control ordering, becauseRequires=/Wants=only control activation, not ordering.
Q4: How would you debug a service that fails to start with systemctl status showing “Failed to start”?
Answer: (1)
systemctl status <service>— often shows the last error message. (2)journalctl -u <service> -n 50 --no-pager— full service logs. (3)systemctl list-dependencies <service>— check if a dependency failed. (4)systemd-analyze critical-chain <service>— dependency chain. (5) Try running theExecStart=command manually as the service user to see the exact error. (6) CheckEnvironmentFileexists and has correct syntax. (7) Check directory/file permissions. (8) CheckUser=andGroup=settings.
2.11 Hands-on Labs
Section titled “2.11 Hands-on Labs”Lab 1: Analyze Boot Performance
Section titled “Lab 1: Analyze Boot Performance”# 1. Check total boot timesystemd-analyze
# 2. Find the 10 slowest unitssystemd-analyze blame | head -10
# 3. Visualize the boot critical pathsystemd-analyze critical-chain
# 4. Generate SVG (optional, if you have a browser)systemd-analyze plot > /tmp/boot-plot.svgLab 2: Write a Robust systemd Service
Section titled “Lab 2: Write a Robust systemd Service”# Create a simple test servicesudo tee /etc/systemd/system/mytest.service << 'EOF'[Unit]Description=My Test ServiceAfter=network.target
[Service]Type=simpleUser=nobodyExecStart=/bin/sleep infinityRestart=alwaysRestartSec=5PrivateTmp=yesNoNewPrivileges=yes
[Install]WantedBy=multi-user.targetEOF
sudo systemctl daemon-reloadsudo systemctl start mytestsudo systemctl status mytestjournalctl -u mytest
# Test restart behaviorsudo systemctl kill --signal=SIGKILL mytest# Wait 5 seconds, then check:sudo systemctl status mytest # Should show restarted
# Clean upsudo systemctl stop mytestsudo systemctl disable mytestsudo rm /etc/systemd/system/mytest.servicesudo systemctl daemon-reloadLab 3: Boot Recovery Simulation
Section titled “Lab 3: Boot Recovery Simulation”# On a VM (do NOT do this on production):# 1. Reboot and interrupt GRUB# 2. Press 'e' to edit the boot entry# 3. Find the linux line# 4. Append: systemd.unit=rescue.target# 5. Press Ctrl+X to boot# 6. You now have a rescue shell# 7. Change root password, fix files, etc.# 8. Reboot normally2.12 Summary
Section titled “2.12 Summary”| Stage | Key Components | Failure Point |
|---|---|---|
| Firmware | BIOS/UEFI, POST | Dead hardware, wrong boot order |
| Bootloader | GRUB2, grub.cfg | Corrupted GRUB, wrong kernel path |
| Kernel init | vmlinuz, initramfs | Missing modules, wrong root device |
| systemd | Unit files, targets | Failed service, dependency issue |
2.13 Related Chapters
Section titled “2.13 Related Chapters”- Chapter 1: Linux Architecture — What the kernel is
- Chapter 7: systemd Deep Dive — Advanced systemd patterns
- Chapter 21: Secure Boot & TPM — Securing the boot chain
Next Chapter: Chapter 3: Process Lifecycle, Threads & Scheduler
Section titled “Next Chapter: Chapter 3: Process Lifecycle, Threads & Scheduler”Prerequisites
Section titled “Prerequisites”Chapter 1 (Linux Architecture); know what a kernel is and what BIOS/UEFI does.
Advantages & Disadvantages
Section titled “Advantages & Disadvantages”Advantages: Highly configurable (GRUB), systemd allows parallel service startup (fast boot). Disadvantages: Complex to troubleshoot (many stages), breaking GRUB can leave the system unbootable.
Common Mistakes
Section titled “Common Mistakes”- Editing
/boot/grub2/grub.cfgdirectly — it’s auto-generated; always edit/etc/default/grubthen rungrub2-mkconfig. - Assuming BIOS and UEFI are interchangeable — UEFI supports Secure Boot, GPT disks, and faster boot; BIOS is MBR-only legacy.
- Forgetting to rebuild initramfs after adding a kernel module — the driver won’t be available at boot time.
- Not knowing systemd targets map to runlevels:
multi-user.target≈runlevel 3,graphical.target≈runlevel 5. - Believing
dmesgshows all boot messages — usejournalctl -bfor the complete boot log including systemd units.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Diagnosis | Fix |
|---|---|---|---|
| GRUB rescue prompt on boot | GRUB can’t find stage2 or config | At prompt: lsto list partitions;set root=(hd0,1)theninsmod normal; normal“ | Reinstall GRUB: grub2-install /dev/sda && grub2-mkconfig -o /boot/grub2/grub.cfg |
| Kernel panic: VFS cannot mount root | Wrong root= parameter or missing driver in initramfs | Boot rescue ISO; blkidto verify UUIDs;cat /etc/fstab“ | Rebuild initramfs: dracut --force; fix GRUB root= to match blkid UUID |
| Boot to emergency mode | Failed mount in /etc/fstab or corrupted filesystem | Read: journalctl -xb; check which unit failed: systemctl list-units —failed“ | Fix /etc/fstab entry; add nofail option; run fsck on suspect filesystem |
| Extremely slow boot (>60s) | Timeout waiting for network or slow service start | “systemd-analyze blame | head -20; systemd-analyze plot > boot.svg“ |
Hands-on Lab
Section titled “Hands-on Lab”Objective: Trace the complete boot sequence on your system.
# 1. Measure boot time breakdownsystemd-analyze timesystemd-analyze blame | head -15
# 2. Generate visual boot timelinesystemd-analyze plot > /tmp/boot.svg# Open boot.svg in a browser
# 3. View GRUB menu entriesgrep -A5 "menuentry" /boot/grub2/grub.cfg | head -30
# 4. Check current GRUB defaultscat /etc/default/grub
# 5. Inspect initramfs contentslsinitrd /boot/initramfs-$(uname -r).img | grep -E "\.ko$" | head -20
# 6. View systemd default targetsystemctl get-defaultsystemctl list-units --type=target --state=active
# 7. Read complete boot journaljournalctl -b | head -80Exercises
Section titled “Exercises”- Run
systemd-analyze blame. Find the slowest unit and research whether it can be safely disabled. - Modify
/etc/default/grubto addquietto kernel parameters; regenerate GRUB config; verify the change takes effect. - Examine your
initramfs: which filesystem drivers does it include? Why does it need them before the root filesystem is mounted? - Use
systemctl list-dependencies graphical.targetto draw the dependency graph for reaching the graphical target. - Write a systemd service unit file for a simple bash script that logs the boot time to
/var/log/myboot.log.
Revision Notes
Section titled “Revision Notes”- Boot sequence: BIOS/UEFI → POST → GRUB stage 1 (MBR) → GRUB stage 2 → kernel + initramfs → real rootfs → PID 1 (systemd).
- initramfs is a temporary RAM-based root filesystem containing drivers needed to mount the real root partition.
- GRUB reads
grub.cfg; generated from/etc/default/grubbygrub2-mkconfig; never editgrub.cfgdirectly. - systemd starts services in parallel using a dependency graph;
systemd-analyzemeasures the result. journalctl -b= current boot logs;-b -1= previous boot logs — essential for debugging boot failures.- Kernel parameters (in GRUB
linuxline):root=,ro/rw,quiet,crashkernel=— all control early boot behavior.
Further Reading
Section titled “Further Reading”man bootup— systemd boot documentation- GRUB2 manual: https://www.gnu.org/software/grub/manual/
- How Linux Works by Brian Ward — Chapter on the boot process
- systemd boot documentation: https://systemd.io/BOOT/
Related Chapters
Section titled “Related Chapters”- Chapter 1 — Kernel architecture and execution context
- Chapter 7 — systemd deep dive and unit files
- Chapter 46 — kexec: fast kernel reboots
Last Updated: July 2026