Linux operating system boot process and system service control

Table of contents

1. Linux operating system boot process

1. Overview of the boot process

2. Detailed explanation of the boot process

2.1 Power-on self-test

2.2 MBR boot

2.3 GRUB menu

2.4 Loading the Linux kernel

2.5 init process initialization

3. System initialization process

3.1 init process (serial startup)

3.2 Systemd can replace service monitoring (start in parallel)

4.Systemd unit type

5. Systemd target corresponding to the run level

2. Repair MBR sector failure

1. Cause of failure

2. Fault phenomenon

3. Solution ideas

4. Specific steps to repair MBR sectors

4.1 Add hard disk and partition

4.2 Back up MBR sector data to other disks (/dev/sdb1) (cannot back up on the same hard disk)

 4.3 Simulate destruction of MBR boot sector

 4.4 Boot the interface into emergency mode, restore the MBR sector data from the backup file and load the disc image first.

 4.5 Enter the shell interface, mount the partition with backup, restore the static backup data to the damaged hard disk, and restart

3. Repair GRUB boot failure

1. Cause of failure

2. Fault phenomenon

3. Solution ideas

4. Specific steps to repair GRUB boot failure (with specific operations and commands)

4.1 Manual repair

4.2 Enter emergency mode

4.3 Boot the interface into emergency mode

5. Rebuild the GRUB menu configuration file

5.1 Simulate deletion of boot files

4. Forgot root user password

1. Cause of failure

2. Fault phenomenon

3. Solution ideas

4. Change password with CD

4.1 Restart the client, press ESC to enter rescue mode, click CD-ROM Drive to enter optical drive mode

4.2 Select Troubleshooting

4.3 Select Rescue a CentOS system

4.4 Enter 1 to enter the optical drive system

4.5 Change password 

5. Change password in single user mode

5.1 Press any key during startup to pause the startup, and press the e key to enter editing mode.

5.2 Move the cursor to the line starting with linux and add the kernel parameter rd.break 

5. System service control

1. Service unit file usually consists of three parts:

2. Common options for Unit segment 

3. Common options of Service segment

6. Operating levels of Linux systems

1. Check the running level

2. The runlevel command can only view the switching run level and the current run level.

 3. The systemctl tool can view the default run level. 


1. Linux operating system boot process

1. Overview of the boot process

 

2. Detailed explanation of the boot process

2.1 Power-on self-test

Hardware settings check, transfer system control
After the server host is turned on, a preliminary test of the CPU, memory, graphics card, keyboard and other devices will be carried out based on the settings in the motherboard BIOS. The test is successful. and then transfers system control according to the preset startup sequence. Most of the time, it will be transferred to the local hard disk, because the operating system is usually installed on the hard disk

Summary: Detect the first device that can boot the system, such as a hard disk or optical drive

2.2 MBR boot

Run the startup GRUB boot program placed in the MBR sector.
When the system is booted from the local hard disk, system control is first transferred to the partition containing the operating system boot file according to the setting of the MBR (Master Boot Record) in the first sector of the hard disk. ; Or directly call the boot menu based on the boot information in the MBR record. For example: GRUB, etc.

Summary: Run the startup GRUB boot program placed in the MBR sector

2.3 GRUB menu

The GRUB boot program obtains the settings and path locations of the kernel and image file systems by reading the GRUB configuration file /boot/grub2/grub.cfg.
For Linux operating systems, GRUB (Unified Boot Loader) is the most widely used multi-system boot program. After system control is passed to FRUB, the boot menu will be displayed for the user to choose. , and loads the Linux kernel file according to the selected options (or adopts the default value), and then transfers system control to the kernel.

GRUB startup phase

primary boot loader:
          1st stage.…The first 446 bytes of MBR boot the hardware to find the kernel

          5 stage:.. The sectors after MBR allow the bootloader in stage1 to identify the file system on the partition where stage2 is located.

secondary boot loader: 2nd stage, partition file/boot/grub2/grub.cfg
(CentOS 7 uses the GRUB2 boot loader)

Summary: The GRUB boot program obtains the settings and path locations of the kernel and image file systems by reading the GRUB configuration file /boot/grub2/grub.cfg.

2.4 Loading the Linux kernel

Load the kernel and image file system into memory.
 The Linux kernel is a pre-compiled special binary file, which is between the respective hardware resources and system programs. It replicates resource allocation and scheduling. After the kernel takes over the control of the system, it will fully control the entire system. The running process of the Linux operating system.

(In CentOS systems, the default kernel file is located at "/boot/vmlinuz-3.10.0-514.e17.x86_64")

Summary: Load the kernel and image files into memory 

2.5 init process initialization

loads the hardware driver, and the kernel loads the init process into the memory to run.
In order to complete the further system boot process, the Linux kernel first loads the "/sbin/init" program in the system into the memory and runs it (the running program is called a process), and the init process is responsible for completing Initialization of the entire system, and finally waiting for the user to log in.

Summary: Load the hardware driver, and the kernel loads the init process into memory and runs it.

