Files
FWS-ISO/configs/releng/airootfs/usr/local/bin/fws-grub-finalize
T
nocode c7ac8a68d8 fix(installer): find the EFI system partition instead of never finding it
Both the kickstart %post and fws-grub-finalize looked for the ESP with

    lsblk -nro PATH,PARTTYPENAME,FSTYPE | awk '$2=="EFI System" && $3=="vfat"'

but "lsblk -r" separates columns with SPACES and an ESP's label is literally
"EFI System" -- with a space. awk therefore sees $2="EFI" and $3="System", so
the condition can never be true and no ESP was ever found. The consequences
chain: /boot/efi never mounted, install_grub returns 1, grub-install and
grub-mkconfig skipped, /boot/grub missing, and the step 4ter grub-mkconfig
fails in turn (visible in the 2026-09-06 install journal as "+ grub-mkconfig"
followed by "+ true"). This is why GRUB had to be repaired by hand in a TTY
after bare-metal installs -- the first-boot safety net carried the same bug.

Detection now reads PARTTYPE (a type CODE: GUID on GPT, 0xef on MBR, never a
space) and queries each vfat partition separately, so an empty column cannot
shift the fields either.

Also in this commit:
- /boot/grub is created before any grub-mkconfig, so the menu is generated even
  when grub-install failed.
- The step 4ter grub-mkconfig no longer swallows its stderr; a failure now
  lands in /var/log/fws-post.log instead of /dev/null.
- clearpart no longer forces --disklabel=msdos: the table type is left to
  Anaconda/blivet, which picks it from the firmware (BIOS -> msdos, UEFI ->
  gpt). Forcing MBR under UEFI is out of spec and makes dual-boot beside a
  GPT Windows impossible.
- The resolv.conf copy to the target removes the destination first and copies
  the CONTENT, since /etc/resolv.conf is now a symlink on both sides.

Signed-off-by: nocode <suzuku295@gmail.com>
2026-09-09 10:08:02 +02:00

167 lines
7.9 KiB
Bash

