How to Repair GRUB Bootloader in CentOS 8

Share on Social Media

In this article, you will learn how to repair GRUB bootloader in CentOS 8 or other Redhat based Linux distros. #centlinux #linux #grub

What is GRUB? :

GRUB (GRand Unified Bootloader) 2 is part of GNU Project and the default bootloader for famous Linux distros like RHEL, CentOS, Ubuntu, etc. GRUB is the first software program that runs when a computer is started. GRUB is responsible for loading the Kernel software. The Kernel then initializes the rest of the operating system.

There are situations in which GRUB bootloader is corrupted. In such scenarios, System Administrators have to repair GRUB bootloader.

In this article, we are discussing the following four scenarios and how to repair GRUB bootloader in CentOS 8.

  1. The GRUB menu i.e. grub.cfg is corrupted or missing.
  2. The CentOS 8 Kernel i.e. vmlinuz-4.x is corrupted or missing.
  3. The CentOS 8 initrd i.e. initramfs-4.x is corrupted or missing.
  4. The /boot directory is corrupted or missing.

Environment Specification:

We have provisioned virtual machine with minimally installed CentOS 8 operating system with following specification.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 1 GB
  • Storage – 20 GB
  • Operating System – CentOS Linux 8.0
  • Hostname – grub-recovery-01.recipes.com
  • IP Address – 192.168.116.206 /24

How to Repair grub.cfg in CentOS 8:

Connect with grub-recovery-01.recipes.com using ssh as root user.

To produce this recovery scenario, we are deleting the grub.cfg.

[root@grub-recovery-01 ~]# rm -f /boot/grub2/grub.cfg

Your system will continue to perform smoothly, unless you restart it and it will then unable to find the grub.cfg file and it will failed to boot CentOS 8 operating system.

Reboot the system now.

[root@grub-recovery-01 ~]# systemctl reboot

The machine will now boot into GRUB prompt because of the missing grub.cfg file.

      Minimal BASH-like line editing is supported. For the first word,
      TAB lists possible command completions. Anywhere else TAB lists
      possible device or file completions.
   
   
grub>

Restart the system and boot using CentOS 8 ISO/DVD.

GRUB Menu from Installation Disk 1

At the CentOS 8 installation menu, select Troubleshooting and press <ENTER>.

GRUB Menu Troubleshooting 1

Select Rescue a CentOS Linux system and press <ENTER>.

GRUB Rescue Prompt 1

It will ask you for mounting your filesystem, choose the option and ‘1’ and press <ENTER>.

Press <ENTER> again to acquire a shell.

Execute chroot command to access your filesystem.

sh-4.4# chroot /mnt/sysimage

By using grub2-mkconfig command, you can regenerate grub.cfg file.

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

If you are working on an UEFI based machine then you have to generate grub.cfg file in /boot/efi/EFI/centos/ directory.

If you are using SELinux, then create following file to trigger SELinux file relabeling process on next startup.

bash-4.4# touch /.autorelabel

Finally exit from chrooted environment and reboot your system.

bash-4.4# exit
sh-4.4# reboot

Boot using your Hard Disk and now it will display the GRUB boot menu this time.

GRUB Menu 1

We have successfully recovered grub.cfg file on CentOS 8.

How to Repair vmlinuz in CentOS 8:

Connect with grub-recovery-01.recipes.com using ssh as root user.

To produce this recovery scenario, we are removing the vmlinuz-4.18.0-80.el8.x86_64 file.

[root@grub-recovery-01 ~]# rm -f /boot/vmlinuz-4.18.0-80.el8.x86_64

Reboot the system.

[root@grub-recovery-01 ~]# systemctl reboot

On boot, GRUB will display the following warning and unable to load Kernel file.

error: ../../grub-core/fs/fshelp.c:258:file '/vmlinuz-4.18.0-80.el8.x86_64' not 
found.
error: ../../grub-core/loader/i386/pc/linux.c:413:you need to load the kernel
first.

Press any key to continue...

Restart your system and boot using CentOS 8 ISO/DVD.

GRUB Menu from Installation Disk 2

At the CentOS 8 installation menu, select Troubleshooting and press <ENTER>.

GRUB Menu Troubleshooting 2

Select Rescue a CentOS Linux system and press <ENTER>.

GRUB Rescue Prompt 2

It will ask you for mounting your filesystem, choose the option and ‘1’ and press <ENTER>.

Press <ENTER> again to acquire a shell.

Go to Packages directory in ISO/DVD.

sh-4.4# cd /mnt/install/repo/BaseOS/Packages

Reinstall CentOS 8 Kernel using following command.

sh-4.4# rpm -ivh --root=/mnt/sysimage --replacepkgs kernel-core-4.18.0-80.el8.x86_64.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:kernel-core-4.18.0-80.el8        ################################# [100%]

Verify that vmlinuz-4.x file is created by the installer.

sh-4.4# ls /mnt/sysimage/boot/vmlinuz*
/mnt/sysimage/boot/vmlinuz-0-rescue-9239012f800e401d86d70eef6bc4f8b6
/mnt/sysimage/boot/vmlinuz-4.18.0-80.el8.x86_64

