summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2021-02-25 16:01:35 -0500
committerAndrew Cady <d@jerkface.net>2021-02-25 16:01:35 -0500
commit61a047b623e86334546e510717dd68cb482f932b (patch)
tree77510dd771ef8472b2e88d0f7df938d139b86c90
parent232e75824980bd2b92c9a2add7029866fdb5da41 (diff)
partvi: fixes related to key-only mode
fixed race condition in kpartx fix for sfdisk kernel reload ptable failure cleaned up some code duplication created make keymu target amended make clean target
-rw-r--r--partitions/Makefile13
-rwxr-xr-xsrc/partvi109
2 files changed, 66 insertions, 56 deletions
diff --git a/partitions/Makefile b/partitions/Makefile
index a1a7fea..f8ef259 100644
--- a/partitions/Makefile
+++ b/partitions/Makefile
@@ -9,17 +9,22 @@ whole.img:
9key.img: 9key.img:
10 partvi key 10 partvi key
11 11
12.PHONY: usb emu key 12.PHONY: usb emu key emu-key keymu
13usb: whole.img 13usb: whole.img
14 usb=$$(usb) && sudo dd status=progress if="$<" of="$$usb" 14 usb=$$(usb) && sudo dd status=progress if="$<" of="$$usb"
15 15
16emu = qemu-system-x86_64 -m 800 -enable-kvm -boot c
17
16emu: whole.img 18emu: whole.img
17 qemu-system-x86_64 -m 800 -enable-kvm -boot c "$<" 19 $(emu) $<
20
21emu-key keymu: key.img
22 $(emu) $<
18 23
19key: key.img 24key: key.img
20 usb=$$(usb) && \ 25 usb=$$(usb) && \
21 sudo dd status=progress bs=1048576 if="$<" of="$$usb" && \ 26 sudo dd status=progress bs=1048576 if="$<" of="$$usb" && \
22 sudo qemu-system-x86_64 -m 800 -enable-kvm -boot c "$$usb" 27 sudo $(emu) "$$usb"
23 28
24clean: 29clean:
25 rm -rf _build whole.img 30 rm -rf _build whole.img key.img
diff --git a/src/partvi b/src/partvi
index ddf4e45..154e35f 100755
--- a/src/partvi
+++ b/src/partvi
@@ -174,6 +174,7 @@ iterate_partitions()
174 174
175 imgfile=$builddir/$img 175 imgfile=$builddir/$img
176 176
177 check_for_key_only_skip || continue
177 "$@" || return 178 "$@" || return
178 done 179 done
179} 180}
@@ -207,7 +208,6 @@ create_ptable_conf()
207 partition-table) start=$((start + devsz)); return;; 208 partition-table) start=$((start + devsz)); return;;
208 efi-system-partition) typecode=C12A7328-F81F-11D2-BA4B-00A0C93EC93B ;; 209 efi-system-partition) typecode=C12A7328-F81F-11D2-BA4B-00A0C93EC93B ;;
209 dm-verity-data|dm-verity-hashes) 210 dm-verity-data|dm-verity-hashes)
210 check_for_key_only_skip || return 0
211 case "$name" in 211 case "$name" in
212 samizdat-rootfs|samizdat-root-patch) typecode=4f68bce3-e8cd-4db1-96e7-fbcaf984b709 ;; 212 samizdat-rootfs|samizdat-root-patch) typecode=4f68bce3-e8cd-4db1-96e7-fbcaf984b709 ;;
213 samizdat-root-patch-verity) typecode=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5 ;; 213 samizdat-root-patch-verity) typecode=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5 ;;
@@ -249,38 +249,6 @@ create_ptable_conf()
249 start=$((start + devsz)) 249 start=$((start + devsz))
250} 250}
251 251
252KEY_ONLY=
253if [ "$1" = 'key' ]
254then
255 KEY_ONLY=y
256fi
257
258set -e
259
260builddir=_build
261mkdir -p "$builddir"
262
263iterate_partitions build_partition_image
264
265if [ "$UID" = 0 ]
266then sudo=
267else sudo=sudo
268fi
269
270if [ "$KEY_ONLY" ]
271then
272 target=key.img
273else
274 target=whole.img
275fi
276
277if whole=$(losetup -j "$target" -O NAME --noheadings)
278then
279 quietly $sudo umount *.mnt
280 quietly $sudo kpartx -dv "$whole"
281 quietly $sudo losetup -D
282fi
283
284sfdisk_init() 252sfdisk_init()
285{ 253{
286 local DEV LAST LAST_LBA 254 local DEV LAST LAST_LBA
@@ -315,9 +283,11 @@ pee_on_table()
315 : > "$DOS_TABLE_FILE" 283 : > "$DOS_TABLE_FILE"
316 iterate_partitions create_ptable_conf 284 iterate_partitions create_ptable_conf
317 285
318 set -x 286 (
319 $sudo sfdisk "$dev" < "$GPT_TABLE_FILE" || return 287 set -x
320 $sudo sfdisk -Y dos "$dev" < "$DOS_TABLE_FILE" || return 288 $sudo sfdisk --no-tell-kernel "$dev" < "$GPT_TABLE_FILE" || return
289 $sudo sfdisk --no-tell-kernel -Y dos "$dev" < "$DOS_TABLE_FILE" || return
290 )
321} 291}
322 292
323check_for_key_only_skip() 293check_for_key_only_skip()
@@ -332,19 +302,67 @@ check_for_key_only_skip()
332clone_parts_to_target() 302clone_parts_to_target()
333{ 303{
334 304
335 check_for_key_only_skip || return 0
336 f=$(readlink -e _build/"${f%.conf}") || return 305 f=$(readlink -e _build/"${f%.conf}") || return
337 ficlonerange.py "$f" "$target" 306 ficlonerange.py "$f" "$target"
338} 307}
339 308
309cleanup()
310{
311 for f in part*.conf
312 do
313 mnt=${f%.conf}.mnt
314 if mountpoint -q "$mnt"
315 then
316 loudly $sudo umount "$mnt"
317 $sudo rmdir "$mnt"
318 fi
319 done
320 if [ "$whole" ]
321 then
322 loudly $sudo kpartx -sd "$whole"
323 loudly $sudo losetup -d "$whole"
324 fi
325}
326
327KEY_ONLY=
328if [ "$1" = 'key' ]
329then
330 KEY_ONLY=y
331fi
332
333set -e
334
335builddir=_build
336mkdir -p "$builddir"
337
338if [ "$UID" = 0 ]
339then sudo=
340else sudo=sudo
341fi
342
343if [ "$KEY_ONLY" ]
344then
345 target=key.img
346else
347 target=whole.img
348fi
349
350iterate_partitions build_partition_image
351
352if whole=$(losetup -j "$target" -O NAME --noheadings)
353then
354 cleanup
355 whole=
356fi
340truncate -s0 "$target" 357truncate -s0 "$target"
341iterate_partitions clone_parts_to_target 358iterate_partitions clone_parts_to_target
342 359
343$sudo losetup -L -f "$target" 360$sudo losetup -L -f "$target"
344whole=$(losetup -j "$target" -O NAME --noheadings) 361whole=$(losetup -j "$target" -O NAME --noheadings)
345(pee_on_table "$whole") || exit 362pee_on_table "$whole"
363$sudo kpartx -su "$whole"
346 364
347$sudo kpartx -u "$whole" 365trap cleanup EXIT
348 366
349for f in part*.conf 367for f in part*.conf
350do 368do
@@ -381,16 +399,3 @@ do
381done 399done
382 400
383loudly $sudo eatmydata -- grub-install --target=i386-pc --recheck --boot-directory="$BOOT_DIR" "$whole" 401loudly $sudo eatmydata -- grub-install --target=i386-pc --recheck --boot-directory="$BOOT_DIR" "$whole"
384loudly $sudo eatmydata -- grub-install --target=x86_64-efi --recheck --removable --efi-directory="$EFI_DIR" "$whole"
385
386for f in part*.conf
387do
388 mnt=${f%.conf}.mnt
389 if mountpoint -q "$mnt"
390 then
391 loudly $sudo umount "$mnt"
392 $sudo rmdir "$mnt"
393 fi
394done
395loudly $sudo kpartx -d "$whole"
396loudly $sudo losetup -d "$whole"