3. System initialization process

3.1 init process (serial startup)

The /sbin/init program is loaded and run by the Linux kernel;
The init process is the first process in the system and is the parent process of all processes;
The PID (process ID) number of the init process is always 1.

3.2 Systemd can replace service monitoring (start in parallel)

Systemd is an init software for Linux operating system; (the first process of centos7 is systemd, the first process of centos6 and before is init)
Used in CentOS 7 The authorized Systemd (parallel startup of service processes) startup method replaces the traditional SysVinit (serial execution of shell scripts to start services);
The first init process running in CentOS 7 is /lib/systemd /systemd
Traditional sysVinit relies on serial execution of Shell scripts to start services, resulting in low efficiency and slow system startup speed
systemd can parallelize more service processes startup, and has the ability to provide on-demand startup services, allowing fewer processes to be started, thereby improving system startup speed


4.Systemd unit type


 

5. Systemd target corresponding to the run level

 

  

2. Repair MBR sector failure

The MBR is located in the first physical sector of the first hard disk (/dev/sda), with a total of 512 bytes. The first 446 bytes are the GRUB program, and the next 64 bytes are the partition table.

1. Cause of failure

Damage caused by viruses, Trojans, etc.
Incorrect partition operations, disk read and write errors

2. Fault phenomenon

The bootloader cannot be found and the startup is interrupted
The operating system cannot be loaded and the screen goes black after booting

3. Solution ideas

Prepare to add another hard disk as a backup;
Make a backup file in advance;
Boot into the rescue mode with the installation CD;
Restore from backup file.

4. Specific steps to repair MBR sectors

4.1 Add hard disk and partition
 

 

 

4.2 Back up MBR sector data to other disks (/dev/sdb1) (cannot back up on the same hard disk)

  1. dd if=/dev/sda of=/mnt/mdr.bak bs=512 count=1

  2. (Copy and overwrite, copy sda under dev to mnt and name it mbr.bak, 512 bytes at a time, copy once)

 

 4.3 Simulate the destruction of MBR boot sector

  1. dd if=/dev/zero of=/dev/sda bs=512 count=1

  2. (Write 512 bytes of space to sda once)

  3. hexdump -C -n 512 /dev/sda
  4. Check the first 512 bytes of the SDA hard disk and find that it has been damaged. At this time, you need to restart and enter the emergency mode. At this time, the system of the optical drive is read.

 

 4.4 Boot the interface into emergency mode, restore the MBR sector data from the backup file and load the disc image first.

1. When the installation wizard interface appears, select the "Troubleshooting" option.


2. Select the "Rescue a CentOS Linux system" option to enter rescue mode


3. Select "1", select Continue and press Enter to continue.


4. Press the Enter key again and you will enter the Bash Shell environment with the "sh-4.2#" prompt.

 

 

 

 4.5 Enter the shell interface, mount the partition with backup, restore the static backup data to the damaged hard disk, and restart


sh-4.2# mkdir bak
sh-4.2# mount /dev/sdb1 /bak
#Mount the partition with backup files < /span>  sh-4.2# reboot Check the first 512 bytes of the sda ​​hard disk and find that it has been repaired. sh -4.2# hexdump -C -n 512 /dev/sda #Restore backup data
sh-4.2# dd if=/bak/mdr.bak of=/dev/ sda



 

 

3. Repair GRUB boot failure

1. Cause of failure

The GRUB boot program (1-446 bytes) in MBR is damaged
The grub.cfg file is missing, the boot configuration is incorrect, and the file location is /boot/grub2/grup. cfg

2. Fault phenomenon

System boot stalls, grub > prompt displayed

3. Solution ideas

Try to manually enter the boot command to repair (not recommended)
Enter rescue mode, rewrite or restore grub.cfg from backup
Enter rescue mode to Rebuild grub program in MBR sector

4. Specific steps to repair GRUB boot failure (with specific operations and commands)

4.1 Manual repair

Just manually enter the following boot command after grub > on the page (it cannot be copied in the input, and the kernel information must be saved before the failure. This method is time-consuming, clumsy and cumbersome, and is not recommended)

grub> insmod xfs
grub> linux16 /vmlinuz-0-rescue-73d7ede256a74b0e975e69f22d862090 root=UUID=d069b243-6623-4983-8d61-3ec6956a4f2b ro rhgb quiet
grub> initrd16 /initramfs-0-rescue-73d7ede256a74b0e975e69f22d862090.img
grub> boot

4.2 Enter emergency mode

Restore the GRUB boot program, rewrite or restore grub.conf from backup (this method is the same as repairing the MBR sector, the only difference is bs=446) MBR is located in the first physical sector of the first hard disk, A total of 512 bytes, the first 446 bytes are the master boot record

