Booting process of Linux

Appearance move to sidebar hide

The Linux booting process involves multiple stages and is in many ways similar to the BSD and other Unix-style boot processes, from which it derives. Although the Linux booting process depends very much on the computer architecture, those architectures share similar stages and software components, including system startup, bootloader execution, loading and startup of a Linux kernel image, and execution of various startup scripts and daemons. Those are grouped into 4 steps: system startup, bootloader stage, kernel stage, and init process. When a Linux system is powered up or reset, its processor will execute a specific firmware/program for system initialization, such as Power-on self-test, invoking the reset vector to start a program at a known address in flash/ROM (in embedded Linux devices), then load the bootloader into RAM for later execution. In personal computer (PC), not only limited to Linux-distro PC, this firmware/program is called BIOS, which is stored in the mainboard. In embedded Linux system, this firmware/program is called boot ROM. After being loaded into RAM, bootloader (also called first-stage bootloader or primary bootloader) will execute to load the second-stage bootloader (also called secondary bootloader). The second-stage bootloader will load the kernel image into memory, decompress and initialize it then pass control to this kernel image. Second-stage bootloader also performs several operation on the system such as system hardware check, mounting the root device, loading the necessary kernel modules, etc. Finally, the first user-space process (init process) starts, and other high-level system initializations are performed (which involve with startup scripts).

For each of these stages and components, there are different variations and approaches; for example, GRUB, coreboot or Das U-Boot can be used as bootloaders (historical examples are LILO, SYSLINUX or Loadlin), while the startup scripts can be either traditional init-style, or the system configuration can be performed through modern alternatives such as systemd or Upstart.

System startup

System startup has different steps based on the hardware that Linux is being booted on, especially between embedded Linux and Linux PC. As mentioned earlier in the introduction part, during system startup stage, the BIOS firmware is called. IBM PC compatible hardware is one architecture Linux is commonly used on; on these systems, the BIOS plays an important role. BIOS will respectively perform power-on self test (POST), which is to check the system hardware, then enumerate local device and finally initialize the system. For system initialization, BIOS will start by searching for the bootable device on the system which stores the OS. A bootable device can be storage devices like floppy disk, CD-ROM, USB flash drive, a partition on a hard disk (where a hard disk stores multiple OS, e.g Windows and Fedora), a storage device on local network, etc. A hard disk to boot Linux stores the Master Boot Record (MBR), which contains the first-stage/primary bootloader in order to be loaded into RAM. IBM PC compatible replaces BIOS by UEFI. In UEFI systems, the Linux kernel can be executed directly by UEFI firmware via EFISTUB, but usually uses GRUB 2 or systemd-boot as a bootloader.

The system startup stage on embedded Linux system starts by executing the firmware/program on the on-chip boot ROM, which is stored on the storage device of the system like USB flash drive, SD card, eMMC, NAND flash, NOR flash, etc. The sequences of system startup in on-chip boot ROM varies by processors but all include hardware initialization and system hardware testing steps. For example in a system with an i.MX7D processor and a bootable device which stores the OS (including U-Boot, an external bootloader), the on-chip boot ROM sets up the DDR memory controller at first which allows the boot ROM's program to obtain the SoC configuration data from the external bootloader on the bootable device. The on-chip boot ROM then loads the U-Boot into RAM for the bootloader stage.

Bootloader stage

The first stage bootloader, which is a part of the MBR, is a 512-byte image containing the vendor-specific program code and a partition table. As mentioned earlier in the introduction part, the first stage bootloader will find and load the second stage bootloader. It does this by searching in the partition table for an active partition. After finding an active partition, first stage bootloader will keep scanning the remaining partitions in the table to ensure that they're all inactive. After this step, the active partition's boot record is read into RAM and executed as the second stage bootloader. The job of the second stage bootloader is to load the Linux kernel image into memory, and optional initial RAM disk. Kernel image isn't an executable kernel, but a "compressed file" of the kernel instead, compressed into either zImage or bzImage formats with zlib.

