add pull support, untested
[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
17 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
18
19 set -eE -o pipefail
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21
22 shopt -s nullglob
23
24 usage() {
25 cat <<EOF
26 usage: $0 SUBVOL_MOUNTPOINT...
27
28 In git, this is not not executable because it's meant to be installed
29 using ./install-my-scripts
30
31 If latest subvols \$@ are not mounted, exit 1, print message, and touch
32 /nocow/btrfs-stale/\$subvol
33
34 Either SUBVOL_MOUNTPOINT is a snapshot of the latest, or
35 the latest snapshot is snapshot of SUBVOL_MOUNTPOINT.
36
37 -d Enable debug output
38 -h|--help Print help and exit.
39
40 Note: Uses GNU getopt options parsing style
41 EOF
42 exit $1
43 }
44
45 ##### begin command line parsing ########
46
47 debug=false # default
48 temp=$(getopt -l help,d hd "$@") || usage 1
49 eval set -- "$temp"
50 while true; do
51 case $1 in
52 -d) debug=true; shift ;;
53 -h|--help) usage ;;
54 --) shift; break ;;
55 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
56 esac
57 done
58
59 if [[ ! $@ ]]; then
60 echo "$0: error: expected mountpoint argument"
61 fi
62
63 stale-file() {
64 stale_dir=/nocow/btrfs-stale
65 stale_file=$stale_dir/$vol
66 if $stale; then
67 mkdir -p $stale_dir
68 printf "%s\n" $freshest_snap > $stale_file
69 ret=1
70 else
71 rm -f $stale_file
72 fi
73
74 }
75 d() {
76 if $debug; then
77 printf "%s\n" "$*"
78 fi
79 }
80
81 ret=0
82 for d; do
83 vol=${d##*/}
84 dev=$(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc/fstab|head -n1)
85 d dev=$dev
86 subvol_dir=$(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc/fstab|head -n1)
87 d subvol_dir=$subvol_dir
88 # note, we need $dev because $d might not be mounted, and we do this loop
89 # because the device in fstab for the rootfs can be different.
90 for devx in $(btrfs fi show $dev| sed -rn 's#.*path (/\S+)$#\1#p'); do
91 root_dir=$(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=0\b.*,\1,p" /etc/fstab|head -n1)
92 if [[ $root_dir ]]; then break; fi
93 done
94 svp=$root_dir/$subvol_dir # subvolume path
95 d svp=$svp
96
97 snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century.
98 if [[ ! $snaps ]]; then
99 # no snapshots yet
100 echo "$0: warning. no snapshots found. this is expected for a brand new volume"
101 continue
102 fi
103
104 # get info on last received sub
105 last_received_cgen=0
106 for f in ${snaps[@]}; do
107 show="$(btrfs sub show $f)"
108 if echo "$show" | grep -E "Received UUID:\s+[[:alnum:]]" &>/dev/null; then
109 cgen=$(echo "$show" | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
110 if [[ $cgen -gt $last_received_cgen ]]; then
111 last_received_cgen=$cgen
112 last_received=$f
113 fi
114 fi
115 done
116 d last_received_cgen=$cgen
117 d last_received=$f
118
119 # Get last_snap by date.
120 # when a btrbk bugfix makes it into the distro,
121 # we might replace this with btrbk list latest /mnt/root/$vol | ...
122 last_snap=$(
123 for s in ${snaps[@]}; do
124 f=${s##*/}
125 unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
126 printf "%s %s\n" $unix_time $s
127 done | sort -r | head -n 1 | awk '{print $2}'
128 )
129 if [[ ! $last_snap ]]; then
130 # should not happen.
131 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}"
132 ret=1
133 continue
134 fi
135
136 if [[ ! -e $svp ]]; then
137 echo "$0: warning: subvol does not exist: $svp"
138 echo "$0 assuming this host was just for receiving and latest snap is freshest"
139 freshest_snap=$last_snap
140 stale=true
141 stale-file
142 continue
143 fi
144
145
146 # if there is a last_received, we can assume stale or fresh if we are newer/older
147 if [[ $last_received ]]; then
148 svp_cgen=$(btrfs sub show $svp | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
149 d svp_cgen=$svp_cgen
150 if [[ $svp_cgen -ge $last_received_cgen ]]; then
151 stale=false
152 else
153 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"
154 freshest_snap=$last_received
155 stale=true
156 fi
157 stale-file
158 continue
159 fi
160
161 # fallback to using last_snap as the freshest
162 freshest_snap=$last_snap
163 stale=true
164 # fresh if $svp has $last_snap as a snapshot,
165 if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
166 grep -xF btrbk/$last_snap &>/dev/null; then
167 stale=false
168 else # or else $svp is a snapshot of $last_snap. we use a uuid
169 # comparison, which if I remember from the docs, is a bit more
170 # robust, perhaps to renames.
171 last_snap_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
172 if btrfs sub show $svp| grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev/null; then
173 stale=false
174 fi
175 fi
176
177 stale-file
178
179 done
180 exit $ret