While experimenting with various linux distros and dual booting, we often delete or alter linux partitions mistakenly. That's when GRUB rescue is necessary.
At this point, GRUB fails to load a proper configuration file or related modules to boot. And basically there could be four different types of GRUB rescue mode.
- If the configuration file grub.cfg is missing or corrupted, then it should show the
grub>prompt to perform further steps.
- The entire
/boot/grub/directory is corrupted or missing and showing the
grub rescue>prompt, and possibly with unknown filesystem error. Then you've to perform some different rescue steps to boot.
- The GRUB 2 bootloader is replaced by another bootloader like of Windows bootmgr or syslinux, then it's another story.
- You don't want to dual boot windows and Linux any more, so formatted the entire Linux partitions. But GRUB is still present, and it's not booting to Windows.
The third situation happens when you install Windows over an existing Linux installation. As the Windows bootloader can't detect the Linux partitions by default. And the fourth point is an entirely different situation, and we're not going to fix that in this tutorial.
So, here we'll deal with 3 different situations of broken GRUB bootloader on older BIOS systems, or the operating systems were installed with BIOS compatibility mode.
Rescue GRUB when grub.cfg is missing or corrupted
To perform grub rescue when the configuration file is deleted or corrupted,
- First run the
ls -lcommand to list all available disk partitions and filesystem type.
- Now identify your Linux system's root filesystem, in my case it was
(hd0,msdos5), highlighted with the red underline. Why msdos? As I was using the older MBR partition table.
- To check if everything is all right under the linux partition you can use the ls command again.
- If you can see the vmlinuz-xxxx and initrd.img-xxxx files under the /boot directory then start loading the kernel and initrd as instructed below.
- Type the command below one by one, but not the exactly same commands. Your system will be different from mine most probably. Tip: Use the Tab key while typing.
set root=(hd0,5) linux (hd0,5)/boot/vmlinuz-4.10.0-28-generic root=/dev/sda5 initrd (hd0,5)/boot/initrd.img-4.10.0-28-generic boot
- After typing the last command and hitting the Enter key, Linux should start booting.
- When the boot process is completed, login as usual and open up the the terminal application to update the GRUB configuration.
- Just run the
sudo update-grubcommand and it should do all the hard work for you.
Basically you need to set the proper path for the kernel and the initrd image file. The good news is the Tab autocomplete works just fine withing the grub rescue shell. So you can hit the
Tab key to get visual feedback and avoid typing long lines.
Note: If you have a backup copy of the grub.cfg file, then you can use the loadconfig command to easily boot to the Linux distro.
grub> loadconfig (hd0,msdos1)/root/backup/grub.cfg
The above command is an example, assuming the system have a backup grub configuration file under the
GRUB repair when the grub folder is missing or corrupted
If the contents of
/boot/grub/ directory is accidentally deleted or corrupted somehow, the rescue process is little different.
And there's two possibility in this case, first try to load few modules with the insmod command, like
If it can load the modules, then it's possible to rescue without a Live CD. But if it can't, the you'll definitely need a live CD or USB, and follow the next step below.
So, if the grub rescue prompt can load the modules, then you can recover it. You've to type the commands listed below one by one, but don't forget to modify them according to your system.
set root=(hd0,5) set prefix=(hd0,5)/boot/grub insmod normal normal
If there is any grub.cfg file under the prefix directory,
/boot/grub in this case, then it will be used. The GRUB selection menu will pop out just after typing the normal command.
If there's no GRUB selection menu, then you've to specify the kernel and initrd files and manually start the boot process. Then the commands should look like below.
set root=(hd0,5) set prefix=(hd0,5)/boot/grub insmod normal insmod linux linux (hd0,5)/vmlinuz root=/dev/sda5 initrd (hd0,5)/initrd.img boot
In Ubuntu or Debian, the linux kernel and initrd files are symlinked to the
/ directory, so using the symlinked files here.
Again after booting to the linux system, run the
sudo update-grub command to regenerate the grub configuration file.
GRUB rescue or restoration if it's over written
If Windows is installed after installing Linux, most probably Linux won't boot at all. The windows bootloader can create Linux entries, but that's another long way. This step is also for you if the GRUB installation directory is deleted or corrupted.
So, here we will use a linux live USB to fix this issue. If the installed linux is 64 bit, then any 64 bit live CD should work well.
First get a Live CD or USB and boot your PC/Laptop from the USB or the CD drive. That's should be pretty easy for you, I guess.
After booting to the live session, recognize the linux root partition with
fdisk -l command. In my case, it was
/dev/sda5, but your's could be different.
Now mount the Linux root partition, as we're going to to setup a chroot environment. I'm making a new directory,
/mnt/chroot for easier understanding.
sudo mkdir -p /mnt/chroot sudo mount /dev/sda5 /mnt/chroot/
Again, don't just type the commands above, modify them according to your system. Now check if the the linux root partition is mounted properly or not with the command below.
ls -l /mnt/chroot
If everything is mounted properly, then start the chroot process, commands listed below.
sudo -i # login as root, on the live session mount -B /dev/ /mnt/chroot/dev/ mount -B /dev/pts/ /mnt/chroot/dev/pts/ mount -B /proc/ /mnt/chroot/proc/ mount -B /sys/ /mnt/chroot/sys/ chroot /mnt/chroot/
At this point you should be inside the chroot shell. Additionally you can run the
source /etc/bash_completion command to enable Tab auto completion.
Finally install GRUB, make sure you know the correct device you want to install. In my case, the targeted device for grub installation was /dev/sda .
grub-install /dev/sda --boot-directory=/boot/ --target=i386-pc # inside the chroot
Grub installation should be complete within a minute, if there's no problem, update the GRUB configuration
update-grub # inside the chroot
Now exit the chroot by running the
exit command, and reboot the Laptop/PC. Though it's better to unmount the linux partitions before reboot, but jsut leave it this time.
If everything was done correctly you should be able to see the GRUB's OS selection menu, along with other OS entries, if there is any.
I hope this tutorial will help you to deal with varuious grub rescue related situations, and it's simple enough to understand by everyone.
Soon I'll be posting another tutorial about how to do the same if Linux was installed with UEFI compatibility and on a disk with GPT partition table.
But here the real point, did it worked on your system to rescue or repair grub? If not please let me know through the comments, and also don't forget to share your opinions.