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