Title : Rescuing GRUB

Author: cyr4x3

Date : 10 September 2023

Tags : linux

Some years ago I ran into a problem where, when starting my computer, I got

thrown into a GRUB rescue command prompt instead of GRUB showing up and letting

me choose which OS to boot in directly. I was running Debian 11 "Bullseye" (the

stable version at that time) and this started happening after performing a

`dist-upgrade`. Probably this "breakage" had something to do with me fiddling

with my GRUB config sometime before the update and making changes that were

incompatible with the new version of GRUB installed by the update (at least,

that's what I think may've happened as of today, though I may be completely

wrong). In other words, I'm pretty sure it was my fault.

When this happened I had never seen a GRUB rescue prompt before, so I freaked

out. It turns out that the problem wasn't as bad as I first thought.

Initial fix

After the initial shock after upgrading my system, I had to find a way to fix

this problem. I found a temporary solution: in order for my computer to boot up

normally I just had to enter the series of commands

    set prefix=(hd0,gpt2)/boot/grub
    set root=(hd0,gpt2)
    insmod linux
    insmod normal
    normal

and my computer booted up perfectly fine. Let's see what these commands actually

mean.

value of an environment variable. If invoked with no arguments, all

environment variables and their values will be print out.

understand as loading or activating them).

Therefore, the first two commands set the value of the `prefix` and `root`

environment variables, defining the path to the GRUB directory and the bootable

partition respectively.

Later on, the `linux` and `normal` modules are inserted. The `linux` module is a

loader for Linux images, while `normal` provides us with "Normal Mode" (the

opposite of "Rescue Mode", the one in which we enter these commands).

Once these modules are loaded one can switch to "Normal Mode" by simply typing

`normal`. Then, the usual GRUB menu, that I'm sure you're familiar with, is

shown (the one that lets you choose the OS you want to boot in).

Final solution

It was a very busy season, so I kept postponing finding a better and definitive

solution to this problem and I had to enter these series of commands every time

I had to boot.

One day I got tired and decided to find a better solution. It was quite simple.

Once my computer booted up, I ran the commands shown in the previous section so

I could start Debian and, once I was logged in into my user account, I simply

ran [^1]:

    # update-grub
    # grub-mkconfig -o /boot/grub/grub.cfg
    # grub-install /dev/sda

As described in the `grub-mkconfig(8)` manpage, by default the `grub-mkconfig`

command prints the generated GRUB configuration file to `stdout` (standard

output). The `-o` flag outputs the generated config to a file, so the path

following that flag should correspond to the location were GRUB expects its

config file to be (in my case it was `/boot/grub/grub.cfg`).

The command `grub-install` will install GRUB to a device. The path you specify

must refer to a disk and not one of its partitions. Tools like `lsblk` can help

you identify the disk path on which you should ask `grub-install` to install

GRUB.

Resources and references

GNU GRUB Manual 2.06

Musings: GRUB2 Modules

grub-mkconfig Manpage

grub-install Manpage

lsblk Manpage

[^1]: The number sign, hash or pound sign shown at the beginning of every

command means that the command should be ran as a superuser. Either precede

every command with the word `sudo` (or `doas` or any equivalent) or switch to

the root use with the `su` command and then type the commands shown in the

code block.