remove kinsis / unused input settings
[distro-setup] / check-subvol-stale
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: $0 SUBVOL_MOUNTPOINT...
17 #
18 # In git, this is not not executable because it's meant to be installed
19 # using ./install-my-scripts
20 #
21 # If latest subvols $@ are not mounted, exit 1, print message, and touch
22 # /nocow/btrfs-stale/$subvol
23 #
24 # Either SUBVOL_MOUNTPOINT is a snapshot of the latest, or
25 # the latest snapshot is snapshot of SUBVOL_MOUNTPOINT.
26
27 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
28
29 set -eE -o pipefail
30 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
31
32 shopt -s nullglob
33
34 if [[ ! $@ ]]; then
35 echo "$0: error: expected mountpoint argument"
36 fi
37
38 stale-file() {
39 stale_dir=/nocow/btrfs-stale
40 stale_file=$stale_dir/$vol
41 if $stale; then
42 mkdir -p $stale_dir
43 printf "%s\n" $freshest_snap > $stale_file
44 ret=1
45 else
46 rm -f $stale_file
47 fi
48
49 }
50
51 ret=0
52 for d; do
53 vol=${d##*/}
54 dev=$(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc/fstab|head -n1)
55 subvol_dir=$(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc/fstab|head -n1)
56 # note, we need $dev because $d might not be mounted, and we do this loop
57 # because the device in fstab for the rootfs can be different.
58 for devx in $(btrfs fi show $dev| sed -rn 's#.*path (/\S+)$#\1#p'); do
59 root_dir=$(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=0\b.*,\1,p" /etc/fstab|head -n1)
60 if [[ $root_dir ]]; then break; fi
61 done
62 svp=$root_dir/$subvol_dir # subvolume path
63
64 snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century.
65 if [[ ! $snaps ]]; then
66 # no snapshots yet
67 echo "$0: warning. no snapshots found. this is expected for a brand new volume"
68 continue
69 fi
70
71 # get info on last received sub
72 last_received_gen=0
73 for f in ${snaps[@]}; do
74 show="$(btrfs sub show $f)"
75 if echo "$show" | grep -E "Received UUID:\s+[[:alnum:]]" &>/dev/null; then
76 cgen=$(echo "$show" | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
77 if [[ $cgen -gt $last_received_gen ]]; then
78 last_received_cgen=$cgen
79 last_received=$f
80 fi
81 fi
82 done
83
84 # Get last_snap by date.
85 # when a btrbk bugfix makes it into the distro,
86 # we might replace this with btrbk list latest /mnt/root/$vol | ...
87 last_snap=$(
88 for s in ${snaps[@]}; do
89 f=${s##*/}
90 unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
91 printf "%s %s\n" $unix_time $s
92 done | sort -r | head -n 1 | awk '{print $2}'
93 )
94 if [[ ! $last_snap ]]; then
95 # should not happen.
96 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}"
97 ret=1
98 continue
99 fi
100
101 if [[ ! -e $svp ]]; then
102 echo "$0: warning: subvol does not exist: $svp"
103 echo "$0 assuming this host was just for receiving and latest snap is freshest"
104 freshest_snap=$last_snap
105 stale=true
106 stale-file
107 continue
108 fi
109
110
111 # if there is a last_received, we can assume stale or fresh if we are newer/older
112 if [[ $last_received ]]; then
113 svp_cgen=$(btrfs sub show $svp | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
114 if [[ $svp_cgen -ge $last_received_cgen ]]; then
115 stale=false
116 else
117 echo "$svp stale: it's gen at creation, $svp_cgen, is earlier than the last received snapshot, $last_received's gen at creation: $last_received_cgen"
118 freshest_snap=$last_received
119 stale=true
120 fi
121 stale-file
122 continue
123 fi
124
125 # fallback to using last_snap as the freshest
126 freshest_snap=$last_snap
127 stale=true
128 # fresh if $svp has $last_snap as a snapshot,
129 if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
130 grep -xF btrbk/$last_snap &>/dev/null; then
131 stale=false
132 else # or else $svp is a snapshot of $last_snap. we use a uuid
133 # comparison, which if I remember from the docs, is a bit more
134 # robust, perhaps to renames.
135 last_snap_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
136 if btrfs sub show $svp| grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev/null; then
137 stale=false
138 fi
139 fi
140
141 stale-file
142
143 done
144 exit $ret