Linux boot repair

System boot is the beginning of the operation of an operating system. If there is a problem in boot, the system will not work properly. Is there any way to repair it? The answer is yes.

CentOS 7 boot process

1. Boot-up self-check BIOS
2.MBR boot
3.GRUB menu
4. Loading kernel and memory file system initramfs
5. Loading hardware driver and initialization process

Repair of MBR Sector Fault

MBR boot record is located in the first sector of physical hard disk (512B), also known as the main boot sector, which contains not only some data of system boot program, but also partition table records of the whole hard disk.

Backup MBR sector data

1. Prepare another hard disk for MBR sector backup data

[root@localhost ~]# fdisk /dev/sdb
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=1310656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5242624, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

2. Mount / dev/sdb1 partition

[root@localhost ~]# mkdir /bak
[root@localhost ~]# mount /dev/sdb1 /bak/
[root@localhost ~]# df -hT
//File System Type Capacity Used Available% Mount Points
/dev/sda2      xfs        20G  3.4G   17G   17% /
devtmpfs       devtmpfs  1.9G     0  1.9G    0% /dev
tmpfs          tmpfs     1.9G     0  1.9G    0% /dev/shm
tmpfs          tmpfs     1.9G  9.1M  1.9G    1% /run
tmpfs          tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda5      xfs        10G   37M   10G    1% /home
/dev/sda1      xfs       2.0G  174M  1.9G    9% /boot
tmpfs          tmpfs     378M  8.0K  378M    1% /run/user/42
tmpfs          tmpfs     378M     0  378M    0% /run/user/0
/dev/sdb1      xfs        20G   33M   20G    1% /bak

3. Backup MBR sector data

[root@localhost ~]# ls /bak/
[root@localhost ~]# dd if=/dev/sda of=/bak/sda.mbr.bak bs=512 count=1
 Record 1 + 0 reads
 Write down 1 + 0
 512 bytes (512 B) copied, 0.000289995 seconds, 1.8 MB/seconds
[root@localhost ~]# ls /bak/
sda.mbr.bak

Simulated MBR sector failure

1. Man-made coverage of MBR sector records

[root@localhost ~]# dd if=/dev/zero of=/dev/sda bs=512 count=1
 Record 1 + 0 reads
 Write down 1 + 0
 512 bytes (512 B) copied, 0.000157851 seconds, 3.2 MB/seconds

2. Restart

[root@localhost ~]# init 6

3. Unable to start

Restoring MBR sector data

1. Boot Operating System with CD or U Disk Boot Disk

2. Enter Troubleshooting Problem Solution Mode

3. Enter Rescue a CentOS system first aid mode

4. Enter the shell environment for recovery

mkdir /bak
mount /dev/sdb1 /bak
ls /bak
dd if=/bak/sda.mbr.bak of=/dev/sda
reboot

5. Successful recovery

GRUB Boot Fault Repair

GRUB is the default bootstrap for most Linux systems and can be selected to enter different operating systems by launching menus. If the configuration file is lost or the key configuration is wrong, the startup process cannot be completed.

Simulated GRUB Fault

1. Delete configuration files

[root@localhost ~]# cp -p /boot/grub2/grub.cfg /boot/grub2/grub.cfg.bak
[root@localhost ~]# rm -f /boot/grub2/grub.cfg
[root@localhost ~]# init 6

2. Unable to start

Repair GRUB fault

The front part, like the above part, also enters the system first aid mode for repair.

1. Load the system image and switch to the Linux system root environment to be repaired

chroot /mnt/sysimage/

2. Reinstall grub bootstrap in the MBR sector of sda

grub2-install /dev/sda

3. Reconstruct grub menu configuration file

grub2-mkconfig -o /boot/grub2/grub.cfg

4. Exit and restart, waiting for the system to start

exit
reboot

Forget ROOT password

When you forget the root password, you will not be able to perform management, maintenance and other tasks on the Linux system. When the password cannot be changed by other users, the root password can also be changed by using the first aid mode.

Simple, the operation is as follows:

chroot /mnt/sysimage/
echo "000000" | passwd --stdin root or passwd root
exit
reboot

Keywords: Linux CentOS ascii shell

Added by dgs on Tue, 27 Aug 2019 11:45:09 +0300