Boot process and service control (including simulating MBR sector failures and repairing GRUB boot failures!)

Boot process and service control

1. The boot process of Linux operating system

Flow chart of the boot process

Insert picture description here

1. POST

After the server host is turned on, the CPU, memory, graphics card, keyboard and other devices will be initially tested according to the settings in the motherboard BIOS. After the test is successful, the system control will be transferred according to the preset startup sequence, and most of the time will be transferred to the local hard disk.
Summary: The first device capable of booting the system is detected, such as a hard disk or an optical drive

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 files according to the MBR (Master Boot Record) setting in the first sector of the hard disk; or directly according to the boot in the MBR record The 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 locations

4. 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

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
  • The PID (Process ID) 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, replacing the traditional SysVinit
  • The first init process running in CentOS is /lib/systemd/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 provide on-demand startup services, so that fewer processes can be started, thereby improving System startup speed.

3.Systemd unit type

Unit type extension name Description
Service .service Describe a system service
Socket .socket Describe a socket for inter-process communication
Device .device Describe a device file recognized by the kernel
Mount .mount Describe the mount point of a file system
Automount .automount Describe the automatic mount point of a file system
Swap .swap Describe a memory swap device or file
Path .path Describe a file or directory in a file system
Timer .timer Describe a timer
Target .target Describe a set of systemd units
Snapshot .snapshot Used to save the state of a systemd
Scope .scope Use systemd's bus interface to programmatically create external processes
Slice .slice Describe a group of management system processes that reside in Cgroup through hierarchical organization

4. Systemd target corresponding to the run level

init 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 multi-user.target User-defined/domain-specific run level, the default is equal to 3, compared to 3, no network
3 multi-user.target Full user mode with character interface, most server hosts run at this level
4 multi-user.target User-defined/domain-specific run level, not used by default in centos6, reserved, it is equivalent to 3 in centos7
5 graphical.target The multi-user mode of the graphical interface provides a graphical desktop operating environment
6 reboot.target Restart, when this level is used, the host will be shut down and restarted normally

2 and 4 are not commonly used, 5 is often used for individuals, and 0, 3, 5, and 6 are used for servers and are more commonly used.

Third, the troubleshooting of startup faults

1. MBR sector failure analysis

1) Cause of 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

2. Simulate MBR sector failure and repair

  • MBR is located at the first physical sector of the first hard disk (/dev/sda), a total of 512 bytes

1) Back up MBR sector data to other disks (/dev/sdb1)

mkdir /backup  #创建一个文件夹作为挂载点
mount /dev/sdb1 /backup
dd if=/dev/sda of=/backup/mbr.bak bs=512 count=1

2) Simulate destruction of MBR boot sector

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

3) The boot interface enters emergency mode and restores MBR sector data from the backup file

Load the CD image first, restart the operating system,
when the installation wizard interface appears, select the "Troubleshooting" option,
and then select the "Rescue a CentOS Linux system" option to enter the emergency mode.
Select "1", select Continue and press Enter to continue
pressing again After the Enter key, you will enter the Bash Shell environment with "sh-4.2#" prompt

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

①Add a new hard disk in shutdown state

Insert picture description here

②Turn on and check the partition

Insert picture description here

③Partition management, partition number and partition type

Insert picture description here

④Format and mount

Insert picture description here

⑤Back up the contents of the MBR master boot record in /dev/sda1, and then make a task to make the /dev/sda master boot partition damaged, and then restart

Insert picture description here

⑥ Next, go to the virtual machine side to operate, enter the emergency mode, and then select rescue system

Insert picture description here

Insert picture description here

⑦Enter 1 to choose continue, double-click Enter to enter the shell environment, the follow-up operation is as follows, after the final repair is completed, enter exit and enter to start normally

Insert picture description here

3. Repair GRUB boot failure

The package in the /boot/grub/ directory is used for the background image and style of the boot menu

/boot/grub2/grub.cfg			    #GRUB配置文件

1) Method 1: Manually enter the boot command (clumsy and cumbersome, 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	

Information such as the name and location of the kernel

grub> initrd16 /initramfs-3.10.0-693.el7.x86_64.img			#镜像系统文件
grub> boot						   	#引导boot

2) Method 2: Enter emergency mode and restore GRUB boot program (similar to MBR boot sector)

MBR is located at the first physical sector of the first hard disk (/dev/sda), a total of 512 bytes, the first 446 bytes are the master boot record, the partition table is stored in the 447-510 bytes in the MBR sector in.

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

Simulates the destruction of the GRUB boot program in MRB, but does not destroy the partition table

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

The boot interface enters emergency mode, and the GRUB boot program is restored 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

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

rm -rf /boot/grub2/grub.cfg

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

sh-4.2# chroot /mnt/sysimage

Reinstall the GRUB boot program to the MRB sector of the first hard disk (/dev/sda)

bash-4.2# grub2-install /dev/sda

Rebuild the configuration file of the GRUB menu

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

Exit the chroot environment and restart

bash-4.2# exit
sh-4.2# reboot

① First delete the configuration file /boot/grub2/grub.cfg under the boot program

This configuration file contains the settings and path settings of the kernel and mirror file system. The GRUB boot program can read this configuration file to obtain the relevant settings. Deleting this file will cause the boot to fail.

Insert picture description here

②Change the starting method

Insert picture description here

Remember to F10 to save and exit after the startup mode is changed

Insert picture description here

Enter emergency mode

Insert picture description here

Rescue system

Insert picture description here

③Switch to the system root environment, reinstall the grub boot program and rebuild the configuration file

Insert picture description here

④ Change back to the original startup mode, F10 to save and exit

Insert picture description here

Four, 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

Enter the emergency mode, rescue the system, and then follow-up operations are as follows (password input is recommended to use the number keys above)

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/111183356