use btrfs-send for data syncing, testing still in progress
[distro-setup] / mount-latest-subvol
1 #!/bin/bash
2
3 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
4
5 errcatch() {
6 set -E; shopt -s extdebug
7 _err-trap() {
8 err=$?
9 exec >&2
10 set +x
11 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
12 bash-trace 2
13 echo "$0: exiting with code $err"
14 exit $err
15 }
16 trap _err-trap ERR
17 set -o pipefail
18 }
19 bash-trace() {
20 local -i argc_index=0 arg frame i start=${1:-1} max_indent=8 indent
21 local source
22 local extdebug=false
23 if [[ $(shopt -p extdebug) == *-s* ]]; then
24 extdebug=true
25 fi
26
27 for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
28 argc=${BASH_ARGC[frame]}
29 argc_index+=$argc
30 ((frame < start)) && continue
31 if (( ${#BASH_SOURCE[@]} > 1 )); then
32 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
33 fi
34 indent=$((frame-start+1))
35 indent=$((indent < max_indent ? indent : max_indent))
36 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
37 if $extdebug; then
38 for ((i=argc_index-1; i >= argc_index-argc; i--)); do
39 printf " %s" "${BASH_ARGV[i]}"
40 done
41 fi
42 echo \'
43 done
44 }
45 errcatch
46
47
48 e() { printf "%s\n" "$*"; "$@"; }
49
50 ret=0
51
52 first_root_crypt=$(awk '$2 == "/" {print $1}' /etc/mtab)
53 tu /etc/fstab <<EOF
54 $first_root_crypt /q btrfs noatime,subvol=q 0 0
55 /q/a /a none bind 0 0
56 EOF
57 case $HOSTNAME in
58 treetowl|x2|frodo)
59 tu /etc/fstab <<EOF
60 $first_root_crypt /p btrfs noatime,subvol=p 0 0
61 EOF
62 ;;
63 esac
64 if [[ $HOSTNAME == treetowl ]]; then
65 # partitioned it with fai partitioner outside of fai,
66 # because it\'s worth it to have 1% space reserved for boot and
67 # swap partitions in case I ever want to boot off those drives.
68 # as root:
69 # . /a/bin/fai/fai-wrapper
70 # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
71 # fai-setclass ROTATIONAL
72 # export LUKS_DIR=/q/root/luks/
73 # # because the partition nums existed already
74 # fai-setclass REPARTITION
75 # /a/bin/fai/fai/config/hooks/partition.DEFAULT
76
77 # just the first in the btrfs raid
78 dev=ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
79 tu /etc/fstab <<EOF
80 /dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i 0 0
81 EOF
82 tu /etc/crypttab <<EOF
83 crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-treetowl discard,luks
84 EOF
85
86 else
87 tu /etc/fstab <<'EOF'
88 /q/i /i none bind 0 0
89 EOF
90
91 fi
92
93 mkdir -p /q /p /i
94
95 for vol in q p; do
96 d=/$vol
97 if ! awk '{print $2}' /etc/fstab | grep -xF $d &>/dev/null; then
98 continue
99 fi
100
101
102 binds=()
103 roots=($d)
104 while true; do
105 new_roots=()
106 for r in ${roots[@]}; do
107 # /q/a /a none bind 0 0
108 new_roots+=($(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#" /etc/fstab))
109 done
110 (( ${#new_roots} )) || break
111 binds+=(${new_roots[@]})
112 roots=( ${new_roots[@]} )
113 done
114
115 if e check-subvol-stale $d; then
116 for b in ${binds[@]}; do
117 mount $b
118 done
119 continue
120 fi
121
122 last_snap=$(</nocow/btrfs-stale/$vol)
123 if [[ ! $last_snap ]]; then
124 echo "$0: error. empty last_snap var"
125 ret=1
126 continue
127 fi
128
129 umount_ret=true
130 unmounted=()
131 for dir in $(echo $d ${binds[*]}\ |tac -s\ ); do
132 if mountpoint $dir; then
133 if e umount -R $dir; then
134 unmounted+=($dir)
135 else
136 umount_ret=false
137 echo "$0: failed to umount $dir"
138 break
139 fi
140 fi
141 done
142
143 if ! $umount_ret; then
144 for dir in ${unmounted[@]}; do
145 mount $dir
146 done
147 ret=1
148 continue
149 fi
150
151 cd /mnt/root
152 if [[ -e $vol ]]; then
153 e btrfs sub del $vol
154 fi
155 # Note, we make a few assumptions in this script, like
156 # $d was not a different subvol id than $vol, and
157 # things otherwise didn't get mounted very strangely.
158 e btrfs sub snapshot btrbk/$last_snap $vol
159 for dir in $d ${binds[@]}; do
160 e mount $dir
161 done
162 done
163 exit $ret