Centos6/Centos7 system startup process and solutions to many problems during startup

Section 12: Centos6/Centos7 system startup process and solutions to related problems in the three stages of GRUB stage

12.0 What is the system startup process?

Centos6 system

  1. Power-on self-checking BIOS loading stage: press the power button when power on, the computer is powered on, and the BIOS starts to load the basic input output system, which involves information such as CPU, memory, hard disk hardware information, device startup sequence, clock and other information.

  2. MBR reading stage: The computer searches for the first sector of the hard disk that is the MBR sector (designated by BIOS), loads the first 446 bytes of the sector, the Bootloader, into the memory, and prepares for system boot.

  3. GRUB boot stage: Three stages of files include: stage1, stage1.5, stage2.

    • Stage1 is the Bootloader ( the first 446 bytes of the MBR sector ) to locate and load stage1.5 through the contents of the file (covering the specified load address);
    • As the transition between stage1 and stage2, stage1.5 aims to **/boot partition file system**, that is, load the contents of the 27 sectors after the MBR sector to the memory; from then on, Stage1 has the ability to identify the file system through Stage1.5 Ability to start loading Stage2;
    • stage2 finds and loads the kernel file by reading the grub.conf file under /boot/grub (which has been loaded in Stage1.5) , and prepares to hand over the control to Linux; the grub option information seen at startup and the background of grub, etc. Information is provided in stage2.
  4. Kernel boot phase: decompress the kernel file, decompress it into memory, and start the kernel; then the kernel releases the root file system through the initramfs temporary file system, and then switches to the /sbin/init program;

  5. Init program operation stage:

    • The init process is the first process. Since it is a meta-process, it will first read the /etc/inittab file as the operating level of the system;
    • Then the init program loads the user-level file /etc/rc.d/rc.sysinit, which covers: set the kernel parameter /etc/sysctl.conf, enable swap, mount the root partition in read-write mode, check and mount Other file systems in /etc/fstab, etc.;
    • According to the set system operation sector, the system runs script programs in the rc0.d to rc6.0 directory to complete the services required for system initialization. If the files in the directory start with K, it means that the corresponding service is stopped, and it starts with S. Start the corresponding service, the number after SK indicates the startup sequence number, the smaller the number, the earlier the execution;
    • After the system starts the corresponding service, execute the /etc/rc.d/rc.local file, and the system executes the startup task line by line according to the file content;
    • The system executes the /bin/lgin program, and the user enters the user name and password to log in to the system. After the login is successful, the system loads the configuration file and enters the shell terminal

    Linux operating system is started

12.1 Stage1 file damage repair

Centos6 system

  • First back up the sector files, and then simulate damage, delete the first 446 bytes of the MBR partition (if it is a virtual machine system simulation, please take a snapshot in advance), and finally restart the system in init 6, the system hard disk no longer has the boot function, go directly to the next Boot device

Insert picture description here
Insert picture description here

  • After restarting the system, enter the following mode
    Insert picture description here

  • Select Rescue installed system to enter rescue mode

    • Choose language US

    • Choose whether to configure the network card, no

    • Switch to the root directory prompt, choose continue, then ok, and finally choose to run the shell, as follows
      Insert picture description here
      Insert picture description here

Insert picture description here

  • input the command

    1. #chroot /mnt/sysimage/ #Change the disk root directory

    2. #grub-install /dev/sda #Generate grub, this command can restore grub content at all stages

Insert picture description here

  • After executing grub, the sync command updates the disk. Then exit the shell and restart the system

Insert picture description here

Centos7 system

  • The method is the same as the centos6 repair process
  • Need to pay attention to the modification
系统启动如何进入救援模式?
界面选择:Troubleshooting,在选择:Rescue a CentOS system,然后执行chroot /mnt/sysimage切跟
安装命令:grub2-inatall /dev/sda   #带个2

12.2 State2 sector (loaded /boot partition file system) damaged repair in GRUB stage1.5 stage

Centos6 system

  • Simulated destruction

    dd if=/dev/zero of=/dev/sda bs=1 count=10000 skip=512 seek=512   
    #MBR后27个扇区的数据:原则上是512*27=13824字节,我们这里是从/dev/sda 的512字节后开始破坏,将10000个字节存储空间破坏清零
    
  • Refer to 12.5, the method is the same

  • GRUB Stage2 stage file damage also uses the same method

12.3 Delete grub.conf and repair it by restarting the system

Centos7 system

The Centos7 system uses the Grub2 version to boot the system. The boot menu is automatically generated from /boot instead of manual configuration in menu.lst. For specific content, refer to the configuration file: /boot/grub2/grub.cfg This file is automatically generated every time grub2-mkconfig is executed, so modifying this file will become invalid after the kernel is upgraded.

  1. Record system related parameters
