Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save smartbit/7cd3f0e52ea99f28fe2a0b173d46a9d1 to your computer and use it in GitHub Desktop.

Select an option

Save smartbit/7cd3f0e52ea99f28fe2a0b173d46a9d1 to your computer and use it in GitHub Desktop.
Fedora UEFI GRUB Restore Guide (for BTRFS/LUKS after Windows Install)

Purpose

This script automates the process of restoring the Fedora GRUB bootloader on a UEFI system after it has been removed or overwritten, typically by a Windows installation.

It is designed for a common Fedora setup: a LUKS-encrypted BTRFS root partition and a separate /boot partition. The script performs the following actions:

  1. Prompts for the LUKS encryption password to unlock the Fedora system partition.
  2. Correctly mounts the BTRFS root subvolume, the /boot partition, and the EFI System Partition (ESP).
  3. Binds the necessary system directories from the live environment.
  4. Executes a chroot command to run commands inside the installed Fedora system.
  5. Inside the chroot, it reinstalls the GRUB2 and shim packages, which places the boot files in the ESP and creates a new boot entry in the UEFI firmware.
  6. Regenerates the grub.cfg file to ensure it detects all available operating systems.
  7. Performs two critical verification checks after the restore process:
    • Boot Partition Check: It compares the actual UUID of your /boot partition with the UUID written inside GRUB's configuration file. This ensures that when GRUB loads, it knows exactly where to find your Linux kernels and boot files.
    • EFI Partition Check: It compares the actual UUID of the new EFI System Partition (ESP) with the UUID listed in your Fedora system's /etc/fstab file. This is crucial because a Windows installation creates a new ESP with a new UUID. This check ensures your Fedora system can correctly mount the ESP after rebooting, which is necessary for future kernel and bootloader updates.

Official Documentation

This guide follows the official Fedora documentation but is tailored for recent Fedora installations (Fedora 33+) that use a BTRFS filesystem by default and do not use LVM: Restoring the bootloader using the Live disk

Prerequisites & Assumptions

  • You must be booted from a Fedora Live USB.
  • The commands should be run step-by-step manually with sudo su privileges.
  • It assumes a partition layout similar to the one below, where Windows has just been installed, overwriting the original EFI partition.

Assumed lsblk -f -p Output

After cryptsetup luksOpen /dev/nvme0n1p6 myvolume:

/dev/nvme0n1
├─/dev/nvme0n1p1      vfat        FAT32       (EFI System Partition)
├─/dev/nvme0n1p2
├─/dev/nvme0n1p3      ntfs                    (Windows C: Drive)
├─/dev/nvme0n1p4      ntfs                    (Windows Recovery)
├─/dev/nvme0n1p5      ext4        1.0         (Fedora /boot)
└─/dev/nvme0n1p6      crypto_LUKS 2           (Fedora LUKS Container)
  └─/dev/mapper/luks- btrfs       fedora      (Fedora BTRFS Volume)

How to Use

  1. Open a terminal in your Fedora Live environment.
  2. Gain root privileges for the entire session by running sudo su.
  3. Open the restore_grub.sh script in a text editor to view the commands.
  4. Carefully copy and paste each command from the script into your terminal, one at a time, and press Enter.
  5. Pay close attention to the output of each command, especially the final verification steps, to ensure everything completes successfully before rebooting.
# A barebones command sequence to restore the Fedora GRUB bootloader.
# verify we're running as root `sudo su`
[ "$EUID" -ne 0 ] && { echo "Please run as root or with sudo."; exit 1; }
# 1. Display partition layout for verification
lsblk -f -p
# 2. Unlock LUKS partition (will prompt for password)
cryptsetup luksOpen /dev/nvme0n1p6 myvolume
# 3. Mount all filesystems
mount -o subvol=root /dev/mapper/myvolume /mnt
mount /dev/nvme0n1p5 /mnt/boot
mount /dev/nvme0n1p1 /mnt/boot/efi
# 4. Bind system directories needed for chroot
mount -o bind /dev /mnt/dev
mount -o bind /proc /mnt/proc
mount -o bind /sys /mnt/sys
mount -o bind /run /mnt/run
mount -o bind /sys/firmware/efi/efivars /mnt/sys/firmware/efi/efivars
# 5. Enter chroot and reinstall GRUB in a single command
chroot /mnt /bin/bash -c "dnf reinstall -y shim-\* grub2-efi-\* grub2-common && grub2-mkconfig -o /boot/grub2/grub.cfg"
# 6. Verification Step for /boot partition
echo "--- Verifying UUIDs (they should match) ---"
echo "UUID of /boot partition (/dev/nvme0n1p5):"
blkid -s UUID -o value /dev/nvme0n1p5
echo "UUID in GRUB config (/boot/efi/EFI/fedora/grub.cfg):"
grep -oP '(?<=--fs-uuid --set=dev )[^ ]+' /mnt/boot/efi/EFI/fedora/grub.cfg
echo "-------------------------------------------"
# 7. Verification Step for ESP in fstab
echo "--- Verifying ESP UUID in /etc/fstab (they should match) ---"
echo "Actual UUID of ESP (/dev/nvme0n1p1):"
blkid -s UUID -o value /dev/nvme0n1p1
echo "UUID for /boot/efi in /etc/fstab:"
grep '\s/boot/efi\s' /mnt/etc/fstab | awk '{print $1}' | cut -d'=' -f2
echo "---------------------------------------------------------"
echo "If the ESP UUIDs do NOT match, edit /mnt/etc/fstab with vim before rebooting."
echo "--- GRUB restore complete. Syncing disks and rebooting now. ---"
sync && reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment