various improvements and fixes, some flidas related
[distro-setup] / mount-latest-subvol
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # usage: mount-latest-subvol
17
18 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
19
20 errcatch() {
21 set -E; shopt -s extdebug
22 _err-trap() {
23 err=$?
24 exec >&2
25 set +x
26 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
27 bash-trace 2
28 echo "$0: exiting with code $err"
29 exit $err
30 }
31 trap _err-trap ERR
32 set -o pipefail
33 }
34 bash-trace() {
35 local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent
36 local source
37 local extdebug=false
38 if [[ $(shopt -p extdebug) == *-s* ]]; then
39 extdebug=true
40 fi
41
42 for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
43 argc=${BASH_ARGC[frame]}
44 argc_index+=$argc
45 ((frame < start)) && continue
46 if (( ${#BASH_SOURCE[@]} > 1 )); then
47 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
48 fi
49 indent=$((frame-start+1))
50 indent=$((indent < max_indent ? indent : max_indent))
51 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
52 if $extdebug; then
53 for ((i=argc_index-1; i >= argc_index-argc; i--)); do
54 printf " %s" "${BASH_ARGV[i]}"
55 done
56 fi
57 echo \'
58 done
59 }
60 errcatch
61
62 tu() {
63 while read -r line; do
64 file="$1"
65 grep -xFq "$line" "$file" || tee -a "$file"<<<"$line"
66 done
67 }
68 e() { printf "%s\n" "$*"; "$@"; }
69 mnt() {
70 dir=$1
71 if ! mountpoint $dir &>/dev/null; then
72 mkdir -p $dir
73 e mount $dir
74 fi
75 }
76 fstab() {
77 while read -r start mpoint end; do
78 l="$start $mpoint $end"
79 # kill off any lines that duplicate the mount point.
80 sed --follow-symlinks -ri "\%$l%b;\%^\s*\S+\s+$mpoint\s%d" /etc/fstab
81 tu /etc/fstab <<<"$l"
82 done
83 }
84
85 ret=0
86
87 ##### begin setup fstab for subvols we care about ######
88 first_root_crypt=$(awk '$2 == "/" {print $1}' /etc/mtab)
89 fstab <<EOF
90 $first_root_crypt /a btrfs noatime,subvol=a 0 0
91 EOF
92
93 shopt -s nullglob
94
95 f=(/mnt/root/btrbk/q.*)
96 if [[ -e $f ]]; then
97 fstab <<EOF
98 $first_root_crypt /q btrfs noatime,subvol=q 0 0
99 /q/p /p none bind 0 0
100 EOF
101 fi
102
103 f=(/mnt/root/btrbk/o.*)
104 if [[ -e $f ]]; then
105 fstab <<EOF
106 $first_root_crypt /o btrfs noatime,subvol=o 0 0
107 /o/m /m none bind 0 0
108 EOF
109 fi
110
111 if [[ $HOSTNAME == frodo ]]; then
112 fstab <<EOF
113 $first_root_crypt /i btrfs noatime,subvol=i 0 0
114 EOF
115 fi
116 ##### end setup fstab for subvols we care about ######
117
118 for vol in q a o i; do
119 d=/$vol
120 if ! awk '{print $2}' /etc/fstab | grep -xF $d &>/dev/null; then
121 continue
122 fi
123
124
125 ##### begin building up list of bind mounts ######
126 binds=() # list of bind mounts
127 roots=($d) # list of bind mounts, plus the original mount
128 while true; do
129 new_roots=()
130 for r in ${roots[@]}; do
131 # eg. when r=/q/p, for lines like
132 # /q/p /p none bind 0 0
133 # output /p
134 new_roots+=($(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#p" /etc/fstab))
135 done
136 (( ${#new_roots} )) || break
137 binds+=(${new_roots[@]})
138 roots=( ${new_roots[@]} )
139 done
140 ##### end building up list of bind mounts ######
141
142
143 # if latest is already mounted, make sure binds are mounted and move on
144 if e check-subvol-stale $d; then
145 mnt $d
146 for b in ${binds[@]}; do
147 mnt $b
148 done
149 continue
150 fi
151
152 fresh_snap=$(</nocow/btrfs-stale/$vol)
153 if [[ ! $fresh_snap ]]; then
154 echo "$0: error. empty fresh_snap var"
155 ret=1
156 continue
157 fi
158
159 umount_ret=true
160 unmounted=()
161 for dir in $(echo $d ${binds[*]}\ |tac -s\ ); do
162 if mountpoint $dir; then
163 if e umount -R $dir; then
164 unmounted+=($dir)
165 else
166 if pids=$(timeout 4 lsof -t $dir); then
167 timeout 4 lsof -w $dir
168 kill $pids
169 fi
170
171 # fuser will find open sockets that lsof won't, for example from gpg-agent.
172 # note: -v shows kernel processes, which then doesn't return true when we want
173 if timeout 4 fuser -m $dir &>/dev/null; then
174 fuser -TERM -mvk $dir
175 fi
176
177 sleep .5
178 if e umount -R $dir; then
179 unmounted+=($dir)
180 else
181 echo "$0: failed to umount $dir"
182 umount_ret=false
183 ret=1
184 continue
185 fi
186 fi
187 fi
188 done
189
190 if ! $umount_ret; then
191 for dir in ${unmounted[@]}; do
192 mnt $dir
193 done
194 continue
195 fi
196
197 # todo: decipher /mnt/root, like we do in check-subvol-stale
198 cd /mnt/root
199 if [[ -e $vol ]]; then
200 e mv $vol $vol.leaf.$(date +%Y%m%dT%H%M%S%z)
201 fi
202 # Note, we make a few assumptions in this script, like
203 # $d was not a different subvol id than $vol, and
204 # things otherwise didn't get mounted very strangely.
205 e btrfs sub snapshot $fresh_snap $vol
206 for dir in $d ${binds[@]}; do
207 e mnt $dir
208 done
209 stale_dir=/nocow/btrfs-stale
210 rm -f $stale_dir/$d
211 done
212
213 ### disabled
214 if [[ $HOSTNAME == treetowlxxxxxxxxx ]]; then
215 # partitioned it with fai partitioner outside of fai,
216 # because it\'s worth it to have 1% space reserved for boot and
217 # swap partitions in case I ever want to boot off those drives.
218 # as root:
219 # . /a/bin/fai/fai-wrapper
220 # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
221 # fai-setclass ROTATIONAL
222 # export LUKS_DIR=/q/root/luks/
223 # # because the partition nums existed already
224 # fai-setclass REPARTITION
225 # /a/bin/fai/fai/config/hooks/partition.DEFAULT
226
227 devs=(
228 ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
229 ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1
230 ata-TOSHIBA_MD04ACA500_8471K430FS9A-part1
231 ata-TOSHIBA_MD04ACA500_8481K493FS9A-part1
232 )
233 first=true
234 for dev in ${devs[@]}; do
235 if $first; then
236 first=false
237 tu /etc/fstab <<EOF
238 /dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i,noauto 0 0
239 /dev/mapper/crypt_dev_$dev /mnt/iroot btrfs noatime,subvolid=0,noauto 0 0
240 EOF
241 fi
242 tu /etc/crypttab <<EOF
243 crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-treetowl discard,luks
244 EOF
245 if [[ ! -e /dev/mapper/crypt_dev_$dev ]]; then
246 cryptdisks_start crypt_dev_$dev
247 fi
248 done
249 # note, could do an else here and have some kind of mount for /i
250 # on other hosts.
251 fi
252
253 exit $ret