In x86 PC, first- and second-stage bootloaders are combined into the GRand Unified Bootloader (GRUB), and formerly Linux Loader (LILO). GRUB 2, which is now used, differs from GRUB 1 by being capable of automatic detection of various operating systems and automatic configuration. The stage1 is loaded and executed either by the BIOS from the Master boot record (MBR). The intermediate stage loader (stage1.5, usually core.img) is loaded and executed by the stage1 loader. The second-stage loader (stage2, the /boot/grub/ files) is loaded by the stage1.5 and displays the GRUB startup menu that allows the user to choose an operating system or examine and edit startup parameters. After a menu entry is chosen and optional parameters are given, GRUB loads the linux kernel into memory and passes control to it. GRUB 2 is also capable of chain-loading of another bootloader. In UEFI systems, the stage1 and stage1.5 usually are the same UEFI application file (such as grubx64.efi for x64 UEFI systems).

Beside GRUB, there are some more popular bootloaders:

Historical bootloaders, no longer in common use, include:


The kernel stage occurs after the bootloader stage. The Linux kernel handles all operating system processes, such as memory management, task scheduling, I/O, interprocess communication, and overall system control. This is loaded in two stages – in the first stage, the kernel (as a compressed image file) is loaded into memory and decompressed, and a few fundamental functions are set up such as basic memory management, minimal amount of hardware setup. It's worth noting that kernel image is self-decompressed, which is a part of the kernel image's routine. For some platforms (like ARM 64-bit), kernel decompression has to be performed by the bootloader instead, like U-Boot.

For details of those steps, take an example with i386 microprocessor. When its bzImage is invoked, function start() (of ./arch/i386/boot/head.S) is called to do some basic hardware setup then calls startup_32() (located in ./arch/i386/boot/compressed/head.S). startup_32()will do basic setup to environment (stack, etc.), clears the Block Started by Symbol (BSS) then invokes decompress_kernel() (located in ./arch/i386/boot/compressed/misc.c) to decompressed the kernel. Kernel startup is then executed via a different startup_32() function located in ./arch/i386/kernel/head.S. The startup function startup_32() for the kernel (also called the swapper or process 0) establishes memory management (paging tables and memory paging), detects the type of CPU and any additional functionality such as floating point capabilities, and then switches to non-architecture specific Linux kernel functionality via a call to start_kernel() located in ./init/main.c.

start_kernel()executes a wide range of initialization functions. It sets up interrupt handling (IRQs), further configures memory, mounts the initial RAM disk ("initrd") that was loaded previously as the temporary root file system during the bootloader stage. The initrd, which acts as a temporary root filesystem in RAM, allows the kernel to be fully booted and driver modules to be loaded directly from memory, without reliance upon other devices (e.g. a hard disk). initrd contains the necessary modules needed to interface with peripherals, e.g SATA driver, and support a large number of possible hardware configurations. This split of some drivers statically compiled into the kernel and other drivers loaded from initrd allows for a smaller kernel. initramfs, also known as early user space, has been available since version 2.5.46 of the Linux kernel, with the intent to replace as many functions as possible that previously the kernel would have performed during the startup process. Typical uses of early user space are to detect what device drivers are needed to load the main user space file system and load them from a temporary filesystem. Many distributions use dracut to generate and maintain the initramfs image.

The root file system is later switched via a call to pivot_root() which unmounts the temporary root file system and replaces it with the use of the real one, once the latter is accessible. The memory used by the temporary root file system is then reclaimed.

Finally, kernel_thread (in arch/i386/kernel/process.c) is called to start the Init process (the first user-space process), and then starts the idle task via cpu_idle().

Thus, the kernel stage initializes devices, mounts the root filesystem specified by the bootloader as read only, and runs Init (/sbin/init) which is designated as the first process run by the system (PID = 1). A message is printed by the kernel upon mounting the file system, and by Init upon starting the Init process.