[root@ ~ 06:12:14]#df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       10190100 5330036   4319392  56% /     #重点记录根目录路径
...
[root@ ~ 06:08:30]#cat /etc/fstab                     #记录根分区和boot分区的UUID
#
# /etc/fstab
# Created by anaconda on Mon Jun 22 18:51:04 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=ea1d8d1c-3e80-4a6f-98cc-c83d8897ff86 /                       ext4    defaults        1 1
UUID=35521718-2203-497c-b690-d44622dc6704 /boot                   ext4    defaults        1 2
...

  1. Move /boot/grub2/grub.cfg out of this directory, then restart the system, the host enters the following interface
mv /boot/grub2/grub.cfg /root
init 6 #重启

Insert picture description here

  1. View current disk information

Insert picture description here

  1. Variable settings (completing the path through the tab key)
grub> set root=(hd0,msdos1)  #设置boot分区
grub> linux /vmlinuz-3.10.0-862.e17.x86_64 ro root=/dev/sda2#root=系统根分区路径或UUID=?
grub> initrd /initramfs-3.10.0-862.e17.x86_64.img #内核初始化环境变量设置
grub> boot #启动系统
  1. Log in to the system successfully and regenerate the grub.cfg file
grub2-mkconfig -o /boot/grub2/grub.cfg

Insert picture description here

If you don’t know the root partition path in grub mode, then the above method cannot proceed. At this time, you need to use the CD-ROM boot method to enter:

  1. When booting up, keep pressing the Esc button in the upper left corner of the keyboard. After the menu selection window appears, select 3 CD-ROM Driver, and then select rescue installed system to enter the rescue/rescue mode

  2. Can choose not to allow network configuration

  3. Prompt to follow: you choose ok
    chroot /mnt/sysimage #cut root: temporarily change the system root directory

  4. grub2-mkconfig -o /boot/grub2/grub.cfg #The current directory is already the system root directory

  5. exit #Exit

  6. reboot reboot

Centos6 system

Configuration file *: /boot/grub/grub.conf, to ensure that grub and the kernel are in the same directory; in addition, it creates a link file /etc/grub.conf to point to the configuration file to ensure that users use the configuration file consistency*

  1. Record system related parameters (emphasis: the root partition mount point, some are logical volumes, my system is /dev/sda3)

Insert picture description here

  1. Move grub.conf out of the directory

Insert picture description here

  1. Restart the system init 6, and pop up the grub startup command line

Insert picture description here

  1. After executing the parameter configuration command, execute boot to restart

Insert picture description here

  1. Re-login the system and restore grub.conf to the /boot/grub directory (if it is moved, use the mv command to move the file back)

Insert picture description here

12.4 Linux Login stage: Forget the Linux system password, enter the single user mode to restore the user password

The operating level of the Linux system consists of six levels, and the single-user mode is level 1. The single-user mode login system is mainly used to modify the system root password. Not everyone can enter the single-user mode. We can only log in to the single-user mode system through the local computer, but cannot log in remotely (many ssh services will not provide 1 run level).

Run level Description
0 Shut down, the system default run level cannot be set to 0, otherwise it will not start normally and it will automatically shut down as soon as it is turned on.
1 Single user mode, root authority, used for system maintenance, remote login is prohibited.
2 Multi-user mode, no NFS network support.
3 Complete multi-user text mode, with NFS, enter the console command line mode after logging in. Usually log in to the system is this mode
4 The system is not used, it is generally not used, it can be used to do some things in some special cases. For example, when the battery of a notebook computer is exhausted, you can switch to this mode to make some settings.
5 Graphical mode, enter the graphical GUI mode or GNOME, KDE graphical interface after login, such as X Window System.
6 In restart mode, the default run level cannot be set to 6, otherwise it will not start normally, it will always restart and restart.

Centos7 system

  1. init 6 Restart the system, press the letter e in the countdown interface to enter the edit mode

Insert picture description here

  1. Find the line where the ro string is located, change ro to'rw init=/sysroot/bin/sh', press ctrl+x to enter user mode after modification

Insert picture description here

  1. After entering single user mode, enter the following command to change the password

Insert picture description here

  1. Remaining steps
touch /.autorelabel #修改完密码后要创建的文件以让selinux生效(无创建则无法重启)
exit #退出chroot
reboot #重启系统,生效

Centos6 system

  1. Start the system, when booting, press any key (e) to enter the grub menu interface, as follows
    Insert picture description here

  2. According to the help prompt at the bottom of the grub menu interface, press the letter a to modify the kernel parameters before system boot, add +space+single at the end of the kernel line, and press Enter

Insert picture description here

  1. Start the system, that is, enter the single user mode, and start to change the password
    Insert picture description here

12.5 Encrypt grub

Centos7 system

