How to fix Grub Rescue in CentOS and RHEL 7

Grub Rescue Grub Rescue

Introduction to Grub Rescue

Grub Rescue: This guide is all about how to recover from a boot issue. One of the most facing issues could be the “grub rescue” prompt. Most of the questions in a few days came or how can I recover from grub rescue error. This may be due to human error or due to some patching activity as well. Let’s go through a few steps on how to resolve the grub rescue boot issue.

Grub configuration files

BIOS-based grub config

# /boot/grub2/grub.cfg

UEFI-based grub config

# /boot/efi/EFI/redhat/grub.cfg

If you need to configure any kernel parameter we should not touch the above configuration files, instead, edit the below file.

# vim /etc/default/grub

The available grub parameters are well documented in kernel official admin-guide.

Accidentally deleted files from /boot

In case, if you have accidentally deleted all files from /boot partition this guide may help to recover from it. For demonstration purpose, we have intentionally deleted all the files from /boot mount point.

/boot mount point without files
/boot mount point without files

Type of Grub errors

While the reboot you may get the below grub prompts. Let’s see why we getting different grub prompts.

The below one with only “grub>” because grub.cfg file missing or corrupted.

grub>
grub>

This could be due to all the files related to grub are deleted, /boot/grub2/ is empty or even this directory may be missing.

grub Rescue prompt
grub Rescue prompt

Any type of grub errors can be fixed easily by booting from the ISO/DVD rescue option.

Starting the Recovery process

Boot from RHEL ISO or DVD. Select “Troubleshooting” option from the menu.

RHEL ISO boot screen
RHEL ISO boot screen

Choose Rescue mode by selecting “Rescue a Red Hat Enterprise Linux System” from the menu.

RHEL Rescue boot screen menu
RHEL Rescue boot screen menu

This will load the anaconda installer with rescue mode. Once we get the options for rescue mode select 1 and press return key to enter into chroot environment.

Select 1 to proceed rescue mode
Select 1 to proceed rescue mode

Doing Chroot

By running the below command access the chroot environment.

# chroot /mnt/sysimage
run chroot to get root environment
run chroot to get root environment

Find and locate the /boot partition

Now it’s time to recover the grub. Before that, we need to find under which disk /boot mount resides. Run # lsblk and # blkid command to verify and confirm the available partition and disk layouts.

find the /boot residing disk
find the /boot residing disk

Install Grub

While installing a grub we should not install on a partition, instead install the grub on the whole disk. In my case /dev/sda1 is the partition used for /boot so I need to install the grub on /dev/sda.

Install the grub under /dev/sda by running below command.

# grub2-install /dev/sda

Once it installed we will get only the grub related files under /boot. But still, we need to follow more steps to get the vmlinuz, initramfs for a successful boot.

grub2-install to install the grub
grub2-install to install the grub

Reinstalling Kernel

Mount the RHEL Disk/ISO under any location and reinstall the kernel. By reinstalling we will get back all the files like vmlinuz and initramfs.

# mount /dev/sr0 /mnt
# cd /mnt/Packages/
# yum reinstall kernel

Saving or Regenerating GRUB Configuration

Finally, run the “grub2-mkconfig” to write the changes to a file. By running this command it will regenerate the required grub configuration file “grub.cfg”. The location will be under /boot/grub2/grub.cfg.

grub2-mkconfig to save to config
grub2-mkconfig to save to config

At last, just list the files to verify. Hope, we are good to move forward now.

All files are in the place
All files are in the place

Exiting Chroot/Rescue Shell

Type exit two times to exit from the chroot and rescue shell. Once you are exiting the rescue shell it will reboot the server. During the reboot, it will relabel SELinux for new files so avoid interrupting the process. Moreover, it will reboot automatically once again after the relabeling process.

Grub menu
Fixed Grub menu

After the reboot, we can see the grub menu and server boots normally.

Recovering a UEFI based Machine

In UEFI based servers the configuration file location will be under /boot/efi/EFI/redhat/. In case, If we get “grub rescue” in UEFI based machine we can fix it with similar above steps. but the configuration location will be a little different from bios-based machines.

[root@server1 ~]# 
[root@server1 ~]# ls -lthr /boot/
 total 99M
 drwx------. 3 root root 4.0K Jan  1  1970 efi
 -rw-r--r--. 1 root root 135K Oct 19  2016 config-3.10.0-514.el7.x86_64
 -rw-------. 1 root root 3.0M Oct 19  2016 System.map-3.10.0-514.el7.x86_64
 -rwxr-xr-x. 1 root root 5.2M Oct 19  2016 vmlinuz-3.10.0-514.el7.x86_64
 -rw-r--r--. 1 root root 272K Oct 19  2016 symvers-3.10.0-514.el7.x86_64.gz
 -rw-r--r--. 1 root root 599K Jan 18 05:09 initrd-plymouth.img
 -rw-------. 1 root root  47M Jan 18 05:13 initramfs-0-rescue-de38118e96024ea6a9193166a0a52701.img
 -rwxr-xr-x. 1 root root 5.2M Jan 18 05:13 vmlinuz-0-rescue-de38118e96024ea6a9193166a0a52701
 -rw-------. 1 root root  20M Jan 18 05:13 initramfs-3.10.0-514.el7.x86_64.img
 -rw-------. 1 root root  18M Jan 18 05:16 initramfs-3.10.0-514.el7.x86_64kdump.img
[root@server1 ~]# 
[root@server1 ~]# ls -lthr /boot/efi/EFI/redhat/
 total 5.7M
 -rwx------. 1 root root  1.3M Jul 20  2015 shim.efi
 -rwx------. 1 root root  1.3M Jul 20  2015 shim-redhat.efi
 -rwx------. 1 root root  1.3M Jul 20  2015 MokManager.efi
 -rwx------. 1 root root   176 Jul 20  2015 BOOT.CSV
 -rwx------. 1 root root 1001K Aug 29  2016 grubx64.efi
 -rwx------. 1 root root 1001K Aug 29  2016 gcdx64.efi
 drwx------. 2 root root  4.0K Jan 18 17:08 fonts
 -rwx------. 1 root root  1.0K Jan 18 17:11 grubenv
 -rwx------. 1 root root  4.2K Jan 18 17:11 grub.cfg
[root@server1 ~]#

Follow all the above steps except installing the packages and saving the grub configuration.

Install the packages

In UEFI based machine we need to reinstall the below packages to fix the grub rescue prompt.

# yum reinstall grub2-efi kernel shim -y
How to fix Grub Rescue in CentOS and RHEL 7 1

Saving Grub configuration

Finally, save the configuration under below location.

# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

To complete the rescue operation exit from chroot shell and rescue shell as mentioned in the earlier steps. Rebooting will take some time to complete the SELinux relabeling. Once done, it will be fine with booting the server.

Conclusion

Troubleshooting a boot issue is the most important role of a System administrator. To know which file and what steps need to be carried out to fix the boot issue is the most important part. Will update this article frequently with changes. Subscribe to the newsletter and go through similar troubleshooting guides.