The overall process of system startup:
The first step of any system startup is to power on, that is, press the power, then the computer hardware will actively read the BIOS to load the hardware device information and the hardware device self-test, and then the system will actively read the first boot The device of the program, the boot program can specify which kernel is used to start and load it into the memory to run. At the same time, the kernel also loads other hardware devices and corresponding drivers to make the host components start running, and all hardware devices After the loading is complete, the system will actually start, and then the system will operate some external programs to prepare the software running environment. Then load some software programs needed for system operation. The last step is to wait for the user to log in.
Start the first step-power-on self-test
When you turn on the computer, the computer loads the BIOS information first. The BIOS information is so important that the computer must find it at the very beginning. This is because the BIOS contains CPU-related information, device boot sequence information, hard disk information, memory information, clock information, and so on. After this, the computer has a spectrum in its mind and knows which hardware device to read. After the BIOS transfers the control of the system to the first sector of the hard disk, Linux begins to control the system.
Start the second step-select the startup sequence, load MBR
The BIOS determines which device is the first boot item, (the default is the hard disk) the first 446 bytes of the MBR of the hard disk is called the Boot Loader. The main function of the Boot Loader is to identify the core file loaded in the operating system and submit it to the memory to run Then start the operating system. When we install the operating system, it will install its Boot Loader in the first 446 bytes of the MBR, and will also install a copy of Boot Loader on the boot sector of the corresponding partition. Another main function of Boot Loader is to provide a menu and transfer its boot management functions to other loaders
When MBR is started by the hard disk, the BIOS usually shifts to the first sector of the first hard disk, that is, MBR. The only thing it does is to load the second boot loader GRUB, and its role is actually to introduce more advanced functions to allow users to load a specific operating system.
After the BIOS self-check, the MBR will be loaded into the memory. It means that the boot program is activated and the partition table information has been loaded into the memory. It also means that the control of the system is transitioned from the BIOS to GRUB. GRUB is the abbreviation of GRand Unified Bootloader. It is a multi-operating system boot manager. Used to boot different systems. GRUB is a system boot program, divided into three stages:
The stage1 stage completes the most basic boot function and determines what file system your partition is.
Stage1.5 is to load this file system. According to the different file systems, the stage is divided into many stage1.5, which means that the purpose of stage1.5 is to load the driver of the file.
In stage2, we can access the partition corresponding to the boot directory, so that we can access the file called grub.conf under boot.
Detailed description of the grub.conf file
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6(2.6.32-696.el6.x86_64)root(hd0,0)
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=d98fccb0-b74e-4de3-9953-27b74542267a rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-696.el6.x86_64.img
~
We access the grub.conf file through hardware interrupts. At this time, we don’t have any mounting behavior in the grub.conf file. We can only access the corresponding files through hardware interrupts, through the above (hd0, 0) to access, not through the path of the file
.**vim /boot/grub/grub.conf ****root (hd0,0)**In this step, we specify the root of the file. This root is not the future root of the operating system. (The complete root of our operating system is sda2 ) Is the temporary root but below, the kernel and initrd are found under this root, so this (hd0,0) is to store the kernel and image files, so we can use the path instead of the hardware interrupt.
kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=234f756b-b613-425b-af46-269c7cafd100 rhgb (graphical interface progress bar, without it, the character interface starts) quiet (Without it, the modules and information loaded by the kernel will be loaded together, and with it, these will be shielded)
initrd /initramfs-2.6.32-696.el6.x86_64.img (virtual disk image file, because more drivers need to be loaded, graphics card, sound card, etc.)
Boot kernel
When stage2 is loaded into the memory for execution, it will first parse the grub configuration file /boot/grub/grub.conf, then load the kernel image into the memory, and transfer control to the kernel. The kernel will immediately initialize each device in the system and do related configuration work, including CPU, I/O, and storage devices.
Regarding the loading of Linux device drivers, part of the drivers are directly compiled into the kernel image, and the other part of the drivers are placed in the initrd (ramdisk) in the form of modules.
The Linux kernel needs to adapt to a variety of different hardware architectures, but it is impractical to incorporate all hardware drivers into the kernel, and it is impossible for the kernel to write the device drivers of the hardware into the kernel every time a new hardware structure is created. In fact, the Linux kernel image only contains basic hardware drivers. During system installation, system hardware information will be detected, and part of the device drivers will be written to initrd according to the installation information and system hardware information. In this way, when the system is started later, part of the device drivers will be loaded in the initrd. Here it is necessary to introduce you more about initrd:
The English meaning of initrd is bootloader initialized RAM disk, which is the memory disk initialized by boot loader. Before the kernel starts, the Boot Loader will load the initrd file in the storage medium into the memory. When the kernel starts, it will access the initrd file system in the memory before accessing the real root file system. In the case that the boot loader is configured with initrd, the kernel startup is divided into two stages. The first stage executes init in the initrd file system, completes tasks such as loading the driver module, and the second stage executes the real root file system. /Sbin/init process.
Another concept: initramfs
initramfs is a technology introduced in kernel 2.5. In fact, its meaning is: attach a cpio package to the kernel image. This cpio package contains a small file system. When the kernel is started, the kernel will untie the cpio package. , And release the file system contained in it to rootfs, part of the initialization code in the kernel will be placed in this file system, and executed as a user-level process. The obvious benefit brought by this is that the initialization code of the kernel is simplified, and the initialization process of the kernel is easier to customize.
Through the above analysis and our verification, we did come to this conclusion:
grub's stage2 loads the initrd into the memory, and then releases the contents into the contents. The kernel then executes the init script in the initrd. At this time, the kernel transfers control to the init file. We briefly browsed the content of the init script and found that it also mainly loads device drivers related to various storage media. When the required drivers are loaded, a root device will be created, and then the root file system rootfs will be mounted read-only. After this step is over, release the unused memory, switch to the real root file system, and run the /sbin/init program at the same time to execute the system's No. 1 process. After that, the control of the system is given to the /sbin/init process.
Start the fourth step-start the first executable program in user space /sbin/init
1)、 Execute the system initialization script (/etc/rc.d/rc.sysinit), perform basic configuration of the system, mount the root file system and other file systems in read-write mode, the system is basically up and running. Determining the run level and starting the corresponding service
When /etc/rc.d/rc.sysinit is executed, the system can work smoothly, but various services needed by the system need to be started, so that the host can provide related network and host functions, so it will execute The script below.
2)、 Execute the /etc/rc.d/rc script. This file defines the order of service startup is K first, then S, and the specific service status of each run level is placed in the /etc/rc.d/rc*.d (=0~6) directory, all The files are all symbolic links pointing to the corresponding files under /etc/init.d. rc.sysinit determines the startup level of the system by analyzing the /etc/inittab file, and then executes the files under /etc/rc.d/rc.d.
/etc/init.d-> /etc/rc.d/init.d
/etc/rc ->/etc/rc.d/rc
/etc/rc*.d ->/etc/rc.d/rc*.d
/etc/rc.local-> /etc/rc.d/rc.local
/etc/rc.sysinit-> /etc/rc.d/rc.sysinit
In other words, init.d, rc, rc*.d, rc.local and rc.sysinit in the /etc directory are all symbolic links pointing to the corresponding files and folders in the /etc/rc.d directory. Let's take the startup level 3 as an example to briefly explain.
The /etc/rc.d/rc3.d directory, the contents of this directory are all linked files starting with S or K, which are all linked to various shells in the "/etc/rc.d/init.d" directory script. S represents the service content that needs to be started at startup, and K represents the service content that needs to be closed when shutting down. The system services in /etc/rc.d/rc*.d will be started in the background of the system. If you want to customize the services in a certain run level more specifically, use the chkconfig command to operate, or through setup, ntsys, system -config-services to customize. If we need to increase the startup content ourselves, we can add related shell scripts in the init.d directory, and then create a link file in the rc*.d directory to point to the shell script. The start or end sequence of these shell scripts is determined by the number after the letter S or K. The smaller the number, the more the script is executed first. For example, /etc/rc.d/rc3.d /S01sysstat is executed before /etc/rc.d/rc3.d /S99local.
3)、 Execute user-defined boot program /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 to rc.local. Generally speaking, a custom program does not need to perform the cumbersome steps of establishing a shell and adding a link file as mentioned above, just put the command in rc.local. This shell script is reserved for the user to customize the startup. contents.
4)、 After completing all the startup tasks of the system, linux will start the terminal or X-Window to wait for the user to log in. tty1, tty2, tty3... This means that "/sbin/mingetty" will be executed when running level 1, 2, 3, and 4, and 6 of them will be executed, so Linux will have 6 plain text terminals. Mingetty is Start the terminal command.
At this point, the system has started up!
Recommended Posts