summaryrefslogtreecommitdiff
path: root/samizdat-eject.sh
blob: e9e71e5bf1c34698efe715a191bbf379f29ce3dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
die()
{
    printf '%s\n' "$*" >&2
    exit 1
}

target=$1

[ "$target" ] || die "Usage: $0 <target filename>"
[ ! -e "$target" ] || die "Error: file exists: $target"


. mdadm-dup.sh || exit 1

btrfs_subdevices()
{
    local mountpoint="$1"
    btrfs filesystem show "$mountpoint" | sed -ne 's/^[ \t]*devid.* path //p'
}

devices=$(btrfs_subdevices /) || exit 1

set --
seen=
for dev in $devices; do
    [ -b "$dev" ] || exit 1
    case "$dev" in
        /dev/mapper/loop*) set -- "$@" "$dev" ;;
        /dev/mapper/samizdatcrypt) seen=y ;;
    esac
done
[ "$seen" ] || set -- # avoid messing up someone's btrfs!

copy()
{
    mdadm_copy_eject_crypt /dev/md55 "$target"
}

remove()
{
    for dev; do
        (set -x; btrfs device remove "$dev" /)
        dmsetup remove "$dev"
        losetup -D
    done
}

# Doing the mdadm copy before removing the devices means our filesystem includes
# devices that are ultimately backed by files ON THE SAME FILESYSTEM. As such, it
# might conceivably put btrfs into a deadlock of some kind.

# If it turns out this doesn't work, the order can be reversed. This will
# perform the read IO twice, though, almost doubling the total installation
# time.

copy & remove "$@"

set -x
wait