lots of updates
[distro-setup] / check-subvol-stale
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16 # usage: $0 SUBVOL_MOUNTPOINT...
17 #
18 # In git, this is not not executable because it's meant to be installed
19 # using ./install-my-scripts
20 #
21 # If latest subvols $@ are not mounted, exit 1, print message, and touch
22 # /nocow/btrfs-stale/$subvol
23 #
24 # Either SUBVOL_MOUNTPOINT is a snapshot of the latest, or
25 # the latest snapshot is snapshot of SUBVOL_MOUNTPOINT.
26
27 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
28
29 set -eE -o pipefail
30 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
31
32 shopt -s nullglob
33
34 if [[ ! $@ ]]; then
35 echo "$0: error: expected mountpoint argument"
36 fi
37
38 stale-file() {
39 stale_dir=/nocow/btrfs-stale
40 stale_file=$stale_dir/$vol
41 if $stale; then
42 mkdir -p $stale_dir
43 printf "%s\n" $freshest_snap > $stale_file
44 ret=1
45 else
46 rm -f $stale_file
47 fi
48
49 }
50
51 ret=0
52 for d; do
53 vol=${d##*/}
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
58
59
60
61 snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century.
62 if [[ ! $snaps ]]; then
63 # no snapshots yet
64 echo "$0: warning. no snapshots found. this is expected for a brand new volume"
65 continue
66 fi
67
68 # get info on last received sub
69 last_received_gen=0
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
76 last_received=$f
77 fi
78 fi
79 done
80
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 | ...
84 last_snap=$(
85 for s in ${snaps[@]}; do
86 f=${s##*/}
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}'
89 )
90 if [[ ! $last_snap ]]; then
91 # should not happen.
92 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}"
93 ret=1
94 continue
95 fi
96
97 if [[ ! -e $svp ]]; then
98 echo "$0: warning: subvol we want to check does not exist: $svp"
99 stale-file=$last_snap
100 stale-file
101 continue
102 fi
103
104
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
109 stale=false
110 else
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
113 stale=true
114 fi
115 stale-file
116 continue
117 fi
118
119 freshest_snap=$last_snap
120 stale=true
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
124 stale=false
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
130 stale=false
131 fi
132 fi
133
134 stale-file
135
136 done
137 exit $ret