-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootctlu
executable file
·320 lines (292 loc) · 10.4 KB
/
bootctlu
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env bash
set -u +vx
# bootctlu - Install/register systemd-bootmanager on Ubuntu
# Required: gdisk(sgdisk) coreutils(tee sort cut mkdir cat cp mv rm cd ls diff)
# grep sed systemd(systemd-boot*.efi) uuid-runtime(uuidgen) xargs
# [memtest]: wget tar [register]: efibootmgr [install]: diffutils(diff)
version=0.2.5
maxkernels=2
Help(){
cat <<-EOH
$self v$version - Install/register systemd-bootmanager on Ubuntu
Usage: $self [<option>...]
<option>:
-h/--help: Only display this help text.
-V/--version: Only display the version.
-v/--verbose: Show more detail of the actions.
-q/--quiet: Fatal errors only output to the terminal.
-n/--nogo: No writing to the system at all.
-I/--install: Only install the script and kernel install hooks.
-U/--uninstall: Only uninstall the script and kernel install hooks.
-M/--maxkernels: Maximum number of kernels to install, default: $maxkernels.
-i/--imgdir <dir>: Kernel & initrd images directory, default: '$boot'.
$boot, overrides BOOTCTLU_BOOT.
-p/--part <dev>: EFI System Partition device
-e/--esp <dir>: EFI System Partition mountpoint, default: '$defesp'.
$defesp, overrides BOOTCTLU_ESP.
-L/--label: Label for the UEFI entry.
-m/--memtest: Also download and set up a MemTest86 entry.
-r/--register: Also register the bootmanager with UEFI.
Extra arguments ignored so this can work as install/remove kernel hook.
EOH
}
Echo(){ # I:go,verbose,quiet 1:message 2:errorcode(exit if not 0 and go==1)
# No message if errorcode 0 and verbose 0)
local msg=$1 err=${2:-}
if [[ $err ]]
then
((err)) && echo -e "\nABORT: $msg" && ((go)) && exit $err
((!verbose)) && msg=
fi
((!quiet)) && echo -ne "$msg"
}
# Default UEFI entry label
label='Linux bootmanager'
# Default timeout for bootmenu (seconds)
timeout=6
# Default directory for kernel initrd images
boot=/boot
# ESP directory: used when none specified and more than 1 is mounted
defesp=/boot/efi
# Install location bootctlu
inst=/usr/local/bin
# Install/remove kernel hooks
hook=zz-bootctlu
hookidir=/etc/kernel/postinst.d hooki=$hookidir/$hook
hookrdir=/etc/kernel/postrm.d hookr=$hookrdir/$hook
# Downloads
memurl='good4.eu/memtest.tgz' memver='8.1'
# SecureBoot binaries PreLoader.efi and HashTool.efi urls
preloader='https://apt.blobfolio.com/other/PreLoader/PreLoader.efi'
hashtool='https://apt.blobfolio.com/other/PreLoader/HashTool.efi'
[[ $- = *i* ]] &&
echo "Don't source, but install or run like: bash $BASH_SOURCE" &&
return 1
self=${0##*/} imgdir=$boot specesp=${BOOTCTLU_ESP:-} off=/dev/null rd=$off
go=1 quiet=0 verbose=0 register=0 memtest=0 esp= partition=
while (($#))
do
case $1 in
-h|--help) Help && exit 0 ;;
-V|--version) echo "$self v$version" && exit 0 ;;
-I|--install)
[[ $inst/$self ]] && diff "$0" "$inst/$self" ||
cp -v "$0" "$inst"
ln -sfv "$inst/$self" "$hooki"
ln -sfv "$inst/$self" "$hookr"
exit 0 ;;
-U|--uninstall) rm -v "$inst/$self" "$hooki" "$hookr"; exit 0 ;;
-n|--nogo) go=0 ;;
-q|--quiet) quiet=1 ;;
-v|--verbose) verbose=1 rd=/dev/stdout ;;
-i|--imgdir)
[[ $BOOTCTLU_BOOT ]] &&
Echo "WARNING: overriding BOOTCTLU_BOOT with '$2'"
imgdir=$2
shift ;;
-L|--label)
label=$2
shift ;;
-p|--part)
[[ -b $2 ]] || Echo "Partition '$2' is not a block device" 1
partition=$2
shift ;;
-e|--esp)
[[ ${2%%/*} ]] && Echo "Mountpoint '$2' must start with /" 1
[[ $specesp ]] && Echo "WARNING: overriding BOOTCTLU_ESP with '$2'"
specesp=$2
shift ;;
-M|--maxkernels)
[[ $2 =~ ^[1-9]$ ]] || Echo "Max.kernels not 1..9: '$2'" 2
maxkernels=$2; shift ;;
-m|--memtest) memtest=1 ;;
-r|--register) register=1 ;;
-*) Help && Echo "Unrecognized flag: $1" 3 ;;
*) Echo "Ignored argument '$1'" 0
esac
shift
done
# Bootmanager file
ldr=/usr/lib/systemd/boot/efi/systemd-boot*.efi
loader=$(echo $ldr)
if [[ ! -f $loader ]]
then # Try getting it elsewhere
wget -q good4.eu/gummi.efi
[[ -f gummi.efi ]] &&
loader=gummi.efi ||
Echo "The bootmanager cannot be found: '$ldr'" 3
fi
((EUID)) && echo "ABORT: Privileges required, run with sudo or as root" && exit
if ((go))
then
((register)) && ! ebm=$(type -P efibootmgr) &&
Echo "Can't register with UEFI, efibootmgr not available" 4
if [[ ! -f /etc/machine-id ]]
then
uuidgen=$(type -P uuidgen) ||
Echo "Can't generate a machine-id, package uuid-runtime not installed" 5
$uuidgen |tr -d '-' >/etc/machine-id
fi
else
Echo "\n### Simulation, no actual copying/writing/registering\n"
fi
# Get EFI System Partitions
esps=
parts=$(ls -Al /dev/disk/by-partuuid |grep -o '[a-z][a-z0-9]*$')
[[ $partition ]] && parts=${partition##*/}
for part in $parts
do
num=$(grep -o '[0-9]*$' <<<"$part")
disk=${part%$num}
[[ ${disk: -1} = p && ${disk: -2:1} = [0-9] ]] && disk=${disk%p}
esp=$(sgdisk -i$num /dev/$disk |grep C12A7328-F81F-11D2-BA4B-00A0C93EC93B)
[[ $esp ]] && esps+=" /dev/$part"
done
[[ $esps ]] && esps=${esps:1} || Echo "No EFI System Partitions present" 6
Echo "EFI System Partitions: $esps\n" 0
# Usable EFI partition must be mounted, collect all mounted
mp=$(egrep "${esps// /|}" /proc/mounts |cut -d' ' -f2)
[[ $mp ]] || Echo "No EFI System Partitions mounted" 7
mp=${mp//$'\n'/ }
Echo "Mount points of EFI partitions: $mp\n" 0
# Pick EFI mountpoint
if [[ $specesp ]]
then # EFI-mount specified (commandline / env.variable): check if mounted
[[ " $mp " == *" $specesp "* ]] && esp=$specesp ||
Echo "Specified EFI mountpoint '$specesp' has no EFI partition mounted" 8
else # No EFI-mount specified
if [[ " $mp " == *" $defesp "* ]]
then # Use default if mounted
esp=$defesp
else # Use the discovered one
[[ $mp == *$'\n'* ]] &&
Echo "More than 1 EFI System Partition mounted, must specify one" 9
esp=$mp
fi
fi
part=$(grep " $esp " /proc/mounts) part=${part%% *}
partnum=${part##*[^0-9]} disk=${part%$partnum}
[[ ${disk: -1} = p && ${disk: -2:1} = [0-9] ]] && disk=${disk%p}
Echo "--- Using mountpoint $esp on $part\n"
Echo "--- Copying images from $imgdir into EFI mountpoint $esp\n"
# Initialize and prepare bootmanager confguration
id=$(</etc/machine-id)
entriesdir="$esp/loader/entries" iddir="$esp/loader/$id"
p=$(grep ' / ' /proc/mounts) root=${p%% *}
[[ -f /etc/os-release ]] && p=$(grep PRETTY_NAME /etc/os-release) ||
p=$(grep DISTRIB_DESCRIPTION /etc/lsb-release)
p=${p#*\"} release=${p:0: -1}
cmdline=$(sed -e 's@initrd=[^ ]*@@g' -e 's@BOOT_IMAGE=[^ ]*@@' /proc/cmdline |xargs)
out="$esp/loader/loader.conf"
file="#auto-entries 0\n#auto-firmware 0\n#console-mode auto\n"
file+="default $id-linux-*\ntimeout $timeout\neditor yes"
# Install boot manager file and loader configuration
if ((go))
then
# Remove all linux entries for the current machine
rm -rf -- "$iddir"/linux-* "$entriesdir/$id-linux-"*.conf
mkdir -p "$iddir" "$entriesdir" "$esp/EFI/loader" "$esp/EFI/BOOT" ||
Echo "Problem making required directories" 10
[[ -f "$out" && ! $(echo -e "$file") = $(<"$out") ]] &&
mv "$out" "$out~" &&
Echo "\nRenaming previous loader.conf to '$out~'\n" 0
Echo "\n--- Writing '$out'"
Echo ":\n" 0
echo -e "$file" |tee "$out" >$rd
Echo "\nInstalling boot manager file\n" 0
cp "$loader" "$esp/EFI/loader/loader.efi"
cp "$loader" "$esp/EFI/BOOT/BOOTX64.EFI"
# Install the SecureBoot files
Echo "\n--- Ensuring the presence of SecureBoot files"
if [[ ! -f "$esp/EFI/loader/PreLoader.efi" ]]
then
Echo "\nDownloading SecureBoot:PreLoader.efi" 0
wget -qO "$esp/EFI/loader/PreLoader.efi" "$preloader"
fi
if [[ ! -f "$esp/EFI/loader/HashTool.efi" ]]
then
Echo "\nDownloading SecureBoot:HashTool.efi" 0
wget -qO "$esp/EFI/loader/HashTool.efi" "$hashtool"
fi
else
Echo "\n--- Writing '$out'"
Echo ":\n$file" 0
Echo "\nInstalling boot manager file(s)\n" 0
Echo "\n--- Ensuring the presence of SecureBoot files"
fi
# Making new entries for each kernel binary
shopt -s nullglob extglob
n=0 kernels=$(printf '%s\n' /boot/vmlinuz-* |sort -r)
for kernel in $kernels
do
((++n>maxkernels)) && break
version=${kernel#$imgdir/vmlinuz-}
initrd=$imgdir/initrd.img-$version
entry="$iddir/linux-$version"
((go)) && ! mkdir -p "$entry" &&
Echo "Problem making directory $entry" 11
Echo "\n--- Copying images to $entry"
((go)) && cp "$kernel" "$entry/kernel"
((go)) && cp "$initrd" "$entry/initrd"
out="$entriesdir/$id-linux-$version.conf"
Echo "\n--- Writing '$out'"
Echo ":" 0
file="title $release on $root (linux-$version)\nversion $version\n"
file+="machine-id $id\noptions $cmdline\nlinux /loader/$id/linux-$version/kernel\n"
file+="initrd /loader/$id/linux-$version/initrd"
((go)) && Echo "\n" && echo -e "$file" |tee "$out" >$rd ||
Echo "\n$file\n" 0
done
# Downloading and installing memtest86
if ((memtest))
then
Echo "\nDownloading and extracting MemTest86...\n"
memdir="loader/$id/memtest86"
tmp=$(mktemp -d)
cd "$tmp"
wget -q "$memurl" || Echo "Can't download MemTest86" 12
((go)) && mkdir -p "$esp/$memdir" && rm -rf -- "$esp/$memdir/*"
((go)) &&
! tar xf memtest.tgz --no-same-owner -C "$esp/$memdir" &&
Echo "Can't setup the files for MemTest86" 13
cd - >/dev/null
rm -r -- "$tmp"
out="$entriesdir/$id-memtest86.conf"
Echo "\n--- Writing '$out'"
Echo ":" 0
file="title MemTest86\nversion $memver\nefi $memdir/BOOTX64.EFI"
((go)) && Echo "\n" 0 && echo -e "$file" |tee "$out" >$rd ||
Echo "\n$file\n" 0
fi
# Register the boot manager with UEFI
if ((register))
then
[[ ! -d /sys/firmware/efi/efivars ]] &&
Echo "Not in UEFI mode, no EFI variables, unable to register" 14
Echo "\nRegistering the boot manager with UEFI\n"
ebm="$ebm -d $disk -p $partnum"
bootentries=$($ebm |grep " $label$" |grep -o 'Boot....')
bootentries+=$($ebm |grep " PreLoader$" |grep -o 'Boot....')
Echo "\nLooking at entries: "$bootentries 0
for entry in $bootentries
do
((go)) && $ebm -b ${entry:4:4} -B >$off
Echo "\nUEFI entry '$entry $label' removed" 0
done
echo
shopt -u nullglob
if test -e /sys/firmware/efi/efivars/SecureBoot-* &&
[[ $(od -Anone -b -j4 -N1 /sys/firmware/efi/efivars/SecureBoot-*) = ' 001' ]]
then ((go)) &&
$ebm -c -t $timeout -D -l '\EFI\loader\PreLoader.efi' -L "PreLoader" &&
Echo "\nSuccessfully registered PreLoader\n" 0
else ((go)) &&
$ebm -c -t $timeout -D -l '\EFI\loader\loader.efi' -L "$label" &&
Echo "\nSuccessfully registered $label\n" 0
fi
else
Echo "\nNot registering with UEFI, no -r/--register option given\n" 0
fi
((go)) ||
Echo "\n### End of simulation, nothing actually copied/written/registered\n"
exit 0