Booting Linux System - Introduction

Booting Linux System

In this article I will try to explain the concept of booting system, the main components of the booting process and how the Linux kernel is loaded and executed.

From Power On to Linux Kernel

In order to understand what happens between the moment you press the power button of your computer and the moment you see the login screen, we need to understand the main components of the booting process. This will be a high-level overview, as this is only an introduction to the topic.

The booting process can be divided into following steps:

  1. 1. Power supply - PSU, Power Good signal
  2. 2. BIOS/UEFI - POST, CMOS, Boot order
  3. 3. MBR/GPT - Partition table, Bootloader location
  4. 4. Bootloader - Grub
  5. 5. Kernel - vmlinuz, initramfs
  6. 6. Early userspace - temporary root
  7. 7. Late userspace - systemd, login, GUI
Linux Boot Stages

1. Power supply

This step seems obvious, but there are some important things to know about it. When you press the power button of your computer, the power supply unit (PSU) will start to provide power to the motherboard. The motherboard will then transmit the power to the other components of the computer. There is a signal called Power Good that is sent by the PSU to the motherboard to indicate that the power supply is stable. The motherboard will wait for this signal before starting the boot process, as it's crucial for the system to have a stable power supply.

2. BIOS/UEFI

The BIOS/UEFI is a firmware stored in a chip on the motherboard. It's responsible for initializing the hardware and starting the boot process.

First it will run the POST (Power-On Self-Test) diagnostic to check if the hardware like the CPU, RAM, hard drive, etc. are working properly (in stationary computers, you will hear a single beep if everything is OK, laptops don't have this feature as nowadays everything is soldered to the motherboard, and often it's not possible to replace a broken component by yourself).

Then it will look into the NVRAM (Non-Volatile Random Access Memory) which in basic terms is a memory that doesn't lose its content when the power is turned off. In modern computers, the NVRAM refers to a CMOS (Complementary Metal-Oxide Semiconductor). Resetting the CMOS, achieved by removing the battery or using the Clear CMOS jumper on the motherboard, restores default settings.

Up to this point, the BIOS/UEFI approach was quite similar, however, from now on they approach will differ; BIOS stores boot order in the CMOS, while modern UEFI systems will search for that information in a disk partition named EFI (EFI System Partition). UEFI accesses this partition to retrieve boot order details.

3. MBR/GPT

The MBR (Master Boot Record) and GPT (GUID Partition Table) are two different partition tables that are used to store information about the partitions on the disk. The MBR is the older one, and it's limited to 2 TB of storage and 4 primary partitions. It's also called msdos partition table, because it was introduced by Microsoft in 1983. The MBR is stored in the first sector of the disk (512 bytes) and it contains the bootloader.

The GPT is the one that is used by modern systems, as it can handle up to 9.4 ZB of storage with huge number of 128 partitions. It's stored in the first and last sectors of the disk, and it contains the bootloader. It's also better protected as it has a backup of the partition table at the end of the disk.

4. Bootloader

This is the part where the BIOS/UEFI hands over the control to the bootloader, which main job is to load the kernel with configured kernel parameters. We can choose between different bootloaders, but the most popular is Grub (Grand Unified Bootloader). We can have multiple kernels installed on the system, and the bootloader will give us the option to choose which one to load.

In order to load the kernel, the bootloader needs to know where it is located on the disk. It will look for the kernel in the /boot directory, specifically for the compressed vmlinuz image that contains the kernel and the initramfs image that holds the initial RAM file system. We can configure the bootloader to load the kernel from a different location, but this is the default one.

5. Kernel

vmlinuz is the compressed Linux kernel executable image. After it's decompressed, and loaded into memory, it will start to execute. During initialization process the kernel will look for the initramfs image, which is a compressed cpio archive that contains the initial RAM file system.

The main purpose of the initramfs is to bootstrap the system to the point where it can access the root file system. It's firstly unpacked into / (root) directory, which is only a temporary file system. This is where the early userspace stage will take place.

6. Early userspace

The function of this stage is to detect what drivers are needed to properly initialize the main user space filesystem. It operates on the files provided by the initramfs, and it's executed by the kernel. This stage is over when the kernel mounts the real root file system and executes the init program.

7. Late userspace

Finally, this is the last stage of the booting process. The init program is executed by the kernel, it's the first daemon process that is started, and last one to be stopped during shutdown. There are different init systems, but the most popular one is systemd. This particular init system fairly new, and it's used by most of the modern Linux distributions. It has a lot of features, like journaling, socket activation, parallelization, etc. The init program is the one with PID 1, and it's the parent of all other processes.

We can check which init system is used by running the following command:

ps -p 1 -o comm= ## output systemd

or by running pstree command:

pstree | head -n 1 ## output systemd-+-NetworkManager---3*[{NetworkManager}]

The last step is to start the display manager, which is responsible for displaying the login screen. The most popular display managers are GDM (Gnome Display Manager) and LightDM (Light Display Manager).

That's it, we have successfully booted our Linux system. Thanks for reading!