A deep understanding of the Linux Startup Process

Source: Internet
Author: User

The inux system startup process consists of many steps.
Using a standard x86 desktop computer or an Embedded PowerPC target board, most of the processes are surprisingly similar. This article explores Linux from initial preparation to users
The startup process between the startup of a program in the space. Following this process, you can also learn many other startup-related knowledge, such as boot.
Loaders, kernel decompression, memory disk initialization, and other Linux Startup components.
Very early on, starting a computer meant to feed the tapes containing the boot program, or manually loading the boot program by using the front-panel address, data, and control switch. However, it is not necessary to simplify the process.
Let's first make a comprehensive analysis on Linux Startup from a high level. Then, we will review what happened in every independent process. The reference source code on the way will help you not lose your way in the kernel tree, and you can continue to explore it in depth.

I. Overview
Figure 1 provides an overall review of the startup process:

Figure 1. The 20,000-foot view of the Linux boot process




 
When the system is started at the beginning or restarted, the processor will execute code in a well-known location. On a PC, this location is in the Basic Input/Output System, which is what we call BIOS.
Is stored in the flash memory on the motherboard. In an embedded system, the central processor (CPU) activates the reset area to start executing known programs in flash or ROM. In other cases
This process is the same. The BIOS must determine which device is used to start the startup process because the PC provides many flexible optional devices. In the next article, we will discuss more about this.
.
When the boot device is found, the boot loader used in the first phase is loaded into RAM and executed. Here, the boot loader is smaller than the size of one slice, that is, 512 bytes. Its task is to load the boot loader of the second stage.
When the second-stage boot loader is in memory and executed, a pop-up screen is usually displayed, then Linux and optional memory disk initialization (a temporary root file system. For details, visit
Http://likunarmstrong.bokee.com/5502266.html

)
Will be loaded into the memory. When the system image is loaded, the second-stage boot
Loader transfers the control to the kernel image. At the same time, the kernel starts self-extracting and initialization. In this phase, the second phase of the boot
Loader checks the system hardware, enumerates the attached hardware devices, mounts the root device, and then loads the required kernel modules. After completion, the first user space program (init) starts to execute, higher
Hierarchical system initialization starts.
This is the Linux Startup Process. Now, let's go further and explore more details about the Linux Startup Process.

Ii. System Startup

The system startup stage depends on the hardware device on which Linux is started. In an embedded system, when the system is turned on or restarted, the loading environment must be used. Examples include:
U-BOOT, Redboot, and Lucent micromonitor. Embedded platforms are usually bound with boot monitors. These programs are located on the target hardware flash
The specific location of the memory provides a method to download the Linux kernel image to the flash memory, and execute it in the next process. In addition to the ability to store and start Linux images
Some boot monitors can also perform system detection and hardware initialization to a certain extent. In an Embedded Target Board, these boot monitors typically cover the first and second phases of boot
Loader function.

/*************************************** **************************************** *****************/
TIPS: how to view your MBR content. If you want to view the specific content of your MBR, use the following command:
# Dd If =/dev/hda of = MBR. Bin BS = 512 COUNT = 1
# OD-Xa MBR. Bin
The DD command that needs to be run as root reads the first 512 bytes of your first integrated electronic drive or IDE drive and writes them
MBR. Bim file. The OD command prints the binary file in hexadecimal and ASCII code format.
/*************************************** **************************************** *****************/

In a PC, Linux starts from 0xffff0. The first step of BIOS is to perform power-on self-check (post ). The job of post is to check the hardware device. The second step of BIOS is to enumerate the local device and initialize it.
Because of the different BIOS functions, it consists of two parts: the post code runtime service. After the post is completed, it will be cleared from the memory, but the BIOS runtime service will be retained for the target operating system.
To start the operating system, the BIOS runtime service searches for the activated or boot-able devices. The search sequence is determined by the CMOS settings (that is, what we call
BiOS boot sequence ). A soft drive, an optical drive, a partition on a hard disk, a network device, or even a USB flash drive, can be used as a boot device.
Of course, Linux is usually started from a hard disk. The MBR (Master Boot Record) on the hard disk contains a basic boot loader, which is a 512-byte sector located in the first sector of the disk (0 head, 0 track, 1 sector ). When the MBR is loaded into RAM, the BIOS transfers the control to the MBR.

Iii. First-stage Boot Loader

Figure 2. Anatomy of the MBR




 
Master Boot in MBR
Loader is a 512-byte image, which not only contains program code, but also contains a small partition table, as shown in figure 2. The first 446 bytes are the Master Boot
Loader, which contains executable code and error message text. The next 64 bytes is a partition table, which contains records of the four partitions (each partition occupies 16 bytes ). MBR connection
Special number 0xaa55 (note: in the electronics industry, aa55 is indeed a legend. Do you want to know why? Expand it into binary form to see what is the rule) as two bytes
End mark. 0x55aa is also an effective validation for MBR.

The job of the Main Boot Loader is to find and load the second boot
Loader. It analyzes the partition table and finds the active partition to complete this task. When it finds an active partition, it continues to scan the partitions in the remaining partition table to confirm that they are not activated.
After confirmation, the activation record of the active partition is read from the device Ram and executed.

Iv. Phase 2 Boot Loader

