#!/bin/sh die() { printf '%s\n' "$*" >&2 exit 1 } target=$1 if [ ! "$target" ] && mountpoint -q /srv && [ ! -e /srv/samizdat.iso ]; then target=/srv/samizdat.iso fi [ "$target" ] || die "Usage: $0 " [ ! -e "$target" ] || die "Error: file exists: $target" if mountpoint -q /outerfs; then temp_target=$(mktemp --tmpdir=/outerfs) else die "Error: /outerfs is not a mountpoint. Please mount a safe filesystem to temporarily store the ISO on /outerfs" fi . 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 "$temp_target" && mdadm_copy_eject /dev/md55 "$target".part && mv "$target".part "$target" } remove() { for dev; do (set -x; btrfs device remove "$dev" /) dmsetup remove "$dev" 2>/dev/null 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