Linux boot process

Linux boot process:

1. Load the BIOS (Basic Input Output System), and load the CMOS information through the BIOS program, and obtain various hardware configuration information of the host through the setting values ​​in the CMOS, and perform a power-on self-test (POST : Power-on Self Test), and then start the initialization of hardware detection, and set the PnP (Plug and Play) device, then define the bootable device sequence, and obtain the first , Next, the data reading of the boot device will start (MBR-related tasks start).


2. Read and execute the Boot Loader (that is, programs such as grub, lilo, or spfdisk) of the MBR (Master Boot Record) in the first boot device. The main function of Boot Loader is to know the operation. The file format of the system and loads the kernel into main memory for execution accordingly .


3. Load the Kernel (/boot/vmlinuz) according to the settings of the Boot Loader, the Kernel will start to detect the hardware and load the driver
(the boot loader can load the kernel and initrd, and then decompress the initrd into the root directory in the memory, the kernel will This can load the appropriate driver, finally release the virtual file system, and mount the actual root directory file system, and the subsequent normal boot process can be started.
initrd ( /boot/initrd ) virtual file system (Initial RAM Disk), the feature of this file is that it can also be loaded into the memory through the boot loader, and then the file will be decompressed and emulated as a root in the memory directory, and the file system emulated in the memory can provide an executable program through which the core modules most needed during the boot process are loaded, usually these modules are file systems such as USB, RAID, LVM, SCSI, etc. The driver for the interface with the disk! After the loading is complete, it will help the core to call /sbin/init again to start the subsequent normal boot process).


4. After the hardware driver is successful, Kernel will actively call the init (/sbin/init, PID=1) program, and init will obtain (/etc/inittab) runlevel information
(the main function of /sbin/init is to prepare software for execution environment, including the hostname of the system, network settings, language processing, file system format and startup of other services, etc. All actions will be planned through the init configuration file, that is, /etc/inittab, and inittab There is also a very important setting item, that is the default runlevel, the boot execution level)


5. init executes the /etc/rc.d/rc.sysinit file to prepare the operating environment for software execution (such as network, time zone, etc.)


6 . init executes the extra kernel modules inside /etc/modules.conf

7. init executes the startup of each service under /etc/rc.d[1-6].d corresponding to the run-level (script mode)


8. init executes /etc/rc.d/rc.local file


9. init executes the terminal emulator mingetty to start the login program, and finally waits for the user to log in



runlevel: 0-6
0: halt
1: single user mode
2: multi user mode, without NFS
3: multi user mode, text mode
4: reserved
5: multi user mode, graphic mode
6: reboot


init processing Process:

1. Set the default run level
2. Run the system initialization script (/etc/rc.d/rc.sysinit)
3. Run the script in the directory corresponding to the specified run level (/etc/rc.d/rc[runlevel ] ].d/)
4. Set the operation of the Ctrl+Alt+Del key combination
5. Define the operation performed by the UPS power supply in the event of a power failure/recovery
6. Start the six virtual terminals of mingetty (tty1-tty6)
7. Start the graphics Terminal


init handles the system initialization process (/etc/rc.d/rc.sysinit)

1. Obtain the network environment and host type
   Read the network configuration file /etc/sysconfig/network to obtain the host name and the default gateway (gateway) and other networks environment.

2. Test and mount memory device /proc and USB device /sys
   In addition to mounting the memory device /proc, it will also actively detect whether there is a usb device on the system. If there is, it will actively load the usb driver and program, and try to mount the usb file system.

3. Decide whether to start SELinux or not.

4. Start the random number generator of the system. The random number
   generator can help the system to perform some functions of password encryption. Here, it is necessary to start the random number generator twice.

5. Set the terminal (console) font

6. Set the welcome screen (text banner) displayed during the boot process;

7. Set the system time (clock) and time zone settings
   need to read /etc/sysconfig/clock settings Fixed value

8. Interface device detection and Plug and Play (PnP) parameter testing
   According to the result of core detection at boot time (/proc/sys/kernel/modprobe ), start to perform ide / scsi / network / sound effects and other interface devices

