• 周五. 12月 2nd, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Bootloader and service control in Linux operating system

[db:作者]

1月 6, 2022

1. Linux System boot

2. System initialization process

3. Repair MBR Sector failure

4. Repair GRUB Boot failure

5. Forget root User password

6. System service control

7. Linux System operation level

8. Optimize the startup process

 

 

 

 

 

 

 

 

1. Linux System boot

Self checking on startup

After the server is turned on , Will be based on the motherboard BIOS The settings in are right for CPU、 Memory 、 The graphics card 、 Keyboard and other equipment for preliminary testing , After the test is successful Transfer control of the system according to the preset start-up sequence , Most of the time, it will be handed over to the local hard disk .

MBR guide

When booting the system from the local hard disk , First, according to the first sector of the hard disk MBR( Master boot record ) Set up , Pass system control to the partition that contains the operating system boot file , namely Run on MBR Boot in sector GRUB Bootstrap .

GRUB menu

about Linux For the operating system ,GRUB ( Unified boot loader ) Is the most widely used multi system bootloader program . Control of the system is passed on to GRUB in the future , The Startup menu will be displayed for the user to choose , And according to the selection ( Or use the default value ) load Linux Kernel files , Then transfer control of the system to the kernel .CentOs 7 It’s using GRUB2 Start the bootloader .

load Linux kernel

Linux The kernel is a special pre compiled binary , Between various hardware resources and system programs , Responsible for resource allocation and scheduling . After the kernel takes control of the system , Will be in full control of the whole Linux The operation of the operating system , That is to say Load the kernel and image file system into memory .CentOS 7 In the system , The default kernel file is located in “/boot/vmlinuz-3.10.0-514.el7.x86_ 64”.

init Process initialization

In order to complete the further system boot process ,Linux The kernel first converts “/sbin/ init” Program loaded into memory to run ( A running program is called a process ),init The process is responsible for the initialization of the whole system , Finally, wait for the user to log in .

Tradition sysvinit Depend on Serial execution Shell The script starts the service , It leads to inefficiency , The system starts slowly .
systemd More service processes Parallel start , And has the ability to provide on-demand startup Services , Make it possible to start fewer processes , thus Speed up system startup .

2. System initialization process

init process

  • from linux The kernel loads and runs /sbin/init Program
  • init process It’s in the system The first process
  • init process Of PID( Process tag ) The number will always be 1

systemd

  • Systemd yes linux One of the operating systems init Software
  • CentOS 7 Use the brand new Systemd Starting mode , Replace the traditional SysVinit
  • CentOS 7 The first one running in init The process is /lib/systemd/systemd

systemd Unit type

Unit type Extension explain
Service .service Describe a system service
Socket .socket Describes a socket for interprocess communication IP:povt Port communication
Device .device Describe a device file identified by the kernel
Mount .mount Describe the mount point of a file system
Automount .automount Describes the auto 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
Snapshot .snapshot Used to save a systemd The state of
Scope .scope Use systemd To create external processes programmatically
Slice .slice Describe cgroup A group of management system processes that are organized hierarchically
Target .target Describe a group systemd Unit of

  Run level corresponding to systemd The goal is

Operation level systemd Of target explain
0 target To turn it off state , Use this level to shut down the host
1 rescue.target Single user mode , Login without password , For system maintenance
2 multi-user.target User defined domain specific run level , Default is equivalent to 3
3 multi-user.target Character interface The complete multi-user mode of , Most server hosts run at this level
4 multi-user.target User defined domain specific run level , Default is equivalent to 3
5 graphical.target The graphical interface User mode , Provides a graphical desktop operating environment
6 reboot.target Restart , Use this level to restart the host

3. Repair MBR Sector failure

The cause of the problem

  • Viruses 、 Damage caused by Trojans, etc
  • Incorrect partition operation 、 Disk read / write misoperation

The fault phenomenon

  • Bootloader not found , Start interrupt
  • Unable to load operating system , Black screen after power on

Solutions

  • We should do well in advance Backup file
  • Use the installation CD to guide you into emergency mode
  • from Recovery from backup files

step :

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

Simulated destruction MBR Boot sector

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