It plays a secondary role, or is the second boot loader, which can be called a kernel loader. In this phase, the task is to load the Linux kernel and initialize the memory disk.

/*************************************** ****************************/
TIPS: boot loaders in grub stage
The/boot/GRUB directory contains the boot loaders of stage1, stage2 and stage1.5, and there are also many optional loaders (for example, the CD-ROM uses the iso9660_stage_1_5)
/*************************************** ****************************/

Combine the boot loaders of the first and second phases, that is, in the X86 PC, what we call Linux Loader (lilo) or grand
Uniied
Bootloader (grub ). Grub fixes some lilo defects, so let's take a look at grub (if you want to get more information about
Grub, Lilo, and resources for discussing related topics. For more information, see references)
For grub, a better aspect is that it contains the knowledge of Linux file systems. Unlike Lilo's bare sector, grub is able to access the ext2 or ext3 File System
Load the Linux kernel. It converts a two-stage boot loader to a three-stage boot
Loader. In Stage 1 (MBR), stage1.5 boot will be started.
Loader to understand the special file system formats in Linux kernel images, for example, reiserfs_stage1-5 (used to add
Load) or e2fs + stage00005 (used for loading from wxt2 or ext3 File System ). When the stage1.5 boot
When the loader is loaded and running, the boot loader of stage2 can be loaded.
When stage2 is loaded, grub can display a list of optional kernels (defined in/etc/grub. conf) based on the request conditions, and there are several soft symbolic links.
/Etc/GRUB/menu. lst and
/Etc/grub. conf ). You can select a kernel and modify its additional kernel parameters. At the same time, you can choose to use the shell of the command line to perform more in-depth manual industrial control during the startup process.
.
In the second phase, after the boot loader exists and the memory is in place, you can query the file system. At the same time, the default kernel image and the initialized memory disk image are also loaded into the memory. After all preparations are completed, the boot loader in the second stage will call the kernel image.

V. Kernel

(Note: when translating this chapter, the translator finds that there are already translated articles on the IBM website. Therefore, this chapter focuses on the content on the official website ). When the kernel image is loaded into the memory, and phase 2
After the boot loader releases control, the kernel stage begins. The kernel image is not an executable kernel, but a compressed kernel image. Usually it is
Zimage (compressed image, smaller than KB) or a bzimage (larger compressed image, larger than KB), which uses zlib in advance
Compressed. 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 there is an initial Ram
The disk image 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, start from./ARCH/i386/boot/head. s
The Assembly routine starts execution (see Figure 3 for the main flowchart ). This routine executes some basic hardware settings and calls
Startup_32 in./ARCH/i386/boot/compressed/head. s
Routine. This routine sets a basic environment (stack, etc.) and clears block started by symbol (BSS ). Then call
Decompress_kernel C function (in./ARCH/i386/boot/compressed/Misc. c
To decompress the kernel. After the kernel is decompressed to the memory, you can call it. This is another startup_32 function, but this function is
./ARCH/i386/kernel/head. S.

In this new startup_32 function (also known as the purge program or process)
0), the page table is initialized, 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 call
The start_kernel function (in init/Main. c) brings you into the Linux kernel that is not related to the architecture. Actually, this is
Linux Kernel main function.
/*************************************** ****************************/
TIPS: Manual guidance in grub
In the grub command line, we can use the initrd image to boot a specific kernel. The method is as follows:

Grub> kernel/bzImage-2.6.14.2
[Linux-bzimage, set = 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 list of kernel and initrd images.
/*************************************** ****************************/

Figure 3. Major functions flow for the Linux kernel i386 boot




 
By calling start_kernel, a series of initialization functions are called to set the interrupt, perform further memory configuration, and load the initial RAM disk. Finally, call
Kernel_thread (in arch/i386/kernel/process. c) to start init
Function, which is the first user-space process ). Finally, start the empty task, and now the scheduler can take over the control (when you call cpu_idle
Later ). By enabling interrupt, The preemptible scheduler can periodically take over control and provide multi-task processing capabilities.
During kernel boot, the initial RAM disk (initrd) is loaded into the memory by Phase 2 boot loader, Which is copied to Ram and mounted to the system. This
Initrd will act as Ram
The temporary root file system in and allows the kernel to perform boot without attaching any physical disk. The module required for interaction with peripheral devices may be initrd
Therefore, 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 (through
T_t_root): The initrd root file system is detached and the real root file system is mounted.
The initrd function allows us to create a small Linux
The kernel, including the driver that can be compiled as a load 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
The system is a file system on the disk, so initrd
The function provides a startup method to access the disk and mount the real root file system. In an embedded environment without a hard disk, initrd
It can be the final root file system, or the final root file system can be mounted through the Network File System (NFS.

Vi. Introduction and conclusion of 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 started is usually/sbin/init. But this is not certain. Few embedded systems need to use init.
Provides rich initialization functions (this is configured through/etc/inittab ). In many cases, we can call a simple Shell
Script to start the necessary embedded application.
Similar to Linux, Linux
The boot process is also very flexible and supports a large number of processors and hardware platforms. Initially, loading the bootstrap loader provides a simple way to guide
Linux. Lilo Boot Loader expands the boot capability, but it lacks the file system awareness capability. The latest boot loader, such as grub, allows
Linux boot from some file systems (from minix to reise.

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.