Why encrypt grub? Because if grub is not encrypted, other users can enter single-user mode at will to modify the system account password, which poses a security risk.

  • View the current centos version

Insert picture description here

  • Set password (save it)
[root@ ~ 11:13:00]#grub2-mkpasswd-pbkdf2
Enter password: 
Reenter password: 
PBKDF2 hash of your password is grub.pbkdf2.sha512.10000.216202CEB237E45256DD02E80D7FAC425A4CAEEEED8850541CAB02D19A138DF224AEB2603A7CE9A7C0BE6C3FA94015AA95091B8DFE67F308AA00972FA43C0EA6.50165007B8DC1514BF95859DD8852B0AF922E0BF60773382D721A33D1A1F6C308B53C81821A850074162A3DAC95A4D087298A2EF1C61B7232E7CC431DF2CBE58
  • Modify the configuration file
[root@ ~ 11:13:27]#vim /etc/grub.d/00_header
cat <<EOF
set superusers='root'  #单用户使用的用户名
password_pbkdf2 root grub.pbkdf2.sha512.10000.216202CEB237E45256DD02E80D7F    AC425A4CAEEEED8850541CAB02D19A138DF224AEB2603A7CE9A7C0BE6C3FA94015AA95091B    8DFE67F308AA00972FA43C0EA6.50165007B8DC1514BF95859DD8852B0AF922E0BF6077338    2D721A33D1A1F6C308B53C81821A850074162A3DAC95A4D087298A2EF1C61B7232E7CC431D
F2CBE58   			   #尾部是加密密码,一定要加上,用于输入密码判定
EOF
  • Recompile the grub.cfg file
[root@ ~ 11:22:38]#grub2-mkconfig -o /boot/grub2/grub.cfg 
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-09726f1d0db4408cbc2fe751f0b185ea
Found initrd image: /boot/initramfs-0-rescue-09726f1d0db4408cbc2fe751f0b185ea.img
done
  • init 6 restart the system, press the letter e in the countdown interface, the grub login interface will pop up (grub encryption is effective), enter the user name and password, and press the enter key;

Insert picture description here
Insert picture description here

Centos6 system

  1. Generate encrypted password based on MD5 algorithm
[root@lin ~]# grub-md5-crypt
Password: 
Retype password: 
$1$mgqu/1$5vcAFwFSL4Xtb574S2.U30

2. Add the password string in the /boot/grub/grub.conf file

[root@lin ~]# vi /boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda3
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 $1$mgqu/1$5vcAFwFSL4Xtb574S2.U30  #添加在这里是对grub菜单整体加密,锁定编辑模式
title CentOS 6 (2.6.32-696.el6.x86_64)
#添加在这里是对某个菜单进行加密,不能锁定编辑模式
        root (hd0,0)
        kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=56*****3a-6**4-**4b-adb7-d40********e01 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-696.el6.x86_64.img
~
  1. Press init 6 to start the system, press any key to enter the grub menu interface, the following prompt requires you to press the p key to enter the password to enter the grub editing mode
    Insert picture description here
    Insert picture description here

12.6 Two kernel files in Boot partition are damaged and repaired

Cntos6 system

1.参考12.1,进入救援模式;开启shell步骤;
bash-4.1# df
bash-4.1# mkdir /mnt/cdrom    
bash-4.1# mount /dev/sr0 /mnt/cdrom #挂载
bash-4.1# rpm -ivh /mnt/cdrom/Packages/kernel-******.x86_64.rpm --root=/mnt/sysimage --force                 #安装内核文件
bash-4.1# ls /mnt/sysimage/boot   #查看是否有内核文件
bash-4.1# reboot   #重启

Cnetos7 system

同上

12.7 The kernel file in the Boot partition is damaged, and the /etc/fstab file is also damaged

Centos6 system

  • Idea: First enter the rescue mode, find the root partition through the temporary file system /mnt/sysimage
  • First view all partitions through fdisk -l
  • Create a directory file and mount partitions to the directory file one by one until the root partition file is not empty under the file
  • Modify the /etc/fstab file under the mount directory, add related mount entries, and complete other entries after the system is repaired
  • Restart into rescue mode, repair the boot kernel file, refer to 12.6

12.8 Repair of damaged init.d file in the first process of Centos6 system

Centos6 system

1.进入救援模式
2.安装 rpm -ivh --force --root=/mnt/sysimage /mnt/cdrom/Packages/upstart-****.x86_64.rpm
3.查看安装情况:ls /mnt/sysimage/sbin/init
4.重启

12.9 Then all the files under /boot/grub are damaged?

1.进入救援模式安装内核文件:参考12.8
2.安装grub引导程序:参考12.1 就是生成grub目录
3.进入grub2目录安装grub.cfg ,参考12.3 第五步
4.重启系统

Guess you like

Origin blog.csdn.net/weixin_31789689/article/details/108219309