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.
16 # usage: $0 SUBVOL_MOUNTPOINT...
18 # In git, this is not not executable because it's meant to be installed
19 # using ./install-my-scripts
21 # If latest subvols $@ are not mounted, exit 1, print message, and touch
22 # /nocow/btrfs-stale/$subvol
24 # Either SUBVOL_MOUNTPOINT is a snapshot of the latest, or
25 # the latest snapshot is snapshot of SUBVOL_MOUNTPOINT.
27 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
30 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
35 echo "$0: error: expected mountpoint argument"
39 stale_dir
=/nocow
/btrfs-stale
40 stale_file
=$stale_dir/$vol
43 printf "%s\n" $freshest_snap > $stale_file
54 dev
=$
(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc
/fstab|
head -n1)
55 subvol_dir
=$
(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc
/fstab|
head -n1)
56 root_dir
=$
(sed -rn "s,^\s*$dev\s+(\S+).*\bsubvolid=0\b.*,\1,p" /etc
/fstab|
head -n1)
57 svp
=$root_dir/$subvol_dir # subvolume path
61 snaps
=($root_dir/btrbk
/$subvol_dir.20*) # Assumes we are in the 21st century.
62 if [[ ! $snaps ]]; then
64 echo "$0: warning. no snapshots found. this is expected for a brand new volume"
68 # get info on last received sub
70 for f
in ${snaps[@]}; do
71 show
="$(btrfs sub show $f)"
72 if echo "$show" |
grep -E "Received UUID:\s+[[:alnum:]]" &>/dev
/null
; then
73 cgen
=$
(echo "$show" |
sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
74 if [[ $cgen -gt $last_received_gen ]]; then
75 last_received_cgen
=$cgen
81 # Get last_snap by date.
82 # when a btrbk bugfix makes it into the distro,
83 # we might replace this with btrbk list latest /mnt/root/$vol | ...
85 for s
in ${snaps[@]}; do
87 printf "%s %s\n" $
(date -d $
(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s
) $f
88 done |
sort -r |
head -n 1 |
awk '{print $2}'
90 if [[ ! $last_snap ]]; then
92 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}"
97 if [[ ! -e $svp ]]; then
98 echo "$0: warning: subvol we want to check does not exist: $svp"
105 # if there is a last_received, we can assume stale or fresh if we are newer/older
106 if [[ $last_received ]]; then
107 svp_cgen
=$
(btrfs sub show
$svp |
sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
108 if [[ $svp_cgen -ge $last_received_cgen ]]; then
111 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"
112 freshest_snap
=$last_received
119 freshest_snap
=$last_snap
121 # fresh if $svp has $last_snap as a snapshot,
122 if btrfs sub show
$svp 2>/dev
/null |
sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
123 grep -xF btrbk
/$last_snap &>/dev
/null
; then
125 else # or else $svp is a snapshot of $last_snap. we use a uuid
126 # comparison, which if I remember from the docs, is a bit more
127 # robust, perhaps to renames.
128 last_snap_uuid
=$
(btrfs sub show
$last_snap|
awk '$1 == "UUID:" {print $2}')
129 if btrfs sub show
$svp|
grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev
/null
; then