Linux_ system boot process, repair MBR sector failure

1. The boot process of linux operating system

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 optical drive

2. MBR boot

When starting the system from the local hard disk, first transfer system control 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 boot in the MBR record The information calls the boot menu (eg 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 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

The difference between sysvinit and systend:

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, thereby improving System startup speed.

2. System initialization process

1. init process

●The /sbin/init program is loaded and run by the linux kernel

●init process is the first process in the system

●The PID (process identifier) ​​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

Insert picture description here

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 swap file
Path .path Describe a file or directory in a file system
Timer .timer Describe a timer (used to implement cron-like scheduling tasks)
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 organized by hierarchy in a cgroup
Target .target Describe a set of systemd units

4. The systemd target corresponding to the run level

Run level Systemd的target Description
0 target Shutdown state, the host will be shut down when using this level
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 equivalent to 3
3 multi-user.target Full multi-user mode with character interface, most server hosts run at this level
4 multi-user.target User-defined/domain specific run level. The default is equivalent to 3
5 graphical.target The multi-user mode of the graphical interface provides a graphical desktop operating environment
6 reboot.target Restart, the host will be restarted when using this level

3. Repair MBR sector failure

1. Cause of failure

●Destruction caused by viruses, Trojan horses, etc.
●Incorrect partition operations, disk read/write errors

2. Failure phenomenon

●The boot program cannot be found, the startup is interrupted
●The operating system cannot be loaded, and the screen goes black after booting

3. Solutions

●Backup files should be made in advance
● Boot
from the installation CD into emergency mode ●Recover from backup files

4. Eliminate startup faults (experimental steps)

-Repair MBR sector failure

MBR is located at the first physical sector of the first hard disk (dev/dsa). 512 bytes in total

1. Backup MBR sector data to other disks (/dev/dsb1)

①: Add a new hard disk, fdisk -l to view the hard disk status
②: fdisk /dev/sdb n-----p ------…enter-----w ,### Create disk partition
③: mkfs.xfs /dev/sdb1 ### Formatting
④: mkdir /backup ### Create backup directory
⑤: mount /dev/sdb1/backup/ ### Mount the disk partition to the directory
df -h check whether it is mounted Successfully loaded
⑥: dd if =/dev/sda of =/backup/mbr.bak bs=512 count =1 ### Backup file
: Take the file from the first hard disk, backup mount point directory, specify the save file The name
is 512 bytes in size, and the number of extractions is 1
⑦: cd /backup -------ls ------ll #### Check whether the file is well preserved

2. Simulate destroying MBR boot sector: (destroying the 512-byte size of the first hard disk)

①: dd if =/dev/zero of =/dev/sda bs=512 count=1
: Take out the empty character file , the first hard disk 512 bytes once
②: init 6 #### Restart

3. The boot interface enters emergency mode and restores MBR sector data from the backup file
———First load the CD image and restart the system

①: When the installation item interface appears, select the "Trouble shooting" option

②: Then select "Rescue a Centos Linux system" option to enter emergency mode

③: Select "1", select Continue and press Enter to continue

④: After pressing enter again, you will enter the Bash shell environment with "sh -4.2#" prompt

sh -4.2# mkdir /backupdir ####### Create a directory

sh-4.2# mount /dev/sdb1 /backupdir #### Mount the partition with backup files

sh-4.2# dd if =/backupdir /mbr.bak of=/dev/sda bs=512 count=1
####################Restore backup data

sh-4.2# exit #########Exit

Five. Experimental steps
1. Backup MBR sector data to other disks (/dev/dsb1)

①: Add a new hard disk, fdisk -l to check the hard disk status

Insert picture description here
Insert picture description here
②: fdisk /dev/sdb n-----p ------…enter-----w ,### Create a disk partition

Insert picture description here
③: mkfs.xfs /dev/sdb1 ### to format
Insert picture description here ④: mkdir /backup ### Create backup directory
⑤: mount /dev/sdb1/backup/ ### Mount the disk partition to the directory
df -h check Whether the mounting is successful
Insert picture description here
⑥: dd if =/dev/sda of =/backup/mbr.bak bs=512 count =1 ### Backup file⑦
: cd /backup -------ls ----- -ll #### Check if the file is well preserved

Insert picture description here

2. Simulate destroying MBR boot sector: (destroying the 512-byte size of the first hard disk)

①: dd if =/dev/zero of =/dev/sda bs=512 count=1
: Take out the empty character file , the first hard disk 512 bytes once
②: init 6 #### Restart

Insert picture description here

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

①: When the installation item interface appears, select the "Trouble shooting" option
Insert picture description here②: Then select the "Rescue a Centos Linux system" option to enter the emergency mode
Insert picture description here
③: Select "1", select Continue and press Enter to continue

④: After pressing enter again, you will enter the Bash shell environment with "sh -4.2#" prompt

Insert picture description hereInsert picture description here
④: After pressing enter again, you will enter the Bash shell environment with "sh -4.2#" prompt

Insert picture description here

sh -4.2# mkdir /backupdir ####### Create a directory

sh-4.2# mount /dev/sdb1 /backupdir #### Mount the partition with backup files

sh-4.2# dd if =/backupdir /mbr.bak of=/dev/sda bs=512 count=1
####################Restore backup data

sh-4.2# exit #########Exit
Insert picture description hereand then enter exit——————————Exit and restart

Recovery:
Insert picture description here
At this time the fault has been repaired, restarting to return to normal, you can enter the system:
Insert picture description here

Guess you like

Origin blog.csdn.net/Wsxyi/article/details/113697569