System boot is the beginning of the operation of the operating system. Before the user can log in to the system normally, the Linux boot process completes a series of initialization tasks and loads the necessary programs and command terminals to prepare for the user to log in.
1. Overview of the boot process
1.1 Power-on self-test
After the server host is turned on, the CPU, memory, graphics card, keyboard and other devices will be preliminarily tested according to the settings in the motherboard BIOS (Basic Input Output System). After the test is successful, the system control will be transferred according to the preset startup sequence. Most of the time, it will be transferred. To the local hard drive
Detect the first device capable of booting the system, such as a hard disk or optical drive
1.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 Information to call the boot menu (eg GRUB)
Run the boot GRUB boot program placed in the MBR sector
1.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 GROB, the boot menu will be displayed for the user to choose, and according to the selected option (or use the default value) ) Load the Linux kernel file, and then transfer system control to the kernel. Centos 7 uses GRUB2 boot loader. GRUB boot program reads the GRUB configuration file /boot/grub2/grub.cfg to obtain the kernel and mirror file system settings
1.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.e17.x86_64"
Load the kernel and mirror file system into memory
1.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
Load the hardware driver, the kernel loads the init process into the memory to run
2.init process
The processes (running programs) in the Linux system are marked with numbers. The identification number of each process is called PID. In the process of booting Linux, "/sbin/init" is the first program loaded by the kernel. Therefore, the PID number corresponding to the init process is always "1"
After the init process runs, it will continue to execute other programs in the system, and continuously generate new processes. These processes are called child processes of the init process. On the other hand, the init process is the parent process of these processes. Of course, these child processes can also be further generated. The respective sub-processes continue to multiply at a time, and finally form a leafy process tree, which together provide services for users
Init is the "ancestor" of all processes that maintain the entire Linux system. Therefore, the init process is not allowed to be easily terminated. When you need to switch between different system operating states, you can send the correct execution parameters to the init process, which is done by init itself. Related operations
3.Systemd
Systemd is an init software of Linux operating system
CentOS7 uses a new Systemd startup method to replace the traditional SysVinit
Traditional SysVinit relies on the serial execution of Shell scripts to start the service, resulting in low efficiency and slow system startup
Systemd can start more service processes in parallel, and has the ability to start services on demand, so that fewer processes are started, thereby increasing the system startup speed
The first init process running in CentOS7 is /lib/systemd/systemd
4. 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 a memory swap device or swap file
Swap
.swap
Describe a memory swap device or swap file
Path
.path
Describe a file or directory in a file system
Timer
.timer
Description-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 Cgroup
Target
.target
Describe a set of systemd units
5. 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 equal to 3 (access to the network is not supported)
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
2. Eliminate startup faults
1. Repair MBR sector failure
1.1 Causes of failure
Damage caused by viruses, Trojan horses, etc.
Incorrect partition operation, disk read and write errors
1.2 Failure phenomenon
Cannot find the boot program, start interrupted
Unable to load operating system
1.3 Solutions
Backup files should be made in advance
Boot into emergency mode with installation CD
Restore from backup file
1.4 Practice
1.4.1 Back up MBR sector data to other disks (/dev/sdb1)
First add a new hard drive
New partition
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xbd9ad88f 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-83886079,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-83886079,默认为 83886079):
将使用默认值 83886079
分区 1 已设置为 Linux 类型,大小设为 40 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost backup]# init 6
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(CentOS 7-3) at 16:58:38.
Type `help' to learn how to use Xshell prompt.
[D:\~]$
1.4.3 Boot interface
After restarting, I found that the system interface was not entered, but the installation wizard appeared
Here we select the "Troubleshooting" option to enter the emergency mode
Type "1" to select Continue and press Enter to continue
Press Enter again to enter the Bash Shell environment with "sh-4.2#" prompt
Here we do not need to backup, because it is directly rebuilt
[root@localhost boot]# cd grub2
[root@localhost grub2]# ls
device.map fonts grub.cfg grubenv i386-pc locale
[root@localhost grub2]# rm -rf grub.cfg
[root@localhost grub2]# ls
device.map fonts grubenv i386-pc locale
Enter reboot to restart, then you will enter this interface
Attention, how do we enter the boot interface at this time, the hand speed must be fast, press "ESC" immediately after clicking the restart, try several times, it will always succeed
Here we choose CD
2.4.2 Boot interface
Enter emergency mode and restore CenOS system
Enter 1, press enter
Switch to the system root environment (this time you can use the TAB key to complete)
sh-4.2# chroot /mnt/sysimage
bash-4.2#
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
OK, you can enter the system interface, let's verify it again and find that grub.cfg has it again
[root@localhost ~]# cd /boot/grub2
[root@localhost grub2]# ls
device.map fonts grub.cfg grubenv i386-pc locale