83 lines
3.6 KiB
Bash
83 lines
3.6 KiB
Bash
#!/bin/bash
|
|
# D-108: immutable Arch mirror snapshot. Create, retain at most three, publish
|
|
# the read-only views. A snapshot for the same UTC date is never replaced, so a
|
|
# pinned build can never be silently rewritten underneath itself.
|
|
set -euo pipefail
|
|
dataset=tank/archipelago/archlinux
|
|
today=$(date -u +%Y-%m-%d)
|
|
tag="monthly-${today}"
|
|
snap="${dataset}@${tag}"
|
|
|
|
if zfs list -H -t snapshot -o name "$snap" >/dev/null 2>&1; then
|
|
echo "snapshot $snap already exists; immutable captures are not replaced" >&2
|
|
else
|
|
zfs snapshot "$snap"
|
|
echo "created $snap"
|
|
fi
|
|
|
|
# Retain at most three monthly snapshots (contract). Names sort by UTC date.
|
|
# ZFS refuses to destroy a mounted snapshot ("dataset is busy"), and two kinds of
|
|
# mounts keep a snapshot busy: our own published read-only view, and
|
|
# `.zfs/snapshot` automounts created whenever another process (the mirror rsync
|
|
# container, for example) reads a snapshot through that path. Both live in the
|
|
# mount namespace of whoever opened them, so retention detaches the view first
|
|
# and then every remaining namespace mount before the destroy.
|
|
view=$(realpath -m /opt/archipelago/data/public/arch-repo-snapshot)
|
|
unmount_view() {
|
|
local target=$1 attempt
|
|
mountpoint -q "$target" || return 0
|
|
for attempt in 1 2 3 4 5; do
|
|
umount "$target" 2>/dev/null && return 0
|
|
sleep 1
|
|
done
|
|
# A reader can hold the retired view open; a lazy detach removes it from the
|
|
# mount table (what ZFS checks) without waiting for that reader to finish.
|
|
umount -l "$target" 2>/dev/null || true
|
|
! mountpoint -q "$target"
|
|
}
|
|
# A mount shared by several processes disappears for all of them at once, so a
|
|
# failed attempt is only an error when the entry is still present afterwards.
|
|
detach_snapshot_mounts() {
|
|
local snap=$1 entry pid target failed=0
|
|
for entry in /proc/[0-9]*/mountinfo; do
|
|
target=$(grep -F " - zfs $snap " "$entry" 2>/dev/null | awk 'NR == 1 { print $5 }')
|
|
[[ -n $target ]] || continue
|
|
pid=${entry#/proc/}; pid=${pid%/mountinfo}
|
|
nsenter -t "$pid" -m -- umount "$target" 2>/dev/null || true
|
|
if grep -qF " - zfs $snap " "$entry" 2>/dev/null; then
|
|
# A container keeps the propagated copy busy (cwd, open files) and
|
|
# refuses a normal unmount. A lazy detach drops it from that mount
|
|
# table without stopping the container; ZFS only needs the table.
|
|
nsenter -t "$pid" -m -- umount -l "$target" 2>/dev/null || true
|
|
fi
|
|
if grep -qF " - zfs $snap " "$entry" 2>/dev/null; then
|
|
echo "warning: $snap is still mounted at $target in the namespace of pid $pid ($(tr '\0' ' ' < /proc/"$pid"/cmdline 2>/dev/null | cut -c1-60))" >&2
|
|
failed=1
|
|
fi
|
|
done
|
|
return "$failed"
|
|
}
|
|
mapfile -t snaps < <(zfs list -H -o name -t snapshot -r "$dataset" | grep -E '@monthly-' | sort)
|
|
while ((${#snaps[@]} > 3)); do
|
|
snap=${snaps[0]}
|
|
date=${snap##*@monthly-}
|
|
if ! unmount_view "$view/$date"; then
|
|
echo "cannot unmount $view/$date: a client is still reading $snap" >&2
|
|
echo "stop that client, or detach with 'umount -l $view/$date', and rerun" >&2
|
|
exit 1
|
|
fi
|
|
if ! detach_snapshot_mounts "$snap"; then
|
|
echo "retention stopped before destroying $snap: it is still mounted elsewhere" >&2
|
|
exit 1
|
|
fi
|
|
echo "destroying oldest $snap"
|
|
if ! zfs destroy "$snap"; then
|
|
echo "retention stopped: cannot destroy $snap" >&2
|
|
exit 1
|
|
fi
|
|
snaps=("${snaps[@]:1}")
|
|
done
|
|
echo "retained ${#snaps[@]} monthly snapshot(s)"
|
|
|
|
/usr/local/sbin/archipelago-mirror-snapshot-mount
|
|
echo "url=https://mirror.nw-sys.ru/archipelago/arch-repo-snapshot/${today}/\$repo/os/\$arch"
|