a3d0149e24fa574ccec2d62a5e427db81935851e
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 If latest subvols \$@ are not mounted, print a message, and print
31 the unstale subvol name into /nocow/btrfs-stale/\$subvol
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
37 -p Args are SUBVOL_PATH, not mountpoints from which we derive
39 -v|--verbose Be more verbose
40 -h|--help Print help and exit.
42 Note: Uses GNU getopt options parsing style
47 ##### begin command line parsing ########
51 temp
=$
(getopt
-l help,verbose hpv
"$@") || usage
1
55 -p) subvol_path
=true
;;
56 -v|
--verbose) verbose
=true
;;
59 *) echo "$0: unexpected args: $*" >&2 ; usage
1 ;;
65 echo "$0: error: expected mountpoint argument"
69 stale_dir
=/nocow
/btrfs-stale
70 stale_file
=$stale_dir/$vol
73 printf "%s\n" $freshest_snap > $stale_file
85 # duplicated in mount-latest-sub
89 if [[ $devref == /dev
/dm-
* ]]; then
90 for mapdev
in /dev
/mapper
/*; do
91 if [[ $
(readlink
-f $mapdev) == "$devref" ]]; then
100 if $subvol_path; then
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)
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
115 d subvol_dir
=$subvol_dir
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
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
134 if [[ ! $root_dir ]]; then
135 echo "$0: error could not find root subvol mount for $dev" >&2
138 ### end getting root_dir
139 svp
=$root_dir/$subvol_dir # subvolume path
140 d
"svp=$svp # subvolume path"
143 snaps
=($root_dir/btrbk
/$subvol_dir.20*) # Assumes we are in the 21st century.
144 if [[ ! ${snaps[*]} ]]; then
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"
151 # get info on last received sub
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
165 d last_received_cgen
=$last_received_cgen
166 d last_received
=$last_received
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 | ...
172 for s
in ${snaps[@]}; do
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
177 done |
sort -r |
head -n 1 |
awk '{print $2}' ||
[[ ${PIPESTATUS[1]} == 141 ||
${PIPESTATUS[0]} == 32 ]]
179 if [[ ! $last_snap ]]; then
181 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
184 d last_snap
=$last_snap
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
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')
200 if [[ $svp_cgen -ge $last_received_cgen ]]; then
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
211 # fallback to using last_snap as the freshest
212 freshest_snap
=$last_snap
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
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