According to Red Hat, the detailed kernel process at this stage is therefore summarized as follows:

"When the kernel is loaded, it immediately initializes and configures the computer's memory and configures the various hardware attached to the system, including all processors, I/O subsystems, and storage devices. It then looks for the compressed initrd image in a predetermined location in memory, decompresses it, mounts it, and loads all necessary drivers. Next, it initializes virtual devices related to the file system, such as LVM or software RAID before unmounting the initrd disk image and freeing up all the memory the disk image once occupied. The kernel then creates a root device, mounts the root partition read-only, and frees any unused memory. At this point, the kernel is loaded into memory and operational. However, since there are no user applications that allow meaningful input to the system, not much can be done with it." An initramfs-style boot is similar, but not identical to the described initrd boot.

At this point, with interrupts enabled, the scheduler can take control of the overall management of the system, to provide pre-emptive multi-tasking, and the init process is left to continue booting the user environment in user space.

Init process

Once the kernel has started, it starts the init process, a daemon which then bootstraps the user space, for example by checking and mounting file systems, and starting up other processes. The init system is the first daemon to start (during booting) and the last daemon to terminate (during shutdown).

Historically this was the "SysV init", which was just called "init". More recent Linux distributions are likely to use one of the more modern alternatives such as systemd. Below is a summary of the main init processes:

See also


  1. ^ M. Tim Jones 2006, "Introduction", "The process of booting a Linux® system consists of a number of stages. But whether you're booting a standard x86 desktop or a deeply embedded PowerPC® target, much of the flow is surprisingly similar."
  2. ^ a b c d e f g M. Tim Jones 2006, "Overview", "Figure 1. The 20,000-foot view of the Linux boot process"
  3. ^ M. Tim Jones 2006, "Linux booting process are grouped into 4 stages, based on IBM source"
  4. ^ Bin, Niu; Dejian, Li; Zhangjian, LU; Lixin, Yang; Zhihua, Bai; Longlong, He; Sheng, Liu (August 2020). "Research and design of Bootrom supporting secure boot mode". 2020 International Symposium on Computer Engineering and Intelligent Communications (ISCEIC). pp. 5–8. doi:10.1109/ISCEIC51027.2020.00009. ISBN 978-1-7281-8171-4. S2CID 231714880.
  5. ^ a b c d Alberto Liberal De Los Ríos 2017, p. 28, , "Linux Boot Process".
  6. ^ a b c d e f M. Tim Jones 2006, , "Stage 1 boot loader".
  7. ^ a b c d e M. Tim Jones 2006, , "System startup".
  8. ^ "EFI stub kernel - Gentoo Wiki". Retrieved 2020-11-02.
  9. ^ Kinney, Michael (1 September 2000). "Solving BIOS Boot Issues with EFI" (PDF). pp. 47–50. Archived from the original (PDF) on 23 January 2007. Retrieved 14 September 2010.
  10. ^ "MS denies secure boot will exclude Linux". The Register. 23 September 2011. Retrieved 24 September 2011.
  11. ^ Alberto Liberal De Los Ríos 2017, p. 29, , "Linux Boot Process".
  12. ^ a b M. Tim Jones 2006, , "Stage 2 boot loader".
  13. ^ a b c d e f g h i j k l m n M. Tim Jones 2006, , "Kernel".
  14. ^ a b "Product Documentation". 2013-09-30. Retrieved 2014-01-22.
  15. ^ "Product Documentation". 2013-09-30. Retrieved 2014-01-22.
  16. ^ Alberto Liberal De Los Ríos 2017, p. 20, , "Bootloader".
  17. ^ "Initramfs arrives". Retrieved 14 November 2011.
  18. ^ a b c Linux Boot Process - by Kim Oldfield (2001)
  19. ^ M. Tim Jones 2006, , "Init".
  20. ^ "From Power Up To Bash Prompt: Init".
  21. ^ "init".
  22. ^ "systemd README". Retrieved 2012-09-09.

Works cited

External links