Skip to content

Instantly share code, notes, and snippets.

@zajdee
Created September 17, 2024 11:41
Show Gist options
  • Save zajdee/a83467f2ec698e192e4923247784231e to your computer and use it in GitHub Desktop.
Save zajdee/a83467f2ec698e192e4923247784231e to your computer and use it in GitHub Desktop.
Fix for the grub/uefi/dual-efi-partition issue. Runs on every `update-grub` or `grub-mkconfig`, but not on `grub-install`.
#!/bin/bash
#
# Save the file as /etc/grub.d/90_efi_fix
#
# if recreating the boot partitions, do not forget to update partition IDs in /etc/fstab
# use the following to find out the UUIDs
# lsblk -o UUID -nr /dev/sda1
# lsblk -o UUID -nr /dev/sdb1
# also add ",nofail" to the fstab entry options, in case the drive dies, so that you are not stuck during boot
#
set -e
BOOT_PATH='\EFI\ubuntu\shimx64.efi'
UEFI1_MOUNTPOINT="/boot/efi"
UEFI2_MOUNTPOINT="/boot/efi2"
if [ -z "${UEFI1_MOUNTPOINT}" ] || [ -z "${UEFI2_MOUNTPOINT}" ]; then
echo "EFI nvram fix: mountpoints not set properly!"
exit 1
fi
if mountpoint -q "${UEFI1_MOUNTPOINT}" && mountpoint -q "${UEFI2_MOUNTPOINT}" ; then
rsync -t --recursive --delete "${UEFI1_MOUNTPOINT}/" "${UEFI2_MOUNTPOINT}/"
UEFI1_DEVICE="$(grep -w "${UEFI1_MOUNTPOINT}" /proc/mounts | awk '{print $1}')"
UEFI2_DEVICE="$(grep -w "${UEFI2_MOUNTPOINT}" /proc/mounts | awk '{print $1}')"
UEFI1_PARTNO="$(echo ${UEFI1_DEVICE} | grep -Eo '[0-9]+' | head -1)"
UEFI2_PARTNO="$(echo ${UEFI2_DEVICE} | grep -Eo '[0-9]+' | head -1)"
UEFI1_PARENT="/dev/$(lsblk -o PKNAME -nr ${UEFI1_DEVICE})"
UEFI2_PARENT="/dev/$(lsblk -o PKNAME -nr ${UEFI2_DEVICE})"
UEFI1_PARTUUID="$(lsblk -o PARTUUID -nr ${UEFI1_DEVICE})"
UEFI2_PARTUUID="$(lsblk -o PARTUUID -nr ${UEFI2_DEVICE})"
# echo "${UEFI1_DEVICE}, ${UEFI2_DEVICE}, ${UEFI1_PARTUUID}, ${UEFI2_PARTUUID}, ${UEFI1_PARENT}, ${UEFI2_PARENT}, ${UEFI1_PARTNO}, ${UEFI2_PARTNO}, ${BOOT_PATH}"
efibootmgr -v | grep "${UEFI1_PARTUUID}" | grep -q "ubuntu" || (
# echo "EFI nvram fix: Will install efi entry for ${UEFI1_DEVICE}"
efibootmgr -c -d "${UEFI1_PARENT}" -p "${UEFI1_PARTNO}" -L "ubuntu ${UEFI1_PARENT}" -l "${BOOT_PATH}" > /dev/null 2>&1
)
efibootmgr -v | grep "${UEFI2_PARTUUID}" | grep -q "ubuntu" || (
# echo "Will install efi entry for ${UEFI2_DEVICE}"
efibootmgr -c -d "${UEFI2_PARENT}" -p "${UEFI2_PARTNO}" -L "ubuntu ${UEFI2_PARENT}" -l "${BOOT_PATH}" > /dev/null 2>&1
)
fi
exit 0
# If your /boot/efi is on a raid1 device (e.g. md127) and your grub-install fails with:
# grub-install: error: failed to register the EFI boot entry: Operation not permitted.
# You can migrate to plain efi partition set-up with two partitions (one on sda1, the other one on sdb1), like this:
# mdadm --fail /dev/md127 /dev/sdb1
# mdadm --remove /dev/md127 /dev/sdb1
# wipefs -a /dev/sdb1
# mkfs.vfat -F32 /dev/sdb1
# mkdir /boot/efi2
# mount /dev/sdb1 /boot/efi2
# rsync -arvP /boot/efi/* /boot/efi2/
# umount /boot/efi
# mdadm --stop /dev/md127
# wipefs -a /dev/sda1
# mkfs.vfat -F32 /dev/sda1
# mount /dev/sda1 /boot/efi
# rsync -arvP /boot/efi2/* /boot/efi/
# now you have /boot/efi and /boot/efi2 mounted, add them to /etc/fstab (see the guide at the beginning of the file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment