Skip to content

Commit d9bbb2b

Browse files
committed
Improve ESP auto-mount detection across Fedora layouts
1 parent 741fc32 commit d9bbb2b

2 files changed

Lines changed: 69 additions & 26 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This project is intended for systems booting in **UEFI mode** with a mounted **E
2525
- Linux distribution with `dracut`, `kernel-install`, and `efibootmgr` available.
2626
- UEFI boot mode.
2727
- Root privileges.
28-
- ESP mounted at `/boot/efi` or `/efi` (the script now attempts to auto-mount it when possible).
28+
- ESP mounted at one of `/boot/efi`, `/efi`, `/boot`, `/boot/EFI`, or `/esp` (the script attempts automatic ESP discovery/mounting).
2929

3030
> [!DANGER]
3131
> **Make a full system backup before running this script.** A tested restore path (snapshot rollback, rescue image, or offline backup) is strongly recommended.
@@ -228,7 +228,7 @@ sudo rm -f \
228228
## Troubleshooting
229229

230230
- **UEFI not detected**: Ensure firmware boot mode is UEFI and that `efivars`/ESP are accessible.
231-
- **ESP not mounted**: The script now tries to mount `/boot/efi` or `/efi` automatically (via fstab first, then ESP partition detection). If that still fails, mount it manually and rerun.
231+
- **ESP not mounted**: The script now checks `/boot/efi`, `/efi`, `/boot`, `/boot/EFI`, and `/esp`, then attempts automatic mounting (via fstab first, then ESP partition detection). If that still fails, mount it manually and rerun.
232232
- **UKI fails to boot**: Re-check `CMDLINE` and storage-related boot args.
233233
- **Missing EFI stub**: Install your distro's systemd-boot package and verify stub path.
234234
- **No Secure Boot signing**: Install `sbsigntools`; this script only warns when absent.

‎uki-setup.sh‎

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,39 @@ die() { echo "${RED}${BLD}[err]${RST} $*" >&2; exit 1; }
7070
hr() { echo "──────────────────────────────────────────────────────────────"; }
7171
require_cmd() { command -v "$1" &>/dev/null || die "Required command missing: $1"; }
7272

73+
ESP_MOUNT_CANDIDATES=(
74+
/boot/efi
75+
/efi
76+
/boot
77+
/boot/EFI
78+
/esp
79+
)
80+
7381
find_esp_device() {
7482
# GPT ESP type GUID: c12a7328-f81f-11d2-ba4b-00a0c93ec93b
7583
lsblk -pnro PATH,PARTTYPE,FSTYPE 2>/dev/null \
7684
| awk '$2=="c12a7328-f81f-11d2-ba4b-00a0c93ec93b" && tolower($3) ~ /fat|vfat/ {print $1; exit}'
7785
}
7886

