Saturday, March 30, 2019

Recovering or restoring grub in CentOS or RHEL 7

What if GRUB is corrupted on your RHEL or CentOS 7 ?

GRUB corruption is a common problem in RHEL or CentOS, which could occur due these reasons:
  1. If any file is missing or deleted from "/boot/grub2/" folder e.g. "grub.cfg"
  2. Mistakenly ran "dd" utility to clear boot sector
  3. Due to bad sector or corrupted disk
  4. Bad or incompatible firmware/driver installation  e.t.c.
In case GRUB is corrupted, usually after powering on your server you see below kind of shell, also known as GRUB shell:


Once you get to this prompt there is a way to restore or recover GRUB so that your system can boot up properly. To recover GRUB we need to have RHEL or CentOS 7 ISO image, which we usually use for OS installation. Make sure you mount same OS version iso image which is already installed on system. Below procedure will work only on BIOS based X86 servers and virtual machines.

Follow these steps to recover GRUB:

Step 1 : Mount RHEL or CentOS 7 ISO image on your physical server and boot from it. In case you are using HPE Prolient server you can mount this ISO image on iLO, if this is virtual environment then mount it accordingly and reboot server or VM from ISO. Once rebooted you will see below options:


Step 2 : Scroll down and select "Troubleshooting" option from menu and PRESS "Enter"


Step 3 : Select "Rescue a CentOS system" and press ENTER:


Step 4 : Press ENTER key again to continue:


Step 5 : To continue, Type "1" and press ENTER, this will bring you to rescue mode, chroot sysimage filesystem using below command:

# chroot /mnt/sysimage



Step 6 : Next step is to install GRUB2 on the boot disk this will be usually "/dev/sda":

# grub2-install /dev/sda


On this step you should not see any error. In case "grub2-install" command is not available then you will have to install it through "grub2-tools" package which can be found in ISO image.

Step 7 : Once GRUB2 is installed, next step is to generate "grub.cfg" in "/boot/grub2" directory. 

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


File is generated, you can check as shown in above screenshot.

Once done, exit from the chroot and again exit to reboot the server.

No comments:

Post a Comment