Skip to content

Commit 2195f35

Browse files
authored
armbian-install: fix boot on UEFI systems (#9945)
EFI partition wasn't detect properly or was mixed between drives, causing boards like *cix-acpi* to fail boot from nvme after armbian-install was issued
1 parent 3072b4a commit 2195f35

1 file changed

Lines changed: 63 additions & 13 deletions

File tree

packages/bsp/common/usr/bin/armbian-install

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ mtdcheck=$(grep 'mtdblock' /proc/partitions | awk '{print $NF}' | xargs)
153153
# 3 - Extract the UUID=<uuid> from $sdblkid via regex without " - as e.g.: UUID=2e1d1509-a8fc-4f8b-8a51-88fb8593f8d6
154154
sduuid=$(echo "$sdblkid" | sed -nE 's/^.*[[:space:]](UUID="[0-9a-zA-Z-]*").*/\1/p' | tr -d '"')
155155

156-
#recognize EFI
156+
# recognize EFI
157157
[[ -d /sys/firmware/efi ]] && DEVICE_TYPE="uefi"
158-
efi_partition=$(LC_ALL=C fdisk -l "/dev/$diskcheck" 2>/dev/null | grep "EFI" | awk '{print $1}')
158+
# Don't detect efi_partition globally - it causes issues when multiple disks are present
159+
# EFI partition will be detected per-target disk in check_partitions() and create_armbian()
159160

160161
# define makefs and mount options
161162
declare -A mkopts mountopts
@@ -503,7 +504,19 @@ create_armbian()
503504
echo "GRUB_DISABLE_OS_PROBER=false" >> "${TempDir}"/rootfs/etc/default/grub.d/98-armbian.cfg
504505

505506
echo "$satauuid / $FilesystemChoosen ${mountopts[$FilesystemChoosen]}" >> "${TempDir}"/rootfs/etc/fstab
506-
echo "UUID=$(lsblk -io KNAME,LABEL,UUID,PARTLABEL | grep $diskcheck | grep -i efi | awk '{print $3}') /boot/efi vfat defaults 0 2" >> "${TempDir}"/rootfs/etc/fstab
507+
# Find EFI partition on TARGET disk only, using lsblk for reliability
508+
local target_efi_uuid
509+
target_efi_uuid=$(lsblk -o KNAME,UUID,PARTTYPENAME,PARTLABEL -n -l "/dev/$diskcheck" | grep -iE 'efi|EFI' | awk '{print $2}' | head -1)
510+
if [[ -z "$target_efi_uuid" ]]; then
511+
# Fallback: try p1 as common EFI partition location
512+
target_efi_uuid=$(blkid -s UUID -o value "/dev/${diskcheck}p1" 2>/dev/null)
513+
fi
514+
if [[ -z "$target_efi_uuid" ]]; then
515+
echo "ERROR: Cannot find EFI partition UUID on /dev/$diskcheck" >> $logfile
516+
umount_device "$2"
517+
exit 20
518+
fi
519+
echo "UUID=$target_efi_uuid /boot/efi vfat defaults 0 2" >> "${TempDir}"/rootfs/etc/fstab
507520
echo "/swapfile none swap sw 0 0" >> "${TempDir}"/rootfs/etc/fstab
508521

509522
cat <<-hibernatemenu >"${TempDir}"/rootfs/etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla
@@ -518,18 +531,44 @@ create_armbian()
518531
ResultActive=yes
519532
hibernatemenu
520533

521-
efi_partition=$(LC_ALL=C fdisk -l "/dev/$diskcheck" 2>/dev/null | grep "EFI" | awk '{print $1}')
534+
# Find EFI partition on TARGET disk only using lsblk (more reliable than fdisk|grep)
535+
efi_partition=$(lsblk -o KNAME,PARTTYPENAME,PARTLABEL -n -l "/dev/$diskcheck" | grep -iE 'efi|EFI' | awk '{print "/dev/"$1}' | head -1)
536+
if [[ -z "$efi_partition" ]] || [[ ! -b "$efi_partition" ]]; then
537+
# Fallback: try p1 as common EFI partition location
538+
efi_partition="/dev/${diskcheck}p1"
539+
if [[ ! -b "$efi_partition" ]]; then
540+
echo "ERROR: Cannot find EFI partition on /dev/$diskcheck" >> $logfile
541+
umount_device "$2"
542+
exit 21
543+
fi
544+
fi
522545

523546
echo "Install GRUB to $efi_partition"
547+
echo "INFO: Installing GRUB to $efi_partition on /dev/$diskcheck" >> $logfile
524548
mkdir -p "${TempDir}"/rootfs/{dev,proc,sys}
525-
mount $efi_partition "${TempDir}"/rootfs/boot/efi
549+
if ! mount "$efi_partition" "${TempDir}"/rootfs/boot/efi; then
550+
echo "ERROR: Failed to mount EFI partition $efi_partition" >> $logfile
551+
umount_device "$2"
552+
exit 22
553+
fi
554+
# Verify mount succeeded
555+
if ! mountpoint -q "${TempDir}"/rootfs/boot/efi; then
556+
echo "ERROR: EFI partition mount verification failed" >> $logfile
557+
umount_device "$2"
558+
exit 23
559+
fi
526560
mount --bind /dev "${TempDir}"/rootfs/dev
527561
mount --make-rslave --bind /dev/pts "${TempDir}"/rootfs/dev/pts
528562
mount --bind /proc "${TempDir}"/rootfs/proc
529563
mount --make-rslave --rbind /sys "${TempDir}"/rootfs/sys
530564
arch_target=$([[ $(arch) == x86_64 ]] && echo "x86_64-efi" || echo "arm64-efi")
531-
chroot "${TempDir}/rootfs/" /bin/bash -c "grub-install --target=$arch_target --efi-directory=/boot/efi --bootloader-id=Armbian" >> $logfile
532-
chroot "${TempDir}/rootfs/" /bin/bash -c "grub-mkconfig -o /boot/grub/grub.cfg" >> $logfile
565+
if ! chroot "${TempDir}/rootfs/" /bin/bash -c "grub-install --target=$arch_target --efi-directory=/boot/efi --bootloader-id=Armbian --removable" >> $logfile 2>&1; then
566+
echo "ERROR: GRUB installation failed" >> $logfile
567+
umount_device "$2"
568+
exit 24
569+
fi
570+
chroot "${TempDir}/rootfs/" /bin/bash -c "grub-mkconfig -o /boot/grub/grub.cfg" >> $logfile 2>&1
571+
echo "INFO: GRUB installation completed successfully" >> $logfile
533572
grep "${TempDir}"/rootfs/sys /proc/mounts | cut -f2 -d" " | sort -r | xargs umount -n
534573
umount "${TempDir}"/rootfs/proc
535574
umount "${TempDir}"/rootfs/dev/pts
@@ -837,25 +876,36 @@ check_partitions()
837876
dialog --yes-label "Proceed" --no-label 'Exit' --title "$title" --backtitle "$backtitle" --yesno "\nDestination $diskcheck has ${FREE_SPACE}GB of available space. \n\nAutomated install will generate needed partition(s)!" 9 55
838877
[[ $? -ne 0 ]] && exit 11
839878
if [[ "$DEVICE_TYPE" == uefi ]]; then
840-
if [[ -z "$efi_partition" ]]; then
879+
# Check for EFI partition on TARGET disk only using lsblk
880+
local target_efi
881+
target_efi=$(lsblk -o KNAME,PARTTYPENAME,PARTLABEL -n -l "/dev/$diskcheck" | grep -iE 'efi|EFI' | awk '{print "/dev/"$1}' | head -1)
882+
if [[ -z "$target_efi" ]] || [[ ! -b "$target_efi" ]]; then
883+
echo "INFO: No EFI partition found on /dev/$diskcheck, creating..." >> $logfile
841884
wipefs -aq /dev/$diskcheck
842885
# create EFI partition
843886
{
844887
echo n; echo ; echo ; echo ; echo +200M;
845888
echo t; echo EF; echo w;
846-
} | fdisk /dev/$diskcheck &> /dev/null || true
847-
yes | mkfs.vfat /dev/${diskcheck}p1 &> /dev/null || true
848-
fatlabel /dev/${diskcheck}p1 EFI
889+
} | fdisk "/dev/$diskcheck" &> /dev/null || true
890+
sync
891+
partprobe "/dev/$diskcheck" || true
892+
sleep 1
893+
yes | mkfs.vfat -F 32 "/dev/${diskcheck}p1" &> /dev/null || true
894+
fatlabel "/dev/${diskcheck}p1" EFI
895+
target_efi="/dev/${diskcheck}p1"
849896
fi
850897
{
851898
echo n; echo ; echo ; echo ; echo
852899
echo w
853-
} | fdisk /dev/$diskcheck &> /dev/null || true
900+
} | fdisk "/dev/$diskcheck" &> /dev/null || true
901+
sync
902+
partprobe "/dev/$diskcheck" || true
903+
sleep 1
854904
yes | mkfs.ext4 /dev/${diskcheck}p2 &> /dev/null || true
855905

856906
# re-read
857907
emmccheck=$(ls -d -1 /dev/mmcblk* 2>/dev/null | grep -w 'mmcblk[0-9]' | grep -v "$root_partition_device");
858-
efi_partition=$(LC_ALL=C fdisk -l "/dev/$diskcheck" 2>/dev/null | grep "EFI" | awk '{print $1}')
908+
# No longer needed - EFI partition detection happens in create_armbian()
859909
else
860910
# Create new partition of max free size
861911
{

0 commit comments

Comments
 (0)