finish bind mount, removed in next commit
[distro-setup] / check-subvol-stale
index d9582286126696086ebf425997270a936210d3d7..127718257ba1000ac95dbe65964d296b726b4584 100644 (file)
@@ -1,4 +1,28 @@
 #!/bin/bash
+# Copyright (C) 2016 Ian Kelling
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# usage: $0 SUBVOL_MOUNTPOINT...
+#
+# In git, this is not not executable because it's meant to be installed
+# using ./install-my-scripts
+#
+# If latest subvols $@ are not mounted, exit 1, print message, and touch
+# /nocow/btrfs-stale/$subvol
+#
+# Either SUBVOL_MOUNTPOINT is a snapshot of the latest, or
+# the latest snapshot is snapshot of SUBVOL_MOUNTPOINT.
 
 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
 
@@ -7,11 +31,15 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
 shopt -s nullglob
 
+if [[ ! $@ ]]; then
+    echo "$0: error: expected mountpoint argument"
+fi
+
 ret=0
 for d; do
     vol=${d##*/}
     cd /mnt/root/btrbk
-    snaps=($vol.20*)
+    snaps=($vol.20*) # Assumes we are in the 21st century.
     if [[ ! $snaps ]]; then
         # no snapshots yet
         continue
@@ -25,12 +53,17 @@ for d; do
         done | sort -r | head -n 1 | awk '{print $2}'
              )
     if [[ ! $last_snap ]]; then
-        echo "$d stale"
+        # should not happen.
+        echo "$0: error: could not find latest snapshot for $d among ${snaps[@]}"
         ret=1
         continue
     fi
     stale=true
-    if btrfs sub show $d|sed '0,/^\t*Snapshot(s):/d;s/^\s*//' | \
+    # check that $d has $last_snap as a snapshot,
+    # or else $d is a snapshot of $last_snap. In the second
+    # case, we use a uuid comparison, which if I remember from the
+    # docs, is a bit more robust, perhaps to renames.
+    if btrfs sub show $d 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
             grep -xF btrbk/$last_snap &>/dev/null; then
         stale=false
     else
@@ -52,8 +85,3 @@ for d; do
     fi
 done
 exit $ret
-
-# todo: figure out what to do when there are no
-# snapshots yet. I guess that should be
-# yes to being stale? see the implications
-# in the other script.