Boot process and service control

Posted by jocknerd on Mon, 03 Jan 2022 17:02:24 +0100

Preface

I. Linux Operating System Should Be Processed

Overview of the boot process

Post

After the server host is powered on, it will initially detect cpu (central processor), memory, graphics card, keyboard and other devices according to the settings in the motherboard BIOS. After successful detection, system control will be transferred according to the preset boot order, most of the time to the hard disk.

Summary: Detect the first device that can boot the system, such as a hard disk or CD-ROM drive or a U-disk

MBR boot

When the system is started from its own hard disk, control is first passed to the partition containing the operating system boot file based on the MBR (Master Boot Record) settings in the first sector of the hard disk. Or call the Startup menu (such as grub) directly from the boot information in the MBR record

Summary: Run the boot grub bootstrapper placed in the MBR sector (main bootstrapper is 446 bytes, partition table DPT 16 bytes 16*4=64 MBR valid identifier occupies 2 bytes)

GRUB Menu

For Linux operating systems, GRUB (GRand Unified Bootloader) is the most widely used multi-system bootstrapper. When system control is passed to GRUB, a Startup menu is displayed giving the user a choice, a Linux kernel file is loaded based on the choice (or default value), and system control is given to the kernel. It should be noted that centos 7 uses a grub2 boot bootstrapper.

Summary: The grub bootstrapper reads the grub configuration file/boot/grub2/grub.cfg, to get the settings and path locations for the kernel and mirror file systems.

Loading Linux Kernel

The Linux kernel is a special pre-compiled binary file, between various hardware resources and system programs, responsible for resource allocation and scheduling. When the kernel takes over control of the system, it will have full control over the entire Linux operating system. On centos systems, the default kernel file is located at "/boot/vmlinuz-3.10.0-514.e17.x86_64".

**Summary: Load the kernel and mirror file system into memory**

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 memory to run (the running program is called a process), and the init process is responsible for completing a series of system initialization processes, and finally waits for the user to log in to the system.

Summary: Load hardware drivers and the kernel loads init processes into memory to run

Traditional sysVinit relies on serial execution of shell scripts to start services, resulting in inefficiencies. systemd, which is slower, can start more service processes in parallel, and has the ability to provide on-demand startup services, resulting in fewer programs being started, thereby increasing the system startup speed.

Differences between GRUB2 and Grub

(1) Support multiple file system formats

(2) stage L-5 stage 2 has been cancelled in grub

(3) grub2 uses a module mechanism to extend functionality by dynamically loading the required modules, which allows centos to be small

(4) Support for automatic decompression

