lots o fixes, beets, shellcheck 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 tmpf=$(mktemp)
101 d tmpf=$tmpf
102 for d; do
103
104 if $subvol_path; then
105 svp=$d
106 root_dir=${d%/*}
107 subvol_dir=${d##*/}
108 vol=$subvol_dir
109 else
110 vol=${d##*/}
111 # second field, non-comment line == $d
112 dev=$(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc/fstab /etc/mtab|head -n1)
113
114 d dev=$dev
115 subvol_dir=$(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc/fstab /etc/mtab|head -n1)
116 if [[ ! $subvol_dir ]]; then
117 continue
118 fi
119 d subvol_dir=$subvol_dir
120
121 ### begin getting root_dir
122 ### this is duplicated in mount-latest-subvol
123 # note, we need $dev because $d might not be mounted, and we do this loop
124 # because the device in fstab for the rootfs can be different.
125 for devx in $(btrfs fil show $dev| sed -rn 's#.*path (\S+)$#\1#p'); do
126 if [[ $devx == dm-* ]]; then
127 devx=/dev/$devx
128 d mapper-dev $devx
129 mapper-dev devx
130 fi
131 d devx=$devx
132 root_dir=$(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=[05]\b.*,\1,p" /etc/mtab /etc/fstab|head -n1)
133 if [[ $root_dir ]]; then
134 d root_dir=$root_dir
135 break
136 fi
137 done
138 if [[ ! $root_dir ]]; then
139 echo "$0: error could not find root subvol mount for $dev" >&2
140 exit 1
141 fi
142 ### end getting root_dir
143 svp=$root_dir/$subvol_dir # subvolume path
144 d "svp=$svp # subvolume path"
145 fi
146
147 # Assumes we are in the 21st century.
148 ls -1dvrq $root_dir/btrbk/$subvol_dir.20* >$tmpf
149 mapfile -t snaps <$tmpf
150 if [[ ! ${snaps[*]} ]]; then
151 # no snapshots yet
152 # TODO: make this an error and override with a cli flag
153 echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume"
154 continue
155 fi
156
157 # last_snap by date.
158 last_snap="${snaps[0]}"
159 ## alternate slower alternative which would not rely on ls sorting:
160 # last_snap=$(
161 # for s in ${snaps[@]}; do
162 # f=${s##*/}
163 # unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
164 # printf "%s %s\n" $unix_time $s # part of the pipeline
165 # # sort will fail
166 # done | sort -r | head -n 1 | awk '{print $2}' || [[ ${PIPESTATUS[1]} == 141 || ${PIPESTATUS[0]} == 32 ]]
167 # )
168 # if [[ ! $last_snap ]]; then
169 # # should not happen.
170 # echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
171 # exit 1
172 # fi
173 d last_snap=$last_snap
174
175 if [[ ! -e $svp ]]; then
176 echo "$0: warning: subvol does not exist: $svp"
177 echo "$0 assuming this host was just for receiving and latest snap is freshest"
178 freshest_snap=$last_snap
179 stale=true
180 stale-file
181 continue
182 fi
183
184 # get info on last received sub
185 last_received=
186 last_received_cgen=0
187 for f in ${snaps[@]}; do
188 show="$(btrfs sub show $f)"
189 if echo "$show" | grep -E "Received UUID:\s+[[:alnum:]]" &>/dev/null; then
190 d found received uuid in $f
191 cgen=$(echo "$show" | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
192 if [[ $cgen -gt $last_received_cgen ]]; then
193 last_received_cgen=$cgen
194 last_received=$f
195 elif [[ $last_received ]]; then
196 # optimization: we are looking in reverse order by date, so if
197 # we find one that has a lesser cgen, assume the rest will all
198 # be lesser.
199 break
200 fi
201 fi
202 done
203 d last_received_cgen=$last_received_cgen
204 d last_received=$last_received
205
206
207 # if there is a last_received, we can assume stale or fresh if we are newer/older
208 if [[ $last_received ]]; then
209 svp_cgen=$(btrfs sub show $svp | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
210 d svp_cgen=$svp_cgen
211 if [[ $svp_cgen -ge $last_received_cgen ]]; then
212 stale=false
213 else
214 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"
215 freshest_snap=$last_received
216 stale=true
217 fi
218 stale-file
219 continue
220 fi
221
222 # fallback to using last_snap as the freshest
223 freshest_snap=$last_snap
224 stale=true
225 # fresh if $svp has $last_snap as a snapshot,
226 if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
227 grep -xF ${last_snap#"$root_dir"/} >/dev/null; then
228 stale=false
229 else # or else $svp is a snapshot of $last_snap. we use a uuid
230 # comparison, which if I remember from the docs, is a bit more
231 # robust, perhaps to renames.
232 last_snap_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
233 if btrfs sub show $svp| grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev/null; then
234 stale=false
235 fi
236 fi
237
238 stale-file
239 done
240 rm $tmpf