Linux system boot process and troubleshooting

1. The Linux operating system boot process

1. POST
after boot server host, will be based on the motherboard BIOS settings for CPU, memory, video card, keyboard and other equipment preliminary testing, testing has been successfully handed over control of the system according to the preset startup sequence, most of the time will be handed over to the machine hard disk.
Summary: Detect the first device capable of booting the system, such as hard disk or CD-ROM
2. MBR boot
When starting the system from the local hard disk, first transfer the system control right to the partition containing the operating system boot file according to the MBR (Master Boot Record) setting in the first sector of the hard disk; or directly according to the MBR record The boot information calls the boot menu (such as GRUB).
Summary: Run the boot GRUB boot program placed in the MBR sector
3. GRUB menu
For the Linux operating system, GRUB (Unified Boot Loader) is the most widely used multi-system bootloader program. After the system control is passed to GRUB, the boot menu will be displayed for the user to choose, and the Linux kernel file will be loaded according to the selected option (or the default value), and then the system control will be transferred to the kernel.
CentOS 7 uses the GRUB2 boot loader.
Summary: The GRUB boot program reads the GRUB configuration file /boot/grub2/grub.cfg to obtain the kernel and mirror file system settings and path locations4
. Load the Linux kernel The
Linux kernel is a pre-compiled special binary file, between various hardware resources and system programs, responsible for resource allocation and scheduling. After the kernel takes over the control of the system, it will fully control the running process of the entire Linux operating system.
In CentOS 7 system, the default kernel file is located at "/boot/vmlinuz-3.10.0-514.el7.x86_64".
Summary: Load the kernel and mirror file system into memory
5. init process initialization
In order to complete the further system boot process, the Linux kernel first loads the "/sbin/init" program in the system into the memory to run (the running program is called a process), the init process is responsible for completing the initialization of the entire system, and finally waits for the user to proceed log in.
Summary: Load the hardware driver, the kernel loads the init process into the memory to run

Insert picture description here

Second, the system initialization process

1.init process

  • Load and run the /sbin/init program by the Linux kernel
  • The init process is the first process in the system and is the parent process of all processes
  • The PID (Process Tag) number of the init process is always 1

2.Systemd

  • Systemd is a kind of init software of Linux operating system
  • CentOS7 uses a new Systemd startup method to replace the traditional SysVinit
  • The first init process running in CentOS7 is /liblsystemd/systemd

Traditional sysVinit relies on serial execution of shell scripts to start services, resulting in low efficiency and slow system startup speed. Systemd can start more service processes in parallel, and has the ability to start services on demand, so that fewer processes can be started, thus Improve system startup speed.

3. Systemd unit type
Insert picture description here
4. Systemd target corresponding to the run level

Run level Systemd的target Description
0 target Shutdown state, the host will be shut down when this level is used
1 rescue.target Single user mode, you can log in to the system without password verification, mostly used for system maintenance
2=3 multi-user.target Equivalent to 3
3 multi-user.target The complete multi-user mode of the character interface, most server hosts run at this level~
4=3 multi-user.target Equivalent to 3
5 graphical.target The multi-user mode of the graphical interface provides a graphical desktop
6 reboot.target Reboot, reboot the host

3. Eliminate startup faults

(1) Repair MBR sector failure

1. The cause of the failure

  • Damage caused by viruses, Trojan horses, etc.
  • Incorrect partition operation, misoperation of disk read and write

2. Failure phenomenon

  • The boot program is not found, the startup is interrupted
  • Unable to load operating system, black screen after boot

3. Solutions

  • Backup files should be made in advance
  • Boot into emergency mode with the installation CD
  • Restore from backup file

Specific operations
1. Back up MBR sector data to other disks
c Create a wind area and format and mount the MBR backup directory to
Insert picture description here
Insert picture description here
back up MBR512 bytes of content

mkdir /backup 
mount /dev/sdb1 /backup 
dd if=/dev/sda of=/backup/mbr.bak bs=512 count=1

Insert picture description here

2. Simulate destruction of MBR boot sector