87+
find_mounted_esp_target() {
88+
local candidate target
89+
90+
for candidate in "${ESP_MOUNT_CANDIDATES[@]}"; do
91+
target="$(findmnt -n -o TARGET "$candidate" 2>/dev/null || true)"
92+
[[ -n "$target" ]] && { echo "$target"; return 0; }
93+
done
94+
95+
while read -r target; do
96+
[[ -n "$target" ]] && { echo "$target"; return 0; }
97+
done < <(findmnt -rn -t vfat,fat -o TARGET 2>/dev/null | awk '$1 ~ /^\// && system("test -d " $1 "/EFI") == 0 {print $1}')
98+
99+
return 1
100+
}
101+
79102
ensure_esp_mounted() {
80103
local esp_mount="" esp_dev="" candidate
81104

82-
esp_mount="$(findmnt -n -o TARGET /boot/efi 2>/dev/null || findmnt -n -o TARGET /efi 2>/dev/null || true)"
105+
esp_mount="$(find_mounted_esp_target || true)"
83106
if [[ -n "$esp_mount" ]]; then
84107
info "ESP mounted at ${esp_mount}."
85108
return 0
@@ -88,7 +111,7 @@ ensure_esp_mounted() {
88111
warn "ESP not currently mounted. Attempting automatic mount..."
89112

90113
# First try fstab-based mount by mount point.
91-
for candidate in /boot/efi /efi; do
114+
for candidate in "${ESP_MOUNT_CANDIDATES[@]}"; do
92115
mkdir -p "$candidate"
93116
if mount "$candidate" &>/dev/null && findmnt "$candidate" &>/dev/null; then
94117
info "Mounted ESP at ${candidate} using fstab entry."
@@ -99,16 +122,13 @@ ensure_esp_mounted() {
99122
# Fallback: detect the ESP partition and mount directly.
100123
esp_dev="$(find_esp_device || true)"
101124
if [[ -n "$esp_dev" ]]; then
102-
if [[ -d /boot ]]; then
103-
candidate="/boot/efi"
104-
else
105-
candidate="/efi"
106-
fi
107-
mkdir -p "$candidate"
108-
if mount -t vfat "$esp_dev" "$candidate" &>/dev/null && findmnt "$candidate" &>/dev/null; then
109-
info "Mounted ESP device ${esp_dev} at ${candidate}."
110-
return 0
111-
fi
125+
for candidate in "${ESP_MOUNT_CANDIDATES[@]}"; do
126+
mkdir -p "$candidate"
127+
if mount -t vfat "$esp_dev" "$candidate" &>/dev/null && findmnt "$candidate" &>/dev/null; then
128+
info "Mounted ESP device ${esp_dev} at ${candidate}."
129+
return 0
130+
fi
131+
done
112132
fi
113133

114134
return 1
@@ -213,7 +233,7 @@ phase_preflight() {
213233
info "UEFI environment confirmed."
214234
fi
215235

216-
ensure_esp_mounted || die "ESP not mounted at /boot/efi or /efi and automatic mount failed. Mount it manually, then re-run."
236+
ensure_esp_mounted || die "ESP is not mounted and automatic mount failed. Checked: ${ESP_MOUNT_CANDIDATES[*]}. Mount it manually, then re-run."
217237
}
218238

219239
# =============================================================================
@@ -278,22 +298,45 @@ warn() { echo -e "${YLW}[uki-build]${RST} $*" >&2; }
278298
die() { echo -e "${RED}[uki-build]${RST} $*" >&2; exit 1; }
279299
require_cmd() { command -v "$1" &>/dev/null || die "Required command missing: $1"; }
280300
301+
ESP_MOUNT_CANDIDATES=(
302+
/boot/efi
303+
/efi
304+
/boot
305+
/boot/EFI
306+
/esp
307+
)
308+
281309
find_esp_device() {
282310
lsblk -pnro PATH,PARTTYPE,FSTYPE 2>/dev/null \
283311
| awk '$2=="c12a7328-f81f-11d2-ba4b-00a0c93ec93b" && tolower($3) ~ /fat|vfat/ {print $1; exit}'
284312
}
285313
314+
find_mounted_esp_target() {
315+
local candidate target
316+
317+
for candidate in "${ESP_MOUNT_CANDIDATES[@]}"; do
318+
target=$(findmnt -n -o TARGET "$candidate" 2>/dev/null || true)
319+
[[ -n "$target" ]] && { echo "$target"; return 0; }
320+
done
321+
322+
while read -r target; do
323+
[[ -n "$target" ]] && { echo "$target"; return 0; }
324+
done < <(findmnt -rn -t vfat,fat -o TARGET 2>/dev/null | awk '$1 ~ /^\// && system("test -d " $1 "/EFI") == 0 {print $1}')
325+
326+
return 1
327+
}
328+
286329
ensure_esp_mounted() {
287330
local esp_mount="" esp_dev="" candidate
288331
289-
esp_mount=$(findmnt -n -o TARGET /boot/efi 2>/dev/null || findmnt -n -o TARGET /efi 2>/dev/null || true)
332+
esp_mount=$(find_mounted_esp_target || true)
290333
if [[ -n "$esp_mount" ]]; then
291334
info "ESP mounted at ${esp_mount}"
292335
return 0
293336
fi
294337
295338
warn "ESP not mounted. Attempting automatic mount..."
296-
for candidate in /boot/efi /efi; do
339+
for candidate in "${ESP_MOUNT_CANDIDATES[@]}"; do
297340
mkdir -p "$candidate"
298341
if mount "$candidate" &>/dev/null && findmnt "$candidate" &>/dev/null; then
299342
info "Mounted ESP at ${candidate} using fstab entry"
@@ -303,13 +346,13 @@ ensure_esp_mounted() {
303346
304347
esp_dev=$(find_esp_device || true)
305348
if [[ -n "$esp_dev" ]]; then
306-
candidate="/boot/efi"
307-
[[ -d /boot ]] || candidate="/efi"
308-
mkdir -p "$candidate"
309-
if mount -t vfat "$esp_dev" "$candidate" &>/dev/null && findmnt "$candidate" &>/dev/null; then
310-
info "Mounted ESP device ${esp_dev} at ${candidate}"
311-
return 0
312-
fi
349+
for candidate in "${ESP_MOUNT_CANDIDATES[@]}"; do
350+
mkdir -p "$candidate"
351+
if mount -t vfat "$esp_dev" "$candidate" &>/dev/null && findmnt "$candidate" &>/dev/null; then
352+
info "Mounted ESP device ${esp_dev} at ${candidate}"
353+
return 0
354+
fi
355+
done
313356
fi
314357
315358
return 1
@@ -326,7 +369,7 @@ require_cmd lsblk
326369
require_cmd efibootmgr
327370
[[ -f "$KERNEL_IMG" ]] || die "Kernel image not found: ${KERNEL_IMG}"
328371
mkdir -p "$EFI_DIR"
329-
ensure_esp_mounted || die "ESP is not mounted and automatic mount failed."
372+
ensure_esp_mounted || die "ESP is not mounted and automatic mount failed. Checked: ${ESP_MOUNT_CANDIDATES[*]}"
330373
331374
# Build effective cmdline
332375
if [[ "$AUTO_DETECT_CMDLINE" -eq 1 ]]; then
@@ -373,7 +416,7 @@ info "UKI built successfully: ${UKI_OUT} ($(du -sh "$UKI_OUT" | cut -f1))"
373416
LABEL="Linux UKI ${KERNEL_VER}"
374417
375418
# Determine ESP mount point, disk, and partition number
376-
ESP_MOUNT=$(findmnt -n -o TARGET /boot/efi 2>/dev/null || findmnt -n -o TARGET /efi 2>/dev/null) \
419+
ESP_MOUNT=$(find_mounted_esp_target) \
377420
|| { warn "Cannot detect ESP mount — skipping efibootmgr."; exit 0; }
378421
ESP_DEV=$(findmnt -n -o SOURCE "$ESP_MOUNT") \
379422
|| { warn "Cannot detect ESP device — skipping efibootmgr."; exit 0; }

0 commit comments

Comments
 (0)