DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · INTERNALS · 9 OF 187

How Linux Boots: Firmware, GRUB, Kernel, systemd

Every stage from power-on to a login prompt, shown on a real boot: firmware (BIOS or UEFI), the boot loader (GRUB), the kernel and its command line, the initramfs, PID 1 (systemd), targets and services. We booted Ubuntu's 6.8 kernel with a 1.1 MB initramfs in QEMU: user space started 2.49 to 2.76 s after power-on, with 61 processes running. Plus how to debug a server that will not boot.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Notes

1 · Firmware

Power on, and the CPU starts running firmware stored on the motherboard. Older machines use BIOS, newer ones UEFI. The firmware checks the hardware and finds something to boot. BIOS reads the first sector of a disk; UEFI reads a boot program from a small EFI partition. In our test virtual machine the firmware was SeaBIOS, a free BIOS. In the cloud, the hypervisor provides the firmware for you.

BIOS or UEFI
  • power on: the CPU runs firmware stored on the motherboard
  • checks hardware (POST)
  • finds a boot device: BIOS reads the first sector UEFI reads an EFI partition
  • cloud VMs: the hypervisor provides the firmware

2 · Boot loader

Next comes the boot loader, usually GRUB. It can show a menu of installed kernels, which is how you boot the previous kernel when a new one fails. It loads two files into memory, the kernel and the initramfs, and passes the kernel a command line of options, such as which disk holds the root filesystem and where to print messages. Its config is generated; you change settings in the defaults file and regenerate it.

GRUB
  • shows a menu of kernels (often hidden)
  • loads the kernel file and the initramfs into memory
  • passes the kernel command line: root=, console=, quiet
  • config: /boot/grub/grub.cfg (generated, do not edit; use /etc/default/grub)

3 · Kernel

The kernel starts. It decompresses itself, sets up memory management, detects the CPU and devices, and loads drivers. Every step is logged, and dmesg shows the log with seconds since boot. Our run printed the version line at time zero, Ubuntu's six point eight kernel, fifteen megabytes compressed. A couple of seconds later it freed the memory it only needed while starting. Kernel panics and missing disk drivers show up right here.

Our boot (dmesg)
[0.000000] Linux version
  6.8.0-151-generic ...
  gcc 13.3.0 ...
[2.468470] Freeing unused kernel
  image (initmem) memory: 4948K

kernel file: 15.1 MB
  (vmlinuz, compressed)

4 · initramfs

Here is a puzzle. The real root filesystem may sit on a disk that needs a driver, RAID, LVM or decryption before it can be read. So the kernel first unpacks an initramfs, a small archive of tools held in memory, and runs it. Its job is to find and mount the real root, then hand over. Ours was tiny, one point one megabytes: a single busybox program and a short script. Ubuntu's real initramfs is larger, with drivers for many disks.

Why an initramfs
  • the real root disk may need drivers, RAID, LVM or decryption first
  • initramfs: a small archive of tools in memory
  • it mounts the real root, then hands over ours: 1.1 MB, one busybox binary + an init script

5 · PID 1

Then the kernel starts process number one, PID one, the ancestor of every other process. On real servers that is systemd. In our test it was our own small init script, which printed when user space began: two point seven six, two point six seven and two point four nine seconds after power-on in three boots. Sixty one processes already existed, most of them kernel threads. Our CPU was fully emulated, with no hardware virtualisation, so a normal VM boots faster.

Our first process
INIT: userspace started at
  2.76 s / 2.67 s / 2.49 s
PID1: init
KERNEL: 6.8.0-151-generic
PROCS: 61

no KVM: the CPU was emulated,
  so a real VM boots faster

6 · systemd

systemd reads unit files, which describe services, mounts, sockets and timers, and starts them in parallel, respecting their dependencies. It aims for a target. Servers usually aim for multi user target: networking, SSH and your services, no desktop. Desktops aim for graphical target. systemd also restarts services that crash and collects their logs, which episode nine covers in depth.