dd if=/dev/zero of=/dev/sda bs=512 count=1

Insert picture description here

3. The boot interface enters emergency mode and restores the MBR sector data from the backup file
—> first load the CD image, restart the operating system
—> when the installation wizard interface appears, select the "Troubleshooting" option
—> then select "Rescue a CentOS Linux system" option, enter the emergency mode
—> select “1”, select Continue and press Enter to continue
—> press Enter again, you will enter the Bash Shell environment with the “sh-4.2#” prompt
—> copy mbr.bak Content, 512 byte content

 mkdir /backupdir
 mount /dev/sdb1 /backupdir   挂载带有备份文件的分区
 dd if=/backupdir/mbr.bak of=/dev/sda    恢复备份数据
 exit   执行exit命令退出临时shell环境,系统将会自动重启

Insert the picture description here)(https://img-blog.csdnimg.cn/2021020915393739.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dwXFF_center_color=FF_center)

Insert picture description here
Insert picture description here
Insert picture description here

(2) Repair GRUB boot failure

1. The cause of the failure

  • The GRUB boot program in MBR is corrupted
  • The grub.conf file is missing, and the boot configuration is incorrect

2. Failure phenomenon

  • System boot stalls
  • Display "grub>" prompt

3. Solutions

  • Try to manually enter the boot command
  • Enter emergency mode, rewrite or restore grub.conf from backup
  • Rebuild the grub program into the MBR sector
/boot/grub/                  目录下的包是用于启动菜单的背景图片及样式显示图形 
/boot/grub2/grub.cfg	     GRUB配置文件 内核,镜像文件

Method 1: Manually enter the guide command (clumsy and cumbersome, inputting too many characters and input errors will not be able to start, it is not recommended)

grub> insmod xfs   #加载指定的模块到内核
grub> linux16 /vmlinuz-3.10.0-693.el7.x86_64 root=UUID=8fd74986-ae66-4ffd-b7d8-a19f2eca7b6f ro rhgb quiet LANG=zh_CN.UTF-8  #内核名字及位置信息   
grub> initrd16 /initramfs-3.10.0-693.el7.x86_64.img     #镜像系统文件
grub> boot        #引导boot

Method 2: Enter the emergency mode and restore the GRUB boot program (similar to repairing the MBR boot sector)
1. MBR is located at the first physical sector of the first hard disk (/dev/sda), a total of 512 bytes, the first 446 The byte is the master boot record, and the partition table is stored in bytes 447-510 in the MBR sector.

mkdir /bak
mount /dev/sdb1 /bak
dd if=/dev/sda of=/bak/grub.bak bs=446 count=1

2. Simulate the destruction of the GRUB boot program in the MRB, but does not destroy the partition table
dd if=/dev/zero of=/dev/sda bs=446 count=1

3. The boot interface enters emergency mode and restores the GRUB boot program from the backup file

sh-4.2# mkdir /backupdir
sh-4.2# mount /dev/sdb1 /backupdir 	
sh-4.2# dd if=/backupdir/grub.bak of=/dev/sda	
sh-4.2# exit

Method 3: Boot the interface into emergency mode and rebuild the GRUB menu configuration file

1.rm -rf /boot/grub2/grub.cfg

Insert picture description here
2. Enter the emergency mode, load the CD image, and switch to the system root environment

sh-4.2# chroot /mnt/sysimage

3. Re-install the GRUB boot program to the MRB sector of the first hard disk (/dev/sda)

bash-4.2# grub2-install /dev/sda 

4. Rebuild the configuration file of the GRUB menu

bash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg

5. Exit the chroot environment and restart

bash-4.2# exit
sh-4.2# reboot

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Restore the CD-ROM Drive location

(4) Forget the password of the root user

1. Enter the emergency mode, load the system image, and switch to the system root environment

sh-4.2# chroot /mnt/sysimage

2. Reset the root user password

bash-4.2# passwd root

3. Exit the root system environment and restart

Guess you like

Origin blog.csdn.net/weixin_53567573/article/details/113769574