Linux boot process insider

Source: Internet
Author: User

The process of guiding the Linux system involves many stages. Whether you boot a standard x86 desktop system or an Embedded PowerPC machine, many procedures are surprisingly similar. This article will explore the Linux boot process, from initial boot to starting the first user space application. In this article, you will learn a variety of boot-related topics, such as boot loader, kernel decompression, initial RAM disk, and other Linux boot elements.

In the early days, starting a computer meant to feed the computer with a tape containing the boot program, or manually loading the boot program using the front-end panel address/data/control switch. Although the current computer has been equipped with many tools to simplify the boot process, it is not necessary to simplify the entire process.

Let's first look at the Linux boot process from an advanced perspective, so that we can see the entire process. Then we will review what happened in each step. In the entire process, refer to the kernel source code to help us better understand the kernel source code tree and conduct in-depth analysis on it later.

Overview

Figure 1 shows the view at the height of 20,000 feet.

Figure 1. View of Linux boot process at 20,000 feet

When the system is started for the first time or the system is reset, the processor executes a code at a known position. In a personal computer (PC), this location is in the Basic Input/Output System (BiOS), which is stored in the flash memory on the motherboard. The Central Processing Unit (CPU) in the embedded system calls this Reset vector to start a program located at a known address in the Flash/ROM. In both cases, the results are the same. Because the PC provides a lot of flexibility, the BIOS must determine which device to use to boot the system. We will introduce this process in detail later.

After a boot device is found, the first-stage boot loader is loaded into RAM and executed. This pilot loader is smaller than 512 bytes (one slice) in size and serves to load the pilot loader in the second stage.

