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
102 if $subvol_path; then
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)
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
117 d subvol_dir
=$subvol_dir
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
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
136 if [[ ! $root_dir ]]; then
137 echo "$0: error could not find root subvol mount for $dev" >&2
140 ### end getting root_dir
141 svp
=$root_dir/$subvol_dir # subvolume path
142 d
"svp=$svp # subvolume path"
145 snaps
=($root_dir/btrbk
/$subvol_dir.20*) # Assumes we are in the 21st century.
146 if [[ ! ${snaps[*]} ]]; then
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"
153 # get info on last received sub
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
167 d last_received_cgen
=$last_received_cgen
168 d last_received
=$last_received
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 | ...
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
183 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
186 d last_snap
=$last_snap
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
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')
202 if [[ $svp_cgen -ge $last_received_cgen ]]; then
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
213 # fallback to using last_snap as the freshest
214 freshest_snap
=$last_snap
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/} >/dev
/null
; then
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