| 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 |
set -eu |
| 42 |
|
| 43 |
NAME="Alloy" |
| 44 |
DRY=0 |
| 45 |
|
| 46 |
usage() { |
| 47 |
sed -n '2,39p' "$0" |
| 48 |
echo |
| 49 |
echo "Usage: alloy-boot-entry [--report]" |
| 50 |
echo " --report, -n say what would change and write nothing. Needs no root." |
| 51 |
} |
| 52 |
|
| 53 |
while [ $# -gt 0 ]; do |
| 54 |
case "$1" in |
| 55 |
--report|--dry-run|-n) DRY=1; shift ;; |
| 56 |
-h|--help) usage; exit 0 ;; |
| 57 |
*) printf 'error: unknown argument %s\n' "$1" >&2; exit 2 ;; |
| 58 |
esac |
| 59 |
done |
| 60 |
|
| 61 |
[ -d /sys/firmware/efi ] || { echo "not an EFI boot; no entry to name"; exit 0; } |
| 62 |
command -v efibootmgr >/dev/null 2>&1 || { echo "no efibootmgr; leaving the boot entry alone"; exit 0; } |
| 63 |
|
| 64 |
dump="$(efibootmgr -v)" || { echo "efibootmgr could not read NVRAM; leaving it alone"; exit 0; } |
| 65 |
|
| 66 |
|
| 67 |
stale="$(printf '%s\n' "$dump" \ |
| 68 |
| grep -E '^Boot[0-9A-Fa-f]{4}\*?[[:space:]]' \ |
| 69 |
| grep -iE '\\EFI\\[^\\]+\\shim' \ |
| 70 |
| grep -viE "[[:space:]]${NAME}[[:space:]]" || true)" |
| 71 |
|
| 72 |
if [ -z "$stale" ]; then |
| 73 |
echo "the firmware boot entry already says $NAME" |
| 74 |
exit 0 |
| 75 |
fi |
| 76 |
|
| 77 |
order="$(printf '%s\n' "$dump" | sed -nE 's/^BootOrder: (.*)$/\1/p')" |
| 78 |
new_order="$order" |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
entries="$(mktemp)" |
| 85 |
trap 'rm -f "$entries"' EXIT |
| 86 |
printf '%s\n' "$stale" > "$entries" |
| 87 |
|
| 88 |
while read -r entry; do |
| 89 |
[ -n "$entry" ] || continue |
| 90 |
num="$(printf '%s\n' "$entry" | sed -nE 's/^Boot([0-9A-Fa-f]{4})\*?[[:space:]].*/\1/p')" |
| 91 |
loader="$(printf '%s\n' "$entry" | sed -nE 's/.*(\\EFI\\[^\\]+\\[^ \t]*\.efi).*/\1/p' | head -1)" |
| 92 |
uuid="$(printf '%s\n' "$entry" | sed -nE 's/.*HD\([0-9]+,GPT,([0-9a-fA-F-]+),.*/\1/p')" |
| 93 |
[ -n "$num" ] && [ -n "$loader" ] && [ -n "$uuid" ] || { |
| 94 |
echo "could not read an entry off: $entry" >&2 |
| 95 |
continue |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
dev="$(lsblk -rno PATH,PARTUUID 2>/dev/null | awk -v u="$uuid" 'tolower($2)==tolower(u){print $1}' | head -1)" |
| 103 |
[ -n "$dev" ] || { echo "no device carries PARTUUID $uuid; leaving Boot$num alone" >&2; continue; } |
| 104 |
disk="/dev/$(lsblk -no PKNAME "$dev" | head -1)" |
| 105 |
partn="$(cat "/sys/class/block/$(basename "$dev")/partition" 2>/dev/null || true)" |
| 106 |
[ -b "$disk" ] && [ -n "$partn" ] || { echo "could not place $dev; leaving Boot$num alone" >&2; continue; } |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
if [ "$DRY" -eq 1 ]; then |
| 112 |
printf 'would rename: Boot%s -> a new entry "%s" (%s on %s partition %s), then delete Boot%s\n' \ |
| 113 |
"$num" "$NAME" "$loader" "$disk" "$partn" "$num" |
| 114 |
continue |
| 115 |
fi |
| 116 |
|
| 117 |
before="$(efibootmgr | sed -nE 's/^Boot([0-9A-Fa-f]{4})\*?.*/\1/p' | sort -u)" |
| 118 |
efibootmgr -q -c -d "$disk" -p "$partn" -L "$NAME" -l "$loader" |
| 119 |
after="$(efibootmgr | sed -nE 's/^Boot([0-9A-Fa-f]{4})\*?.*/\1/p' | sort -u)" |
| 120 |
created="$(printf '%s\n' "$before" "$after" | sort | uniq -u | head -1)" |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
[ -n "$created" ] || { |
| 126 |
echo "no new boot entry appeared; Boot$num is unchanged" >&2 |
| 127 |
exit 1 |
| 128 |
} |
| 129 |
efibootmgr | grep -q "^Boot${created}\*\?[[:space:]]*${NAME}\b" || { |
| 130 |
echo "Boot$created is not the $NAME entry expected; Boot$num is unchanged" >&2 |
| 131 |
exit 1 |
| 132 |
} |
| 133 |
|
| 134 |
efibootmgr -q -b "$num" -B |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
printf 'boot entry: Boot%s -> Boot%s "%s" (%s)\n' "$num" "$created" "$NAME" "$loader" |
| 139 |
|
| 140 |
new_order="$(printf '%s' "$new_order" | sed "s/\b$num\b/$created/I")" |
| 141 |
done < "$entries" |
| 142 |
|
| 143 |
if [ "$DRY" -eq 1 ]; then |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
echo "boot order: $order, each renamed entry taking the slot of the one it replaces" |
| 148 |
echo "nothing was written" |
| 149 |
exit 0 |
| 150 |
fi |
| 151 |
|
| 152 |
if [ -n "$new_order" ] && [ "$new_order" != "$order" ]; then |
| 153 |
efibootmgr -q -o "$new_order" |
| 154 |
echo "boot order: $order -> $new_order" |
| 155 |
fi |
| 156 |
|