(5) The most obvious differences are in the configuration files. / boot/grub/grub.conf has been/boot/grub2/grub.cfg

  • /boot/grub2/grub.cfg. Do not edit this file even if you are using Roor users, it will be executed automatically every time you execute grub2-mkconfig
  • /etc/default/grub is the main configuration file that changes the appearance of the boot menu
  • /etc/grub.d/* Various for generating grubs. CFG script file, which executes every time grub2-mkconfig is executed

System Initialization Process

init process

# Run/sbin/init programs loaded by the Linux kernel

The init process is the first process in the system

The PID number of the init process is always 1

Systemd

Systemd is an init software for Linux operating system

< centos 7 uses a new Systemd startup method instead of the traditional SysVinit

The first init process running in Centos7 is/lib/systemd/systemd

Systemd cell type

Systemd target for runlevel

Role at all levels

  • 0 (poweroff.target): Shutdown - system down mode, the default system run level cannot be set to L, otherwise it cannot start normally, the machine shuts down;
  • 1(rescue.target): single user mode - single user mode, root rights, for system maintenance, remote login is prohibited (only through the real machine), the mode of entry can be Baidu operation; Just like login in secure mode under windows; (Root can log in without a password when entering single-user mode, where the root password can be modified)
  • 2 Multi-user mode, no NFS and network support;
  • 3 Complete multi-user text mode with NES and network, enter console command line mode after login.
  • 4 The system is not in use, it is generally not used to retain, and can be used to do something in some special cases. For example, when the notebook battery is exhausted, switch to this mode to make some settings;
  • (2-4 (mulit-user.target): Multi-purpose text interface)
  • 5 (graphical.target) - multi-purpose graphical interface - Graphical mode, enter graphical cUI mode after login, X windows system;
  • 6 (reboot.target): Restart-Restart mode, the default runlevel cannot be set to 6, otherwise it will not start properly. Running init6 machine will restart

Repair MBR Sector Failure

Failure Reason

Disruption caused by viruses, trojans, etc.

Incorrect partition operation, disk read/write error operation

Failure Phenomena

# Bootstrapper not found, start interrupt

Failed to load operating system, black screen after power on

Solution ideas

Backup files should be done well in advance

Compact Disc Installed to Guide to First Aid Mode

Resume from backup file

dd if=/dev/sda of=/guazai/ruocheng.bak bs=512 count=1 (if="specifies the output device (or file)
Of="Specifies the output device (or file) bs=" Specifies the size of the read block count="Specifies the number of read blocks to test whether the data is still there after adding it to the new mount directory)

MBR Failure Demo

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

Simulate Destroy MBR Boot Sector

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

Boot interface into emergency mode, recover MBR sector data from backup files, load the disc image, restart the operating system, select Troubleshooting option when the installation wizard interface appears, and then select Rescue a centos Linux system option. Enter first aid mode Select "1" Select continue and press Enter to continue pressing Enter again will enter Bash Shell environment with "sh-4.2#" prompt

sh-4.2# mkdir /backupdir

sh-4.2# mount /dev/sdb1 /backupdir    
###Mount partitions with backup files

sh-4.2# dd if=/ backupdir/mbr.bak of=/dev/sda bs=512 count=1             
###Restore backup data.

sh-4.2# exit               
###Execute exit command to exit temporary shell environment, system will restart automatically

Repair GRUB Failure

Fault-Induced Causes

  • The GRUB bootstrapper in the MBR was damaged
  • grub.conf file missing, boot configuration error

Failure Phenomena
System boot stalls, showing a "grub>" prompt

Solution ideas
Try to enter the boot command manually
Enter first aid mode, rewrite or restore grub from backup. Conf
Rebuild grub program to MBR sector

Specific operation process
Packages in the /boot/grub/directory are background pictures and styles used to launch the menu
/boot/grub2/ grub.cfg

GRUB Failure Demo

Manually enter boot commands

(clumsy and cumbersome, not recommended)

grub> insmod xfs    
###Load the specified module into the kernel

grub> linux16 /vmlinuz-3.10.0-693.el7.x86_64l

root=UOID=8fd74986-ae66-4ffd-b7d8-a19f2eca7b6f ro rhgb quiet

LANG=zh_ CN.UTF-8    
###Name and location of the kernel

grub> initrd16 /initramfs-3.10.0-693.e17.x86_64.img     
###Mirror System Files

grub> boot             
###Boot boot

Enter first aid mode to restore GRUB bootstrapper

The MBR is located in the first physical sector of the first hard disk (/dev/sda), totaling 512 bytes. The first 446 bytes are the primary boot record, and the partition table is stored in 447-510 bytes of the MBR sector.

mkdir /bak

mount /dev/sdb1 /bak

dd if=/dev/sda of=/bak/grub.bak bs=446 count=1   

dd if=/dev/zero of=/dev/sda  bs=446 count=1   ###Simulate damage to GRUB bootstrapper in MRB without breaking partition table

Guidance interface to first aid mode, restore GRUB bootstrapper from 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

Guides the interface into first aid mode and rebuilds the GRUB menu profile

rm -rf /boot/grub2/grub.cfg

Enter first aid mode, load disc image, switch to system root environment:
sh-4.2# chroot /mnt/sysimage

Re-install GRUB Bootstrapper installed to first hard drive(/dev/sda)Of MRB Sector, if more than one partition can be omitted:
bash-4.2# grub2-install /dev/sda


Rebuild GRUB Configuration file for menu:
bash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg


Sign out chroot Environment, and restart
bash-4.2# exit
sh-4.2# reboot

Forget root user password

Failure Reason

Forget root user password

Failure Phenomena

Unable to perform administrative operations that require root privileges
You will not be able to log on to the system without other available accounts

Solution ideas

Enter first aid mode, reset password

Enter first aid mode, load system image, switch to system root environment

sh-4.2#chroot /mnt/sysimage

bash-4.2#passwd root   ###Reset root user password

For example, enter single-user mode to modify the root password.

1. Restart boot press esc, press E in the first line to select the editing option in the boot GRUB menu, and press "e" to enter the editing;
_. Edit Modification: Find "ro" at approximately line 16 and change "ro" to "rw init=/sysroot/bin/bash"
3. Press ctrl+X to restart and enter commands into the system

sh-4.2#chroot /sysroot

Character set can be modified before modification

sh-4.2#LANG=en_US.UFT-8

:sh-4.2#passwd root     
###Reset root password

Sign out chroot Environment, and restart:
bash-4.2# exit
sh-4.2# init 6

Service control and switching runlevels

System Service Control

systemctl [control type] [service name]

Control type:

start:Start
Stop:stop
restart:Restart
Reload: reload
Status: View service status

Set Runlevel

Command format:
systemctl [ command] [unit.target]

command parameters:

get-default: Get the current target

set-default: Sets the specified target to the default runlevel

isolate: Switch to the specified runlevel

unit.target: runlevels listed above

Example:

systemctl get-default                         Get the current runlevel

systemctl set-default multi-user.target       Set the default runlevel to mulit-user

systemctl isolate multi-user.target           Switch to runlevel without restarting mulit-user lower

systemctl isolate graphical.target            Switch to GUI without restarting

init 0 systemctl isolate poweroff.target
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 get -default   
###View system default runlevel

ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
 or
systemctl set-default multi-user.target    
###Set Permanent Runlevel

hostnamectl set-hostname newname           
###Permanently modify hostname

hostnamectl status            
 ###View the status of the hostname

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

localectl [status ]       
###View the language used by the current system

Optimize startup process

ntsysv tool:

Provide an interactive, visualized window
Can run at character terminal
Easy centralized management of multiple services

systemctl tool:

No interactive, visual windows are available
Managing individual services is more efficient

View the startup status of system services

systemctl is-enable Service Name

Set the startup status of system services

systemctl enable/disable  Service Name

Example:

systemctl  enable  httpd.service     ### apache service set to startup
 
systemctl  is-enabled  httpd.service  ### View the status of apache services
 
systemctl  disable  httpd.service   ###apache service set to power on and off
 
systemctl  list-units --type=service    ###View all active system services
 
systemctl  list-unit-files | grep enabled     ###View all self-starting servers
 
systemctl  list-unit-files | grep disabled     ###View all self-turning on servers

summary

Topics: Linux Operation & Maintenance server