diff options
Diffstat (limited to 'src/initrd/samizdat-eject.sh')
-rwxr-xr-x | src/initrd/samizdat-eject.sh | 92 |
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 | ||
2 | die() | ||
3 | { | ||
4 | printf '%s\n' "$*" >&2 | ||
5 | exit 1 | ||
6 | } | ||
7 | |||
8 | btrfs_subdevices() | ||
9 | { | ||
10 | local mountpoint="$1" | ||
11 | btrfs filesystem show "$mountpoint" | sed -ne 's/^[ \t]*devid.* path //p' | ||
12 | } | ||
13 | |||
14 | btrfs_subdevice_count() | ||
15 | { | ||
16 | btrfs_subdevices "$1" | wc -l | ||
17 | } | ||
18 | |||
19 | remove() | ||
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 | |||
28 | dm_name() | ||
29 | { | ||
30 | dmsetup info "$1" | sed -ne 's/^Name: *//p' | ||
31 | } | ||
32 | |||
33 | md_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 | |||
42 | copy() | ||
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 | |||
50 | target=$1 | ||
51 | |||
52 | if [ ! "$target" ] && mountpoint -q /srv && [ ! -e /srv/samizdat.iso ]; then | ||
53 | target=/srv/samizdat.iso | ||
54 | fi | ||
55 | |||
56 | [ "$target" ] || die "Usage: $0 <target filename>" | ||
57 | [ ! -e "$target" ] || die "Error: file exists: $target" | ||
58 | |||
59 | if ! mountpoint -q /outerfs; then | ||
60 | die "Error: /outerfs is not a mountpoint. Please mount a safe filesystem to temporarily store the ISO on /outerfs" | ||
61 | fi | ||
62 | |||
63 | devices=$(btrfs_subdevices /) || exit 1 | ||
64 | |||
65 | set -- | ||
66 | seen= | ||
67 | for 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 | ||
73 | done | ||
74 | [ "$seen" ] || set -- # avoid messing up someone's btrfs! | ||
75 | |||
76 | |||
77 | |||
78 | |||
79 | |||
80 | |||
81 | if ! md_ready; then | ||
82 | copy & | ||
83 | fi | ||
84 | |||
85 | remove "$@" | ||
86 | |||
87 | wait | ||
88 | |||
89 | if [ "$(btrfs_subdevice_count /)" = 1 ] && md_ready; then | ||
90 | mdadm_copy_eject /dev/md55 "$target".part && | ||
91 | mv "$target".part "$target" | ||
92 | fi | ||