What systemd does
  • reads unit files: services, mounts, sockets, timers
  • starts them in parallel, in dependency order
  • reaches a target: multi-user.target (servers) graphical.target (desktops)
  • restarts services that crash

Measure your boot

On a machine running systemd, you can measure the boot yourself. systemd analyze prints the time spent in firmware, loader, kernel and user space. Blame lists the slowest units first, and critical chain shows what the boot actually waited on, which is often a network wait or a slow disk mount. And journal control with minus b and priority error shows the errors from this boot only. Run these in your lab after a reboot.

On a systemd machine
$ systemd-analyze
  firmware + loader + kernel
  + userspace = total
$ systemd-analyze blame
  slowest units first
$ systemd-analyze critical-chain
  what the boot waited on
$ journalctl -b -p err
  errors from this boot

When it will not boot

When a server will not boot, find the stage that failed. No boot device points at firmware or disk order. Stuck at a GRUB prompt means the boot loader config or the kernel file is missing. A kernel panic saying it cannot mount root means the root option is wrong, or the initramfs lacks a driver. Dropped into an emergency shell usually means a bad line in the filesystem table, fstab. In the cloud, use the serial console to see these messages.

Symptom → stage
  • no boot device → firmware or disk order
  • GRUB prompt → boot loader config or missing kernel
  • kernel panic: unable to mount root → root= or initramfs missing a driver
  • emergency shell → a bad /etc/fstab line
  • service missing → systemd unit

Common mistakes

Mistakes. A typo in fstab that stops the boot; mark optional disks with nofail. Removing the previous kernel before the new one has booted successfully. Editing the generated GRUB file directly, so your change vanishes. Having no serial console access for cloud servers. And starting a service by hand but never enabling it, so it is gone after the next reboot.

Avoid these
  • a typo in /etc/fstab: no boot (use nofail for extras)
  • removing the old kernel before the new one is proven
  • editing grub.cfg directly
  • no serial console access in the cloud
  • services enabled nowhere: gone after reboot

Say it like this

In the interview: firmware, BIOS or UEFI, checks the hardware and finds the boot loader. GRUB loads the kernel and the initramfs and passes the kernel command line. The kernel initialises memory and devices, the initramfs finds and mounts the real root filesystem, and the kernel starts PID one, systemd, which starts units in dependency order until it reaches multi user target. I debug boots by finding which of those stages failed.

In the interview
  • "Firmware, BIOS or UEFI, finds
  • the boot loader, GRUB, which loads the kernel and initramfs
  • with a command line. The kernel
  • initialises hardware, the initramfs mounts the real root, then PID 1, systemd, starts
  • units up to multi-user.target."

Interview questions

Q1What are the stages of the Linux boot process?

Firmware (BIOS or UEFI) and hardware checks, the boot loader (GRUB) loading the kernel and initramfs with a command line, the kernel initialising hardware, the initramfs mounting the real root, then PID 1 (systemd) starting units up to a target.

Q2What is the initramfs for?

A small in-memory root filesystem with the drivers and tools needed to find and mount the real root, for example storage drivers, LVM, RAID or decryption.

Q3What is PID 1?

The first user-space process the kernel starts, usually systemd; it starts all other services and adopts orphaned processes.

Q4What is a systemd target?

A named group of units that represents a system state, like multi-user.target for servers or graphical.target for desktops.

Q5How do you find what makes a boot slow?

systemd-analyze for the total split, systemd-analyze blame for slow units, and systemd-analyze critical-chain for what the boot waited on.

Q6A server drops to an emergency shell after reboot. What do you check?

Usually /etc/fstab: a wrong UUID or missing disk. Check journalctl -xb, fix or comment the line, and use nofail for non-essential mounts.

Q7How do you recover from a bad kernel update?

Choose the previous kernel in the GRUB menu (or the cloud serial console), boot it, then remove or fix the new kernel.

Q8What is the difference between BIOS and UEFI?

BIOS is the older firmware that boots from the first disk sector; UEFI is newer, boots programs from an EFI system partition and supports Secure Boot and large disks.