#!/usr/bin/env bash # limine-chroot-repair — répare Limine depuis une clé live (Arch ISO) quand Windows a écrasé # le boot sur un PC en dual-boot à disque chiffré LUKS + Btrfs (Omarchy). # # ⚠️ Démarre la clé USB EN MODE UEFI (sinon efibootmgr ne pourra pas réécrire le boot). # # Il ouvre le LUKS (passphrase), monte le sous-volume Btrfs racine + l'ESP, fait un arch-chroot # et y lance `limine-repair`, puis démonte et referme tout (même en cas d'erreur). # # Usage : # limine-chroot-repair # auto-découverte (LUKS + ESP), interactif # limine-chroot-repair --luks /dev/nvme0n1p2 --esp /dev/nvme0n1p1 # limine-chroot-repair --subvol @ --map root --mnt /mnt # limine-chroot-repair --dry-run # affiche le plan, ne monte/chroot rien # limine-chroot-repair -- --reset-config # passe des options à limine-repair # # Prérequis (présents sur l'ISO Arch) : cryptsetup, arch-chroot (arch-install-scripts), # efibootmgr, lsblk, blkid, findmnt. Et un `limine-repair` dans le système installé # (ce dépôt le fournit via stow). set -euo pipefail DRY_RUN=0 LUKS=""; ESP=""; SUBVOL="@"; MAP="root"; MNT="/mnt" PASS=() # arguments transmis à limine-repair (après --) while (($#)); do case "$1" in --dry-run) DRY_RUN=1 ;; --luks) LUKS="$2"; shift ;; --esp) ESP="$2"; shift ;; --subvol) SUBVOL="$2"; shift ;; --map) MAP="$2"; shift ;; --mnt) MNT="$2"; shift ;; --) shift; PASS=("$@"); break ;; -h|--help) awk 'NR>1 && /^#/{sub(/^# ?/,"");print;next} NR>1{exit}' "$0"; exit 0 ;; *) echo "option inconnue : $1 (voir --help)" >&2; exit 2 ;; esac shift done say() { printf '\033[1;34m==>\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m[!]\033[0m %s\n' "$*" >&2; } die() { printf '\033[1;31m[x]\033[0m %s\n' "$*" >&2; exit 1; } run() { printf '\033[2m+ %s\033[0m\n' "$*"; (( DRY_RUN )) || "$@"; } (( EUID == 0 )) || die "À lancer en root (sudo)." [[ -d /sys/firmware/efi ]] || die "La clé n'a pas démarré en mode UEFI — efibootmgr serait inopérant. Rebooter la clé en UEFI." for t in cryptsetup arch-chroot lsblk blkid findmnt; do command -v "$t" >/dev/null || die "Outil manquant : $t (sur l'ISO Arch : pacman -Sy arch-install-scripts cryptsetup)." done # --- Découverte LUKS --- if [[ -z "$LUKS" ]]; then mapfile -t cands < <(lsblk -rpno NAME,FSTYPE | awk '$2=="crypto_LUKS"{print $1}') case "${#cands[@]}" in 0) die "Aucune partition LUKS trouvée. Précise-la : --luks /dev/…" ;; 1) LUKS="${cands[0]}" ;; *) printf '%s\n' "${cands[@]}"; die "Plusieurs partitions LUKS — choisis avec --luks /dev/…" ;; esac fi [[ -b "$LUKS" ]] || die "LUKS introuvable : $LUKS" # --- Découverte ESP (partition système EFI) --- ESP_GUID="c12a7328-f81f-11d2-ba4b-00a0c93ec93b" if [[ -z "$ESP" ]]; then mapfile -t esps < <(lsblk -rpno NAME,PARTTYPE | awk -v g="$ESP_GUID" 'tolower($2)==g{print $1}') case "${#esps[@]}" in 0) die "Aucune partition EFI (ESP) trouvée. Précise-la : --esp /dev/…" ;; 1) ESP="${esps[0]}" ;; *) printf '%s\n' "${esps[@]}"; die "Plusieurs ESP (Windows + Linux ?) — choisis la bonne avec --esp /dev/…" ;; esac fi [[ -b "$ESP" ]] || die "ESP introuvable : $ESP" say "Plan :" echo " LUKS : $LUKS -> /dev/mapper/$MAP" echo " ESP : $ESP -> $MNT/boot" echo " Subvol : $SUBVOL -> $MNT" echo " limine-repair args : ${PASS[*]:-(aucun)}" if (( DRY_RUN )); then say "(dry-run : rien n'est monté ni chrooté)"; exit 0; fi read -rp "Continuer ? [y/N] " ok; [[ "$ok" == [yY] ]] || die "Annulé." # --- Nettoyage garanti --- OPENED=0; MOUNTED=0; MOUNTED_ESP=0 cleanup() { set +e (( MOUNTED_ESP )) && umount "$MNT/boot" 2>/dev/null (( MOUNTED )) && umount -R "$MNT" 2>/dev/null (( OPENED )) && cryptsetup close "$MAP" 2>/dev/null } trap cleanup EXIT # --- Ouvrir LUKS (sauf si déjà ouvert) --- if [[ -e "/dev/mapper/$MAP" ]]; then say "/dev/mapper/$MAP déjà ouvert — réutilisation" else say "Ouverture du conteneur LUKS (passphrase demandée)" cryptsetup open "$LUKS" "$MAP"; OPENED=1 fi ROOT="/dev/mapper/$MAP" [[ "$(blkid -o value -s TYPE "$ROOT")" == btrfs ]] || die "$ROOT n'est pas du Btrfs (subvol inattendu ?)." # --- Monter racine (subvol) + ESP --- mkdir -p "$MNT" say "Montage de $ROOT (subvol=$SUBVOL) sur $MNT" mount -o "subvol=$SUBVOL" "$ROOT" "$MNT"; MOUNTED=1 mkdir -p "$MNT/boot" say "Montage de l'ESP $ESP sur $MNT/boot" mount "$ESP" "$MNT/boot"; MOUNTED_ESP=1 # Dans le chroot, root n'a pas ~/.local/bin dans son PATH : on résout le chemin absolu réel. REPAIR_URL="https://raw.githubusercontent.com/arthurnbr/.dotfiles/main/bin/.local/bin/limine-repair" repair="" for p in "$MNT"/usr/local/bin/limine-repair "$MNT"/usr/bin/limine-repair \ "$MNT"/root/.local/bin/limine-repair "$MNT"/home/*/.local/bin/limine-repair; do [[ -x "$p" ]] && { repair="/${p#"$MNT"/}"; break; } done if [[ -z "$repair" ]]; then warn "limine-repair absent du système monté — tentative de téléchargement ($REPAIR_URL)" if command -v curl >/dev/null && curl -fsSL "$REPAIR_URL" -o "$MNT/usr/local/bin/limine-repair"; then chmod +x "$MNT/usr/local/bin/limine-repair"; repair="/usr/local/bin/limine-repair" say "limine-repair téléchargé dans le système monté." else die "limine-repair introuvable et téléchargement impossible (pas de réseau ?). Manuel : arch-chroot $MNT, puis 'limine-update && efibootmgr -o ,…'." fi fi # --- Réparer dans le chroot --- say "arch-chroot → $repair ${PASS[*]:-}" arch-chroot "$MNT" "$repair" "${PASS[@]}" say "Réparation terminée. Démontage et fermeture (trap), puis retire la clé et redémarre."