Overview
Figure 1 Linux system process big view
First Stage boot loader
The master boot loader in the MBR is a 512-byte image that contains the program code and a small partition table (see Figure 2). The first 446 bytes are the master boot loader, which contains the executable code and the error message text. The next 64 bytes are the partitioned table, which contains 4 partitions of records (the size of each record is 16 bytes). The MBR ends with a byte (0xaa55) of two special digits. This number is used to check the validity of the MBR.
Figure 2 MBR Anatomy
Second Stage boot loader
The secondary boot loader (second-stage boot loader) can be more visually referred to as the kernel loader. The task at this stage is to load the Linux kernel and the optional initial RAM disk .
GRUB Stage Boot Loader
/boot/grubThe Table of contents containsstage1、stage1.5Andstage2boot loader, and many other loaders (for example, Cr-rom is usingiso9660_stage_1_5)。
The good thing about GRUB is that it contains knowledge about the Linux file system. Instead of using bare sectors like LILO, GRUB can load the Linux kernel from the ext2 or ext3 file system. It does this by converting the two-stage boot loader into a three-stage bootloader. Phase 1 (MBR) guides a phase 1.5 boot loader that understands the special file system that contains the Linux kernel image. Examples in this regard includereiserfs_stage1_5(To be loaded from the Reiser log file system) ore2fs_stage1_5(To be loaded from the ext2 or ext3 file system). When the boot loader for phase 1.5 is loaded and running, the boot loader for phase 2 is ready to load.
When phase 2 is loaded, GRUB can display a list of available kernels on request (/etc/grub.confAs well as a few soft symbolic links/etc/grub/menu.lstAnd/etc/grub.conf)。 We can choose the kernel and even modify the additional kernel parameters. Alternatively, we can use a command-line shell to perform advanced manual control of the boot process.
After the second stage of the boot loader is loaded into memory, the file system can be queried and the default kernel image and initrd image are loaded into memory . When these image files are ready, the boot loader for stage 2 can invoke the kernel image.
Kernel
Figure 3 Linux Kernel i386 boot function flow
by callingstart_kernel, a series of initialization functions are called to set interrupts, perform further memory configuration, and load the initial RAM disk. Finally, to invoke thekernel_thread(Inarch/i386/kernel/process.c) to start theinitfunction, which is the first user-space process (user-space). Finally, start the empty task, and now the scheduler can take over control (in the callcpu_idleLater). By enabling interrupts, the preemptive scheduler can periodically take over control to provide multitasking capabilities.
During the kernel boot process, the initial RAM disk (initrd) is loaded into memory by the Phase 2 boot loader, which is copied into RAM and mounted on the system. This oneinitrdis used as a temporary root file system in RAM and allows the kernel to fully boot without mounting any physical disks. Because the modules needed to interact with the peripherals may beinitrd, so the kernel can be very small, but it still needs to support a large number of possible hardware configurations. After the kernel boots, you can formally equip the root file system (bypivot_root): This willinitrdThe root file system is unloaded and the real root filesystem is mounted.
Decompress_kernel output
The function decompress_kernel is where we usually see the decompression message:
Uncompressing Linux ... Ok, booting the kernel.
initrdfunction allows us to create a small Linux kernel that includes drivers that are compiled as loadable modules. These loadable modules provide the kernel with a way to access the file system on disk and disk, and provide drivers for other hardware. Because the root file system is one on diskFile SystemSoinitrdThe function provides a startup method to gain access to the disk and mount the real root filesystem. In an embedded environment without a hard drive,initrdCan be the ultimate root file system, or you can mount the final root file system via a network File system (NFS)
Init
Once the kernel is booted and initialized, the kernel can launch its first user-space application. This is the first call to a program compiled using the standard C library. Prior to this, no standard C application was implemented.
On a desktop Linux system, the first program to start is usually/sbin/init。 But that's not certain. Few embedded systems will need to useinitThe rich initialization functionality provided by the/etc/inittabConfigured). In many cases, we can invoke a simple shell script to launch the required embedded application.