2 # Copyright (C) 2016 Ian Kelling
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
19 source /usr
/local
/lib
/err
25 usage: $0 SUBVOL_MOUNTPOINT... | -p SUBVOL_PATH...
27 In git this is not not executable because it's meant to be installed
28 using ./install-my-scripts
30 Print the unstale subvol name into /nocow/btrfs-stale/\$subvol
32 If latest subvols \$@ are not mounted, print a message to terminal.
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
38 -p Args are SUBVOL_PATH, not mountpoints from which we derive
40 -v|--verbose Be more verbose
41 -h|--help Print help and exit.
43 Note: Uses GNU getopt options parsing style
48 ##### begin command line parsing ########
52 temp
=$
(getopt
-l help,verbose hpv
"$@") || usage
1
56 -p) subvol_path
=true
;;
57 -v|
--verbose) verbose
=true
;;
60 *) echo "$0: unexpected args: $*" >&2 ; usage
1 ;;
66 echo "$0: error: expected mountpoint argument"
70 stale_dir
=/nocow
/btrfs-stale
71 stale_file
=$stale_dir/$vol
74 printf "%s\n" $freshest_snap > $stale_file
80 pre
="check-subvol-stale:${SSH_CLIENT:+ $HOSTNAME:}"
84 printf "$pre %s\n" "$*"
88 # duplicated in mount-latest-sub
89 # Reassign $1 var from /dev/dm- to corresponding /dev/mapper/
93 if [[ $devref == /dev
/dm-
* ]]; then
94 for mapdev
in /dev
/mapper
/*; do
95 if [[ $
(readlink
-f $mapdev) == "$devref" ]]; then
106 if $subvol_path; then
113 # second field, non-comment line == $d
114 dev
=$
(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc
/fstab
/etc
/mtab|
head -n1)
117 subvol_dir
=$
(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc
/fstab
/etc
/mtab|
head -n1)
118 if [[ ! $subvol_dir ]]; then
121 d subvol_dir
=$subvol_dir
123 ### begin getting root_dir
124 ### this is duplicated in mount-latest-subvol
125 # note, we need $dev because $d might not be mounted, and we do this loop
126 # because the device in fstab for the rootfs can be different.
127 for devx
in $
(btrfs fil show
$dev|
sed -rn 's#.*path (\S+)$#\1#p'); do
128 if [[ $devx == dm-
* ]]; then
134 root_dir
=$
(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=[05]\b.*,\1,p" /etc
/mtab
/etc
/fstab|
head -n1)
135 if [[ $root_dir ]]; then
140 if [[ ! $root_dir ]]; then
141 echo "$0: error could not find root subvol mount for $dev" >&2
144 ### end getting root_dir
145 svp
=$root_dir/$subvol_dir # subvolume path
146 d
"svp=$svp # subvolume path"
149 # note: relying on null glob
150 ls_args
=($root_dir/btrbk
/$subvol_dir.20*)
151 if (( ${#ls_args[@]} )); then
152 # Assumes we are in the 21st century.
153 ls -1dvrq $root_dir/btrbk
/$subvol_dir.20* >$tmpf
154 mapfile
-t snaps
<$tmpf
157 # TODO: make this an error and override with a cli flag
158 echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume"
163 last_snap
="${snaps[0]}"
166 $root_dir/btrbk
/$subvol_dir.20*) : ;;
168 echo "$0: error: unexpected last_snap:$last_snap"
173 d last_snap
=$last_snap
174 ## alternate slower alternative which would not rely on ls sorting:
176 # for s in ${snaps[@]}; do
178 # unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
179 # printf "%s %s\n" $unix_time $s # part of the pipeline
181 # done | sort -r | head -n 1 | awk '{print $2}' || [[ ${PIPESTATUS[1]} == 141 || ${PIPESTATUS[0]} == 32 ]]
183 # if [[ ! $last_snap ]]; then
184 # # should not happen.
185 # echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
189 if [[ ! -e $svp ]]; then
190 echo "$0: warning: subvol does not exist: $svp"
191 echo "$0 assuming this host was just for receiving and latest snap is freshest"
192 freshest_snap
=$last_snap
198 # get info on last received sub
201 for f
in ${snaps[@]}; do
202 show
="$(btrfs sub show $f)"
203 if echo "$show" |
grep -E "Received UUID:\s+[[:alnum:]]" &>/dev
/null
; then
204 d found received uuid
in $f
205 cgen
=$
(echo "$show" |
sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
206 if [[ $cgen -gt $last_received_cgen ]]; then
207 last_received_cgen
=$cgen
209 elif [[ $last_received ]]; then
210 # optimization: we are looking in reverse order by date, so if
211 # we find one that has a lesser cgen, assume the rest will all
217 d last_received_cgen
=$last_received_cgen
218 d last_received
=$last_received
221 # if there is a last_received, we can assume stale or fresh if we are newer/older
222 if [[ $last_received ]]; then
223 svp_cgen
=$
(btrfs sub show
$svp |
sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
225 if [[ $svp_cgen -ge $last_received_cgen ]]; then
228 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"
229 freshest_snap
=$last_received
236 # fallback to using last_snap as the freshest
237 freshest_snap
=$last_snap
239 # fresh if $svp has $last_snap as a snapshot,
240 if btrfs sub show
$svp 2>/dev
/null |
sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
241 grep -xF ${last_snap#"$root_dir"/} >/dev
/null
; then
243 else # or else $svp is a snapshot of $last_snap. we use a uuid
244 # comparison, which if I remember from the docs, is a bit more
245 # robust, perhaps to renames.
246 last_snap_uuid
=$
(btrfs sub show
$last_snap|
awk '$1 == "UUID:" {print $2}')
247 if btrfs sub show
$svp|
grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev
/null
; then