Guide the interface into emergency mode ( Recover from backup files MBR Sector data

  • Load the disc image first , restart operating system
  • When the installation wizard interface appears , choice “Troubleshooting” Options
  • Re selection “Rescue a CentOS Linux system” Options , Enter first aid mode
  • choice “1” choice Continue And press Enter Key continuation
  • Press again Enter Key will enter into the belt “sh-4.2#” The prompt’s Bash Shell Environmental Science

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 # recovery Backup data
sh-4.2# exit # perform exit command sign out temporary Shell Environmental Science , The system will restart automatically

4. Repair GRUB Boot failure

The cause of the problem

  • MBR Medium GRUB( Master bootstrap 1-446 byte ) The bootloader is broken
  • grub.conf file missing Wrong boot configuration

The fault phenomenon

  • System guidance stalls , Show “grub>” Prompt

Solutions

  • Try Manual Enter the boot command
  • Enter first aid mode , Rewrite or from Recovery in backup grub.conf
  • towards MBR In sector The reconstruction grub Program

/boot/grub/ The package in the directory is the background image and style used to start the menu
/boot/grub2/ grub. cfg                            #GRUB The configuration file

1. Manually enter the boot command

/boot/grub2/grub.cfg                                                               #GRUB The configuration file
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    # Enter information about the kernel
grub> initrd16 /initramfs-3.10.0-693.el7.x86_64.img              # Mirror system files
grub> boot                                                                              # guide boot

2. Enter first aid mode , recovery GRUB Bootstrap

Backup GRUB

MBR On the first hard disk ( /dev/sda) At the first physical sector of , in total 512 byte , front 446 Bytes are Master boot record , Partition table Save in MBR The second in the sector 447-510 In bytes .
mkdir /backup
mount /dev/sdb1 /backup
dd if=/dev/sda of=/backup/grub.bak bs=446 count=1

Simulated pair MBR Medium GRUB The destruction of bootloader , But not Do not destroy the partition table

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

Guide the interface into emergency mode , Recover from backup files GRUB Bootstrap , Restart the operating system

sh-4.2# mkdir /backup
sh-4.2# mount /dev/sdb1 /backup
sh-4.2# dd if=/backup/grub.bak of=/dev/sda
sh-4.2# exit

3. Guide the interface into emergency mode , The reconstruction GRUB Menu profile

Delete GRUB Menu profile

rm -rf /boot/grub2/grub.cfg

Enter first aid mode , Load the disc image , Switch the system root environment

sh-4.2# chroot /mnt/sysimage

Re GRUB The bootloader is installed on the first hard disk (dev/sda) Of MBR A sector ( If there are more than one partition, it can be omitted )

bash-4.2# grub2-install /dev/sda

Rebuild GRUB Menu configuration file

bash-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg

sign out chroot Environmental Science , And restart

bash-4.2# exit
sh-4.2# reboot

5. Forget root User password

The fault phenomenon

  • There is no way to do it root Authority management operation
  • If there is no other account , Will not be able to log on to the system

Solutions

  • Enter first aid mode , Reset password

Enter first aid mode , Load the disc image , Switch the system root environment

sh-4.2# chroot /mnt/sysimage

reset root User password

bash-4.2# passwd root

6. System service control

systemctl Type of control The service name

Type of control

start: start-up
stop: stop it
restart: Restart
reload: Reload ( Online overload , Continuous service
status: View service status

7. Linux System operation level

Check the runlevel

runlevel command
systemctl Tools
systemctl get-default             # Check the default run level of the system
runlevel # View only Switch operation level And Current operating level
systemctl   # Can view Default run level

Temporarily switch the running level ( Comparison table 2)

init command                                 #init The command parameters of are corresponding to the run level Numbers
systemctl Tools                      #systemctl The command parameters for are Concrete target

Set persistent run level

Create a soft connection method

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

Directly modifying

systemctl set-default multi-user.target

8. Optimize the startup process

ntsysv Tools

  • Provide an interactive 、 Visualization window
  • Can be in Character terminal function
  • Facilitate centralized management of multiple services
  • It is used to control whether the service starts automatically

systemctl / chkconfig Tools

  • No interactive 、 Visualization window
  • Managing individual services is more efficient

Set the start state of system services

systemctl enabled The service name                      ( Turn on Boot up )
systemctl disable The service name                       ( close Boot up )

systemctl is-enabled The service name                     # View the system services Start state

chkconfig –add The service name                             # Join the service
chkconfig –list The service name                               # View the current service run level
chkconfig –level List of levels The service name  on/off     # open / close Service levels

 

expand

hostnamectl set-hostname New name )       # Permanently change the host name
hostnamectl status                                      # Check the status of the host name
localectl set-locale LANG=zh_CN.utf8       # Set the system language to Chinese (utf8- The universal character )
localectl [status]                                          # Check the current system language
systemd-analyze                                        # Check system startup time

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注