When the second phase of boot loader is loaded into RAM and executed, an animation screen is usually displayed, and Linux and an optional initial RAM disk (temporary root file system) load to memory. When the image is loaded, the boot loader in the second stage will hand over the control to the kernel image, and then the kernel can be decompressed and initialized. In this phase, the boot loader in the second phase will detect system hardware, enumerate hardware devices connected to the system, mount the root device, and then load the necessary kernel modules. After completing these operations, start the first user space program (initAnd perform advanced system initialization.

This is the entire process of Linux boot. Now let's take a deeper look at this process and take a deeper look at some detailed information about the Linux boot process.



Back to Top

System Startup

The system startup phase depends on the hardware that directs the Linux system. On an embedded platform, a startup environment is used when the system is powered on or reset. Examples include U-boot, Redboot, and Lucent micromonitor. Embedded platforms are usually sold together with the boot monitor. These programs are located in a special area of flash memory on the target hardware and provide methods to download the Linux kernel image to flash and continue executing it. In addition to storing and guiding Linux images, these boot monitors also perform a certain level of system testing and hardware initialization processes. In an embedded platform, these boot monitors generally involve boot loaders in the first and second phases.

Extract MBR Information

To view the MBR content, run the following command:

# dd if=/dev/hda of=mbr.bin bs=512 count=1 # od -xa mbr.bin

ThisddThe command must be run as the root user. It reads the content of the first 512 bytes from/dev/hda (the first IDE disk) and writes itmbr.binFile.odThe command prints the contents of the binary file in hexadecimal format and ASCII format.

In PC, boot Linux starts from the BIOS address 0xffff0. The first step of BIOS is power-on self-check (post ). Post checks the hardware. The second step of BIOS is to enumerate and initialize the local device.

Given the different usage of BIOS functions, BIOS consists of two parts: Post code and runtime service. After the post is completed, it is cleared from the memory, but the service is still kept in the memory during the BIOS runtime, which can be used by the target operating system.

To Boot an operating system, the BIOS searches active and bootable devices in the order defined by the CMOS settings during runtime. The boot device can be a floppy disk, a CD-Rom, a partition on the hard disk, a device on the network, or even a USB flash memory.

Generally, Linux boot is from the hard disk. The primary Boot Record (MBR) contains the primary boot loader. MBR is a 512-byte sector located in the first sector of the disk (0-way, 0-way, 1-sector ). When the MBR is loaded into RAM, the BIOS will hand over the control to the MBR.



Back to Top

Phase 1 Boot Loader

The Master Boot Loader in MBR is a 512-byte image that contains program code and a Small Partition Table (see figure 2 ). The first 446 bytes are the primary boot loader, which contains executable code and error message text. The next 64 bytes are the partition table, which contains records of 4 partitions (the size of each record is 16 bytes ). MBR ends with two special numbers (0xaa55. This number is used to check the validity of MBR.

Figure 2. MBR Analysis

The main bootstrap loader is to find and load the next bootstrap loader (Phase 2 ). This function is implemented by searching for an active partition in a partition table. When an active partition is found, it scans other partitions in the partition table to ensure that they are not active. After the verification is complete, read the Boot Record of the active partition from the device to Ram and execute it.



Back to Top

Stage 2 Boot Loader

The next Boot Loader (the second phase of the boot loader) can be more vividly called the kernel loader. In this phase, the task is to load the Linux kernel and the optional initial RAM disk.

Grub stage Boot Loader

/boot/grubThe directory containsstage1,stage1.5Andstage2Bootstrap loader, and many other loaders (for example, the CR-ROM usesiso9660_stage_1_5).

In an X86 PC environment, the boot loader in the first and second phases is called Linux Loader (lilo) or grand uniied bootloader (grub ). Because lilo has some shortcomings and grub has overcome these shortcomings, let's take a look at grub. (For more information about grub, Lilo, and related topics, see references later in this article .)

One good thing about grub is that it contains knowledge about Linux file systems. Grub does not use bare sectors like Lilo, but can load the Linux kernel from the ext2 or ext3 file system. It implements this function by converting a two-phase boot loader into a three-phase boot loader. Stage 1 (MBR) directs a boot loader of stage 1.5 to understand the special file systems that contain Linux kernel images. Examples include:reiserfs_stage1_5(To load from the Reiser Log File System) ore2fs_stage1_5(To load from the ext2 or ext3 File System ). When phase 2's boot loader is loaded and running, phase 2's boot loader can be loaded.

After phase 2 is loaded, grub can display the list of available kernels during the request (/etc/grub.confAnd there are several soft symbolic links./etc/grub/menu.lstAnd/etc/grub.conf). We can select the kernel or even modify additional kernel parameters. In addition, we can also use a shell command line to perform advanced manual control over the boot process.

After loading the boot loader of the second stage into the memory, you can query the file system and run the default kernel image andinitrdThe image is loaded into the memory. After these image files are ready, the boot loader in phase 2 can call the kernel image.


Kernel

Manual guidance in grub

In the grub command line, we can useinitrdThe image is used to boot a specific kernel as follows:

grub> kernel /bzImage-2.6.14.2
[Linux-bzImage, setup=0x1400, size=0x29672e]

grub> initrd /initrd-2.6.14.2.img
[Linux-initrd @ 0x5f13000, 0xcc199 bytes]

grub> boot

Uncompressing Linux... Ok, booting the kernel.

If you do not know the name of the kernel to boot, you only need to use the slash (/) and press the tab key. Grub displays the kernel andinitrdImage list.

When the kernel image is loaded into the memory and the boot loader in phase 2 releases control, the kernel phase begins. The kernel image is not an executable kernel, but a compressed kernel image. Generally, it is a zimage (compressed image, smaller than KB) or a bzimage (larger compressed image, larger than KB). It is compressed using zlib in advance. In front of this kernel image is a routine that implements a small amount of hardware settings, decompress the kernel contained in the kernel image, and then put it into the high-end memory, if an initial RAM disk image exists, it will be moved to the memory and marked for future use. The routine then calls the kernel and starts the kernel boot process.

When bzimage (for i386 image) is called./arch/i386/boot/head.SOfstartThe Assembly routine starts execution (see Figure 3 for the main flowchart ). This routine executes some basic hardware settings and calls./arch/i386/boot/compressed/head.SInstartup_32Routine. This routine sets a basic environment (stack, etc.) and clears block started by symbol (BSS ). Then calldecompress_kernelC function (in./arch/i386/boot/compressed/misc.cTo decompress the kernel. After the kernel is decompressed to the memory, you can call it. This is anotherstartup_32Function, but this function is./arch/i386/kernel/head.S.

In this newstartup_32The page table is initialized in the function (also known as the purge program or process 0) and the memory paging function is enabled. It then detects the CPU type for any optional Floating Point Unit (FPU) and stores it for future use. Then callstart_kernelFunction (ininit/main.c. In fact, this is the Linux KernelmainFunction.

Figure 3. Main Function procedures guided by Linux kernel i386

By callingstart_kernelWill call a series of initialization functions to set the interrupt, execute further memory configuration, and load the initial RAM disk. Finally, callkernel_thread(Inarch/i386/kernel/process.c) To startinitFunction, which is the first user-space process ). Finally, start the empty task, and now the scheduler can take over the control (cpu_idleLater ). By enabling interrupt, The preemptible scheduler can periodically take over control and provide multi-task processing capabilities.

During kernel boot, the initial RAM disk (initrdIs loaded to the memory by Phase 2 pilot loader, which will be copied to Ram and mounted to the system. ThisinitrdIt is used as a temporary root file system in Ram and allows the kernel to perform boot without attaching any physical disk. The module required for interaction with peripheral devices may beinitrdTherefore, the kernel can be very small, but it still needs to support a large number of possible hardware configurations. After the kernel boot, you can officially equip the root file system (throughpivot_root):initrdUnmount the root file system and mount the real root file system.

Decompress_kernel output

Functiondecompress_kernelIt is the place where we usually see the unzipping message:

Uncompressing Linux... Ok, booting the kernel.

initrdFunction allows us to create a small Linux kernel, including a driver that can be compiled as a module. These loadable modules provide the kernel with methods to access the file systems on disks and disks, and provide drivers for other hardware. Because the root file system isFile System, SoinitrdThe function provides a startup method to access the disk and mount the real root file system. In an embedded environment without a hard disk,initrdIt can be the final root file system, or the final root file system can be mounted through the Network File System (NFS.



Back to Top

Init

After the kernel is booted and initialized, the kernel can start its first user space application. This is the first program to call compiled using the standard C library. No standard C applications have been executed before.

In a Desktop Linux system, the first program to be started is usually/sbin/init. But this is not certain. Few embedded systems need to useinitThe rich initialization functions provided/etc/inittab). In many cases, we can call a simple shell script to start the necessary embedded application.



Back to Top

Conclusion

Similar to Linux, the Linux boot process is flexible and supports many processors and hardware platforms. Initially, loading the boot loader provides a simple way to boot Linux without any overhead. Lilo Boot Loader expands the boot capability, but it lacks the file system awareness capability. The latest boot loader, such as grub, allows Linux to boot from some file systems (from minix to reise.

References

Learning

  • For more information, see the original article on the developerworks global site.

  • Boot records revealed is a good resource for MBr and various Boot Loader programs. This resource is not only a compilation of MBR data, but also a discussion of grub, Lilo, and various Windows boot loader issues.
  • View the disk geometry page to understand the disk and its structure. You will find useful disk attributes.
  • Live CD is an operating system that can be booted from a CD or DVD. It does not require a hard disk.
  • "Boot loader contention: Learn about Lilo and grub" (developerworks, August 2005) Details Lilo and grub boot loader.
  • In the LPI test preparation series on developerworks, we can learn more about guiding Linux systems and the basic Linux knowledge required to take the System Administrator certification test.
  • Lilo is a pioneer in grub, but we may find that it can still guide Linux.
  • The mkintrd command is used to create an initial RAM disk image. This command can be used to build an initial root file system. It can be used to guide the configuration of Block devices required to access the real root file system in advance.
  • In the Debian Linux kernel project, we can find more information about Linux kernel, boot, and embedded development.
  • In the developerworks Linux area, you can find more resources for Linux developers.
  • Stay tuned to developerworks technical events and network broadcasts.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.