]> git.proxmox.com Git - pve-kernel-meta.git/blob - bin/proxmox-boot-tool
079fa266ede6ff38621ea3400f3f1506fb98fb3b
[pve-kernel-meta.git] / bin / proxmox-boot-tool
1 #!/bin/sh
2
3 set -e
4
5 . /usr/share/pve-kernel-helper/scripts/functions
6
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 }
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
99 part_basename=$(basename "$bdev")
100 if [ -z "$part_basename" ]; then
101 if [ $part != $bdev ]; then
102 symlinkmsg=" -> '$bdev'"
103 fi
104 warn "E: unable to determine basename of '$part'$symlinkmsg"
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
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.."
164 grub-install.real \
165 --boot-directory $esp_mp \
166 --target i386-pc \
167 --no-floppy \
168 --bootloader-id='proxmox' \
169 "/dev/$PKNAME"
170 fi
171 echo "Unmounting '$part'."
172 umount "$part"
173
174 echo "Adding '$part' to list of synced ESPs.."
175 _add_entry_to_list_file "$ESP_LIST" "$UUID"
176
177 echo "Refreshing kernels and initrds.."
178 refresh
179 }
180
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
208
209 echo "Sorting and removing duplicate ESPs.."
210 sort -uo "$ESP_LIST".tmp "$ESP_LIST"
211 mv "$ESP_LIST".tmp "$ESP_LIST"
212 }
213
214 refresh() {
215 hook=$1
216 hookscripts='proxmox-auto-removal zz-proxmox-boot'
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
225 fi
226
227 for script in $hookscripts; do
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
235 done
236 }
237
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"
252 echo "Added kernel '$ver' to manual kernel list. Use the 'refresh' command to update the ESPs."
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"
266 echo "Removed kernel '$ver' from manual kernel list. Use the 'refresh' command to update the ESPs."
267 else
268 echo "Kernel '$ver' not found in manual kernel list."
269 fi
270 }
271
272 list_kernels() {
273 boot_kernels="$(boot_kernel_list)"
274
275 if [ -e "$MANUAL_KERNEL_LIST" ]; then
276 manual_kernels="$(cat "$MANUAL_KERNEL_LIST" || true)"
277 boot_kernels="$(echo "$boot_kernels" | grep -Fxv -f "$MANUAL_KERNEL_LIST" || true)"
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
291 usage() {
292 warn "USAGE: $0 <commands> [ARGS]"
293 warn ""
294 warn " $0 format <partition> [--force]"
295 warn " $0 init <partition>"
296 warn " $0 clean [--dry-run]"
297 warn " $0 refresh [--hook <name>]"
298 warn " $0 kernel <add|remove> <kernel-version>"
299 warn " $0 kernel list"
300 warn " $0 status [--quiet]"
301 warn " $0 help"
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 ""
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 ""
317 echo "USAGE: $0 refresh [--hook <name>]"
318 echo ""
319 echo " refresh all configured EFI system partitions. Use --hook to only run the specified hook, omit to run all."
320 echo ""
321 echo "USAGE: $0 kernel <add|remove> <kernel-version>"
322 echo ""
323 echo " add/remove pve-kernel with ABI <kernel-version> to list of synced kernels, in addition to automatically selected ones."
324 echo " NOTE: you need to manually run 'refresh' once you're finished with adding/removing kernels from the list"
325 echo ""
326 echo "USAGE: $0 kernel list"
327 echo ""
328 echo " list kernel versions currently selected for inclusion on ESPs."
329 echo ""
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
356 result="uefi"
357 if [ ! -d "${mountpoint}/$PMX_ESP_DIR" ]; then
358 warn "${path}/$PMX_ESP_DIR does not exist"
359 fi
360 fi
361 if [ -d "${mountpoint}/grub" ]; then
362 if [ -n "$result" ]; then
363 result="${result},grub"
364 else
365 result="grub"
366 fi
367 fi
368 echo "$curr_uuid is configured with: $result"
369 umount "${mountpoint}" || \
370 { warn "umount of ${path} failed - failure"; exit 0; }
371
372 rmdir "${mountpoint}" || true
373 }
374
375 status() {
376 quiet="$1"
377 if [ ! -e "${ESP_LIST}" ]; then
378 if [ -z "$quiet" ]; then
379 warn "E: $ESP_LIST does not exist."
380 fi
381 exit 2
382 fi
383 if [ -z "$quiet" ]; then
384 loop_esp_list _status_detail
385 fi
386 }
387
388 if [ -z "$1" ]; then
389 usage
390 exit 0
391 fi
392
393 case "$1" in
394 'format')
395 shift
396 if [ -z "$1" ]; then
397 warn "E: <partition> is mandatory."
398 warn ""
399 usage
400 exit 1
401 fi
402 format "$@"
403 exit 0
404 ;;
405 'init')
406 reexec_in_mountns "$@"
407 shift
408 if [ -z "$1" ]; then
409 warn "E: <partition> is mandatory."
410 warn ""
411 usage
412 exit 1
413 fi
414 init "$@"
415 exit 0
416 ;;
417 'clean')
418 shift
419 clean "$@"
420 exit 0
421 ;;
422 'refresh')
423 shift
424 if [ "$#" -eq 0 ]; then
425 refresh
426 elif [ "$#" -eq 2 ] && [ "$1" = "--hook" ]; then
427 refresh "$2"
428 else
429 usage
430 exit 1
431 fi
432 exit 0
433 ;;
434 'kernel'|'kernels')
435 shift
436 if [ -z "$1" ]; then
437 warn "E: subcommand is mandatory for 'kernel'."
438 warn ""
439 usage
440 exit 1
441 fi
442 cmd="$1"
443 case "$cmd" in
444 'add')
445 add_kernel "$2"
446 exit 0
447 ;;
448 'remove')
449 remove_kernel "$2"
450 exit 0
451 ;;
452 'list')
453 list_kernels
454 exit 0
455 ;;
456 *)
457 warn "E: invalid 'kernel' subcommand '$cmd'."
458 warn ""
459 usage
460 exit 1
461 ;;
462 esac
463 ;;
464 'status')
465 if [ "$#" -eq 2 ] && [ "$2" = '--quiet' ]; then
466 shift
467 status "$1"
468 elif [ "$#" -eq 1 ]; then
469 reexec_in_mountns "$@"
470 shift
471 status
472 else
473 usage
474 exit 1
475 fi
476 exit 0
477 ;;
478 'help')
479 shift
480 help
481 exit 0
482 ;;
483 *)
484 warn "Invalid/unknown command '$1'."
485 warn ""
486 usage
487 exit 1
488 ;;
489 esac
490
491 exit 1