Skip to content

Grub cheatsheet

Published:

Grub is the bootloader used by most Linux distributions. Recently I had the misfortune of running the following python script:

python -c 'import shutil; [shutil.rmtree(p, True) for p in ("build", "dist", "docs/_build")]'

which, when expanded, is equivalent to:

import shutil
shutil.rmtree(p, True) for p in ("build", "dist", "docs/_build")

The problem is that, in a hurry to reutilize the script, I made the terrible mistake of not adding a comma after a tuple with a single element.

import shutil
shutil.rmtree(p, True) for p in ("docs/_build")

which means that the script will try to delete the d, o, c, s, /, _, B, u, i, l, d files. But, as you may have noticed, the fifth “file”, /, is actually the root directory. Every single file not protected by elevated permissions was going to be deleted. My reaction was to slow, and I found myself with more holes in my system than a Swiss cheese, and in dire need for a complete reinstall. At least most of my work was backed up on either Github, GitLab or Google Drive.

Accessing the Grub menu

On some systems, the Grub menu is the first thing you see when you boot your computer. But on others, it must be accessed explicitly. The method I found to work in my case was to press Esc multiple times during the boot process. I have also read that pressing Del or F2 may work.

Password protecting Grub

After accessing the Grub menu, I was prompted for a password. Trying my user’s credentials did not yield any results. A bit of investigation later, I discovered the password was set in the /etc/grub.d/10_linux file.

set superusers="root"
password_pbkdf2 root grub.pbkdf2.sha512.10000.1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF

Not wanting to recover the password, I decided to change it to something more simple (and way less secure)

set superusers="root"
password root root

After saving the file, I ran sudo update-grub and rebooted the system.

Recovering the system

After booting into the system, I was greeted by the Grub menu. My goal was to use the USB stick with the Ubuntu 24.04 iso to reinstall the system. Using the c key, I accessed the Grub command line and ran the following commands:

ls # To list the available drives
set root=(hd0) # To set the root drive to the usb stick
find / # To make sure i selected the right drive
chainloader /EFI/boot/bootx64.efi # To boot from the usb stick
boot # To boot from the usb stick

From there, I was able to follow Ubuntu’s installation process and recover my system.