#Create a new directory to mount the backup disk and backup the GRUB boot program
mkdir /bak
mount /dev/sdb1 /bak/< /span> sh-4.2# exit sh-4.2# dd if=/data/grup.bak of=/dev/sda bs=446 count=1 sh-4.2# mount /dev/sdb1 /data/ sh-4.2# mkdir /data #Boot the interface into emergency mode, from Restore the GRUB boot program from the backup file  dd if=/dev/zero of=/dev/sda bs=446 count=1 #Simulate the destruction of the GRUB boot program in the MBR, But do not destroy the partition table
dd if=/dev/sda of=/bak/grup.bak bs=446 count=1
 


 




4.3 Boot the interface into emergency mode

Rebuild GRUB menu configuration file

#Delete the configuration file and simulate GRUB failure
rm -rf /boot/grub2/grub.cfg    
 
#Enter rescue mode and load the CD image , switch to the system root environment                   
sh-4.2# chroot /mnt/sysimage                    
 
#Reinstall the GRUB boot program to the first hard disk (dev/sda) MBR sectors, if there are multiple partitions, you can omit them
bash-4.2# grub2-install /dev/sda                                                                                                                                                                    /span>                 sh-4.2# reboot bash-4.2# exit #Exit the chroot environment and restart       bash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg 
 


 


5. Rebuild the GRUB menu configuration file

5.1 Simulate deletion of boot files

5.2 After restarting, press ESC within half a second to enter emergency mode.

 

5.3 Select and click CD-ROM Drive to enter CD-ROM drive mode

 

 5.4 Select Troubleshooting

 

5.5 Select Rescue a CentOS system

 

5.6 Enter 1 after entering to enter the optical drive system

 

5.7 Rebuild grub program

 

4. Forgot root user password

1. Cause of failure

Forgot root user password

2. Fault phenomenon

Unable to perform management operations that require root privileges
If there are no other available accounts, you will not be able to log in to the system

3. Solution ideas

Enter emergency mode and reset password

4. Change password with CD


4.1 Restart the client, press ESC to enter rescue mode, click CD-ROM Drive to enter optical drive mode
 

4.2 选择Troubleshooting

 

4.3 选择Rescue a CentOS system

 

4.4 Enter 1,enter the optical drive system

 

4.5 Change password 

 

5. Change password in single user mode

5.1 Press any key during startup to pause the startup, and press the e key to enter editing mode.

 

 

5.2 Move the cursor to the line starting with linux and add the kernel parameter rd.break 

If SELinux is enabled, you need to execute touch /.autorelabel before executing the exit operation. If it is not started, you do not need to execute touch /.autorelabel

 

5. System service control

Format: systemctl control type service name

There are programs running in the background of the system.

systemctl enable --now service name Start immediately and do not start automatically after boot
systemctl disable --now service name Close the service immediately and do not start automatically after boot
systemctl enable service name does not start automatically at boot
systemctl disable service name does not start automatically at boot

System service control type
Control type effect
start start up
stop stop
restart Restart
reload Reload
status Check service status
is-enabled

Check whether the service is started

 

1. Service unit file usually consists of three parts:

[Unit]: Defines common options independent of the Unit type, used to provide unit description information, unit behavior and dependencies.
[Service]: Special options related to a specific type, here is the Service type
[Install]: Defined by the "systemctl enable" and systemctl disable" commands Options used when starting or disabling the server

2. Common options for Unit segment 

Description: Description information
After: Defines the startup sequence of the unit, indicating which units the current unit should start later. Its function is opposite to Before
Requires: Depends on other units, strong dependency, when the dependent units cannot be activated, the current unit cannot be activated
wants: Depends on other units, weak dependency
conficts: Define conflict relationships between units

3. Common options of Service segment

Type: defines the unit process startup type that affects the functions of ExecStart and related parameters

 - EnvironmentFile: Environment configuration file
- ExecStart: Specify the absolute path to run the command or script to start the unit
- ExecStartPre: Run before ExecStart< /span> - PrivateTmp: When set to yes, the /tmp/systemd-private-UUID-NAME.service-XXXXX/tmp/ directory will be generated - time unit is specified, it is assumed to be in seconds. For example, setting it to "20" is equivalent to setting it to "20s". - RestartSec: Set how long to pause before restarting the service (Restart=). The default value is 100 milliseconds (100ms). If no - Restart: When set When Restart=1, when the daemon service terminates unexpectedly, the service will be automatically started again - ExecStop: Specify the command or script to run to stop the unit
- ExecStartPost: Run after ExecStart




6. Operating levels of Linux systems

1. Check the running level

The runlevel command can only view the switching run level and the current run level
The systemctl tool can view the default run level

2. The runlevel command can only view the switching run level and the current run level.


 

 Use the init command on the terminal to switch levels

 3. The systemctl tool can view the default run level. 

Extension: etc/rc.d/rc#.d starts the service in the corresponding /etc/rc.d/rc#.d directory according to the run level (# is 0-6), which is located in the loading system service step in the boot process middle

Extension: Services written by yourself and compiled and installed will not be loaded into etc/rc.d/rc#.d, and cannot be turned on or off using systemd.

Extension: curl IP address: You can view the web page

Guess you like

Origin blog.csdn.net/weixin_52269501/article/details/128306961