Booting process of Linux explained

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 the 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 IBM PC–compatible personal computers (PCs), this firmware/program is either a BIOS or a UEFI monitor, and is stored in the mainboard. In embedded Linux systems, this firmware/program is called boot ROM.[1] After being loaded into RAM, the 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, and then pass control to this kernel image. The 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.

IBM PC compatible hardware is one architecture Linux is commonly used on; on these systems, the BIOS or UEFI firmware plays an important role.

In BIOS systems, the 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.

In UEFI systems, the Linux kernel can be executed directly by UEFI firmware via EFISTUB,[2] but usually uses GRUB 2 or systemd-boot as a bootloader.[3] [4]

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:

Kernel

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_32will 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_kernelexecutes 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,[7] 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).[8] 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

References

Works cited

External links

Notes and References

  1. Book: Bin . Niu . 2020 International Symposium on Computer Engineering and Intelligent Communications (ISCEIC) . Dejian . Li . Zhangjian . LU . Lixin . Yang . Zhihua . Bai . Longlong . He . Sheng . Liu . August 2020 . 978-1-7281-8171-4 . 5–8 . Research and design of Bootrom supporting secure boot mode . 10.1109/ISCEIC51027.2020.00009 . https://ieeexplore.ieee.org/document/9325327 . 231714880.
  2. Web site: EFI stub kernel - Gentoo Wiki . 2020-11-02 . wiki.gentoo.org.
  3. Web site: Kinney . Michael . 1 September 2000 . Solving BIOS Boot Issues with EFI . dead . https://web.archive.org/web/20070123141151/http://systems.cs.colorado.edu/Documentation/IntelDataSheets/xscalemagazine.pdf . 23 January 2007 . 14 September 2010 . 47–50.
  4. News: 23 September 2011 . MS denies secure boot will exclude Linux . 24 September 2011 . The Register.
  5. Web site: Product Documentation . Redhat.com . 2013-09-30 . 2014-01-22.
  6. Web site: Product Documentation . Redhat.com . 2013-09-30 . 2014-01-22.
  7. Web site: Initramfs arrives . 14 November 2011.
  8. http://oldfield.wattle.id.au/luv/boot.html Linux Boot Process - by Kim Oldfield (2001)
  9. Web site: From Power Up To Bash Prompt: Init . users.cecs.anu.edu.au.
  10. Web site: init . man.he.net.
  11. Web site: systemd README . 2012-09-09 . freedesktop.org.