-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassemble-grub-config.sh
executable file
·57 lines (53 loc) · 2.63 KB
/
assemble-grub-config.sh
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
#!/bin/sh
set -e
boot_dir="${1:-/boot}"
mkdir -p "$boot_dir"/kernels
grubHeader="$("$grub_print_header" "$boot_dir")"
cp -f "$boot_dir"/grub/grub.cfg{,.old}
sync -f "$boot_dir/grub/grub.cfg.old"
mkdir -p "$boot_dir"/grub/fragments.new
for i in /nix/var/nix/profiles/*/ /run/booted-system/ /var/current-system/; do
test -e "$i/boot/for-bootloader/grub.part.cfg" && {
test -d "$boot_dir/grub/fragments/$(basename "$i")" &&
basename "$i" | grep '[-][0-9]*-link$' >/dev/null &&
mv "$boot_dir/grub/fragments/$(basename "$i")" "$boot_dir/grub/fragments.new/$(basename "$i")"
}
done
sync -f "$boot_dir/grub"
rm -rf "$boot_dir"/grub/fragments
mv "$boot_dir"/grub/fragments.new "$boot_dir"/grub/fragments
sync -f "$boot_dir/grub"
for i in "$boot_dir"/kernels/*.efi; do
grep "${i#$boot_dir}" "$boot_dir"/grub/fragments/*/grub.part.cfg -m1 > /dev/null || rm "$i"
done
n=0
rm -rf "$boot_dir"/grub/fragment-index/*
mkdir -p "$boot_dir"/grub/fragment-index/
for i in /var/current-system/ /run/booted-system/ /nix/var/nix/profiles/*-link/ ; do
test -e "$i/boot/for-bootloader/grub.part.cfg" && {
n=$((n+1))
echo "$boot_dir/grub/fragments/$(basename "$i")" > "$boot_dir"/grub/fragment-index/$(printf "%06d" $n)
if test -f "$boot_dir/grub/fragments/$(basename "$i")"; then
rm "$boot_dir/grub/fragments/$(basename "$i")"
fi
if test -d "$boot_dir/grub/fragments/$(basename "$i")"; then
cp -fL "$i/boot/for-bootloader/"/grub.part.cfg "$boot_dir/grub/fragments/$(basename "$i")"/
else
mkdir "$boot_dir/grub/fragments/$(basename "$i")"
cp -fL "$i/boot/for-bootloader/"/grub.part.cfg "$boot_dir/grub/fragments/$(basename "$i")"/ 2>/dev/null
cp -L "$i/boot/for-bootloader/"/*.efi "$boot_dir"/kernels/
fi
}
done
sync -f "$boot_dir/kernels"
for i in "$boot_dir"/grub/fragments/*; do
sed -re "s@^menuentry[^\"]*\"@&$(basename "$i") @" "$i/grub.part.cfg" > "$i/grub.part.labeled.cfg"
( echo "$grubHeader"; cat "$i/grub.part.labeled.cfg" ) > "$i/grub.one.cfg"
done
cp "$boot_dir"/grub/grub.fragmented.cfg{,.old}
sync -f "$boot_dir/grub"
( echo "$grubHeader"; cat "$boot_dir"/grub/fragment-index/* |
sed -e 's@$@/grub.part.labeled.cfg@' | xargs cat ) > "$boot_dir"/grub/grub.fragmented.cfg
cp -f "$boot_dir"/grub/grub{.fragmented,}.cfg
sync -f "$boot_dir/grub"
echo GRUB config updated