Skip to content

Instantly share code, notes, and snippets.

@zajdee
Last active May 12, 2025 11:02
Show Gist options
  • Save zajdee/d280161248849bb5595f5cc813c84e14 to your computer and use it in GitHub Desktop.
Save zajdee/d280161248849bb5595f5cc813c84e14 to your computer and use it in GitHub Desktop.
A dumb script to fix Grub when /boot/efi is on mdraid (RAID1)
#!/usr/bin/env bash
# This script is for Debian/Ubuntu Linux (tested on Ubuntu 22.04)
# This script fixes a case of /boot/efi being installed on a mdraid (RAID1) device
# That's not a supported configuration as of Grub on Ubuntu 22.04
# You have to stop and remove the mdraid, reinitialize the FAT volumes, remount /boot/efi,
# then configure grub to use both devices (using debconf-set-selections),
# and finally run grub reconfiguration (in this case noninteractively), which will install
# grub (both EFI partitions of the former RAID1) to EFI nvram
set -x
if [ ! -d /sys/firmware/efi/efivars ]; then
echo "This system does not utilize UEFI"
exit 0
fi
if ! grep -q -w /boot/efi /proc/mounts; then
echo "/boot/efi is not mounted, bailing out"
exit 0
fi
if ! grep -w /boot/efi /proc/mounts | grep -q /dev/md; then
echo "/boot/efi is not on mdraid"
exit 0
fi
MDRAID=$(grep -w /boot/efi /proc/mounts | awk '{print $1}')
MDRAID_DEVICES=$(mdadm -vDs "${MDRAID}" | awk -F= '/^[ ]+devices/ {print $2}' | tr , '\n')
echo -e "ESP partition mdraid devices: ${MDRAID_DEVICES/$'\n'/, }"
declare -A DEVICE_MAP
for device in /dev/disk/by-id/ata*; do
destination=$(readlink -f $device)
DEVICE_MAP["$destination"]="$device"
done
GRUB_DEVICE_LIST=""
for device in $MDRAID_DEVICES; do
device_by_id=${DEVICE_MAP[$device]}
GRUB_DEVICE_LIST="${GRUB_DEVICE_LIST}, ${device_by_id}"
done
GRUB_DEVICE_LIST=${GRUB_DEVICE_LIST#, }
echo "Grub device list: ${GRUB_DEVICE_LIST}"
echo "Stopping RAID"
umount /boot/efi || exit 1
mdadm --stop "${MDRAID}" || exit 1
for device in $MDRAID_DEVICES; do
# you could also use mdadm --zero-superblock "$device" here
wipefs -a "$device"
mkfs.vfat -F32 -n EFI "$device"
if ! grep -q -w /boot/efi /proc/mounts; then
# fix /boot/efi
FAT_UUID=$(grep -w /boot/efi /etc/fstab | awk '{print $1}')
echo "Replacing FAT UUID ${FAT_UUID} in /etc/fstab"
sed -i "s/${FAT_UUID}/LABEL=EFI/" /etc/fstab
# mount the partition
mount /boot/efi
fi
done
# Change the partition types from "Linux RAID" to "EFI System"
# sgdisk --typecode=Z:ef00 /dev/sdX # replace Z with the partition ID, e.g. "1" for "sdX1", and X with the device, e.g. "sdn"
# sgdisk --typecode=Z:ef00 /dev/sdY # again, replace Z and Y with proper values
# Now reconfigure grub. Note that if you haven't changed the parttition types, interactive
# dpkg-reconfigure will not present you with the list of applicable drives.
debconf-show grub-efi-amd64 | sort
echo RESET grub-efi/install_devices_disks_changed | debconf-communicate grub-efi-amd64
echo RESET grub-efi/install_devices_failed | debconf-communicate grub-efi-amd64
echo RESET grub-efi/install_devices_empty | debconf-communicate grub-efi-amd64
echo RESET grub-efi/install_devices_disks_changed | debconf-communicate grub-efi-amd64
echo "grub-efi-amd64 grub-efi/install_devices string ${GRUB_DEVICE_LIST}" | debconf-set-selections
debconf-show grub-efi-amd64 | sort
# This will reinstall grub into nvram, which you can check by running:
# efibootmgr -v
dpkg-reconfigure -f noninteractive grub-efi-amd64
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment