forked from AsahiLinux/asahi-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-grub
executable file
·82 lines (65 loc) · 1.99 KB
/
update-grub
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh
# SPDX-License-Identifier: MIT
set -e
[ -e /etc/default/update-grub ] && . /etc/default/update-grub
if [ -e "$(dirname "$0")"/functions.sh ]; then
. "$(dirname "$0")"/functions.sh
else
. /usr/share/asahi-scripts/functions.sh
fi
: ${BOOT_PART:="/boot"}
: ${EFI_PART:=}
: ${GRUB_DIR:="$BOOT_PART/grub"}
: ${CONFIG:="$GRUB_DIR/grub.cfg"}
: ${MODULES:="ext2 fat part_gpt search"}
umount=false
if [ -z "$EFI_PART" ] && [ -z "$TARGET" ]; then
EFI_PART=/run/.boot-efi
mount_boot_esp "$EFI_PART"
umount=true
fi
: ${TARGET:="$EFI_PART/EFI/BOOT/BOOTAA64.EFI"}
fs="$(grub-probe "$BOOT_PART" -t fs)"
uuid="$(grub-probe "$BOOT_PART" -t fs_uuid)"
part="$(grub-probe "$BOOT_PART" -t drive | sed -e 's/(.*,/hd0,/' | tr -d ')')"
grub_path="$(grub-mkrelpath "$GRUB_DIR")"
if [ -z "$uuid" ]; then
echo "Error: Unable to determine root filesystem UUID"
exit 1
fi
echo "Filesystem: $fs"
echo "UUID: $uuid"
echo "Partition: $part"
echo "Relative path: $grub_path"
cat > /tmp/grub-core.cfg <<EOF
search.fs_uuid $uuid root $part
set prefix=(\$root)'$grub_path'
EOF
echo "Installing GRUB files..."
mkdir -p "$GRUB_DIR"
touch "$GRUB_DIR"/device.map
dd if=/dev/zero of="$GRUB_DIR"/grubenv bs=1024 count=1 2>/dev/null
cp -r /usr/share/grub/themes "$GRUB_DIR"
cp -r /usr/lib/grub/arm64-efi "$GRUB_DIR"
rm -f "$GRUB_DIR"/arm64-efi/*.module
mkdir -p "$GRUB_DIR"/{fonts,locale}
cp /usr/share/grub/unicode.pf2 "$GRUB_DIR"/fonts
for i in /usr/share/locale/*/LC_MESSAGES/grub.mo; do
lc="$(echo "$i" | cut -d/ -f5)"
cp "$i" "$GRUB_DIR"/locale/"$lc".mo
done
[[ $MODULES != *$fs* ]] && MODULES="$MODULES $fs"
echo "Core image modules: $MODULES"
echo "Generating GRUB image..."
grub-mkimage \
--directory '/usr/lib/grub/arm64-efi' \
-c /tmp/grub-core.cfg \
--prefix "$part/boot/grub" \
--output "$GRUB_DIR"/arm64-efi/core.efi \
--format arm64-efi \
--compression auto \
$MODULES
cp "$GRUB_DIR"/arm64-efi/core.efi "$TARGET"
grub-mkconfig -o "$CONFIG"
$umount && umount "$EFI_PART"
true