]> git.proxmox.com Git - pve-kernel-meta.git/blob - bin/pve-efiboot-tool
Add parameter '--hook' to 'refresh' command
[pve-kernel-meta.git] / bin / pve-efiboot-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 "$part")
100 if [ -z "$part_basename" ]; then
101 warn "E: unable to determine basename of '$part'"
102 exit 1
103 fi
104
105 part_num=$(cat /sys/block/"$PKNAME"/"$part_basename"/partition)
106 if [ -z "$part_num" ]; then
107 warn "E: unable to determine partition number of '$part'"
108 exit 1
109 fi
110
111 if [ -z "$PARTTYPE" ] || [ "$PARTTYPE" != "$ESPTYPE" ]; then
112 echo "Setting partition type of '$part' to '$ESPTYPE'.."
113 sgdisk "-t$part_num:$ESPTYPE" "/dev/$PKNAME"
114 echo "Calling 'udevadm settle'.."
115 udevadm settle --timeout=5
116 fi
117
118 echo "Formatting '$part' as vfat.."
119 mkfs.vfat -F 32 "$part"
120 echo "Done."
121 exit 0
122 }
123
124 init() {
125 part="$1"
126
127 _get_partition_info "$part"
128
129 if [ -z "$PARTTYPE" ] || [ "$PARTTYPE" != "$ESPTYPE" ]; then
130 warn "E: '$part' has wrong partition type (!= $ESPTYPE)."
131 exit 1
132 fi
133
134 if [ -z "$FSTYPE" ] || [ "$FSTYPE" != 'vfat' ]; then
135 warn "E: '$part' has wrong filesystem (!= vfat)."
136 exit 1
137 fi
138
139 if [ -z "$UUID" ]; then
140 warn "E: '$part' has no UUID set, required for mounting."
141 exit 1
142 fi
143
144 esp_mp="/var/tmp/espmounts/$UUID"
145
146 mkdir -p "$esp_mp"
147 echo "Mounting '$part' on '$esp_mp'."
148 mount -t vfat "$part" "$esp_mp"
149
150 echo "Installing systemd-boot.."
151 mkdir -p "$esp_mp/$PMX_ESP_DIR"
152 bootctl --path "$esp_mp" install
153
154 echo "Configuring systemd-boot.."
155 echo "timeout 3" > "$esp_mp/$PMX_LOADER_CONF.tmp"
156 echo "default proxmox-*" >> "$esp_mp/$PMX_LOADER_CONF.tmp"
157 mv "$esp_mp/$PMX_LOADER_CONF.tmp" "$esp_mp/$PMX_LOADER_CONF"
158 echo "Unmounting '$part'."
159 umount "$part"
160
161 echo "Adding '$part' to list of synced ESPs.."
162 _add_entry_to_list_file "$ESP_LIST" "$UUID"
163
164 echo "Refreshing kernels and initrds.."
165 refresh
166 }
167
168 _clean_impl() {
169 if [ ! -e "/dev/disk/by-uuid/" ]; then
170 warn 'E: /dev/disk/by-uuid does not exist, aborting!'
171 exit 1
172 fi
173 echo -n "Checking whether ESP '$curr_uuid' exists.. "
174 if [ -e "/dev/disk/by-uuid/$curr_uuid" ]; then
175 echo "Found!"
176 else
177 echo "Not found!"
178 if [ -z "$dry_run" ] || [ "$dry_run" != '--dry-run' ]; then
179 _remove_entry_from_list_file "$ESP_LIST" "$curr_uuid"
180 fi
181 fi
182 }
183
184 clean() {
185 dry_run="$1"
186 rm -f "$ESP_LIST".tmp
187 loop_esp_list _clean_impl
188 if [ "$?" -eq 2 ]; then
189 warn "E: $ESP_LIST does not exist."
190 exit 1
191 fi
192 if [ -e "$ESP_LIST".tmp ]; then
193 mv "$ESP_LIST".tmp "$ESP_LIST"
194 fi
195 }
196
197 refresh() {
198 hookscripts="$1"
199 if [ -z "$hookscripts" ]; then
200 hookscripts='pve-auto-removal zz-pve-efiboot'
201 fi
202
203 for script in $hookscripts; do
204 scriptpath="/etc/kernel/postinst.d/$script"
205 if [ -f "$scriptpath" ] && [ -x "$scriptpath" ]; then
206 echo "Running hook script '$script'.."
207 $scriptpath
208 else
209 warn "Hook script '$script' not found or not executable, skipping."
210 fi
211 done
212 }
213
214 add_kernel() {
215 ver="$1"
216
217 if [ -z "$ver" ]; then
218 warn "E: <kernel-version> is mandatory"
219 warn ""
220 exit 1
221 fi
222
223 if [ ! -e "/boot/vmlinuz-$ver" ]; then
224 warn "E: no kernel image found in /boot for '$ver', not adding."
225 exit 1
226 fi
227 _add_entry_to_list_file "$MANUAL_KERNEL_LIST" "$ver"
228 echo "Added kernel '$ver' to manual kernel list. Use the 'refresh' command to update the ESPs."
229 }
230
231 remove_kernel() {
232 ver="$1"
233
234 if [ -z "$ver" ]; then
235 warn "E: <kernel-version> is mandatory"
236 warn ""
237 exit 1
238 fi
239
240 if grep -sqFx "$ver" "$MANUAL_KERNEL_LIST"; then
241 _remove_entry_from_list_file "$MANUAL_KERNEL_LIST" "$ver"
242 echo "Removed kernel '$ver' from manual kernel list. Use the 'refresh' command to update the ESPs."
243 else
244 echo "Kernel '$ver' not found in manual kernel list."
245 fi
246 }
247
248 list_kernels() {
249 boot_kernels="$(boot_kernel_list)"
250
251 if [ -e "$MANUAL_KERNEL_LIST" ]; then
252 manual_kernels="$(echo "$boot_kernels" | grep -Fx -f "$MANUAL_KERNEL_LIST" || true)"
253 boot_kernels="$(echo "$boot_kernels" | grep -Fxv -f "$MANUAL_KERNEL_LIST" || true)"
254 fi
255
256 if [ -z "$manual_kernels" ]; then
257 manual_kernels="None."
258 fi
259
260 echo "Manually selected kernels:"
261 echo "$manual_kernels"
262 echo ""
263 echo "Automatically selected kernels:"
264 echo "$boot_kernels"
265 }
266
267 usage() {
268 warn "USAGE: $0 <commands> [ARGS]"
269 warn ""
270 warn " $0 format <partition> [--force]"
271 warn " $0 init <partition>"
272 warn " $0 clean [--dry-run]"
273 warn " $0 refresh [--hook <name>]"
274 warn " $0 kernel <add|remove> <kernel-version>"
275 warn " $0 kernel list"
276 warn " $0 help"
277 }
278
279 help() {
280 echo "USAGE: $0 format <partition> [--force]"
281 echo ""
282 echo " format <partition> as EFI system partition. Use --force to format even if <partition> is currently in use."
283 echo ""
284 echo "USAGE: $0 init <partition>"
285 echo ""
286 echo " initialize EFI system partition at <partition> for automatic synchronization of pve-kernels and their associated initrds."
287 echo ""
288 echo "USAGE: $0 clean [--dry-run]"
289 echo ""
290 echo " remove no longer existing EFI system partition UUIDs from $ESP_LIST. Use --dry-run to only print outdated entries instead of removing them."
291 echo ""
292 echo "USAGE: $0 refresh [--hook <name>]"
293 echo ""
294 echo " refresh all configured EFI system partitions. Use --hook to only run the specified hook, omit to run all."
295 echo ""
296 echo "USAGE: $0 kernel <add|remove> <kernel-version>"
297 echo ""
298 echo " add/remove pve-kernel with ABI <kernel-version> to list of synced kernels, in addition to automatically selected ones."
299 echo " NOTE: you need to manually run 'refresh' once you're finished with adding/removing kernels from the list"
300 echo ""
301 echo "USAGE: $0 kernel list"
302 echo ""
303 echo " list kernel versions currently selected for inclusion on ESPs."
304 echo ""
305 }
306
307 if [ -z "$1" ]; then
308 usage
309 exit 0
310 fi
311
312 case "$1" in
313 'format')
314 shift
315 if [ -z "$1" ]; then
316 warn "E: <partition> is mandatory."
317 warn ""
318 usage
319 exit 1
320 fi
321 format "$@"
322 exit 0
323 ;;
324 'init')
325 reexec_in_mountns "$@"
326 shift
327 if [ -z "$1" ]; then
328 warn "E: <partition> is mandatory."
329 warn ""
330 usage
331 exit 1
332 fi
333 init "$@"
334 exit 0
335 ;;
336 'clean')
337 shift
338 clean "$@"
339 exit 0
340 ;;
341 'refresh')
342 shift
343 if [ "$#" -eq 0 ]; then
344 refresh
345 elif [ "$#" -eq 2 ] && [ "$1" = "--hook" ]; then
346 refresh "$2"
347 else
348 usage
349 exit 1
350 fi
351 exit 0
352 ;;
353 'kernel'|'kernels')
354 shift
355 if [ -z "$1" ]; then
356 warn "E: subcommand is mandatory for 'kernel'."
357 warn ""
358 usage
359 exit 1
360 fi
361 cmd="$1"
362 case "$cmd" in
363 'add')
364 add_kernel "$2"
365 exit 0
366 ;;
367 'remove')
368 remove_kernel "$2"
369 exit 0
370 ;;
371 'list')
372 list_kernels
373 exit 0
374 ;;
375 *)
376 warn "E: invalid 'kernel' subcommand '$cmd'."
377 warn ""
378 usage
379 exit 1
380 ;;
381 esac
382 ;;
383 'help')
384 shift
385 help
386 exit 0
387 ;;
388 *)
389 warn "Invalid/unknown command '$1'."
390 warn ""
391 usage
392 exit 1
393 ;;
394 esac
395
396 exit 1