Article Directory
-
-
- The boot process of the Linux operating system
- System initialization process
- Troubleshoot startup faults
- System service control
- Run level of Linux system
- Optimize the startup process
- Start and control of system services
-
The boot process of the Linux operating system
1. POST
- When the server is turned on, it will perform a power-on self-test to detect whether hardware devices such as CPU, memory, graphics cards, keyboards and other devices are faulty. If there is no fault, the system will be transferred according to the preset startup sequence. This is the normal situation. Machine hard drive.
- 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).
- The MBR is 512 bytes in size and stores pre-boot information, partition table and other information.
- 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.
- GRUB is the first 446 bytes of the MBR program.
- 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
- After the kernel is loaded, the first program to run is "/sbin/init" (the running program is called a process). The init process is responsible for completing the initialization of the entire system, and finally waiting for the user to log in.
- Summary: Load the hardware driver, the kernel loads the init process into the memory to run
System initialization process
init process
- Load and run the /sbin/init program by the Linux kernel
- The init process is the first process in the system and the parent process of all processes
- The PID (Process ID) number of the init process is always 1
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 /lib/systemd/systemd
Comparison of traditional sysvinit and systemd
- Traditional sysVinit relies on the serial execution of shell scripts to start services, 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.
Systemd unit type
Unit type | extension name | Description |
---|---|---|
Service | .service | Describe a system service |
Socket | .socket | Describe a socket (IP port) 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 that reside in Cgroup through hierarchical organization |
Targe | .target | Describe a set of systemd units |
Systemd target corresponding to the run level
init 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, 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 |
Troubleshoot startup faults
Repair MBR sector failure
cause of issue:
- Damage caused by viruses, Trojan horses, etc.
- Incorrect partition operation, misoperation of disk read and write
Trouble phenomenon:
- The boot program is not found, the startup is interrupted
- Unable to load operating system, black screen after boot
Solutions:
- Backup files should be made in advance
- Boot into emergency mode with the installation CD
- Restore from backup file
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 环境,系统将会自动重启
GRUB boot failure
cause of issue
- The GRUB boot program in MBR is corrupted
- The grub. cfg file is missing, and the boot configuration is incorrect (the location of the centos7 file is /boot/grub2/grub. cfg)
Failure phenomenon
- System booting stalls and the "grub>" prompt is displayed
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
Fix GRUB boot failure
/boot/grub/目录下的包是用于启动菜单的背景图片及样式
/boot/grub2/grub.cfg #GRUB配置文件
Method 1: Manually enter the guide 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 #内核的名字及位置等信息
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
-
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, and the partition table is stored in the 477-510 bytes in the MBR sector Medium, so the first 446 bytes are backed up here, and the previous partition table is retained.
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
Method 3: Boot the interface into emergency mode and rebuild the GRUB menu configuration file
-
Delete the grub.cfg file to simulate failure
rm -rf /boot/grub2/grub.cfg
-
Enter the emergency mode, load the system 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
Forgot root user password
cause of issue
- Failure to forget the password of the root user
- Cannot perform management operations that require root privileges
- If there is no other available account, you will not be able to log in to the system.
Solution - Enter emergency mode, reset password
Reset the password of the root user
sh-4.2# chroot /mnt/sysimage #进入急救模式,加载系统镜像,切换到系统根环境
bash-4.1# passwd root #重设 root 用户密码
System service control
System service control
systemctl 控制类型 服务名称
Control type
start:启动
stop:停止
restart:重新启动
reload:重新加载
status:查看服务状态
Run level of Linux system
View run level
- runlevel command
- systemctl tool
Temporarily switch run level
- init command
- systemctl tool
Commands corresponding to the run level
init 0 systemctl isolate poweroff.target systemctl poweroff shutdown -h now
init 1 systemctl isolate rescue.target
init 3 systemctl isolate multi-user.target
init 5 systemctl isolate graphical.target
init 6 systemctl isolate reboot.target systemctl reboot shutdown -r now
Set permanent run level
ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target #设置永久运行级别
或
systemctl set-default multi-user.target #设置永久运行级别
systemctl get-default #查看系统默认的运行级别
Optimize the startup process
ntsysv tool
- Provide an interactive, visual window
- Can be run in a character terminal
- Convenient for centralized management of multiple services
- Used to control whether the service starts automatically after booting
systemct tool
- No interactive, visual window is provided
- Manage a single service more efficiently
Start and control of system services
ntsysv system service management tool
ntsysv
ntsysv --level 级别列表
- Press the space bar to mark the service option
- Press ↑, ↓ to switch service options
- Press the tab key to switch the cursor
Start and control of system services
-
View the startup status of system services
systemctl is-enabled 服务名称
-
The service name sets the startup status of the system service
systemctl enable 服务名称 #开启开机自启动 systemctl disable 服务名称 #关闭开机自启动