summaryrefslogtreecommitdiff
path: root/src/initrd/samizdat-eject.sh
diff options
context:
space:
mode:
Diffstat (limited to 'src/initrd/samizdat-eject.sh')
-rwxr-xr-xsrc/initrd/samizdat-eject.sh92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/initrd/samizdat-eject.sh b/src/initrd/samizdat-eject.sh
new file mode 100755
index 0000000..d95a49d
--- /dev/null
+++ b/src/initrd/samizdat-eject.sh
@@ -0,0 +1,92 @@
1#!/bin/sh
2die()
3{
4 printf '%s\n' "$*" >&2
5 exit 1
6}
7
8btrfs_subdevices()
9{
10 local mountpoint="$1"
11 btrfs filesystem show "$mountpoint" | sed -ne 's/^[ \t]*devid.* path //p'
12}
13
14btrfs_subdevice_count()
15{
16 btrfs_subdevices "$1" | wc -l
17}
18
19remove()
20{
21 for dev; do
22 (set -x; btrfs device remove "$dev" /)
23 dmsetup remove "$dev" 2>/dev/null
24 losetup -D
25 done
26}
27
28dm_name()
29{
30 dmsetup info "$1" | sed -ne 's/^Name: *//p'
31}
32
33md_ready()
34{
35 local mountpoint="$1" count dev
36 count=$(mdadm_subdevices /dev/md55|wc -l)
37 [ "$count" = 1 ] || return
38 dev=$(mdadm_subdevices /dev/md55)
39 [ "$(dm_name "$dev")" = samizdatiso ]
40}
41
42copy()
43{
44 temp_target=$(mktemp --tmpdir=/outerfs)
45 mdadm_copy_eject_crypt /dev/md55 "$temp_target"
46}
47
48. mdadm-dup.sh || exit 1
49
50target=$1
51
52if [ ! "$target" ] && mountpoint -q /srv && [ ! -e /srv/samizdat.iso ]; then
53 target=/srv/samizdat.iso
54fi
55
56[ "$target" ] || die "Usage: $0 <target filename>"
57[ ! -e "$target" ] || die "Error: file exists: $target"
58
59if ! mountpoint -q /outerfs; then
60 die "Error: /outerfs is not a mountpoint. Please mount a safe filesystem to temporarily store the ISO on /outerfs"
61fi
62
63devices=$(btrfs_subdevices /) || exit 1
64
65set --
66seen=
67for dev in $devices; do
68 [ -b "$dev" ] || exit 1
69 case "$dev" in
70 /dev/mapper/loop*) set -- "$@" "$dev" ;;
71 /dev/mapper/samizdatcrypt) seen=y ;;
72 esac
73done
74[ "$seen" ] || set -- # avoid messing up someone's btrfs!
75
76
77
78
79
80
81if ! md_ready; then
82 copy &
83fi
84
85remove "$@"
86
87wait
88
89if [ "$(btrfs_subdevice_count /)" = 1 ] && md_ready; then
90 mdadm_copy_eject /dev/md55 "$target".part &&
91 mv "$target".part "$target"
92fi