misc fixes and improvements
[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
17 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
18
19 source /usr/local/lib/err
20
21 shopt -s nullglob
22
23 usage() {
24 cat <<EOF
25 usage: $0 SUBVOL_MOUNTPOINT... | -p SUBVOL_PATH...
26
27 In git this is not not executable because it's meant to be installed
28 using ./install-my-scripts
29
30 If latest subvols \$@ are not mounted, print a message, and print
31 the unstale subvol name into /nocow/btrfs-stale/\$subvol
32
33 Fresh is opposite of stale. To be fresh, either SUBVOL_MOUNTPOINT is a
34 snapshot of the latest, or the latest snapshot is snapshot of
35 SUBVOL_MOUNTPOINT.
36
37 -p Args are SUBVOL_PATH, not mountpoints from which we derive
38 that information.
39 -v|--verbose Be more verbose
40 -h|--help Print help and exit.
41
42 Note: Uses GNU getopt options parsing style
43 EOF
44 exit $1
45 }
46
47 ##### begin command line parsing ########
48
49 subvol_path=false
50 verbose=false
51 temp=$(getopt -l help,verbose hpv "$@") || usage 1
52 eval set -- "$temp"
53 while true; do
54 case $1 in
55 -p) subvol_path=true ;;
56 -v|--verbose) verbose=true ;;
57 -h|--help) usage ;;
58 --) shift; break ;;
59 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
60 esac
61 shift
62 done
63
64 if [[ ! $1 ]]; then
65 echo "$0: error: expected mountpoint argument"
66 fi
67
68 stale-file() {
69 stale_dir=/nocow/btrfs-stale
70 stale_file=$stale_dir/$vol
71 if $stale; then
72 mkdir -p $stale_dir
73 printf "%s\n" $freshest_snap > $stale_file
74 else
75 rm -f $stale_file
76 fi
77
78 }
79 d() {
80 if $verbose; then
81 printf "%s\n" "$*"
82 fi
83 }
84
85 # duplicated in mount-latest-sub
86 mapper-dev() {
87 local mapdev
88 local -n devref=$1
89 if [[ $devref == /dev/dm-* ]]; then
90 for mapdev in /dev/mapper/*; do
91 if [[ $(readlink -f $mapdev) == "$devref" ]]; then
92 devref=$mapdev
93 break
94 fi
95 done
96 fi
97 }
98
99 for d; do
100 if $subvol_path; then
101 svp=$d
102 root_dir=${d%/*}
103 subvol_dir=${d##*/}
104 vol=$subvol_dir
105 else
106 vol=${d##*/}
107 # second field, non-comment line == $d
108 dev=$(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc/fstab /etc/mtab|head -n1)
109
110 d dev=$dev
111 subvol_dir=$(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc/fstab /etc/mtab|head -n1)
112 if [[ ! $subvol_dir ]]; then
113 continue
114 fi
115 d subvol_dir=$subvol_dir
116
117 ### begin getting root_dir
118 ### this is duplicated in mount-latest-subvol
119 # note, we need $dev because $d might not be mounted, and we do this loop
120 # because the device in fstab for the rootfs can be different.
121 for devx in $(btrfs fil show $dev| sed -rn 's#.*path (\S+)$#\1#p'); do
122 if [[ $devx == dm-* ]]; then
123 devx=/dev/$devx
124 d mapper-dev $devx
125 mapper-dev devx
126 fi
127 d devx=$devx
128 root_dir=$(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=[05]\b.*,\1,p" /etc/mtab /etc/fstab|head -n1)
129 if [[ $root_dir ]]; then
130 d root_dir=$root_dir
131 break
132 fi
133 done
134 if [[ ! $root_dir ]]; then
135 echo "$0: error could not find root subvol mount for $dev" >&2
136 exit 1
137 fi
138 ### end getting root_dir
139 svp=$root_dir/$subvol_dir # subvolume path
140 d "svp=$svp # subvolume path"
141 fi
142
143 snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century.
144 if [[ ! ${snaps[*]} ]]; then
145 # no snapshots yet
146 # TODO: make this an error and override with a cli flag
147 echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume"
148 continue
149 fi
150
151 # get info on last received sub
152 last_received=
153 last_received_cgen=0
154 for f in ${snaps[@]}; do
155 show="$(btrfs sub show $f)"
156 if echo "$show" | grep -E "Received UUID:\s+[[:alnum:]]" &>/dev/null; then
157 d found received uuid in $f
158 cgen=$(echo "$show" | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
159 if [[ $cgen -gt $last_received_cgen ]]; then
160 last_received_cgen=$cgen
161 last_received=$f
162 fi
163 fi
164 done
165 d last_received_cgen=$last_received_cgen
166 d last_received=$last_received
167
168 # Get last_snap by date.
169 # when a btrbk bugfix makes it into the distro,
170 # we might replace this with btrbk list latest /mnt/root/$vol | ...
171 last_snap=$(
172 for s in ${snaps[@]}; do
173 f=${s##*/}
174 unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
175 printf "%s %s\n" $unix_time $s # part of the pipeline
176 # sort will fail
177 done | sort -r | head -n 1 | awk '{print $2}' || [[ ${PIPESTATUS[1]} == 141 || ${PIPESTATUS[0]} == 32 ]]
178 )
179 if [[ ! $last_snap ]]; then
180 # should not happen.
181 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
182 exit 1
183 fi
184 d last_snap=$last_snap
185
186 if [[ ! -e $svp ]]; then
187 echo "$0: warning: subvol does not exist: $svp"
188 echo "$0 assuming this host was just for receiving and latest snap is freshest"
189 freshest_snap=$last_snap
190 stale=true
191 stale-file
192 continue
193 fi
194
195
196 # if there is a last_received, we can assume stale or fresh if we are newer/older
197 if [[ $last_received ]]; then
198 svp_cgen=$(btrfs sub show $svp | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
199 d svp_cgen=$svp_cgen
200 if [[ $svp_cgen -ge $last_received_cgen ]]; then
201 stale=false
202 else
203 d "$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"
204 freshest_snap=$last_received
205 stale=true
206 fi
207 stale-file
208 continue
209 fi
210
211 # fallback to using last_snap as the freshest
212 freshest_snap=$last_snap
213 stale=true
214 # fresh if $svp has $last_snap as a snapshot,
215 if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
216 grep -xF ${last_snap#$root_dir/} ; then
217 stale=false
218 else # or else $svp is a snapshot of $last_snap. we use a uuid
219 # comparison, which if I remember from the docs, is a bit more
220 # robust, perhaps to renames.
221 last_snap_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
222 if btrfs sub show $svp| grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev/null; then
223 stale=false
224 fi
225 fi
226
227 stale-file
228 done