CentOS boot 10 steps
The boot process refers to the entire process from powering on the computer until the user logon screen is displayed in LINUX. Analyzing the LINUX boot process is also a good way to gain a deep understanding of the core operating principles of LINUX.
Start Step 1 -- load BIOS
When you turn on the computer power, the computer will first load the BIOS information, BIOS information is so important that the computer must find it at the very beginning. This is because the BIOS contains CPU information, device startup sequence information, hard disk information, memory information, clock information, and PnP features. After that, the computer will be aware of which hardware device should be read. After the BIOS gives control of the system to the first sector of the hard disk, it starts to be controlled by Linux.
Start Step 2 -- read MBR
The first sector of the 0th magnetic track on the hard disk is known as MBR, namely, MasterBootRecord, which is the Master Boot Record. Its size is 512 bytes, but it contains the pre-start information and partition table information. It can be divided into two parts: the first part is the boot (PRE-BOOT) area, accounting for 446 bytes; the second part is the PARTITION Table (partition pable), a total of 66 bytes, record the PARTITION information of the hard disk. One of the functions of the pre-boot area is to find the partition marked as ACTIVE and read the boot area of the ACTIVE partition into the memory.
After the system finds the MBR of the hard disk specified by the BIOS, it will copy it to the physical memory where the 0 × 7c00 address is located. In fact, the content copied to the physical memory is the Boot Loader, and to your computer, it is lilo or grub.
Step 3 -- Boot Loader
Boot Loader is a small program that runs before the operating system kernel runs. Through this small program, we can initialize hardware devices and build a map of memory space to bring the system's hardware and software environment to a suitable state, in order to make all preparations for the final call to the operating system kernel. Generally, BootL oade is implemented based on hardware. Different system architectures have different Boot loaders.
The content of the Linux boot sector is a program written in assembly language. Its source code is in arch/i386/boot (the CPUs of different systems have their respective boot directories ), there are four program files:
◎ Bootsect. S: the main program for guiding the sector. The compiled code cannot exceed 512 bytes, that is, the size and size of a single sector.
◎ Setup. S: pilot the Helper Program
◎ Edd. S, part of the Helper Program, used to support BIOS enhancement disk device services
◎ Video. S, another part of the Helper Program, used for Screen Display during boot
There are several Boot Loader types, among which Grub, Lilo, and spfdisk are common loaders. Here we will take Grub as an example to explain.
The System reads grub configuration information (generally menu. lst or grub. lst) in the memory and starts different operating systems according to the configuration information.
Step 4-load the kernel
Based on the path of the kernel image set by grub, the system reads the memory image and decompress it. At this time, the screen will usually output the "Uncompressing Linux" prompt. After the kernel is decompressed, the screen outputs "OK, booting the kernel ".
The system places the decompressed kernel in the memory, and calls the start_kernel () function to start a series of initialization functions and initialize various devices to complete the establishment of the Linux core environment. So far, the Linux kernel has been established, and the Linux-based program should be able to run normally.
Start_kenrel () is defined in init/main. in c, it is similar to the main () function in a general executable program. Before that, the system only performs initialization operations that can be performed by the kernel program to the minimum extent, the real kernel initialization process starts from here. The start_kerenl () function will call a series of initialization functions to complete all aspects of the kernel settings. The purpose is to build a basic and complete Linux core environment.
Start_kernel () mainly performs the following operations:
(1) print the current kernel version on the screen.
(2) Run setup_arch () to set the system structure.
(3) execute sched_init () to initialize the scheduling mechanism of the system. Initialize runqueque on each available CPU, and then initialize process 0 (the task struct and system empty M stack have been allocated in startup_32 () as the system idle process, that is, the process that occupies the CPU when the system is idle.
(4) Run parse_early_param () and parsees_args () to parse the system startup parameters.
(5) Run trap_in itQ and set the system interrupt vector table first. The trap gate 0-19 is used for CPU exception handling; then the system call vector is initialized; finally, cpu_init () is called to improve the CPU initialization and support the process scheduling mechanism, this includes the set flag register, task register, and initialization program debugging registers.
(6) execute rcu_init () to initialize the Read-Copy Update mutex mechanism in the system.
(7) execute the init_IRQ () function to initialize the peripheral interrupt and complete the final initialization process of IDT.
(8) execute the init_timers (), softirq_init () and time_init () functions to initialize the timer mechanism, Soft Interrupt mechanism, and system date and time respectively.
(9) execute the mem_init () function, initialize the page data structure descriptor of the physical memory page, and complete the creation of the physical memory management mechanism.
(10) Run kmem_cache_init () to initialize the General slab buffer management mechanism.
(11) execute fork_init () to calculate the number of processes (threads) that can be created by the physical memory capacity of the current system.
(12) execute proc_caches_init (), bufer_init (), unnamed_dev_init (), vfs_caches_init (), signals_init () and other functions to establish a dedicated slab Buffer Queue for various management mechanisms.
(13) execute proc_root_init () Wl number to initialize the Virtual File System/proc.
At the end of start_kenrel (), the kernel creates the first system kernel thread (process 1) Through kenrel_thread (), which executes the init () function in the kernel, it is responsible for the next phase of startup tasks. Finally, call the cpues_idle () function: when the system enters the main loop port, the default_idle () function is executed by default, that is, the CPU halt command, other functions will not be executed until other processes in the ready queue need to be scheduled. At this point, the only process in the system in the ready state is the init process (kernel thread) created by kerne_hread (). Therefore, the kernel does not enter the default_idle () function, but instead changes to init () the function continues the startup process.
Step 5 -- User-layer init sets the running level based on the inittab File
After the kernel is loaded, the first program to run is/sbin/init, which reads the/etc/inittab file and initializes the file based on the file.
In fact, the main function of the/etc/inittab file is to set the Linux running level. The setting format is ": id: 5: initdefault :", this indicates that Linux needs to run on level 5. The Linux operating level is set as follows:
0: Shutdown
1: single-user mode
2: multi-user mode without network support
3: multi-user mode with network support
4: reserved, not used
5. Multi-user mode with network support and X-Window support
6. reboot the system.
Start step 6 -- execute rc. sysinit in the init process
After the running level is set, the first user-layer file executed by Linux is/etc/rc. d/rc. the sysinit script program does a lot of work, including setting PATH, setting network configuration (/etc/sysconfig/network), starting swap partitions, and setting/proc. If you are interested, you can view the rc. sysinit file in/etc/rc. d.
The final completion state of the thread init is a system environment that enables normal user programs to be executed normally, thus truly completing the available program running. It mainly performs the following operations:
(1) execute the do_basic_setup () function, which fully initializes the external device.
(2) construct the system's Virtual File System directory tree and mount the device in the system as the root directory (the specific file system has been registered in the previous step ).
(3) Open the device/dev/console and copy the connection opened twice through the function sys_dup () so that all file numbers 0, 1 and 2 point to the console. These three file connections are commonly referred to as "standard input" stdin, "standard output" stdout, and "standard error information" stderr.
(4) After everything is ready, the system starts to initialize the user layer. The kernel calls execve () to load the user-layer initialization program corresponding to the execution T, and tries to load the program "/sbin/initl", "/etc/init ", "/bin/init ', and"/bin/sh. As long as one program is successfully loaded, the system starts initialization at the user layer, instead of returning to the init () function segment. Now, the init () function ends, and the Linux kernel boot part ends.
Step 7 -- start the kernel module
The kernel module is loaded Based on the/etc/modules. conf file or the file in the/etc/modules. d directory.
Start Step 8-execute script programs of different running levels
Depending on the running level, the system will run the corresponding script programs from rc0.d to rc6.d to complete the initialization and start the corresponding service.
Start Step 9 -- run/etc/rc. d/rc. local
If you open this file, there is a sentence in it. After reading it, you will see the role of this command at a Glance:
# This script will be executed * after * all the other init scripts.
# You can put your own initialization stuff in here if you don't
# Want to do the full Sys V style init stuff.
Rc. local is a place for users to personalize Linux after all initialization work. You can put what you want to set and start here.
Start step 10-run the/bin/login program and enter the logon status
Now, the system has entered the waiting time for the user to enter username and password. You can use your account to log on to the system.
1: after starting the power supply, the first step of the host is to query BIOS (full name: basic input/output system) information. Understand the hardware status of the entire system, such as CPU, memory, video card, and nic. Well, windows computing and it are one. No.
2: Next, the Host reads the boot loader from MBR (the first sector of the hard disk. This is important. It is said that troubleshooting will take this test and give a bad loader and ask you to correct it. Windows does not support the partition format of linux. Therefore, use windows boot. Ini cannot find the linux system. Generally, windows and linux are installed first, and grub is used as the boot loader. Two words: worry-free! Because linux is not as stingy as windows. Grub supports windows partition format.
3: After the Host reads the boot loader, it will read the information and know who is waiting for it. If the host wants to access the linux system, read to the linux core is loaded into the memory in the/boot file directory. Start the next analysis journey.
4: OK. Who is the first running program? Is the/sbin/init program. If you don't believe it, you can use the top program to check whether the PID is 1 or not. It is the ancestor of all things. I am short for it as the nvwa Niang (not like Adam and Eve ).
· 5: init first looks for the start level (run-level ). Because the startup levels are different, the running scripts (that is, services) are different. The default levels include the following:
0-halt (direct system shutdown)
1-single user mode (single user mode, used for system maintenance)
2-Multi-user and withoutNFS (similar to the 3 mode, but NFS service is missing)
3-Full multi-user mode (Full mode, but text mode)
4-unused (system reservation function)
5-X11 (similar to 3 mode, but X terminal display)
6-reboot (reboot)
(Do not select 0 or 4 or 6. Otherwise, the system is improved)
· 6: OK. After the system knows its startup level, the next step is not to start the service, but to set the host runtime environment first. The read file is/etc/rc. D/rc. Sysinit file. Which environments should I set?
· Set the network environment/etc/sysconfig/network, such as the host name, gateway, IP address, and DNS.
· Mount/proc. This file is a special file with a size of 0 because it is in the memory. It is best not to delete the content.
· Based on the kernel boot result/proc/sys/kernel/modprobe. Start detection of peripheral devices.
· Load the User-Defined module/etc/sysconfig/modules /*. Modules
· Read/etc/sysctl. The conf file sets the kernel.
· Set the time, terminal font, hard disk LVM or RAID, and perform disk detection with fsck.
· Record the boot status to/var/log/dmesg. (You can run the dmesg command to view the result)
· 7: OK. Next, start the system service. Different run-levels may start different services. To/etc/rc. In the d directory, different levels have different directories. For example, if the 3 mode is enabled, there will be rc3. D directory, which stores the service. In this example, start indicates that the instance is started, and kill indicates that the instance is not started. A number indicates the startup sequence. The smaller the number, the earlier the startup.
Note that they are all connected to etc/rc. D/init. D/related files in the directory. Therefore, you can use "/etc/rc" to manually start a service. D/init. D/start a service. On the contrary, we can also send a service ln (link command) to different run-level directories. Remember to add S or K + numbers.
· 8: after reading the service, the host will read/etc/rc. D/rc. Local file. Therefore, if you need to start the system, you can write a script or command here. It doesn't have to be as troublesome as above. It is also convenient to delete it later.
OK. After a long journey, the system can finally start shell with peace of mind.