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
86 # duplicated in mount-latest-sub
87 # Reassign $1 var from /dev/dm- to corresponding /dev/mapper/
91 if [[ $devref == /dev
/dm-
* ]]; then
92 for mapdev
in /dev
/mapper
/*; do
93 if [[ $
(readlink
-f $mapdev) == "$devref" ]]; then
104 if $subvol_path; then
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)
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
119 d subvol_dir
=$subvol_dir
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
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
138 if [[ ! $root_dir ]]; then
139 echo "$0: error could not find root subvol mount for $dev" >&2
142 ### end getting root_dir
143 svp
=$root_dir/$subvol_dir # subvolume path
144 d
"svp=$svp # subvolume path"
147 # note: relying on null glob
148 ls_args
=($root_dir/btrbk
/$subvol_dir.20*)
149 if (( ${#ls_args[@]} )); then
150 # Assumes we are in the 21st century.
151 ls -1dvrq $root_dir/btrbk
/$subvol_dir.20* >$tmpf
152 mapfile
-t snaps
<$tmpf
155 # TODO: make this an error and override with a cli flag
156 echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume"
161 last_snap
="${snaps[0]}"
164 $root_dir/btrbk
/$subvol_dir.20*) : ;;
166 echo "$0: error: unexpected last_snap:$last_snap"
171 d last_snap
=$last_snap
172 ## alternate slower alternative which would not rely on ls sorting:
174 # for s in ${snaps[@]}; do
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
179 # done | sort -r | head -n 1 | awk '{print $2}' || [[ ${PIPESTATUS[1]} == 141 || ${PIPESTATUS[0]} == 32 ]]
181 # if [[ ! $last_snap ]]; then
182 # # should not happen.
183 # echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
187 if [[ ! -e $svp ]]; then
188 echo "$0: warning: subvol does not exist: $svp"
189 echo "$0 assuming this host was just for receiving and latest snap is freshest"
190 freshest_snap
=$last_snap
196 # get info on last received sub
199 for f
in ${snaps[@]}; do
200 show
="$(btrfs sub show $f)"
201 if echo "$show" |
grep -E "Received UUID:\s+[[:alnum:]]" &>/dev
/null
; then
202 d found received uuid
in $f
203 cgen
=$
(echo "$show" |
sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
204 if [[ $cgen -gt $last_received_cgen ]]; then
205 last_received_cgen
=$cgen
207 elif [[ $last_received ]]; then
208 # optimization: we are looking in reverse order by date, so if
209 # we find one that has a lesser cgen, assume the rest will all
215 d last_received_cgen
=$last_received_cgen
216 d last_received
=$last_received
219 # if there is a last_received, we can assume stale or fresh if we are newer/older
220 if [[ $last_received ]]; then
221 svp_cgen
=$
(btrfs sub show
$svp |
sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
223 if [[ $svp_cgen -ge $last_received_cgen ]]; then
226 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"
227 freshest_snap
=$last_received
234 # fallback to using last_snap as the freshest
235 freshest_snap
=$last_snap
237 # fresh if $svp has $last_snap as a snapshot,
238 if btrfs sub show
$svp 2>/dev
/null |
sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
239 grep -xF ${last_snap#"$root_dir"/} >/dev
/null
; then
241 else # or else $svp is a snapshot of $last_snap. we use a uuid
242 # comparison, which if I remember from the docs, is a bit more
243 # robust, perhaps to renames.
244 last_snap_uuid
=$
(btrfs sub show
$last_snap|
awk '$1 == "UUID:" {print $2}')
245 if btrfs sub show
$svp|
grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev
/null
; then