Linux Startup Process
Start BIOS Program
When the power key is pressed, the BIOS (Basic nput Output System) will be run by the flash memory on the motherboard. The BIOS program will extract itself to the system memory, and then read the information stored in CMOS (such as system time and Startup Device sequence) to configure the system. POST: Power-On Self Test ). Check whether the system hardware is faulty. After the self-check is passed, the BIOS will load the Boot loader program in the MBR (Master boot Record: Main Boot Sector) of the first boot device.
It should be noted that the boot loader is loaded by the hardware INT 13 interrupt function. That is, after the BIOS reads the disk, it reads the boot loader program in the first sector of the disk through the INT 13 channel!
Below are several BIOS programs:
Figure 1.1: basic configuration page of the BIOS Program
Figure 1.2: BIOS program interface for adjusting device startup sequence
Start the Bootloader program
Linux has a variety of bootloaders, such as the early default use of LILO, and the current mainstream grub. Next, let's take the mainstream grub in Linux as an example to describe what the bootloader mainly does?
When Grub is started, the configuration file/boot/grub. conf is automatically read, and the grub command is executed! Figure 2.1 shows a grub. conf configuration file on CentOS6.5! The Grub command is the part in the red line box. It consists of three parts:
Fig 2.1
1. set the hard disk partition to be started (root command). The parameter passed to this command is a partition. This command analyzes and mounts the partition, and then reads the content in the/boot folder. As shown in figure 2.2, after executing the command, you can read the content under the boot folder!
Fig 2.2
2. Select the boot kernel File and set the kernel parameters (kernel command)
3. Load the initrd file and set the virtual file system. (Initrd command)
Note that this initrd file is actually an Initial RAM Disk ). In Linux, some disk drivers are not compiled in the kernel, but are packaged into modules and stored in the/lib/modules directory, when the kernel is started, the disk driver needs to be loaded. However, the/lib/modules file has not been attached yet, so there is a conflict! To solve this conflict, the concept of a virtual file system is created. We can talk about loading initrd files into the memory (To Worry About Affecting the file system on the disk, the root directory is mounted in read-only mode ), virtualize a root file system, read the disk driver in the virtual root file system, and then mount the real root directory.
As shown in 2.3, these files are extracted from the initrd file.
Fig 2.3
Load System Kernel and hardware driver
After Grub runs the preceding three commands, the kernel file (vmlinuz-$ (uname-r) will be decompressed to the memory to start running! At this point, the kernel will re-detect the hardware and load the driver again! After the check is complete, the kernel will call the Init program to create the first process of the system!
Init process for system initialization
After the Init program is started, it first reads the file/etc/inittab to obtain the Run Level of the operating system ). What is the running level? in Linux, the system sets
The system is divided into seven running levels. Their content is as follows:
Fig 3.1
This is a description of the mode in the/etc/inittab file of Linux.
Mode 0 indicates the shutdown status, Mode 1 indicates the single-user mode (maintenance mode), Mode 2 indicates the command line mode for multiple users, but there is no network. Mode 3 represents the command line mode for multiple users including the network. Mode 4 is a reserved function of the system. Mode 5 represents the graphic interface mode. Mode 6 indicates the system restart status.
Corresponding to each running level, different directories are set under the/etc directory, as shown in 3.2:
Fig 3.2
When the system runs at a certain level, it runs the script files in the corresponding directory (/etc/rc [runtime level]. d. Here, the rc represents the run command, that is, the command to be run at the beginning, and the. d Represents directory, which indicates the directory!
In the Inittab file, you can set the startup level of the system. There are some other functions, as shown in Figure 3.3:
Fig 3.3
This is a description of other configurations in the inittab file on CentOS6.5.
In short, after the Init program gets the running level of the system, it will continue to execute the following functions:
Initialize the system (execute the/etc/init/rc. conf script file or run some custom running level programs through/etc/init/rc. conf)
Load the system service item (run the script file in/etc/rc [running level]. d)
Set the Ctrl + Alt + Del key function
Run mingetty to set up six corresponding terminals
Run the terminal and receive User Login
After the system initialization is complete, the system will call the mingetty function to set the terminal and wait for the user to log on! The specific process is as follows: The init process first fork a sub-process, and then the sub-process to exec a getty program. The Getty program will try to open a terminal. If it is successfully opened, the login text will be displayed on the screen. After you type the user name, the task of the getty program will be completed! Then it calls the login program in exec mode. The Login program reads the user name and password, and then reads the shadow file to check whether the user password is correct. If the password is incorrect, login exits by calling exit 1. At this time, the init process re-fork to repeat the preceding steps.
If the password is successfully verified, the login program will set some system running environments (such as setting the working directory, uid, gid, and user name ). After these system environments are configured, the login program will call the shell program through exec, and the shell program will execute and read some startup scripts (such as/etc/bashrc ). After reading these startup scripts, shell prints the prompt and waits for the user to type the command! At this point, our system will start normally!
This article permanently updates the link address: