#!/bin/bash set -eu function snap { unsnap znap-take bsnap 1 mount_snapshots mkdir -p /a/boot mount --bind /boot /a/boot } function unsnap { if ! [ -d /a ]; then return fi umount /a/boot || true rmdir /a/boot || true if ls &> /dev/null /a/*; then umount /a/* || true rmdir /a/* || true fi rmdir /a || true } function list_snapshots { local fsname for fsname in $(zfs list -H -o name); do zfs list -H -o name -S creation -t snapshot -d 1 -r "$fsname" | \ grep '@znap_....-..-..-...._bsnap' | \ head -n1 done } function mount_snapshots { local snapname local fsname local mountpoint for snapname in $(list_snapshots); do fsname="${snapname%@*}" mountpoint="/a/$(echo "$fsname" | cut -d/ -f2- | tr '/' '_')" mkdir -p "$mountpoint" mount -t zfs "$snapname" "$mountpoint" done } if [ "$(id -u)" -ne 0 ]; then echo "Must be root" exit 1 fi if [ "${1:-}" = "off" ]; then unsnap elif [ "${1:-}" = "on" ]; then snap else echo "Usage: $0 on|off" exit 1 fi