Linux basic boot process and service control (relatively important, need to understand)

1. The boot process of Linux operating system

Insert picture description here

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 card, keyboard and other devices are faulty. If there is no fault, the system will be transferred according to the preset startup sequence, which is the normal situation. Machine hard drive.
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).
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 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

2. System initialization process

1. The 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

2 、 Systemd

  • Systemd is an 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

name Start method effectiveness Start speed
Traditional sysvinit Rely on the serial execution of the Shell script to start the service
(there is a dependency between each file, start according to the dependency, in order)
All services are started, resulting in low efficiency System startup is slow
system Start more service processes in parallel
(ignore the dependencies first, start them at the same time, and establish the dependencies after the corresponding dependent software is started)
Provides the ability to start services on demand, so that fewer processes are started, and efficiency is high Fast system startup

3. 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 organized by hierarchy in Cgroup
Targe .target Describe a set of systemd units
  • The three main unit types are Service, Socket, and Targe, and the others are also required to see what they mean.
  • These unit types are appended to the resource name in the form of a suffix, such as network service network.service.

4. The 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 equivalent 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, the host will be shut down and restarted normally when using this level

The most used are 0, 3, 5, 6, 3 for servers, 5 for personal use, and 1 for system maintenance. The higher levels 2 and 4 use very little.

3. Eliminate startup faults

Demonstration of fault repair in the next blog: linux technical document: simulate and repair the fault

1. MBR sector failure analysis

1) Cause of failure

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

2) Failure phenomenon

  • Cannot find the boot program, start interrupted
  • Unable to load operating system, black screen after boot

3) Solutions

  • Backup files should be made in advance
  • Boot into emergency mode with 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

先加载好光盘镜像,重启操作系统,
当出现安装向导界面时,选择“Troubleshooting”选项,
再选择“Rescue a CentOS Linux system”选项,进入急救模式
选择“1”选择 Continue并按 Enter 键继续
再次按 Enter 键后将进入带“sh-4.2#”提示符的 Bash Shell 环境
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 环境,系统将会自动重启

3. GRUB boot failure analysis

1) Cause of failure

  • 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)

2) Failure phenomenon

  • System booting stalls, displaying "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

4. Repair GRUB boot failure

1) Method 1: Manually enter the boot command (clumsy and cumbersome, not recommended)

/boot/grub/目录下的包是用于启动菜单的背景图片及样式
/boot/grub2/grub.cfg			#GRUB配置文件

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

2) Method 2: Enter emergency mode and restore 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 So here we back up the first 446 bytes and keep the previous partition table.
mkdir /bak
mount /dev/sdb1 /bak
dd if=/dev/sda of=/bak/grub.bak bs=446 count=1
  • Simulate 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: The boot interface enters emergency mode, and the GRUB menu configuration file is rebuilt.
This method generally deals with the situation that the /boot/grub2/grub.cfg file is missing or the boot configuration is incorrect.

  • Delete the grub.cfg file to simulate failure
rm -rf /boot/grub2/grub.cfg
  • Enter emergency mode, load the system image, 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

5. Password analysis of forgotten root user

1. The cause of the fault:
The password of the root user is forgotten.
2. The phenomenon of the fault
. The management operation that requires root privileges cannot be performed.
If there is no other available account, the system will not be able to log in.
3. Solutions to
enter the emergency mode and reset the password

6. Reset the password of the root user

sh-4.2# chroot /mnt/sysimage     #进入急救模式,加载系统镜像,切换到系统根环境
bash-4.1# passwd root      #重设 root 用户密码

Four, system service control

1. System service control

systemctl 控制类型 服务名称

2. Control type

start: start
stop: stop
restart: restart
reload: reload
status: view service status
...

Five, the operating level of the Linux system

1. View the run level

  • runlevel command
  • systemctl tool
    --systemctl get-default (view system default run level)

2. Temporarily switch the run level

  • init command
  • systemctl tool

3. Commands corresponding to the run level

After the function is the command that can be run, and the function is the same

effect init run level systemd的target
Shutdown init 0 systemctl isolate poweroff.target systemctl poweroff poweroff shutdown -h time halt
Single user mode init 1 systemctl isolate rescue.target
Character interface init 3 systemctl isolate multi-user.target
graphic interface init 5 systemctl isolate graphical.target
Reboot init 6 systemctl isolate reboot.target systemctl reboot reboot shutdown -r time

Supplement: shutdown -h and -r must be followed by time, for example: now, shutdown or restart immediately; automatic shutdown or restart after 10 or 10 minutes; 12:00, shutdown or restart at 12:00. Command format: shutdown -r now
If the shutdown or restart is set through the shutdown command, you can use the shutdown -c command to cancel the restart.

4. Set permanent operation level

ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target      #设置永久运行级别
systemctl set-default multi-user.target    #设置永久运行级别
两者作用一样,选其一即可
systemctl get-default  #查看系统默认的运行级别

Insert picture description here

Six, optimize the startup process

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

2. Systemct tool

  • No interactive, visual window is provided
  • Manage a single service more efficiently

7. Start and control of system services

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

After entering ntsysv, it displays
Insert picture description here

2. Start and control 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 服务名称       #关闭开机自启动

Guess you like

Origin blog.csdn.net/weixin_51326240/article/details/110287888