]>
Commit | Line | Data |
---|---|---|
b3d47d2d SI |
1 | #!/bin/sh |
2 | ||
3 | set -e | |
4 | ||
5 | . /usr/share/pve-kernel-helper/scripts/functions | |
6 | ||
2955b2b7 FG |
7 | _add_entry_to_list_file() { |
8 | file="$1" | |
9 | entry="$2" | |
10 | ||
11 | if [ -e "$file" ]; then | |
12 | cp "$file" "$file.new" | |
13 | fi | |
14 | echo "$entry" >> "$file.new" | |
15 | sort -uo "$file.new" "$file.new" | |
16 | mv "$file.new" "$file" | |
17 | } | |
18 | ||
19 | _remove_entry_from_list_file() { | |
20 | file="$1" | |
21 | entry="$2" | |
22 | ||
23 | # guard against removing whole file by accident! | |
24 | if [ -z "$entry" ]; then | |
25 | echo "cannot remove empty entry from '$file'." | |
26 | return | |
27 | fi | |
28 | ||
29 | if [ -e "$file" ]; then | |
30 | grep -vFx "$entry" "$file" > "$file.new" || true | |
31 | mv "$file.new" "$file" | |
32 | else | |
33 | echo "'$file' does not exist.." | |
34 | fi | |
35 | } | |
b3d47d2d SI |
36 | |
37 | _get_partition_info() { | |
38 | if [ ! -e "$1" ]; then | |
39 | warn "E: '$1' does not exist!" | |
40 | exit 1 | |
41 | fi | |
42 | bdev=$(realpath "$1") | |
43 | if [ ! -b "$bdev" ]; then | |
44 | warn "E: '$bdev' is not a block device!" | |
45 | exit 1 | |
46 | fi | |
47 | ||
48 | bdev_info=$( \ | |
49 | lsblk \ | |
50 | --bytes \ | |
51 | --pairs \ | |
52 | -o 'UUID,SIZE,FSTYPE,PARTTYPE,PKNAME,MOUNTPOINT' \ | |
53 | "$bdev" \ | |
54 | ) | |
55 | if [ -z "$bdev_info" ]; then | |
56 | warn "E: unable to get information about block device '$1'!" | |
57 | exit 1 | |
58 | fi | |
59 | ||
60 | count=$(echo "$bdev_info" | grep -c '^') | |
61 | if [ "$count" -ne '1' ]; then | |
62 | echo "$bdev_info" | |
63 | warn "E: block device '$1' has children!" | |
64 | exit 1 | |
65 | fi | |
66 | ||
67 | echo "$bdev_info" | |
68 | eval "$bdev_info" | |
69 | ||
70 | if [ -z "$PKNAME" ]; then | |
71 | warn "E: cannot determine parent device of '$1' - please provide a partition, not a full disk." | |
72 | exit 1 | |
73 | fi | |
74 | ||
75 | if [ -n "$SIZE" ] && [ "$SIZE" -lt 268435456 ]; then | |
76 | warn "E: '$1' is too small (<256M)." | |
77 | exit 1 | |
78 | fi | |
79 | ||
80 | if [ -n "$MOUNTPOINT" ]; then | |
81 | warn "E: '$1' is mounted on '$MOUNTPOINT' - exiting." | |
82 | exit 1 | |
83 | fi | |
84 | } | |
85 | ||
86 | format() { | |
87 | part="$1" | |
88 | force="$2" | |
89 | ||
90 | _get_partition_info "$part" | |
91 | ||
92 | if [ -n "$FSTYPE" ]; then | |
93 | if [ -z "$force" ] || [ "$force" != '--force' ]; then | |
94 | warn "E: '$part' contains a filesystem ('$FSTYPE') - exiting (use --force to override)" | |
95 | exit 1 | |
96 | fi | |
97 | fi | |
98 | ||
5d733131 | 99 | part_basename=$(basename "$bdev") |
b3d47d2d | 100 | if [ -z "$part_basename" ]; then |
78c0b9cb AL |
101 | if [ $part != $bdev ]; then |
102 | symlinkmsg=" -> '$bdev'" | |
103 | fi | |
104 | warn "E: unable to determine basename of '$part'$symlinkmsg" | |
b3d47d2d SI |
105 | exit 1 |
106 | fi | |
107 | ||
108 | part_num=$(cat /sys/block/"$PKNAME"/"$part_basename"/partition) | |
109 | if [ -z "$part_num" ]; then | |
110 | warn "E: unable to determine partition number of '$part'" | |
111 | exit 1 | |
112 | fi | |
113 | ||
114 | if [ -z "$PARTTYPE" ] || [ "$PARTTYPE" != "$ESPTYPE" ]; then | |
115 | echo "Setting partition type of '$part' to '$ESPTYPE'.." | |
116 | sgdisk "-t$part_num:$ESPTYPE" "/dev/$PKNAME" | |
117 | echo "Calling 'udevadm settle'.." | |
118 | udevadm settle --timeout=5 | |
119 | fi | |
120 | ||
121 | echo "Formatting '$part' as vfat.." | |
122 | mkfs.vfat -F 32 "$part" | |
123 | echo "Done." | |
124 | exit 0 | |
125 | } | |
126 | ||
127 | init() { | |
128 | part="$1" | |
129 | ||
130 | _get_partition_info "$part" | |
131 | ||
132 | if [ -z "$PARTTYPE" ] || [ "$PARTTYPE" != "$ESPTYPE" ]; then | |
133 | warn "E: '$part' has wrong partition type (!= $ESPTYPE)." | |
134 | exit 1 | |
135 | fi | |
136 | ||
137 | if [ -z "$FSTYPE" ] || [ "$FSTYPE" != 'vfat' ]; then | |
138 | warn "E: '$part' has wrong filesystem (!= vfat)." | |
139 | exit 1 | |
140 | fi | |
141 | ||
142 | if [ -z "$UUID" ]; then | |
143 | warn "E: '$part' has no UUID set, required for mounting." | |
144 | exit 1 | |
145 | fi | |
146 | ||
147 | esp_mp="/var/tmp/espmounts/$UUID" | |
148 | ||
149 | mkdir -p "$esp_mp" | |
150 | echo "Mounting '$part' on '$esp_mp'." | |
151 | mount -t vfat "$part" "$esp_mp" | |
152 | ||
593001de SI |
153 | if [ -d /sys/firmware/efi ]; then |
154 | echo "Installing systemd-boot.." | |
155 | mkdir -p "$esp_mp/$PMX_ESP_DIR" | |
156 | bootctl --path "$esp_mp" install | |
157 | ||
158 | echo "Configuring systemd-boot.." | |
159 | echo "timeout 3" > "$esp_mp/$PMX_LOADER_CONF.tmp" | |
160 | echo "default proxmox-*" >> "$esp_mp/$PMX_LOADER_CONF.tmp" | |
161 | mv "$esp_mp/$PMX_LOADER_CONF.tmp" "$esp_mp/$PMX_LOADER_CONF" | |
162 | else | |
163 | echo "Installing grub i386-pc target.." | |
72905061 | 164 | grub-install.real \ |
593001de SI |
165 | --boot-directory $esp_mp \ |
166 | --target i386-pc \ | |
167 | --no-floppy \ | |
168 | --bootloader-id='proxmox' \ | |
169 | "/dev/$PKNAME" | |
170 | fi | |
b3d47d2d SI |
171 | echo "Unmounting '$part'." |
172 | umount "$part" | |
173 | ||
174 | echo "Adding '$part' to list of synced ESPs.." | |
2955b2b7 | 175 | _add_entry_to_list_file "$ESP_LIST" "$UUID" |
b3d47d2d SI |
176 | |
177 | echo "Refreshing kernels and initrds.." | |
178 | refresh | |
179 | } | |
180 | ||
0956bd22 FG |
181 | _clean_impl() { |
182 | if [ ! -e "/dev/disk/by-uuid/" ]; then | |
183 | warn 'E: /dev/disk/by-uuid does not exist, aborting!' | |
184 | exit 1 | |
185 | fi | |
186 | echo -n "Checking whether ESP '$curr_uuid' exists.. " | |
187 | if [ -e "/dev/disk/by-uuid/$curr_uuid" ]; then | |
188 | echo "Found!" | |
189 | else | |
190 | echo "Not found!" | |
191 | if [ -z "$dry_run" ] || [ "$dry_run" != '--dry-run' ]; then | |
192 | _remove_entry_from_list_file "$ESP_LIST" "$curr_uuid" | |
193 | fi | |
194 | fi | |
195 | } | |
196 | ||
197 | clean() { | |
198 | dry_run="$1" | |
199 | rm -f "$ESP_LIST".tmp | |
200 | loop_esp_list _clean_impl | |
201 | if [ "$?" -eq 2 ]; then | |
202 | warn "E: $ESP_LIST does not exist." | |
203 | exit 1 | |
204 | fi | |
205 | if [ -e "$ESP_LIST".tmp ]; then | |
206 | mv "$ESP_LIST".tmp "$ESP_LIST" | |
207 | fi | |
33f32d0a SI |
208 | |
209 | echo "Sorting and removing duplicate ESPs.." | |
210 | sort -uo "$ESP_LIST".tmp "$ESP_LIST" | |
211 | mv "$ESP_LIST".tmp "$ESP_LIST" | |
0956bd22 FG |
212 | } |
213 | ||
b3d47d2d | 214 | refresh() { |
27d93251 | 215 | hook=$1 |
b3c98062 | 216 | hookscripts='proxmox-auto-removal zz-proxmox-boot' |
27d93251 FG |
217 | |
218 | if [ -n "$hook" ]; then | |
219 | if echo "$hookscripts" | grep -sqE "(^|[[:space:]]+)$hook([[:space:]]+|$)"; then | |
220 | hookscripts="$hook" | |
221 | else | |
222 | warn "E: '$hook' is not a valid hook script name."; | |
223 | exit 1; | |
224 | fi | |
6e829c9b SR |
225 | fi |
226 | ||
c2c22297 | 227 | for script in $hookscripts; do |
6e829c9b SR |
228 | scriptpath="/etc/kernel/postinst.d/$script" |
229 | if [ -f "$scriptpath" ] && [ -x "$scriptpath" ]; then | |
230 | echo "Running hook script '$script'.." | |
231 | $scriptpath | |
232 | else | |
233 | warn "Hook script '$script' not found or not executable, skipping." | |
234 | fi | |
c2c22297 | 235 | done |
b3d47d2d SI |
236 | } |
237 | ||
427fba71 FG |
238 | add_kernel() { |
239 | ver="$1" | |
240 | ||
241 | if [ -z "$ver" ]; then | |
242 | warn "E: <kernel-version> is mandatory" | |
243 | warn "" | |
244 | exit 1 | |
245 | fi | |
246 | ||
247 | if [ ! -e "/boot/vmlinuz-$ver" ]; then | |
248 | warn "E: no kernel image found in /boot for '$ver', not adding." | |
249 | exit 1 | |
250 | fi | |
251 | _add_entry_to_list_file "$MANUAL_KERNEL_LIST" "$ver" | |
d0519968 | 252 | echo "Added kernel '$ver' to manual kernel list. Use the 'refresh' command to update the ESPs." |
427fba71 FG |
253 | } |
254 | ||
255 | remove_kernel() { | |
256 | ver="$1" | |
257 | ||
258 | if [ -z "$ver" ]; then | |
259 | warn "E: <kernel-version> is mandatory" | |
260 | warn "" | |
261 | exit 1 | |
262 | fi | |
263 | ||
264 | if grep -sqFx "$ver" "$MANUAL_KERNEL_LIST"; then | |
265 | _remove_entry_from_list_file "$MANUAL_KERNEL_LIST" "$ver" | |
d0519968 | 266 | echo "Removed kernel '$ver' from manual kernel list. Use the 'refresh' command to update the ESPs." |
427fba71 FG |
267 | else |
268 | echo "Kernel '$ver' not found in manual kernel list." | |
269 | fi | |
270 | } | |
271 | ||
2d7389fb FG |
272 | list_kernels() { |
273 | boot_kernels="$(boot_kernel_list)" | |
274 | ||
275 | if [ -e "$MANUAL_KERNEL_LIST" ]; then | |
6a3c4ace | 276 | manual_kernels="$(cat "$MANUAL_KERNEL_LIST" || true)" |
4f11dd63 | 277 | boot_kernels="$(echo "$boot_kernels" | grep -Fxv -f "$MANUAL_KERNEL_LIST" || true)" |
2d7389fb FG |
278 | fi |
279 | ||
280 | if [ -z "$manual_kernels" ]; then | |
281 | manual_kernels="None." | |
282 | fi | |
283 | ||
284 | echo "Manually selected kernels:" | |
285 | echo "$manual_kernels" | |
286 | echo "" | |
287 | echo "Automatically selected kernels:" | |
288 | echo "$boot_kernels" | |
289 | } | |
290 | ||
b3d47d2d SI |
291 | usage() { |
292 | warn "USAGE: $0 <commands> [ARGS]" | |
293 | warn "" | |
294 | warn " $0 format <partition> [--force]" | |
295 | warn " $0 init <partition>" | |
0956bd22 | 296 | warn " $0 clean [--dry-run]" |
6e829c9b | 297 | warn " $0 refresh [--hook <name>]" |
992c689e TL |
298 | warn " $0 kernel <add|remove> <kernel-version>" |
299 | warn " $0 kernel list" | |
182bfa3a | 300 | warn " $0 status [--quiet]" |
0b99d576 | 301 | warn " $0 help" |
b3d47d2d SI |
302 | } |
303 | ||
304 | help() { | |
305 | echo "USAGE: $0 format <partition> [--force]" | |
306 | echo "" | |
307 | echo " format <partition> as EFI system partition. Use --force to format even if <partition> is currently in use." | |
308 | echo "" | |
309 | echo "USAGE: $0 init <partition>" | |
310 | echo "" | |
311 | echo " initialize EFI system partition at <partition> for automatic synchronization of pve-kernels and their associated initrds." | |
312 | echo "" | |
0956bd22 FG |
313 | echo "USAGE: $0 clean [--dry-run]" |
314 | echo "" | |
315 | echo " remove no longer existing EFI system partition UUIDs from $ESP_LIST. Use --dry-run to only print outdated entries instead of removing them." | |
316 | echo "" | |
6e829c9b | 317 | echo "USAGE: $0 refresh [--hook <name>]" |
b3d47d2d | 318 | echo "" |
6e829c9b | 319 | echo " refresh all configured EFI system partitions. Use --hook to only run the specified hook, omit to run all." |
b3d47d2d | 320 | echo "" |
992c689e | 321 | echo "USAGE: $0 kernel <add|remove> <kernel-version>" |
427fba71 FG |
322 | echo "" |
323 | echo " add/remove pve-kernel with ABI <kernel-version> to list of synced kernels, in addition to automatically selected ones." | |
d0519968 | 324 | echo " NOTE: you need to manually run 'refresh' once you're finished with adding/removing kernels from the list" |
427fba71 | 325 | echo "" |
992c689e | 326 | echo "USAGE: $0 kernel list" |
2d7389fb FG |
327 | echo "" |
328 | echo " list kernel versions currently selected for inclusion on ESPs." | |
329 | echo "" | |
182bfa3a SI |
330 | echo "USAGE: $0 status [--quiet]" |
331 | echo "" | |
332 | echo " Print details about the ESPs configuration. Exits with 0 if any ESP is configured, else with 2." | |
333 | echo "" | |
334 | } | |
335 | ||
336 | _status_detail() { | |
337 | if ! (echo "${curr_uuid}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}'); then | |
338 | warn "WARN: ${curr_uuid} read from ${ESP_LIST} does not look like a VFAT-UUID - skipping" | |
339 | return | |
340 | fi | |
341 | ||
342 | path="/dev/disk/by-uuid/$curr_uuid" | |
343 | if [ ! -e "${path}" ]; then | |
344 | warn "WARN: ${path} does not exist - clean '${ESP_LIST}'! - skipping" | |
345 | return | |
346 | fi | |
347 | ||
348 | mountpoint="${MOUNTROOT}/${curr_uuid}" | |
349 | mkdir -p "${mountpoint}" || \ | |
350 | { warn "creation of mountpoint ${mountpoint} failed - skipping"; return; } | |
351 | mount "${path}" "${mountpoint}" || \ | |
352 | { warn "mount of ${path} failed - skipping"; return; } | |
353 | ||
354 | result="" | |
355 | if [ -f "${mountpoint}/$PMX_LOADER_CONF" ]; then | |
182bfa3a SI |
356 | if [ ! -d "${mountpoint}/$PMX_ESP_DIR" ]; then |
357 | warn "${path}/$PMX_ESP_DIR does not exist" | |
358 | fi | |
25c7338c SI |
359 | versions_uefi=$(ls -1 ${mountpoint}/$PMX_ESP_DIR | awk '{printf (NR>1?", ":"") $0}') |
360 | result="uefi (versions: ${versions_uefi})" | |
182bfa3a SI |
361 | fi |
362 | if [ -d "${mountpoint}/grub" ]; then | |
25c7338c | 363 | versions_grub=$(ls -1 ${mountpoint}/vmlinuz-* | awk '{ gsub(/.*\/vmlinuz-/, ""); printf (NR>1?", ":"") $0 }') |
182bfa3a | 364 | if [ -n "$result" ]; then |
25c7338c | 365 | result="${result}, grub (versions: ${versions_grub})" |
182bfa3a | 366 | else |
25c7338c | 367 | result="grub (versions: ${versions_grub})" |
182bfa3a SI |
368 | fi |
369 | fi | |
370 | echo "$curr_uuid is configured with: $result" | |
371 | umount "${mountpoint}" || \ | |
372 | { warn "umount of ${path} failed - failure"; exit 0; } | |
373 | ||
374 | rmdir "${mountpoint}" || true | |
375 | } | |
376 | ||
377 | status() { | |
378 | quiet="$1" | |
379 | if [ ! -e "${ESP_LIST}" ]; then | |
380 | if [ -z "$quiet" ]; then | |
381 | warn "E: $ESP_LIST does not exist." | |
382 | fi | |
383 | exit 2 | |
384 | fi | |
385 | if [ -z "$quiet" ]; then | |
8f256d27 SI |
386 | if [ -d /sys/firmware/efi ]; then |
387 | echo "System currently booted with uefi" | |
388 | else | |
389 | echo "System currently booted with legacy bios" | |
390 | fi | |
182bfa3a SI |
391 | loop_esp_list _status_detail |
392 | fi | |
b3d47d2d SI |
393 | } |
394 | ||
395 | if [ -z "$1" ]; then | |
396 | usage | |
397 | exit 0 | |
398 | fi | |
399 | ||
400 | case "$1" in | |
401 | 'format') | |
402 | shift | |
403 | if [ -z "$1" ]; then | |
404 | warn "E: <partition> is mandatory." | |
405 | warn "" | |
406 | usage | |
407 | exit 1 | |
408 | fi | |
409 | format "$@" | |
410 | exit 0 | |
411 | ;; | |
412 | 'init') | |
420039cd | 413 | reexec_in_mountns "$@" |
b3d47d2d SI |
414 | shift |
415 | if [ -z "$1" ]; then | |
416 | warn "E: <partition> is mandatory." | |
417 | warn "" | |
418 | usage | |
419 | exit 1 | |
420 | fi | |
421 | init "$@" | |
422 | exit 0 | |
423 | ;; | |
0956bd22 FG |
424 | 'clean') |
425 | shift | |
426 | clean "$@" | |
427 | exit 0 | |
428 | ;; | |
b3d47d2d SI |
429 | 'refresh') |
430 | shift | |
6e829c9b SR |
431 | if [ "$#" -eq 0 ]; then |
432 | refresh | |
433 | elif [ "$#" -eq 2 ] && [ "$1" = "--hook" ]; then | |
434 | refresh "$2" | |
435 | else | |
436 | usage | |
437 | exit 1 | |
438 | fi | |
b3d47d2d SI |
439 | exit 0 |
440 | ;; | |
992c689e | 441 | 'kernel'|'kernels') |
427fba71 FG |
442 | shift |
443 | if [ -z "$1" ]; then | |
992c689e | 444 | warn "E: subcommand is mandatory for 'kernel'." |
427fba71 FG |
445 | warn "" |
446 | usage | |
447 | exit 1 | |
448 | fi | |
449 | cmd="$1" | |
450 | case "$cmd" in | |
451 | 'add') | |
452 | add_kernel "$2" | |
453 | exit 0 | |
454 | ;; | |
455 | 'remove') | |
456 | remove_kernel "$2" | |
457 | exit 0 | |
458 | ;; | |
2d7389fb FG |
459 | 'list') |
460 | list_kernels | |
461 | exit 0 | |
462 | ;; | |
427fba71 | 463 | *) |
992c689e | 464 | warn "E: invalid 'kernel' subcommand '$cmd'." |
427fba71 FG |
465 | warn "" |
466 | usage | |
467 | exit 1 | |
468 | ;; | |
469 | esac | |
470 | ;; | |
182bfa3a SI |
471 | 'status') |
472 | if [ "$#" -eq 2 ] && [ "$2" = '--quiet' ]; then | |
473 | shift | |
474 | status "$1" | |
475 | elif [ "$#" -eq 1 ]; then | |
476 | reexec_in_mountns "$@" | |
477 | shift | |
478 | status | |
479 | else | |
480 | usage | |
481 | exit 1 | |
482 | fi | |
483 | exit 0 | |
484 | ;; | |
b3d47d2d SI |
485 | 'help') |
486 | shift | |
487 | help | |
488 | exit 0 | |
489 | ;; | |
490 | *) | |
491 | warn "Invalid/unknown command '$1'." | |
492 | warn "" | |
493 | usage | |
494 | exit 1 | |
495 | ;; | |
496 | esac | |
497 | ||
498 | exit 1 |