The boot process and service control in Linux

Linux operating system boot process

Power-on self-test (BIOS)-MBR boot-GRUB menu-load the kernel (kernel)-initial process initialization

  1. POST
    after boot server host, will be based on the motherboard BIOS settings for CPU, memory, video card, keyboard and other equipment preliminary testing, testing has been successfully handed over control of the system according to the preset startup sequence, most of the time will be handed over to the machine 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 file according to the MBR (Master Boot Record) setting in the first sector of the hard disk; or directly according to the MBR record The boot 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 Wait 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

  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
  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.
    Traditional sysvinit relies on 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 Provides the ability to start services on demand, so that fewer processes are started, thereby increasing the speed of system startup.

Systemd unit type

Insert picture description here

Systemd target corresponding to the run level

Insert picture description here

Repair MBR sector failure

  • cause of issue
  1. Damage caused by viruses, Trojan horses, etc.
  2. Incorrect partition operation, misoperation of disk read and write
  • Failure phenomenon
  1. The boot program is not found, the startup is interrupted
  2. Unable to load operating system, black screen after boot
  • Solutions
  1. Backup files should be made in advance
  2. Boot into emergency mode with the installation CD
  3. Restore from backup file

Troubleshoot startup faults

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

  1. Backup 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
    Insert picture description here

  3. The boot interface enters the emergency mode, and the MBR sector data is restored 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 bs=512 count=1	#恢复备份数据
sh-4.2# exit

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here

Fix GRUB boot failure

  • cause of issue
  1. The GRUB boot program in MBR is corrupted
  2. The grub.conf file is missing, and the boot configuration is incorrect
  • Failure phenomenon
  1. System booting stalls and the "grub>" prompt is displayed
  • Solutions
  1. Try to manually enter the boot command
  2. Enter emergency mode, rewrite or restore grub.conf from backup
  3. Rebuild the grub program into the MBR sector

Fix 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 configuration file

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

grub> insmod xfs
#Load the specified module into the kernel 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 # Kernel name and location and other information
grub> initrd16 /initramfs-3.10.0-693.el7.x86_64.img #Mirror system file
grub> boot #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, the partition table is stored in the 477-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

#Simulate the destruction of the GRUB boot program in the MRB, but does not destroy the partition table
dd if=/dev/zero of=/dev/sda bs=446 count=1

#The boot interface enters the emergency mode and restores the GRUB boot program 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

rm -rf /boot/grub2/grub.cfg

Press Esc to
select CD when restarting
Insert picture description here

#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
menubash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg

#Exit the chroot environment and restart
bash-4.2# exit
sh-4.2# reboot
Insert picture description here

Forgotten root user password

  • cause of issue
  1. Forgotten the password of the root user
  • Failure phenomenon
  1. Cannot perform management operations that require root privileges
  2. If there is no other account available, you will not be able to log in to the system
  • Solutions
  1. Enter emergency mode, reset password

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

sh-4.2# chroot /mnt/sysimage #Reset
root user password
bash-4.1# passwd root
Insert picture description here

System service control

systemctl control type service name

Control type

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

Run level of Linux system

View run level

runlevel command ###runlevel can only view the switch run level and the current run level
Insert picture description here

systemctl tool ###systemctl can view the default run level

Temporarily switch run level

init command ### The command parameter of init is the number
systemctl tool corresponding to the run level ### The command parameter of systemctl is the specific target

The connection between init and systemctl

init 0 systemctl isolate poweroff.target systemctl poweroff
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

Set permanent run level

ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

systemctl set-default multi-user.target

Optimize the startup process

ntsysv tool

  1. Provide an interactive, visual window
  2. Can be run in a character terminal
  3. Convenient for centralized management of multiple services
    ntsysv
    ntsysv --level level list
    space to select or unselect
    , ↑ ↓ key to change the position
    Insert picture description here

systemctl tool

  1. No interactive, visual window is provided
  2. Manage a single service more efficiently

View the startup status of system services

systemctl is-enabled service name

Set the startup status of the system service on/off

systemctl enable/disable service name

Supplementary commands

  1. Permanently modify the host name
    hostnamectl set-hostname newname

  2. View the status of the host name
    hostnamectl status

  3. Set the system language to Chinese
    localectl set-locale LANG=zh_CN.utf8

  4. View the language
    localectl used by the current system [status]

  5. View the system startup time
    systemd-analyze

Guess you like

Origin blog.csdn.net/Jun____________/article/details/113401229