One. The Linux boot approximate process is as follows:
POST--Boot Sequence (BIOS)--Boot Loader (MBR)--Kernel (RAMDisk)--rootfs--Switchroot In/init---/etc/inittab,/etc/init/*.conf--Set default run level--system initialization script--close or start the service at the corresponding level and start the terminal, the flowchart is as follows:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/78/F9/wKiom1aE17KTQ8xsAANWq_R4oNA991.png "title=" 6466723_1367538867faiu.png "alt=" Wkiom1ae17ktq8xsaanwq_r4ona991.png "/>
Two. Detailed analysis process
1.
Post post: Computer host when the power is turned on, then you will hear a drop, the system starts the post (Post-power on selftest) self-test start, the process is mainly to detect computer hardware equipment such as: CPU, Memory, motherboard, graphics card, CMOS and other equipment if there is a failure, if there is a hardware failure will be in two cases: for a serious failure (fatal failure) downtime, at this time due to a variety of initialization operation has not been completed, can not give any hint or signal, for non-serious failure to give a hint or sound alarm signal, waiting for user processing), If there is no fault, post complete its own relay task, which will transfer the tail work to the BIOS processing.
2.
BIOS: Computer power-on self-test after the completion of the first reading is the BIOS (basic input and output system), the BIOS recorded the motherboard chipset and related settings, such as CPU and interface device communication frequency, boot device search order, size and type of hard disk, system time, external bus, The I/O address of various interface devices, IRQ interrupt information that has been communicated with the CPU, so start the BIOS setup first if you want to start successfully. The BIOS now reads the first sector of the hard drive (mbr,512 bytes) and executes the code inside.
In fact, the BIOS here does not care what is in the first sector of the boot device, it is simply responsible for reading the sector content and executing it. After that, the control of the system boot is transferred to the code of the MBR section, and in the PC, Linux starts from the 0XFFFF0 address.
3.
Boot Loader system booting, let's first look at the MBR, which is the abbreviation for the Master Boot record. The hard disk's 0 cylinders, 0 heads, and 1 sectors are referred to as the primary boot sector. It consists of three parts, the main boot program (Bootloader) hard disk partition table DPT (disk Partition table) and the hard disk valid flag (55AA), the structure diagram is as follows:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/78/FF/wKioL1aE4G-SNgU8AABOPhrk2l4392.png "title=" linux_ Boot2.png "alt=" Wkiol1ae4g-sngu8aabophrk2l4392.png "/>
The disk partition table contains the following three parts:
1). Partition ID (5: Enshin 82:swap 83:linux 8E:LVM fd:raid)
2). Partition starting Magnetic column
3). Number of magnetic columns in partition
4. Main boot device loading bootloader
Typically, common boot programs such as Lilo and Grub are installed directly in the MBR. Let's take grub as an example to analyze the boot process. Grub guidance is also divided into two stages stage1 stage, stage1.5 stage and stage2 stage;
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/79/01/wKiom1aE4b2RZo8mAADt0NaEP4A521.png "title=" 6466723_1367538871uldj.png "alt=" Wkiom1ae4b2rzo8maadt0naep4a521.png "/> This shows that the Grub directory contains these three stages of the file, what their respective role?
Stage1:stage1 is written directly to the MBR, so that when the machine starts to detect the hardware, it gives control to the grub code. That is, the STAGE1 code is stored in the first 446 bytes of space seen above. The BIOS loads the stage1 into the in-memory 0x7c00 and jumps to execution. Stage1 (/stage1/start. S) is very simple, just read the hard disk 0 first 0 Channel 2 sectors into memory. At this point, stage1 is not capable of identifying the file system.
The STAGE1 is primarily responsible for the handover between the BIOS and grub, loading the boot files stored in each partition. For example Linux under/boot/grub/. Some of the following files. This is part of the bootloader that is actually placed in the MBR.
The size of the Stage1 file is exactly 512 bytes, and the fact that the Stage1 file is actually a backup of bootloader in the MBR is bootloader rather than MBR because the first 446 bytes of the file are the same as the MBR.
Stage1.5:grub started with no OS and no filesystem concept. So when did grub start to have filesystem functionality? This is what stage1.5 does, and after stage1.5, grub can recognize the filesystem, and it can recognize the loading file on disk. Start. The contents of those sectors on the disks that are loaded by s are the code of the file system (that is, start. S of the assembly code, interested in the package can go to learn about it, the (about 14 sectors) loaded into memory, it has the ability to operate the file on the boot device, the file system support code to memory, we do not need to directly call INT 13 loading sector content, we have a file system, We can manipulate the file directly. Then/boot/grub/stage2 such a relatively large file can be directly manipulated. Grub then has the ability to access the Stage2 file in the/boot partition/boot/grub directory, loading the stage2 into memory and executing.
The GRUB approach is to install the stage1.5 on the top 32K of the hard drive, and Stage1 is responsible for going to the area to find and execute the stage1.5.
Stage2:grub core, let the user in the way of the option to load the operating system, modify options and new parameters, usually when the boot start to see the grub options, information, and modify the grub background and other functions are stage2 provided, Stage2 will read into/boot/ grub/grub.conf configuration file.
Summarize:
1) The BIOS will give control to the main boot area of the hard drive, i.e. MBR.
2) the bootloader (stage1) in the MBR loads the stage1_5 through the built-in address;
3) bootloader through the content of Stage1_5, the stage2 in the partition is loaded;
4) Stage2 can now load the grub.conf file in the file system, allowing the user to see the options interface.
Below we can see the Linux kernel Vmlnuz,grub, initrd are in the/boot directory:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/79/05/wKiom1aE7miRdAHCAAAQ5dScYbs309.png "title=" QQ picture 20151231165822.png "alt=" Wkiom1ae7mirdahcaaaq5dscybs309.png "/>
Then look at the grub file:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/79/03/wKioL1aE7rjSpal4AAGWEQLIfT8814.png "title=" 6466723_1367538876hur6.png "alt=" Wkiol1ae7rjspal4aagweqlift8814.png "/>
And look at the grub.conf configuration file.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/79/03/wKioL1aE7x2BTktIAAK9BjWgBvs859.png "title=" 9996466723_1367538886btus.png "alt=" Wkiol1ae7x2btktiaak9bjwgbvs859.png "/>
5.Kernel: When Stage2 is loaded into memory execution, it will first parse the grub configuration file/boot/grub/ Grub.conf, then loads the kernel image into memory and transfers control to the kernel. The kernel immediately initializes the devices in the system and makes the relevant configuration work, including CPU, I/O, storage devices, etc., and mounts the root filesystem (root switch) in a read-write manner, and there is a " The first chicken or the egg file, "What is it exactly?"
To access the real root file system (ROOTFS), you have to load the root file system of the device, then the root file system is not mounted, to mount the root file system and to load the root file system driver, what to do? In order to solve this problem, this is the use of the Initrd file.
In terms of the kernel initialization of the content to be done to do a simple summary:
Probe hardware-load driver (INITRD), Mount root file system->rootfs (/sbin/init)
Detailed explanations are as follows:
Linux device driver loading, some drivers are directly compiled into the kernel image, and the other driver is in the form of modules in INITRD (RAMDisk).
The Linux kernel needs to adapt to a variety of hardware architectures, but it is impractical to incorporate all hardware drivers into the kernel, and it is not possible for the kernel to write device drivers to the kernel for each new hardware structure. In fact, the kernel image of Linux only contains the basic hardware driver, the system hardware information is detected during the system installation, and some device drivers are written to INITRD according to the installation information and system hardware information. This allows a part of the device driver to be loaded in INITRD at a later time when the system is booted. Here it is necessary to give you a little more about initrd this stuff:
The English meaning of INITRD is bootloader initialized RAM disk, which is the memory disk initialized by boot loader. Before the linu2.6 kernel is booted, boot loader loads the Initrd file from the storage media into memory, which accesses the Initrd file system in the memory before accessing the real root filesystem. In the case where boot loader is configured with INITRD, the kernel boot is divided into two phases, the first stage is init in the Initrd file system, the loading of the driver module is completed, and the second stage executes the/sbin/init process in the real root file system.
Let's take a look at the Initrd file:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/79/04/wKioL1aE8tvCqTq2AAKJaCCxuog128.png "title=" Initrd.png "alt=" Wkiol1ae8tvcqtq2aakjaccxuog128.png "/>
Then look at the contents of the Init script:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/79/04/wKioL1aE9CLjfWqmAAJCY2ymd-U053.png "title=" Init script 1.png "alt=" Wkiol1ae9cljfwqmaajcy2ymd-u053.png "/>650) this.width=650; src=" http://s3.51cto.com/wyfs02/ M02/79/05/wkiom1ae9dtsl1ajaaierdx3yii556.png "title=" Init2 script 2.png "alt=" Wkiom1ae9dtsl1ajaaierdx3yii556.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/79/04/wKioL1aE9IXQcAzRAAJhV9hGEfc928.png "title=" Init script 3.png "alt=" Wkiol1ae9ixqcazraajhv9hgefc928.png "/>
From the script content above we can see the main work of the INIT process:
Mount: Mount/proc in Initrd,/sys/dev to the corresponding directory in the current primary partition
Create directory:/dev/mapper
Complete the creation of block or character special files via Mknod
Mounting of related modules
Creating a root device
Mount/sysroot
Finally complete the root switch
From the above picture, initrd such a virtual file root file system, the role is to kernel and the real root file system to establish an association relationship, let kernel to initrd to load the root file system required driver, and read and write to mount the root file system, And let's execute the first process init of the user.
Now let's take this one step further together to sum up:
Grub's stage2 loads the initrd into memory, letting it release the content into the content, and the kernel executes the init script in Initrd, when the kernel gives control to the init file processing. We simply browsed the contents of the Init script and found that it was also primarily loaded with device drivers related to various storage media. When the required drivers are loaded, a root device is created, and the root file system Rootfs is mounted as read-only. At the end of this step, release unused memory, switch to the real root filesystem, run the/sbin/init program, and execute the system 1th process. Thereafter, the control of the system was fully delegated to the/sbin/init process.
6. So far, the trek, the core space work is finally completed, the dawn of Light will appear, the next step is to initialize the system, the kernel space of the task began to the user space transfer. The/sbin/init process is the parent process of all other processes in the system, and when it takes over the control of the system, it first reads the/etc/inittab file to execute the corresponding script for system initialization, such as setting up the keyboard, fonts, loading modules, setting up the network, etc. It is divided into the following tasks:
Now let's look at the details of the/etc/inittab configuration:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/79/05/wKioL1aE91GwHJ6dAAKps1DfPEk562.png "title=" Tab1.png "alt=" Wkiol1ae91gwhj6daakps1dfpek562.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/79/05/wKioL1aE93fDZrswAAL-S4nz-Ss809.png "title=" Tab2.png "alt=" Wkiol1ae93fdzrswaal-s4nz-ss809.png "/>
Definitions for each level
Default Run Level 3,5
0:halt//shutdown, shutdown
1:single user mode//single user maintenance mode), root users, no certification; maintenance mode;
2:multi user mode, without NFS//does not support NFS function, will start the network function, maintenance mode;
3:multi user mode, text mode/full function mode;
4:reserved//System reserved, currently no special use purposes, but accustomed to the same 3-level function use;
5:multi user mode, graphic mode//graphical interface, full function mode
6:reboot//restart
Level switch: Init #
Level View: Who-r | RunLevel
/etc/inittab's format and syntax:
[Option]:[runlevel]:[Behavior]:[command] that is: id:runlevels:action:process
Behavior:
Wait: Executes once while waiting to switch to the level at which the task is located;
Respawn: Once this task is terminated, it will be restarted automatically;
Initdefault: Sets the default RunLevel, at which time the process is omitted;
Sysinit: Set the system initialization mode, here is generally designated/etc/rc.d/rc.sysinit script;
Commands: usually some scripts
1) Execute the System initialization script (/etc/rc.d/rc.sysinit), the basic configuration of the system, to read and write the root file system and other file systems, to this system is basically run up, the need to run the level of determination and the corresponding service start.
We can look at the initialization script defined in Inittab: Rc.sysinit--/etc/rc.d/rc.sysinit
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/79/07/wKiom1aE-pjRkPoSAAE8WQeCBfI422.png "title=" hostname. png "alt=" Wkiom1ae-pjrkposaae8wqecbfi422.png "/>650) this.width=650;" Src= "http://s5.51cto.com/wyfs02/ M02/79/07/wkiom1ae-ssdwyudaaeqcs5u3mg012.png "title=" Selinux.png "alt=" Wkiom1ae-ssdwyudaaeqcs5u3mg012.png "/>
As shown: Some definitions of system initialization are defined in the Rc.sysinit script:
(1) Set the host name;
(2) Set welcome information;
(3) Activate Udev and SELinux;
(4) Mount all file systems defined in the/etc/fstab file;
(5) Detecting the root file system and re-mounting the root file system in read-write mode;
(6) Set the system clock;
(7) Set kernel parameters according to/etc/sysctl.conf file;
(8) activating LVM and soft raid devices;
(9) Activate swap device;
(10) Loading the drivers for additional devices;
(11) Cleaning operation;
(12) then run the related service script according to the system run level:/etc/rc.d/init.d/script and/etc/rc.d/rc#d
When the/etc/rc.d/rc.sysinit is finished, the system will work smoothly, but also need to start the system needs a variety of services, so that the host can provide the relevant network and host functions, so the following script will be executed.
2) Execute the/ETC/RC.D/RC script. The file defines the order in which the service is started with K after S, and the specific service status for each runlevel is placed in the/ETC/RC.D/RC*.D (*=0~6) directory, and all files are symbolic links to the corresponding files under/ETC/INIT.D. Rc.sysinit analyzes the/etc/inittab file to determine the boot level of the system before it executes the files under/ETC/RC.D/RC*.D.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/79/07/wKiom1aE--Ki_9IbAAER7d2imKo762.png "title=" Rc.d.png "alt=" Wkiom1ae--ki_9ibaaer7d2imko762.png "/>
By the know,
RC0-RC6 directory under script:
k* # #只要是以K开头的文件均执行stop工作
s* # #只要是以S开头的文件均执行start工作
0-99 (The order of execution, the smaller the number is executed first)
User-defined Boot program (/etc/rc.d/rc.local), you can according to their own needs to execute some commands or scripts written into the/etc/rc.d/rc.local, when the boot, you can automatically load
In other words, INIT.D, RC, RC*.D, rc.local, and rc.sysinit in the/etc directory are symbolic links to the corresponding files and folders in the/ETC/RC.D directory. Let's take the start Level 3 as an example to explain briefly.
The/ETC/RC.D/RC3.D directory, where the contents are all linked files starting with S or K, are linked to various shell scripts in the "/ETC/RC.D/INIT.D" directory. s represents the service content that requires start at startup, and K indicates the service content that needs to be shut down when the computer is shut down. System services in/ETC/RC.D/RC*.D are started in the background, if you want to make more specific customizations to a service in a runlevel, by Chkconfig command, or through Setup, Ntsys, System-config-services to be customized. If we need to increase the startup content ourselves, we can add the relevant shell script in the INIT.D directory and then establish a link file in the RC*.D directory to point to the shell script. The start or end order of these shell scripts is determined by the numbers that follow the s or K letters, and the smaller the number, the more the script executes. For example,/etc/rc.d/rc3.d/s01sysstat is executed before/etc/rc.d/rc3.d/s99local.
3) Execute user custom bootloader/etc/rc.d/rc.local. In fact, when executing/etc/rc.d/rc3.d/s99local, it is executing/etc/rc.d/rc.local. S99local is a symbolic link that points to rc.local. In general, the custom program does not need to perform the above mentioned cumbersome to build the shell to increase the link file step, only need to put the command in rc.local, the shell script is reserved for the user to customize the boot content.
After all the system startup tasks have been completed, Linux will start the terminal or X-window to wait for the user to log in. Tty1,tty2,tty3 ... This means that when running level 1,2,3,4, "/sbin/mingetty" will be executed, and there are 6 executed, so Linux will have 6 plain text terminals, Mingetty is the command to start the terminal.
In addition to these 6 will also execute "/etc/x11/prefdm-nodaemon" This main start X-window,
At this point, the system started!
During system startup, the main scripts and directories are:
Boot
/grub
/boot/grub/grub.conf
/boot/initrd+ kernel version
/INITRD file/proc//sys//dev/directory mount and Root switch
/etc/inittab Script
/etc/rc.d/rc.sysinit scripts, etc.
This article from "10917621" blog, declined reprint!
Linux system Start-up process detailed