Files
FWS-ISO/configs/releng/airootfs/usr/local/bin/fws-grub-finalize
T
nocode b27378eed3 fix(boot): never write NVRAM on Intel RST machines
Seen on real RST hardware: the installed system boots once, then never again,
the firmware no longer finding a bootable disk.

What is established. In the installer's chroot efivars is not reachable, so
nothing is written to NVRAM and the firmware boots EFI/BOOT/BOOTX64.EFI, which
it finds by scanning the ESP. That first boot works. Then fws-grub-finalize runs
grub-install again, and this time efivars answers: it creates a "FWS" entry and
puts it first in the boot order. That is the only thing this chain changes at
firmware level between the two boots, and firmware level is where the second
one fails.

What is assumed, not verified for lack of a VMD machine: why that entry is
unusable. Under VMD, Linux sees the NVMe in a synthetic PCI domain (10000:...)
that the vmd module creates and the firmware knows nothing about, so a device
path computed from that view resolves to nothing. "efibootmgr -v" on an
affected machine would settle it.

On any machine with an Intel VMD controller, FWS now relies solely on the
--removable path, which the first boot proves works. Detection goes through
fws-intel-rst (exit codes 10/11/12), the project's single VMD logic. Machines
without VMD are untouched and keep their NVRAM entry.

There were three NVRAM writers, and all three are gated:

- the kickstart %post, where the write fails anyway today but would break even
  the first boot in an environment that exposed efivars to the chroot;
- fws-grub-finalize, both grub-install and the efibootmgr -c fallback;
- fws-recovery, which was the worst of them. It goes through arch-chroot, and
  unlike Anaconda's chroot, arch-chroot mounts efivars, so the write succeeded
  there, from the ISO as well as from the installed system. A user whose RST
  machine would not boot would run "Réparer FWS", and the repair would recreate
  exactly the entry that stops it from booting.

Also in fws-grub-finalize: two lsblk calls took the disk from "lsblk -s", which
draws a tree, and got "└─/dev/nvme0n1" (checked on a real NVMe). The efibootmgr
fallback therefore always received a garbage device and never created its
entry, and the ESP search could fall back to the first ESP on the system,
Windows' on a dual-boot. The kickstart had already fixed this very trap in its
BIOS branch; the UEFI branch here had been missed. Fixing it alone would have
re-armed the efibootmgr writer on VMD, which is why it lands together with the
gate.

Tested by replacing the system tools with recording stubs: fws-grub-finalize
and fws-recovery each over five cases (no VMD, VMD in its three states, engine
missing), plus the engine's six detection cases. In every case the --removable
path is still installed, since on these machines it becomes the only way in.

Signed-off-by: nocode <suzuku295@gmail.com>
2026-09-22 15:56:06 +02:00

208 lines
10 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
# --- Intel RST / VMD : NE JAMAIS ÉCRIRE DANS LA NVRAM ------------------------
# SYMPTÔME (vu sur du vrai matériel RST) : 1er démarrage OK, plus jamais ensuite,
# le firmware ne trouvant plus de disque démarrable.
#
# CE QUI EST ÉTABLI. Dans le chroot de l'installateur, efivars est inaccessible :
# rien n'est écrit en NVRAM et le firmware démarre par EFI/BOOT/BOOTX64.EFI, qu'il
# trouve en parcourant l'ESP. Ici, au 1er démarrage, efivars répond : grub-install
# crée l'entrée « FWS » et la place EN TÊTE de l'ordre de démarrage. C'est la
# seule chose que ce script change AU NIVEAU DU FIRMWARE entre les deux
# démarrages — et c'est au niveau du firmware que le 2e échoue.
#
# CE QUI EST SUPPOSÉ, pas vérifié faute de machine VMD : pourquoi cette entrée
# est inutilisable. Sous VMD, Linux voit le NVMe dans un domaine PCI synthétique
# (10000:…) créé par le module vmd, que le firmware ne connaît pas ; un chemin de
# périphérique calculé depuis cette vue ne se résout pas côté firmware. Pour
# trancher : « efibootmgr -v » montre le chemin de l'entrée FWS, et la supprimer
# (« efibootmgr -b XXXX -B ») doit suffire à rendre la machine démarrable.
#
# Sur VMD on s'appuie donc UNIQUEMENT sur le chemin --removable, que le 1er
# démarrage prouve fonctionnel. Détection déléguée à fws-intel-rst (codes
# 10/11/12 = contrôleur VMD présent), seule logique VMD du projet.
VMD=0
if [ -x /usr/local/bin/fws-intel-rst ]; then
/usr/local/bin/fws-intel-rst --detect >/dev/null 2>&1
case $? in 10|11|12) VMD=1 ;; esac
fi
NVRAM_OPT=""
if [ "$VMD" = 1 ]; then
NVRAM_OPT="--no-nvram"
echo "[FWS] contrôleur Intel VMD/RST : aucune écriture NVRAM, chemin --removable seul"
fi
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)"
# « lsblk -s » dessine un ARBRE : sans le grep, $1 vaut « └─/dev/nvme0n1 »
# (vérifié), find_esp ne trouve rien et on retombe sur la PREMIÈRE ESP du
# système — celle de Windows sur un dual-boot. Même filtre que la ligne BIOS.
rootdisk="$(lsblk -spno NAME,TYPE "$rootsrc" 2>/dev/null | awk '$2=="disk"{print $1; exit}' | grep -oE '/dev/[a-z0-9]+')"
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 $NVRAM_OPT --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.
# Coupé sur VMD pour la même raison que --no-nvram ci-dessus : ce bloc
# écrirait la même entrée au chemin irrésoluble.
if [ "$VMD" = 0 ] && 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
# Même piège d'arbre : sans le grep, efibootmgr reçoit
# « └─/dev/nvme0n1 », échoue en silence, et l'entrée NVRAM de
# secours n'est JAMAIS créée.
disk="$(lsblk -spno NAME,TYPE "$espdev" | awk '$2=="disk"{print $1; exit}' | grep -oE '/dev/[a-z0-9]+')"
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