fix btrbk service
[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 script=$(readlink -f -- "$BASH_SOURCE")
17 cd /
18 [[ $EUID == 0 ]] || exec sudo -E "$script" "$@"
19
20
21 usage() {
22 cat <<EOF
23 Usage: ${0##*/} [OPTIONS]
24
25 -h|--help Print help and exit.
26
27
28 Note, at source location, intentionally not executable, run and read
29 install-my-scripts.
30
31 Note: Uses util-linux getopt option parsing: spaces between args and
32 options, short options can be combined, options before args.
33 EOF
34 exit $1
35 }
36
37
38 errcatch() {
39 set -E; shopt -s extdebug
40 _err-trap() {
41 err=$?
42 exec >&2
43 set +x
44 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}:in \`$BASH_COMMAND' returned $err"
45 bash-trace 2
46 echo "$0: exiting with code $err"
47 exit $err
48 }
49 trap _err-trap ERR
50 set -o pipefail
51 }
52 bash-trace() {
53 local -i argc_index=0 frame i start=${1:-1} max_indent=8 indent
54 local source
55 local extdebug=false
56 if [[ $(shopt -p extdebug) == *-s* ]]; then
57 extdebug=true
58 fi
59
60 for ((frame=0; frame < ${#FUNCNAME[@]}-1; frame++)); do
61 argc=${BASH_ARGC[frame]}
62 argc_index+=$argc
63 ((frame < start)) && continue
64 if (( ${#BASH_SOURCE[@]} > 1 )); then
65 source="${BASH_SOURCE[frame+1]}:${BASH_LINENO[frame]}:"
66 fi
67 indent=$((frame-start+1))
68 indent=$((indent < max_indent ? indent : max_indent))
69 printf "%${indent}s↳%sin \`%s" '' "$source" "${FUNCNAME[frame]}"
70 if $extdebug; then
71 for ((i=argc_index-1; i >= argc_index-argc; i--)); do
72 printf " %s" "${BASH_ARGV[i]}"
73 done
74 fi
75 echo \'
76 done
77 }
78 errcatch
79
80 tu() {
81 while read -r line; do
82 file="$1"
83 grep -xFq "$line" "$file" || tee -a "$file"<<<"$line"
84 done
85 }
86 m() {
87 if $verbose; then
88 printf "%s\n" "$*"
89 fi
90 "$@"
91 }
92 x() {
93 printf "%s\n" "$*"
94 "$@"
95 }
96
97 mnt() {
98 dir=$1
99 if ! mountpoint -q $dir; then
100 mkdir -p $dir
101 m mount $dir
102 fi
103 }
104 fstab() {
105 while read -r start mpoint end; do
106 l="$start $mpoint $end"
107 # kill off any lines that duplicate the mount point.
108 sed --follow-symlinks -ri "\%$l%b;\%^\s*\S+\s+$mpoint\s%d" /etc/fstab
109 tu /etc/fstab <<<"$l"
110 done
111 }
112 pid-check() {
113 for p in ${pids}; do
114 for m in ${my_pids[@]}; do
115 if (( p == m )); then
116 echo "$0: error: pids to kill includes our pid or a parent. ps output:" >&2
117 ps -f -p $p
118 exit 1
119 fi
120 done
121 done
122 }
123 kill-dir() {
124 for sig; do
125 echo kill-dir $sig
126 found_pids=false
127 if pids=$(timeout 4 lsof -t $dir); then
128 found_pids=true
129 timeout 4 lsof -w $dir
130 pid-check
131 kill -$sig $pids
132 fi
133 # fuser will find open sockets that lsof won't, for example from gpg-agent.
134 # note: -v shows kernel processes, which then doesn't return true when we want
135 if pids=$(timeout 4 fuser -m $dir 2>/dev/null); then
136 pid-check
137 found_pids=true
138 fuser -$sig -mvk $dir
139 fi
140 sleep .5
141 if ! $found_pids; then
142 return 0
143 fi
144 done
145 return 1
146 }
147
148 ##### begin command line parsing ########
149
150 # you can remove this if you do not have options which can have args with spaces or empty.
151
152 verbose=false
153 force=false
154 temp=$(getopt -l help,force,verbose hfv "$@") || usage 1
155 eval set -- "$temp"
156 while true; do
157 case $1 in
158 -f|--force) force=true ;;
159 -v|--verbose) verbose=true ;;
160 -h|--help) usage ;;
161 --) shift; break ;;
162 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
163 esac
164 shift
165 done
166
167 ##### end command line parsing ########
168
169 ret=0
170
171 ##### begin setup fstab for subvols we care about ######
172 root_dev=$(awk '$2 == "/" {print $1}' /etc/mtab)
173 if [[ $root_dev == /dev/dm-* ]]; then
174 for d in /dev/mapper/*; do
175 if [[ $(readlink -f $d) == "$root_dev" ]]; then
176 root_dev=$d
177 break
178 fi
179 done
180 fi
181
182 if cryptsetup status $root_dev &>/dev/null; then
183 crypt_dev=$root_dev
184 else # if we are in a recovery boot, find the next best crypt device
185 noauto=,noauto
186 for dev in $(dmsetup ls --target crypt | awk '{print $1}'); do
187 dev=/dev/mapper/$dev
188 if awk '{print $1}' /etc/mtab | grep -Fx $dev &>/dev/null; then
189 crypt_dev=$dev
190 break
191 fi
192 done
193 fi
194
195
196 fstab <<EOF
197 $crypt_dev /a btrfs noatime,subvol=a$noauto 0 0
198 EOF
199
200 shopt -s nullglob
201
202 # ssh and probably some other things care about parent directory
203 # ownership, and ssh doesn\'t allow any group writable parent
204 # directories, so we are forced to use a directory structure similar
205 # to home directories
206 f=(/mnt/root/btrbk/q.*); f=${f[0]}
207 if [[ -e $f ]]; then
208 fstab <<EOF
209 $crypt_dev /q btrfs noatime,subvol=q,gid=1000$noauto 0 0
210 /q/p /p none bind$noauto 0 0
211 EOF
212 fi
213
214 f=(/mnt/root/btrbk/o.*); f=${f[0]}
215 if [[ -e $f ]]; then
216 fstab <<EOF
217 $crypt_dev /o btrfs noatime,subvol=o$noauto 0 0
218 /o/m /m none bind$noauto 0 0
219 EOF
220 fi
221
222 if [[ $HOSTNAME == frodo ]]; then
223 fstab <<EOF
224 $crypt_dev /i btrfs noatime,subvol=i$noauto 0 0
225 EOF
226 fi
227 ##### end setup fstab for subvols we care about ######
228
229 # get pids that this program depends on so we dont kill them
230 my_pids=($$ $PPID)
231 loop_limit=30
232 count=0
233 while [[ ${my_pids[-1]} != 1 && ${my_pids[-1]} != ${my_pids[-2]} && $count -lt $loop_limit ]]; do
234 count=$((count + 1))
235 p=$(ps -p ${my_pids[-1]} -o ppid=)
236 if [[ $p == 0 || ! $p ]]; then
237 break
238 fi
239 my_pids+=($p)
240 done
241
242
243 for vol in q a o i; do
244 d=/$vol
245 if ! awk '{print $2}' /etc/fstab | grep -xF $d &>/dev/null; then
246 continue
247 fi
248
249
250 ##### begin building up list of bind mounts ######
251 binds=() # list of bind mounts
252 roots=($d) # list of bind mounts, plus the original mount
253 while true; do
254 new_roots=()
255 for r in ${roots[@]}; do
256 # eg. when r=/q/p, for lines like
257 # /q/p /p none bind 0 0
258 # output /p
259 new_roots+=($(sed -rn "s#^$r/\S+\s+(\S+)\s+none\s+bind\s.*#\1#p" /etc/fstab))
260 done
261 (( ${#new_roots} )) || break
262 binds+=(${new_roots[@]})
263 roots=( ${new_roots[@]} )
264 done
265 ##### end building up list of bind mounts ######
266
267
268 # if latest is already mounted, make sure binds are mounted and move on
269 m check-subvol-stale $d
270 # populated by check-subvol-stale if stale
271 if ! fresh_snap=$(cat /nocow/btrfs-stale/$vol 2>/dev/null); then
272 mnt $d
273 for b in ${binds[@]}; do
274 mnt $b
275 done
276 continue
277 fi
278
279 umount_ret=true
280 unmounted=()
281 for dir in $(echo $d ${binds[*]}\ |tac -s\ ); do
282 if mountpoint -q $dir; then
283 if m umount -R $dir; then
284 unmounted+=($dir)
285 else
286 if ! kill-dir TERM TERM TERM INT INT HUP HUP; then
287 if $force; then kill-dir KILL; fi
288 fi
289
290 if m umount -R $dir; then
291 unmounted+=($dir)
292 else
293 echo "$0: failed to umount $dir"
294 umount_ret=false
295 ret=1
296 continue
297 fi
298 fi
299 fi
300 done
301
302 # if we unmounted some but not all, restore them and move on
303 if ! $umount_ret; then
304 for dir in ${unmounted[@]}; do
305 mnt $dir
306 done
307 continue
308 fi
309
310 #### begin dealing with leaf vols ####
311 # todo: decipher /mnt/root, like we do in check-subvol-stale
312 cd /mnt/root
313 if [[ -e $vol ]]; then
314 leaf=$vol.leaf.$(date +%Y-%m-%dT%H:%M:%S%z)
315 m mv $vol $leaf
316 m btrfs property set -ts $leaf ro true
317
318 ### begin check if leaf is different, delete it if not ###
319 if [[ -e /a/opt/btrfs-snapshots-diff/btrfs-snapshots-diff.py ]]; then
320 source /a/bin/distro-functions/src/package-manager-abstractions
321 pi python-jmespath # dependency
322 parentid=$(btrfs sub show $leaf | awk '$1 == "Parent" && $2 == "UUID:" {print $3}')
323 bsubs=(/mnt/root/btrbk/$vol.*)
324 bsub=
325 # go in reverse order as its more likely to be at the end
326 for ((i=${#bsubs[@]}-1; i>=0; i--)); do
327 if [[ $parentid == $(btrfs sub show ${bsubs[i]} | awk '$1 == "UUID:" {print $2}') ]]; then
328 bsub=${bsubs[i]}
329 break
330 fi
331 done
332 if [[ $bsub ]]; then
333 tmp=$(mktemp)
334 # in testing, same subvol is 136 bytes. allow some overhead
335 btrfs send --no-data -p $bsub $leaf | head -c 1000 > $tmp || [[ $? == 141 ]]
336 if (( $(stat -c%s $tmp) < 1000)); then
337 # example output for an empty diff:
338 # Found a valid Btrfs stream header, version 1
339 # o.leaf.2019-05-15T14:00:50-0400;snapshot: uuid=ba045ea30737dd449003f1ee40ec12d0, ctrasid=109533, clone_uuid=3c7e3544e486834aa71d89e5b8f30056, clone_ctransid=109533
340 lines=$(/a/opt/btrfs-snapshots-diff/btrfs-snapshots-diff.py -s -f $tmp | \
341 grep -vxF "Found a valid Btrfs stream header, version 1" | \
342 grep -cv "^[^;]*;snapshot: ") ||:
343 if [[ $lines == 0 ]]; then
344 x btrfs sub del $leaf
345 fi
346 fi
347 fi
348 fi
349 ### end check if leaf is different, delete it if not ###
350
351 ## begin expire leaf vols ##
352 leaf_vols=($vol.leaf.*)
353 count=1
354 for leaf in ${leaf_vols[@]}; do
355 leaf_secs=$(date -d ${leaf#$vol.leaf.} +%s)
356 if (( $(date +%s) - 60*60*24*60 > leaf_secs || count > 200 )); then # 60 days
357 x btrfs sub del $leaf
358 fi
359 count=$((count+1))
360 done
361 ## end expire leaf vols ##
362 fi
363 #### end dealing with leaf vols ####
364
365 # Note, we make a few assumptions in this script, like
366 # $d was not a different subvol id than $vol, and
367 # things otherwise didn't get mounted very strangely.
368 m btrfs sub snapshot $fresh_snap $vol
369 for dir in $d ${binds[@]}; do
370 m mnt $dir
371 done
372 stale_dir=/nocow/btrfs-stale
373 rm -f $stale_dir/$d
374
375 done
376
377 ### disabled
378 if [[ $HOSTNAME == kdxxxxxxxxx ]]; then
379 # partitioned it with fai partitioner outside of fai,
380 # because it\'s worth it to have 1% space reserved for boot and
381 # swap partitions in case I ever want to boot off those drives.
382 # as root:
383 # . /a/bin/fai/fai-wrapper
384 # eval-fai-classfile /a/bin/fai/fai/config/class/51-multi-boot
385 # fai-setclass ROTATIONAL
386 # export LUKS_DIR=/q/root/luks/
387 # # because the partition nums existed already
388 # fai-setclass REPARTITION
389 # /a/bin/fai/fai/config/hooks/partition.DEFAULT
390
391 devs=(
392 ata-TOSHIBA_MD04ACA500_84REK6NTFS9A-part1
393 ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1
394 ata-TOSHIBA_MD04ACA500_8471K430FS9A-part1
395 ata-TOSHIBA_MD04ACA500_8481K493FS9A-part1
396 )
397 first=true
398 for dev in ${devs[@]}; do
399 if $first; then
400 first=false
401 tu /etc/fstab <<EOF
402 /dev/mapper/crypt_dev_$dev /i btrfs noatime,subvol=i,noauto 0 0
403 /dev/mapper/crypt_dev_$dev /mnt/iroot btrfs noatime,subvolid=0,noauto 0 0
404 EOF
405 fi
406 tu /etc/crypttab <<EOF
407 crypt_dev_$dev /dev/disk/by-id/$dev /q/root/luks/host-kd discard,luks
408 EOF
409 if [[ ! -e /dev/mapper/crypt_dev_$dev ]]; then
410 cryptdisks_start crypt_dev_$dev
411 fi
412 done
413 # note, could do an else here and have some kind of mount for /i
414 # on other hosts.
415 fi
416
417 exit $ret