#!/bin/bash
# ============================================================
# FWS — fws-grub-finalize : verrouille le bootloader au 1er démarrage.
#
# POURQUOI : sur du vrai matériel (bare-metal UEFI — cartes mères ASUS et
# autres), l'installation peut se terminer sans que GRUB soit inscrit dans la
# NVRAM, ou sans que Windows soit détecté (os-prober désactivé / partitions
# pas visibles pendant l'install). Résultats vus en conditions réelles :
# « le PC redémarre sur Windows », ou « GRUB s'ouvre mais le menu est vide ».
#
# Ce service tourne au premier démarrage du système installé (marqueur
# /var/lib/fws/grub-finalize-done posé seulement en cas de SUCCÈS, le service
# ne se relance donc pas ensuite). À ce moment-là, contrairement au chroot de
# l'installateur :
# - /sys/firmware/efi/efivars est accessible → l'entrée NVRAM « FWS »
# (efibootmgr) PEUT être créée/rafraîchie,
# - tous les disques sont présents → os-prober retrouve Windows (et tout
# autre OS) sur les autres disques/partitions,
# - le chemin de secours EFI/BOOT/BOOTX64.EFI (--removable) est (re)posé :
# les firmwares récalcitrants (ASUS…) bootent quand même dessus.
#
# Journal : /var/log/fws-grub-finalize.log
# ============================================================
set -u
LOG=/var/log/fws-grub-finalize.log
DONE=/var/lib/fws/grub-finalize-done
# Déjà finalisé → ne rien faire (le service a une ConditionPathExists=…, on se
# protège aussi d'un lancement manuel).
[ -e "$DONE" ] && exit 0
mkdir -p /var/lib/fws
exec >>"$LOG" 2>&1
echo "== fws-grub-finalize : $(date '+%F %T')"
# Laisse udev finir d'énumérer les disques (os-prober a besoin de tout voir).
udevadm settle --timeout=30 2>/dev/null || true
# os-prober doit être présent et ACTIF (par défaut sur Arch : désactivé → les
# autres OS n'apparaissent jamais au menu). Les paquets sont normalement dans
# l'image installée (os-prober + ntfs-3g listés dans packages.x86_64) ; on
# tente un install de secours sans bloquer si pas de réseau.
if command -v os-prober >/dev/null 2>&1; then
if grep -q '^GRUB_DISABLE_OS_PROBER=' /etc/default/grub 2>/dev/null; then
sed -i 's/^GRUB_DISABLE_OS_PROBER=.*/GRUB_DISABLE_OS_PROBER=false/' /etc/default/grub
else
echo 'GRUB_DISABLE_OS_PROBER=false' >> /etc/default/grub
fi
else
pacman -S --noconfirm --needed os-prober ntfs-3g >/dev/null 2>&1 || \
echo "[FWS] os-prober indisponible — Windows ne sera pas détecté au menu GRUB"
fi
# Première partition ESP (vfat + type de partition EFI), optionnellement
# limitée à un disque donné.
#
# ⚠ NE PAS revenir à « lsblk -nro PATH,PARTTYPENAME,FSTYPE | awk '$2=="EFI
# System" …' » : « lsblk -r » sépare les colonnes par des ESPACES et le
# libellé d'une ESP est littéralement « EFI System » — avec une espace. awk
# voyait $2="EFI" et $3="System", le test n'était JAMAIS vrai, aucune ESP
# n'était trouvée et GRUB n'était donc jamais posé en UEFI (ni ici, ni dans
# le %post du kickstart qui portait le même bug).
# On lit PARTTYPE (un CODE : GUID en GPT, 0xef en MBR — jamais d'espace) et on
# interroge chaque partition séparément : une valeur par appel, donc aucun
# décalage de colonnes même si un champ est vide.
find_esp() {
_disk="${1:-}"
lsblk -nrpo NAME,FSTYPE $_disk 2>/dev/null | awk '$2=="vfat"{print $1}' \
| while read -r _part; do
_ptype="$(lsblk -nrdo PARTTYPE "$_part" 2>/dev/null | tr 'A-Z' 'a-z')"
case "$_ptype" in
c12a7328-f81f-11d2-ba4b-00a0c93ec93b|0xef) printf '%s\n' "$_part" ;;
esac
done | head -n1
}
# --- UEFI : installer GRUB sur une ESP ---------------------------------------
# Règle : on préfère une ESP PORTÉE PAR LE MÊME DISQUE que « / » (installation
# FWS typique : /boot/efi déjà dans le fstab). Si l'installation a réutilisé
# l'ESP d'un autre OS (cas dual-boot vu en réel : ESP Windows sur le 1er
# disque), on retombe sur la première ESP FAT32 trouvée.
OK=1
if [ -d /sys/firmware/efi ]; then
esp=""
if ! mountpoint -q /boot/efi; then
# /boot/efi pas monté (ou pas dans le fstab) : on cherche.
rootsrc="$(findmnt -no SOURCE / 2>/dev/null)"
rootdisk="$(lsblk -spno NAME,TYPE "$rootsrc" 2>/dev/null | awk '$2=="disk"{print $1; exit}')"
if [ -n "$rootdisk" ]; then
esp="$(find_esp "$rootdisk")"
fi
# Repli : première ESP du système (ex. ESP Windows réutilisée).
[ -n "$esp" ] || esp="$(find_esp)"
if [ -n "$esp" ]; then
echo "[FWS] ESP : $esp → montage sur /boot/efi (ne pas démonter, reboot ensuite)"
mkdir -p /boot/efi
mount "$esp" /boot/efi 2>/dev/null || echo "[FWS] montage ESP impossible ($esp)"
else
echo "[FWS] aucune partition EFI trouvée sur ce système"
fi
fi
if mountpoint -q /boot/efi || [ -d /boot/efi/EFI ]; then
# (a) Entrée NVRAM « FWS » + fichiers EFI/FWS (best effort : grub-install
# crée lui-même l'entrée si efivars est accessible) :
grub-install --target=x86_64-efi --efi-directory=/boot/efi \
--bootloader-id=FWS --recheck || echo "[FWS] grub-install (NVRAM) : échec, ignoré"
# (b) Chemin de SECOURS EFI/BOOT/BOOTX64.EFI (--removable) : boote SANS
# aucune entrée NVRAM. INDISPENSABLE sur firmware qui « oublie »
# l'entrée (ASUS…) — c'est lui qui garantit le 1er boot.
if ! grub-install --target=x86_64-efi --efi-directory=/boot/efi \
--bootloader-id=FWS --removable --recheck; then
OK=0
fi
# Copie physique de secours si --removable n'a pas produit le fichier.
if [ ! -f /boot/efi/EFI/BOOT/BOOTX64.EFI ] \
&& [ -f /boot/efi/EFI/FWS/grubx64.efi ]; then
mkdir -p /boot/efi/EFI/BOOT
cp -f /boot/efi/EFI/FWS/grubx64.efi /boot/efi/EFI/BOOT/BOOTX64.EFI \
&& echo "[FWS] BOOTX64.EFI copié depuis EFI/FWS" || OK=0
fi
# (c) Rafraîchir l'entrée NVRAM si elle manque (efibootmgr). GRUB booté
# a déjà créé « FWS » ; on ne duplique pas si l'entrée existe.
if command -v efibootmgr >/dev/null 2>&1 \
&& [ -d /sys/firmware/efi/efivars ]; then
espdev="$(findmnt -no SOURCE /boot/efi 2>/dev/null)"
if [ -n "$espdev" ]; then
disk="$(lsblk -spno NAME,TYPE "$espdev" | awk '$2=="disk"{print $1; exit}')"
part="$(printf '%s' "$espdev" | grep -oE '[0-9]+$')"
if ! efibootmgr 2>/dev/null | grep -qi 'FWS'; then
efibootmgr -c -d "${disk:-/dev/null}" -p "${part:-1}" \
-L "FWS" -l '\EFI\FWS\grubx64.efi' >/dev/null 2>&1 \
&& echo "[FWS] entrée NVRAM FWS créée" || \
echo "[FWS] NVRAM non modifiable — le chemin --removable suffit"
else
echo "[FWS] entrée NVRAM FWS déjà présente"
fi
fi
fi
else
echo "[FWS] aucune ESP montée sur /boot/efi — grub-install UEFI impossible ici"
OK=0
fi
else
# --- BIOS/CSM : GRUB dans le MBR du disque portant « / » ---------------
rootsrc="$(findmnt -no SOURCE / 2>/dev/null)"
disk="$(lsblk -spno NAME,TYPE "$rootsrc" 2>/dev/null \
| awk '$2=="disk"{print $1; exit}' | grep -oE '/dev/[a-z0-9]+')"
if [ -n "$disk" ]; then
grub-install --target=i386-pc --recheck "$disk" || OK=0
else
echo "[FWS] disque BIOS introuvable"
OK=0
fi
fi
# Régénère le menu COMPLET (FWS + recovery + Windows/autres OS via os-prober).
if grub-mkconfig -o /boot/grub/grub.cfg; then
if grep -q 'menuentry' /boot/grub/grub.cfg; then
touch "$DONE"
echo "[FWS] GRUB finalisé : entrées NVRAM + menu OK ($(date '+%T'))"
exit 0
fi
fi
echo "[FWS] grub.cfg absent/vide — nouvel essai au prochain démarrage"
exit 1