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" "$@"
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
26 usage: $0 SUBVOL_MOUNTPOINT...
28 In git, this is not not executable because it's meant to be installed
29 using ./install-my-scripts
31 If latest subvols \$@ are not mounted, exit 1, print message, and print
32 the unstale subvol name into /nocow/btrfs-stale/\$subvol
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 -d Enable debug output
39 -h|--help Print help and exit.
41 Note: Uses GNU getopt options parsing style
46 ##### begin command line parsing ########
49 temp
=$
(getopt
-l help,d hd
"$@") || usage
1
53 -d) debug
=true
; shift ;;
56 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
61 echo "$0: error: expected mountpoint argument"
65 stale_dir
=/nocow
/btrfs-stale
66 stale_file
=$stale_dir/$vol
69 printf "%s\n" $freshest_snap > $stale_file
85 # second field, non-comment line == $d
86 dev
=$
(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc
/fstab
/etc
/mtab|
head -n1)
88 subvol_dir
=$
(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc
/fstab
/etc
/mtab|
head -n1)
89 d subvol_dir
=$subvol_dir
90 # note, we need $dev because $d might not be mounted, and we do this loop
91 # because the device in fstab for the rootfs can be different.
92 for devx
in $
(btrfs
fi show
$dev|
sed -rn 's#.*path (/\S+)$#\1#p'); do
93 root_dir
=$
(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=[05]\b.*,\1,p" /etc
/mtab|
head -n1)
94 if [[ $root_dir ]]; then break; fi
96 if [[ ! $root_dir ]]; then
97 echo "$0: error could not find root subvol mount for $dev" >&2
100 svp
=$root_dir/$subvol_dir
101 d
"svp=$svp # subvolume path"
103 snaps
=($root_dir/btrbk
/$subvol_dir.20*) # Assumes we are in the 21st century.
104 if [[ ! $snaps ]]; then
106 echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume"
110 # get info on last received sub
112 for f
in ${snaps[@]}; do
113 show
="$(btrfs sub show $f)"
114 if echo "$show" |
grep -E "Received UUID:\s+[[:alnum:]]" &>/dev
/null
; then
115 cgen
=$
(echo "$show" |
sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
116 if [[ $cgen -gt $last_received_cgen ]]; then
117 last_received_cgen
=$cgen
122 d last_received_cgen
=$cgen
125 # Get last_snap by date.
126 # when a btrbk bugfix makes it into the distro,
127 # we might replace this with btrbk list latest /mnt/root/$vol | ...
129 for s
in ${snaps[@]}; do
131 unix_time
=$
(date -d $
(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s
)
132 printf "%s %s\n" $unix_time $s
133 done |
sort -r |
head -n 1 |
awk '{print $2}'
135 if [[ ! $last_snap ]]; then
137 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}"
142 if [[ ! -e $svp ]]; then
143 echo "$0: warning: subvol does not exist: $svp"
144 echo "$0 assuming this host was just for receiving and latest snap is freshest"
145 freshest_snap
=$last_snap
152 # if there is a last_received, we can assume stale or fresh if we are newer/older
153 if [[ $last_received ]]; then
154 svp_cgen
=$
(btrfs sub show
$svp |
sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
156 if [[ $svp_cgen -ge $last_received_cgen ]]; then
159 echo "$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"
160 freshest_snap
=$last_received
167 # fallback to using last_snap as the freshest
168 freshest_snap
=$last_snap
170 # fresh if $svp has $last_snap as a snapshot,
171 if btrfs sub show
$svp 2>/dev
/null |
sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
172 grep -xF btrbk
/$last_snap &>/dev
/null
; then
174 else # or else $svp is a snapshot of $last_snap. we use a uuid
175 # comparison, which if I remember from the docs, is a bit more
176 # robust, perhaps to renames.
177 last_snap_uuid
=$
(btrfs sub show
$last_snap|
awk '$1 == "UUID:" {print $2}')
178 if btrfs sub show
$svp|
grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev
/null
; then