Site icon CentLinux

How to Repair GRUB Bootloader in CentOS 8

Share on Social Media

Learn how to repair GRUB bootloader in CentOS 8 with our step-by-step guide. Troubleshoot boot issues and restore your system’s functionality easily. #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.

GRUB (Grand Unified Bootloader) is a crucial component in the boot process of many Unix-like operating systems, including Linux distributions. It is responsible for loading the kernel of the operating system and transferring control to it, thus starting the OS.

Key features and functionalities of GRUB include:

  1. Multi-boot Support: GRUB can manage and boot multiple operating systems installed on a single machine. This is particularly useful for users who run dual-boot or multi-boot setups.
  2. Configurable and Customizable: GRUB configuration can be easily customized through a configuration file, usually located at /boot/grub/grub.cfg. Users can modify boot entries, set default operating systems, and change timeout values.
  3. Command Line Interface: GRUB provides a command line interface (CLI) that allows users to manually enter boot commands, debug issues, and test configurations without modifying the boot configuration file.
  4. Graphical Menu Interface: GRUB offers a graphical menu interface that lists all available operating systems and kernel versions, making it easy for users to select their desired boot option.
  5. Filesystem Support: GRUB can read from a wide range of filesystems including ext2/3/4, Btrfs, XFS, JFS, ReiserFS, and even FAT and NTFS. This allows it to load kernels and other files from different types of partitions.
  6. Network Booting: GRUB supports booting from network locations using protocols like TFTP. This is useful for diskless systems and network installations.
  7. Security Features: GRUB can be configured with password protection to restrict unauthorized access to its configuration and boot entries. This adds a layer of security to the boot process.
  8. Advanced Scripting: GRUB scripting language allows for the creation of complex boot scripts, which can automate various boot tasks and provide advanced boot capabilities.

Understanding and managing GRUB is essential for system administrators and power users, as it directly impacts the bootability and flexibility of the operating system. Problems with GRUB, such as corrupted configurations or missing bootloader files, can prevent a system from starting, making GRUB troubleshooting skills valuable for maintaining system uptime and stability.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

Problem Statement

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.

Linux Server Specification

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

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 file.

[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 Linux 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 to repair grub.cfg.

GRUB Menu 1 – Boot from CentOS 8 CD/ISO

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

Troubleshooting Menu 1 – Boot from CentOS 8 CD/ISO

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

Rescue Shell to Repair grub.cfg

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 from your Hard Disk.

If you have repair grub.cfg file by following the above steps correctly, then it will now display the GRUB boot menu.

GRUB Menu after Repair grub.cfg

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 to repair vmlinuz.

GRUB Menu 2 – Boot from CentOS 8 CD/ISO

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

Troubleshooting Menu 2 – Boot from CentOS 8 CD/ISO

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

Rescue Shell to Repair vmlinuz

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 to repair initrd.

GRUB Menu 3 – Boot from CentOS 8 CD/ISO

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

Troubleshooting Menu 3 – Boot from CentOS 8 CD/ISO

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

Rescue Shell to Repair initrd

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

If you have successfully repair initrd, 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 repaired.

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 to repair boot partition.

GRUB Menu 4 – Boot from CentOS 8 CD/ISO

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

Troubleshooting Menu 4 – Boot from CentOS 8 CD/ISO

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

Rescue Shell to Repair Boot Partition

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 after Repair Boot Partition

We have successfully repair boot partition on CentOS 8.

If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.

Final Thoughts

Repairing the GRUB bootloader in CentOS 8 can be a complex but essential task to restore your system’s functionality. By following the right steps and using the appropriate tools, you can troubleshoot and fix common boot issues. Whether you’re a seasoned system administrator or a beginner, having a solid understanding of GRUB Bootloader and its repair process is crucial for maintaining your system’s health.

If you need professional assistance or a more detailed guide to repair GRUB bootloader in CentOS 8, I offer expert services on Fiverr. Visit my Fiverr profile here to get personalized help and ensure your system is up and running smoothly.

Exit mobile version