Do you know the CentOS system startup process?

  1. The composition of the Linux kernel

Related concepts:

Components of Linux system: kernel + root file system

Kernel: process management, memory management, network protocol stack, file system, driver.

IPC (Inter-Process Communication): refers to the method of communicating between multiple processes and exchanging information. Linux IPC is basically inherited from the Unix platform. Mainly include the original Unix IPC, System V IPC and Socket-based IPC. In addition, Linux also supports POSIX IPC.

The running system environment can be divided into two layers: kernel space and user space;

Kernel space: kernel code (system call)

User space: application (process or thread)

Kernel design genre:

Single-kernel design: integrate all functions into the same program; (Linux)

Microkernel design: each function is implemented by a separate subsystem; (Windows solarls)

Linux kernel features:

(1) Support modularization: .KO (kernel object)

(2) Support dynamic loading or unloading when the module is in motion

component:

Core file: /boot/Vmliuz-VERSION-release

ramdirk:

CentOS5:/boot/initrd-VERSION-release.img

CentOS6,7:/boot/initramfs-VERSION-release.img

Two, CentOS system startup process

**Overall startup sequence: **

POST (plus self-check)-->Boot Sequence (BIOS: basic input output system)-->Boot Loader (MBR: master boot record)-->Kernel (ramdisk)-->rootfs (root file system)--> switchchroot-->/sbin/init/(/etc/inittab /etc/init/*.conf)-->Set the run level-->System initialization script-->Close and start the corresponding service-->Start the terminal

**General flow chart: **

**Detailed description of each process: **

1、 POST power-on self-test

It is mainly to detect whether the hardware device can operate normally, but the realization of the self-check function is mainly by the BIOS (basic input output system) program embedded in the motherboard chip (CMOS), and the hardware device is initialized after the detection is no problem.

2、 Boot Sequence: select the boot sequence to load MBR

Boot Sequence is a program that depends on a certain hard disk hardware, to be precise, the MBR of the first hard disk sector, so as to find each boot device in order.

3、 MBR boot, bootloader boot loader, startup program

MBR (Master Boot Record): This record is in track 0 and 1 sector, totaling 512 bytes, the first 446 bytes are bootloder, the last 64 bytes are the partition table information, the main partition plus the extended partition cannot be more than four, and the last 2 bytes are check information, which is 55AA. Provide a menu that allows the user to select the system to be started or a different kernel version; load the kernel selected by the user into a specific space in the RAM, decompress and expand, and then transfer the system control to the kernel.

4、 kernel kernel implementation

The kernel initializes itself, realizes functions, uses ramdisk to detect identifiable programs, mounts the root file system in self-reading mode, and runs the application: /sbin/init

5、 /sbin/init/Manage user space service process

Set the run level, perform the initialization script, close or start the corresponding program, and start the terminal.

Demo:

GRUB (GRand Unified Bootloader) loads the kernel, which is the first 446 bytes in the MBR. It is a type of BooTLoader. Its function is to select the kernel to be started.

**GRUB related instructions: **

It is mainly composed of device.map, menulst, stage1, stage2, and a series of stage1_5.

device.map: Stores the root partition of the kernel file

menu.lis: is the menu list, which is the selectable menu list, stored in stage2.

Stage: The grub boot program is too large, so it is divided into two sections. The first section is stored in the MBR, and the second section is stored in the kernel file system. After the first section is booted, the second section can be found. However, the second section is stored in the kernel file system. At this time, the file system has not been formatted. How to access the menu.lst of the second section requires the help of the middle layer stage1_5, which can assist the stage1 section. Visit stage2. Stage1_5 is usually located 63 sectors after the stage1 field. Since stage2 stores the usable file system in memory indefinitely, this is why there are multiple stages1_5.

Grub Legacy: Three stages

stage1: stored on the MBR

stage1_5: Stored in the sector after the MBR, so that the bootloader in stage1 can recognize the file system on the partition where stage2 is located;

stage2: Disk partition (/boot/grub/)

The boot loader first reads the first stage of gurb on the MBR. Since the MBR is very small and only 512 bytes, the program is booted using grub. Then it reads the stage1.5 stage in the sector. After reading the stage 1.5, it can be Drive the disk partition where stage2 of the second stage is located. Stage2 is stored on the disk partition and also includes kernel files and ramdisks, etc., which are stored on this partition; this is why the kernel files can be loaded through the grub of the Bootloader.

Note: For the current hardware platform, the motherboard BIOS must be able to recognize the hard disk, and then the BIOS can load the Bootloader in the hard disk. After the Bootloader in the disk is loaded, it can recognize the hard disk device on the current host.

However, the recognition of the hard disk device does not mean that the file system on the hard disk can be recognized, because the file system is an additional layer of file structure organized by software, so to be able to interface with a file system, a file system driver must be used; The application program must be able to recognize and understand such a file system. This program is called a file system driver; the 1.5 stage of grub provides a file system driver for gurb, so that it can access the corresponding second stage and kernel The partition where it is located, which is usually a basic disk partition; so the second stage of grub and the kernel and ramdisk files are usually placed on a basic disk partition; because grub cannot drive the advanced interface of logical volumes.

Stage2 is generally mounted to the /boot/grub/ directory; grub also has its own configuration file: /boot/grub/grub.conf and usually has a symbolic link file: /etc/grub.conf;

The function of stage2:

(1) Provide menu or interactive interface;

(2) Can load the kernel or operating system selected by the user;

(3) A protection mechanism was passed for the menu.

Detailed explanation of /boot/grub/grub.conf configuration file:

**Enter single user mode: **

1 ) Edit the grub menu, select the title to be edited, and then use the e command

2 ) Append 1, s or single after the selected kernel

3 ) In the line where the kernel is located, type the b key

Demo:

Press e key during startup:

Select the kernel up and down and press e to edit and enter 1 or s, S, single, and press b again to restart

6、 Root switch

When mounting the root file system, in order to prevent the root file system from being damaged due to a bug in the kernel or a bug in the operation process, first mount the root file system read-only, and then read and write the mount after the load is completed, and complete the entire mount of the root file After the system, go directly to the /sbin/init program, that is, start running the first program in the user space.

User space startup process

7、 /sbin/init program

The init program mainly relies on the configuration file: /etc/inittab, which is roughly divided into: set the default startup level --> set the system initialization script --> start the service of the corresponding level --> print the login interface of each terminal (if the level is 3 processing provides a text login interface, if the level is 5, it also provides a graphical login interface)

8、 Set default runlevel

(1) Operation level: a mechanism set for the purpose of system operation or maintenance;

0- 6 : A total of 7 levels;

0 : Shutdown, shutdown

1 : Single user mode (single user), root user, no authentication required, maintenance mode;

2 : Multi-user mode (multi user), will start the network function, but will not start NFS, maintenance mode;

3 : Multi-user mode (multi user), fully functional mode, text interface;

4 : Reserved level: There is no special purpose for use at present, but it is used to use the same 3 level functions;

5 : Multi-user mode (multi user), fully functional mode, graphical interface;

6 : Restart, reboot

Icon:

Read configuration file: /etc/inittab

(2) Configuration file: /etc/inittab defines many functions, each line defines an action (action) and the corresponding process (only for CentOS 5), one line defines the task to be executed by init, even A bunch of tasks, the syntax format of each line is:

id:runlevels:action:process

id is the identifier of a task;

runlevels: In which run levels to start this task; for example: 3, 2345, it can also be empty to indicate all levels;

action: under what conditions start this task;

wait: Execute once while waiting to switch to the level of this task (when just switched in);

respawn: Once the task is terminated, it will automatically restart; (eg: log in the terminal to execute logout and logout will start again)

initdefault: set the default allowable level; at this time process is omitted and empty;

sysinit: Set the system initialization method, here is generally designated /etc/rc.d/rc.sysinit script; (CentOS 5,6 use this script, CentOS 7 is done by systemd), in CentOS 6 only Keep the function of starting the run level set in this configuration file.

process: specific tasks; usually applications, scripts, or binary programs, depending on customization.

Demo:

9、 System initialization script

System initialization script: /etc/rc.d/rc.sysinit

(1) Set the host name;

(2) Set welcome message;

(3) Activate udev and selinux;

(4) Mount all file systems defined in the /etc/fstab file;

(5) Detect the root file system, and remount the root file system in read-write mode; (Remounting refers to after the root file is detected)

(6) Set the system clock;

(7) Set the kernel parameters according to the /etc/sysctl.conf file;

(8) Activate lvm that is soft raid equipment;

(9) Activate the swap device;

(10) Load additional device drivers; (kernel load drivers only load the root file system)

(11) Cleaning operation;

Note: In the init configuration file: /etc/inittab, there is a line that defines /etc/rc.d/rc.sysinit. This script file is the script file responsible for completing the system initialization.

10、 Shut down/start the services under the corresponding level

The script file /etc/rc.d/rc is used to start or stop the service when the level is switched; this script accepts the passed parameters to the runlevel variable in the script, and then reads /etc/rcrunlevel.d/K* and /etc/rc$runlevel.d/S* All files, these files are why some services will start automatically after booting up, and some services will not start.

K*: The service to be stopped, K##*, priority. The smaller the number, the more priority it is to shut down. The dependent service is shut down first, and then the dependent service is shut down.

S*: The service to be started, S##*, priority. The smaller the number, the more priority it is to start. The dependent service is started first, and the dependent service is started later.

The /etc/rc.d/init.d directory also has a link directory for the /etc/init.d directory, and the files in these two directories are the same.

For example, the appeal definition will start the process starting with S under 3 levels:

Icon:

11、 Start the terminal (graphic terminal)

Operating system boot is complete

  1. Summary of system startup process

**Kernel level: **

  1. POST is used as a hardware detection function at startup

  2. BootSequence (BIOS) starts loading the boot loader program BootLoader in the MBR of the main boot partition

The current BootLoader in LInux is a three-stage GRUB program (breaking the 446-byte limit).

The first paragraph is written in BootLoader

In paragraph 1.5, the following sector is used for file system boot

The second paragraph provides internal interfaces and calls the system kernel kernel in boot/grub

  1. Kernel recognizes the hardware, loads the driver, mounts the root file system read-only, and delivers it to the first program /sbin/init in the user space at the same time

Special attention should be paid here. In order to adapt to multiple hardware interface driver calls, the system publisher will automatically identify the hardware interface when the system is installed for the first time, and call the unique driver to generate the ramdisk file, using the memory as the disk as the virtual root. After the driver interface, it will switch to the real root file system

CentOS 5 series is initrd, when the disk image file will cause the secondary cache buffer

The CentOS 6/7 series is improved to initramfs, which can not occupy the cache and buffer twice in the form of a file system

User space level

  1. /sbin/init takes over and initializes with its configuration file

  2. According to the configuration in /sbin/init, the default run level will be set, as well as some boot services set in /etc/init.d/

  3. /etc/rc.d/rc.sysinit Run the system initialization script to complete the system initialization

  4. Close the services that need to be stopped under the corresponding level, and start the services that need to be turned on under the corresponding level

  5. Set up login terminal

Author: small ears

Article source: http://purify.blog.51cto.com/10572011/1843124

Recommended Posts

Do you know the CentOS system startup process?
Do you know the CentOS system startup process?
CentOS system startup process
Centos system process management
Centos6 system boot loading process
Linux centos system boot process
Centos7 notes | operating system startup process, Linux users and permissions
[Centos8] The bumpy process of installing docker
Summarize the knowledge points of Centos7 system reinforcement
Centos7 modify the system language to simplified Chinese
CentOS 7 system to view the system version and machine digits
The difference between CentOS and Ubuntu in Linux system