Repair GRUB Installation

From WilliamsNet Wiki
Revision as of 18:57, 17 May 2025 by DrEdWilliams (talk | contribs) (→‎Reinstall GRUB)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Unlike most other problems where you have a (mostly) functioning Linux system to work with, if your GRUB install gets trashed you are almost literally toast.

How it happens

It doesn't really take much to mess with your GRUB configuration. Fortunately, it lives in an infrequently accessed part of your filesystem (/boot) and survives by obscurity. The biggest risk of corruption is actually when applying an update to the GRUB software -- if that gets interrupted and you don't have a chance to re-run the update (i.e. the system freezes) you are in trouble.

Resources

As mentioned above, if you can't boot, you don't have the usual suite of tools to help you fix the problem -- but most major distributions offer "live" CD/DVD ISOs that allow you to 'try before you buy'. This can provide enough of a workable system to allow you to bootstrap your way to a solution. Simply download a recent ISO image of a live system and put it somewhere you can boot from. Note that Ubuntu related distribution ISOs (including kubuntu) operate in this mode by default -- you boot from the ISO and it gives you an option to install from the live system. Other OS distributions have separate 'live' ISOs.

For ubuntu related distributions, there is also a dedicated rescue disk:

https://sourceforge.net/projects/boot-repair-cd/files/boot-repair-disk-64bit.iso/download

It is the same toolset described below, but in a form that is automatically run at boot time.

Fixing It

The process summarized below is from this web page:

https://phoenixnap.com/kb/grub-rescue

That page describes the process for restoring a GRUB configuration from the GRUB command line if you don't have a live ISO image available; it does require a significant level of knowledge about the system being restored ... which may not be available.

Also on that page is the rather simple process using his boot-repair tool (free) to rescue a ubuntu system. There is a lot of context on that page, but the gist is here. Once you get the live image booted, open a terminal and issue these commands:

sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt update
sudo apt install boot-repair -y
sudo boot-repair

The application will show a status dialog with a bunch of activities, then give you a big dialog with an option to implement the recommended changes ... if you have anything other than a single-disk simple configuration, click on the 'Advanced Options' link to look at what it wants to do. Make sure the location of your deployment is correct; it assumes the first disk with a reasonably valid configuration is what you want (not always the case). Make any changes and then hit the 'Apply' button. Cross your fingers ... and you will hopefully get the success dialog. Then you reboot back into your normal configuration.

Reinstall GRUB

If the repair tool can't salvage your configuration, you will need to reinstall GRUB. This does require a live ISO image of the same distribution as is on the main system drive.

The trick here is that you have to fool the GRUB installer into thinking it is working on the OS image on the system drive, but with the multitude of faux filesystem in a current Linux system, you need to make sure enough of them are available in the main disk image:

sudo mount /dev/sda1 /mnt
sudo mount --bind /dev /mnt/dev &&
sudo mount --bind /dev/pts /mnt/dev/pts &&
sudo mount --bind /proc /mnt/proc &&
sudo mount --bind /sys /mnt/sys

Now you can do the installation of the new grub configuration:

sudo grub-install -root-directory=/mnt/ /dev/sda

If that succeeds, then you need to clean things up:

sudo umount /mnt/sys &&
sudo umount /mnt/proc &&
sudo umount /mnt/dev/pts &&
sudo umount /mnt/dev &&
sudo umount /mnt

Of course, substitute the correct device name for your main system drive ...