summaryrefslogtreecommitdiff
path: root/raveos-calamares-theme/etc/calamares/modules/calamares-fix-bootentry.sh
blob: f49e1d09fcb5d356fb0a9892e485287edb91cbdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
set -e

echo ">>> [fix-bootentry] Running INSIDE target system"

ENTRYDIR="/boot/loader/entries"
[ -d "${ENTRYDIR}" ] || { echo ">>> [fix-bootentry] No ${ENTRYDIR}, skipping."; exit 0; }

# 95-raveos.install (kernel-install hook, fired during pacstrap/mkinitcpio)
# detects the root device via findmnt at hook-time. Depending on the
# chroot/mount-namespace context that detection can silently come back
# empty, producing a loader entry with no "root=" in its options line
# (system then drops to an emergency shell on boot: "Failed to mount ''
# on real root"). On btrfs it can also come back in findmnt's bracket form
# ("/dev/vda2[/@]"), which blkid cannot resolve -- that produces a
# syntactically present but unusable root= (emergency shell: "Failed to
# mount '/dev/vda2[/@]' on real root"). By this point in the install
# sequence /etc/fstab is already correctly generated, so use it as the
# authoritative source and patch both the missing and malformed cases.

ROOT_UUID="$(findmnt -no UUID / || true)"
if [ -z "${ROOT_UUID}" ]; then
  ROOT_UUID="$(awk '$2 == "/" && $1 ~ /^UUID=/ {print $1; exit}' /etc/fstab | sed 's/^UUID=//')"
fi

if [ -z "${ROOT_UUID}" ]; then
  echo ">>> [fix-bootentry] Could not determine root UUID from findmnt or fstab, leaving entries untouched."
  exit 0
fi

echo ">>> [fix-bootentry] Root UUID: ${ROOT_UUID}"

for f in "${ENTRYDIR}"/*.conf; do
  [ -f "$f" ] || continue

  if grep -qE '^options[[:space:]].*\broot=' "$f"; then
    if grep -qE 'root=[^[:space:]]*\[' "$f"; then
      echo ">>> [fix-bootentry] Fixing malformed (findmnt-style) root= in: $f"
      sed -i -E "s#root=[^[:space:]]*\[[^]]*\]#root=UUID=${ROOT_UUID}#" "$f"
      continue
    fi
    echo ">>> [fix-bootentry] Already has root=, skipping: $f"
    continue
  fi

  echo ">>> [fix-bootentry] Patching missing root= in: $f"
  sed -i -E "s#^options[[:space:]]+#options root=UUID=${ROOT_UUID} #" "$f"
done

echo ">>> [fix-bootentry] Done."