If you are using SELinux then create following file to trigger SELinux file relabeling process on next startup.

sh-4.4# touch /mnt/sysimage/.autorelabel

Reboot the system now.

sh-4.4# reboot

The GRUB will be able to load the Kernel now and you will reach at the login prompt after successful startup.

CentOS Linux 8 (Core)
Kernel 4.18.0-80.el8.x86_64 on an x86_64

grub-recovery-01 login:

We have successfully recovered CentOS 8 Kernel.

How to Repair initrd in CentOS 8:

Connect with grub-recovery-01.recipes.com using ssh as root user.

To initiate this recovery scenario, we are removing the initramfs-4.18.0-80.el8.x86_64.img file.

[root@grub-recovery-01 ~]# rm -f /boot/initramfs-4.18.0-80.el8.x86_64.img

Reboot your system.

[root@grub-recovery-01 ~]# systemctl reboot

After reboot the GRUB will display following warning and unable to find initramfs-4.x file.

error: ../../grub-core/fs/fshelp.c:258:file
'/initramfs-4.18.0-80.el8.x86_64.img' not
found.

Press any key to continue...

Restart the system and boot using CentOS 8 ISO/DVD.

GRUB Menu from Installation Disk 3

At the CentOS 8 installation menu, select Troubleshooting and press <ENTER>.

GRUB Menu Troubleshooting 3

Select Rescue a CentOS Linux system and press <ENTER>.

GRUB Rescue Prompt 3

It will ask you for mounting your filesystem, choose the option and ‘1’ and press <ENTER>.

Press <ENTER> again to acquire a shell.

Execute chroot command to access our filesystem.

sh-4.4# chroot /mnt/sysimage

Use dracut command to recreate initramfs-4.x file.

bash-4.4# dracut

Verify that initramfs-4.x file is created by dracut command.

bash-4.4# ls /boot/initramfs-*
/boot/initramfs-0-rescue-9239012f800e401d86d70eef6bc4f8b6.img
/boot/initramfs-4.18.0-80.el8.x86_64.img

Exit chroot and reboot the system now.

bash-4.4# exit
sh-4.4# reboot

The GRUB will be able to load the initrd image now and you will reach at the login prompt after successful startup.

CentOS Linux 8 (Core)
Kernel 4.18.0-80.el8.x86_64 on an x86_64

grub-recovery-01 login:

CentOS 8 initrd image has been recovered.

How to Repair boot Partition in CentOS 8:

Connect with grub-recovery-01.recipes.com using ssh as root user.

To initiate this recovery scenario, we are removing /boot directory.

[root@grub-recovery-01 ~]# rm -rf /boot/*

Reboot your system.

[root@grub-recovery-01 ~]# systemctl reboot

After reboot the GRUB will show the “grub rescue” prompt.

.
error: ../../grub-core/fs/fshelp.c:258:file '/grub2/i386-pc/normal.mod' not foun
d.
Entering rescue mode...
grub rescue>

Restart the system and boot using CentOS 8 ISO/DVD.

GRUB Menu from Installation Disk 4

At the CentOS 8 installation menu, select Troubleshooting and press <ENTER>.

GRUB Menu Troubleshooting 4

Select Rescue a CentOS Linux system and press <ENTER>.

GRUB Rescue Prompt 4

It will ask you for mounting your filesystem, choose the option and ‘1’ and press <ENTER>.

Press <ENTER> again to acquire a shell.

The CentOS 8 DVD/ISO is mounted in mnt directory.

Go to BaseOS packages directory.

sh-4.4# cd /mnt/install/repo/BaseOS/Packages

Reinstall kernel-core package using following command.

sh-4.4# rpm -ivh --root=/mnt/sysimage --replacepkgs kernel-core-4.18.0-80.el8.x86_64.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:kernel-core-4.18.0-80.el8        ################################# [100%]
grub2-editenv: error: cannot open '/boot/grub2/grubenv.new': No such file or directory.

Ignore above error, we will create this file manually.

Run chroot to mount /mnt/sysimage as root directory.

sh-4.4# chroot /mnt/sysimage

Reinstall GRUB 2 using following command.

bash-4.4# grub2-install /dev/sda
Installing for i386-pc platform.
Installation finished. No error reported.

Recreate grub.cfg (GRUB menu) using grub2-mkconfig command.

bash-4.4# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
done

If you are using SELinux, then create following file to trigger SELinux file relabeling process on next startup.

bash-4.4# touch /.autorelabel

Finally exit from chrooted environment and reboot your system.

bash-4.4# exit
sh-4.4# reboot

Boot using your Hard Disk and now it will display the GRUB boot menu this time.

GRUB Menu 2

We have successfully repaired boot partition on CentOS 8.

Conclusion – How to Repair GRUB Bootloader:

We have discussed all four scenarios and demonstrate how to repair GRUB bootloader in CentOS 8.

Scroll to Top