various fixes
[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, print a message, and print
32 the unstale subvol name into /nocow/btrfs-stale/\$subvol
33
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
36 SUBVOL_MOUNTPOINT.
37
38 -d Enable debug output
39 -h|--help Print help and exit.
40
41 Note: Uses GNU getopt options parsing style
42 EOF
43 exit $1
44 }
45
46 ##### begin command line parsing ########
47
48 debug=false # default
49 temp=$(getopt -l help,d hd "$@") || usage 1
50 eval set -- "$temp"
51 while true; do
52 case $1 in
53 -d) debug=true; shift ;;
54 -h|--help) usage ;;
55 --) shift; break ;;
56 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
57 esac
58 done
59
60 if [[ ! $@ ]]; then
61 echo "$0: error: expected mountpoint argument"
62 fi
63
64 stale-file() {
65 stale_dir=/nocow/btrfs-stale
66 stale_file=$stale_dir/$vol
67 if $stale; then
68 mkdir -p $stale_dir
69 printf "%s\n" $freshest_snap > $stale_file
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 for d; do
82 vol=${d##*/}
83 # second field, non-comment line == $d
84 dev=$(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc/fstab /etc/mtab|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 /etc/mtab|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 d devx=$devx
92 root_dir=$(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=[05]\b.*,\1,p" /etc/mtab|head -n1)
93 if [[ $root_dir ]]; then
94 d root_dir=$root_dir
95 break
96 fi
97 done
98 if [[ ! $root_dir ]]; then
99 echo "$0: error could not find root subvol mount for $dev" >&2
100 exit 1
101 fi
102 svp=$root_dir/$subvol_dir
103 d "svp=$svp # subvolume path"
104
105 snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century.
106 if [[ ! $snaps ]]; then
107 # no snapshots yet
108 echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume"
109 continue
110 fi
111
112 # get info on last received sub
113 last_received=
114 last_received_cgen=0
115 for f in ${snaps[@]}; do
116 show="$(btrfs sub show $f)"
117 if echo "$show" | grep -E "Received UUID:\s+[[:alnum:]]" &>/dev/null; then
118 d found received uuid in $f
119 cgen=$(echo "$show" | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
120 if [[ $cgen -gt $last_received_cgen ]]; then
121 last_received_cgen=$cgen
122 last_received=$f
123 fi
124 fi
125 done
126 d last_received_cgen=$last_received_cgen
127 d last_received=$last_received
128
129 # Get last_snap by date.
130 # when a btrbk bugfix makes it into the distro,
131 # we might replace this with btrbk list latest /mnt/root/$vol | ...
132 last_snap=$(
133 for s in ${snaps[@]}; do
134 f=${s##*/}
135 unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
136 printf "%s %s\n" $unix_time $s
137 done | sort -r | head -n 1 | awk '{print $2}'
138 )
139 if [[ ! $last_snap ]]; then
140 # should not happen.
141 echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
142 exit 1
143 fi
144 d last_snap=$last_snap
145
146 if [[ ! -e $svp ]]; then
147 echo "$0: warning: subvol does not exist: $svp"
148 echo "$0 assuming this host was just for receiving and latest snap is freshest"
149 freshest_snap=$last_snap
150 stale=true
151 stale-file
152 continue
153 fi
154
155
156 # if there is a last_received, we can assume stale or fresh if we are newer/older
157 if [[ $last_received ]]; then
158 svp_cgen=$(btrfs sub show $svp | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
159 d svp_cgen=$svp_cgen
160 if [[ $svp_cgen -ge $last_received_cgen ]]; then
161 stale=false
162 else
163 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"
164 freshest_snap=$last_received
165 stale=true
166 fi
167 stale-file
168 continue
169 fi
170
171 # fallback to using last_snap as the freshest
172 freshest_snap=$last_snap
173 stale=true
174 # fresh if $svp has $last_snap as a snapshot,
175 if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
176 grep -xF ${last_snap#$root_dir/} ; then
177 stale=false
178 else # or else $svp is a snapshot of $last_snap. we use a uuid
179 # comparison, which if I remember from the docs, is a bit more
180 # robust, perhaps to renames.
181 last_snap_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
182 if btrfs sub show $svp| grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev/null; then
183 stale=false
184 fi
185 fi
186
187 stale-file
188 done