+#!/bin/bash
+
+[[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
+
+errcatch() {
+ set -E; shopt -s extdebug
+ _err-trap() {
+ err=$?
+ exec >&2
+ set +x
+ echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
+ bash-trace 2
+ echo "$0: exiting with code $err"
+ exit $err
+ }
+ trap _err-trap ERR
+ set -o pipefail
+}
+bash-trace() {
+ local -i argc_index=0 arg frame i start=${1:-1} max_indent=8 indent
+ local source
+ local extdebug=false
+ if [[ $(shopt -p extdebug) == *-s* ]]; then
+ extdebug=true
+ fi
+
+ for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
+ argc=${BASH_ARGC[frame]}
+ argc_index+=$argc
+ ((frame < start)) && continue
+ if (( ${#BASH_SOURCE[@]} > 1 )); then
+ source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
+ fi
+ indent=$((frame-start+1))
+ indent=$((indent < max_indent ? indent : max_indent))
+ printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
+ if $extdebug; then
+ for ((i=argc_index-1; i >= argc_index-argc; i--)); do
+ printf " %s" "${BASH_ARGV[i]}"
+ done
+ fi
+ echo \'
+ done
+}
+errcatch
+
+
+e() { printf "%s\n" "$*"; "$@"; }
+
+ret=0
+
+first_root_crypt=$(awk '$2 == "/" {print $1}' /etc/mtab)
+tu /etc/fstab <<EOF
+$first_root_crypt /q btrfs noatime,subvol=q 0 0
+/q/a /a none bind 0 0
+EOF
+case $HOSTNAME in
+ treetowl|x2|frodo)
+ tu /etc/fstab <<EOF
+$first_root_crypt /p btrfs noatime,subvol=p 0 0
+EOF
+ ;;
+esac
+if [[ $HOSTNAME == treetowl ]]; then
+ # partitioned it with fai partitioner outside of fai,
+ # because it\'s worth it to have 1% space reserved for boot and
+ # swap partitions in case I ever want to boot off those drives.
+ # as root:
+ # . /a/bin/fai/fai-wrapper
+ # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
+ # fai-setclass ROTATIONAL
+ # export LUKS_DIR=/q/root/luks/
+ # # because the partition nums existed already
+ # fai-setclass REPARTITION
+ # /a/bin/fai/fai/config/hooks/partition.DEFAULT
+
+ # just the first in the btrfs raid
+ dev=ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
+ tu /etc/fstab <<EOF
+/dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i 0 0
+EOF
+ tu /etc/crypttab <<EOF
+crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-treetowl discard,luks
+EOF
+
+else
+ tu /etc/fstab <<'EOF'
+/q/i /i none bind 0 0
+EOF
+
+fi
+
+mkdir -p /q /p /i
+
+for vol in q p; do
+ d=/$vol
+ if ! awk '{print $2}' /etc/fstab | grep -xF $d &>/dev/null; then
+ continue
+ fi
+
+
+ binds=()
+ roots=($d)
+ while true; do
+ new_roots=()
+ for r in ${roots[@]}; do
+ # /q/a /a none bind 0 0
+ new_roots+=($(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#" /etc/fstab))
+ done
+ (( ${#new_roots} )) || break
+ binds+=(${new_roots[@]})
+ roots=( ${new_roots[@]} )
+ done
+
+ if e check-subvol-stale $d; then
+ for b in ${binds[@]}; do
+ mount $b
+ done
+ continue
+ fi
+
+ last_snap=$(</nocow/btrfs-stale/$vol)
+ if [[ ! $last_snap ]]; then
+ echo "$0: error. empty last_snap var"
+ ret=1
+ continue
+ fi
+
+ umount_ret=true
+ unmounted=()
+ for dir in $(echo $d ${binds[*]}\ |tac -s\ ); do
+ if mountpoint $dir; then
+ if e umount -R $dir; then
+ unmounted+=($dir)
+ else
+ umount_ret=false
+ echo "$0: failed to umount $dir"
+ break
+ fi
+ fi
+ done
+
+ if ! $umount_ret; then
+ for dir in ${unmounted[@]}; do
+ mount $dir
+ done
+ ret=1
+ continue
+ fi
+
+ cd /mnt/root
+ if [[ -e $vol ]]; then
+ e btrfs sub del $vol
+ fi
+ # Note, we make a few assumptions in this script, like
+ # $d was not a different subvol id than $vol, and
+ # things otherwise didn't get mounted very strangely.
+ e btrfs sub snapshot btrbk/$last_snap $vol
+ for dir in $d ${binds[@]}; do
+ e mount $dir
+ done
+done
+exit $ret