9. Loading of user-defined modules Users
   can add user-defined modules in /etc/sysconfig/modules/*.modules, and they will be Load it into the system

10. Load the relevant settings of the core The
   system will actively read the settings of the /etc/sysctl.conf file, so that the core function becomes what we want.

11. Set the host name and initialize the power management Module (ACPI)

12. Initialize the software disk array
   Mainly set through /etc/mdadm.conf

13. Initialize the file system function of LVM

14. Check the disk file system with fsck and
   perform filesystem check

15. Perform the conversion of disk quota quota (not necessary)

16. Restart with Mount the system disk in rewritable mode

17. Start the quota function
   so we don't need to customize the action of quotaon

18. Start the system virtual random number generator (pseudo-random)

19. Clear the temporary files during the boot process

20. Turn on the boot related Information loading /var/log/dmesg file


# dmesg #Display information about the boot process


Start

system services and related startup configuration files (/etc/rc.d/rc N & /etc/sysconfig) Load the core to prepare the entire system for instructions Come to work, and after the initialization of the system module and related hardware information in /etc/rc.d/rc.sysinit, your CentOS system should be working smoothly. What's more, we have to start various "services" required by the system, so that the host can provide our related network or host functions! At this time, according to the run level setting value we mentioned in /etc/inittab, we can decide to start the service project.

The scripts to be executed by each run level are placed mainly through the command /etc/rc.d/rc to process related tasks.

/etc/rc.d/rc instruction description:
 1. Obtain the script directory you want to execute through the external first parameter ($1). That is, the directory /etc/rc5.d/ can be obtained from /etc/rc.d/rc 5 to prepare for processing related script programs
 2. Find the file starting with /etc/rc5.d/K??*, and Perform the action of "/etc/rc5.d/K??* stop"
     (close services starting with K)
 3. Find the file starting with /etc/rc5.d/S??*, and perform "/etc/ rc5.d/S??* start ” action
     (starting services starting with S)

Note: The documents under /etc/rc5.d/ are all connection documents, linking to the directory /etc/init.d/ where the service is started

# runlevel # View the current run level
N 3            
# init 5 # Switch the run level to 5
# runlevel
3 5 # 3: the last run level, 5: the current run level


Core

and core modules: During the entire boot process, whether it is possible to It is the kernel's job to successfully drive the hardware configuration of our host.

Core and core module location:

Kernel: /boot/vmlinuz or /boot/vmlinuz-version
RAM Disk required for core decompression: /boot/initrd (/boot/initrd-version)
Core module: /lib/modules/version/kernel or /lib/modules/$(uname -r)/kernel
Core source code: /usr/src/linux (only available after installation! Otherwise, it is not installed by default!)

If the core is successfully loaded into the system, then several pieces of information will be recorded:
Core version: /proc/version
System core functions: /proc/sys/kernel/

# lsmod # (lsmod: list modules) View the core Loaded core module

# modinfo [-adhpV][module file] # Display core module information

Two stages of boot loader:

Boot loader is an important tool for loading the kernel (Kernel)

After the BIOS reads the information, the next step is It will go to the MBR of the first boot device to read the boot loader. The boot loader can have menu functions, direct loading of core files and transfer of control rights, etc. The system must have a boot loader to be able to load the core of the operating system. But we all know that MBR is a block in the first sector of the entire hard disk, and at best the entire size is only 446 bytes. In order to solve this problem, Linux divides the program code execution of the boot loader and the loading of set values ​​into two parts. There are stages to execute:

Stage 1: Execute the main program of the boot loader:
The first stage is to execute the main program of the boot loader. This main program must be installed in the boot area, that is, the MBR or the boot sector. But as mentioned above, because the MBR is too small, the MBR or boot sector usually only installs the minimal main program of the boot loader, and does not install the related configuration files of the loader.

Stage 2: The main program loads the configuration file:
the second stage is Load all configuration files and related environment parameter files (including file system definitions and main configuration files menu.lst-->grub.conf ) through the boot loader. Generally speaking, configuration files are under /boot












Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327015828&siteId=291194637