Detach mounted snapshots before repository retention

This commit is contained in:
Archipelago 2026-11-20 20:43:47 +03:00
parent 9b501f49b9
commit bc7cfec84a
3 changed files with 89 additions and 3 deletions

View file

@ -1,6 +1,32 @@
# Infrastructure implementation status
Updated 2026-11-17. Architectural acceptance is separate from implementation.
Updated 2026-11-20. Architectural acceptance is separate from implementation.
## Snapshot retention detaches mounted snapshots (2026-11-20)
The fourth monthly capture exposed a retention bug: `zfs destroy` of the oldest
snapshot failed with `dataset is busy`, the capture script aborted, and the
portal refused the build (`Снимок не создан`). Two independent mounts keep a
snapshot busy: the published read-only view
(`/opt/archipelago/data/public/arch-repo-snapshot/<date>` is a
`mount -t zfs -o ro` of the snapshot itself) and `.zfs/snapshot` automounts
created in *another* mount namespace, here the `/arch-rsync` mirror container
that had read `monthly-2026-11-12` through `/data/archlinux/.zfs/snapshot/...`.
Unmounting the host view was not enough; a mount inside the container's
namespace also blocks the destroy.
`deploy/mirror-snapshot.sh` now detaches the published view with bounded
retries, then scans `/proc/*/mountinfo` and unmounts the snapshot in every
namespace that still holds it (`nsenter -t PID -m -- umount`), verifying the
entry is really gone before continuing. Retention stops with a named PID and
mountpoint rather than silently keeping a fourth snapshot. The publish step
then mounts the new view, prunes the retired one and republishes
`snapshots.json`.
Applied on terra and verified: snapshots `2026-11-15`, `2026-11-17`,
`2026-11-20`; the retired `2026-11-12` is destroyed; all three views answer
HTTPS with `200`; the rsync container kept running. `infrastructure/tests/
control_portal.py` asserts the detach happens before `zfs destroy`.
## Complete CLI help and human daemon errors (2026-11-18)

View file

@ -16,10 +16,56 @@ else
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
return 1
}
# 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
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
echo "destroying oldest ${snaps[0]}"
zfs destroy "${snaps[0]}"
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)"

View file

@ -179,6 +179,20 @@ class Portal(unittest.TestCase):
self.assertIn('container_serves', script)
self.assertIn('excluded from the published list', script)
def test_snapshot_retention_detaches_before_destroy(self):
# A published view is a mount of the snapshot itself, and `zfs destroy`
# of a mounted snapshot fails with "dataset is busy". Retention must
# detach the view and every other namespace mount (`.zfs/snapshot`
# automounts of the mirror rsync container) before the destroy.
script = (Path(__file__).parents[1] / 'deploy/mirror-snapshot.sh').read_text()
destroy = script.index('zfs destroy')
self.assertLess(script.index('unmount_view "$view/$date"'), destroy)
self.assertLess(script.index('detach_snapshot_mounts "$snap"'), destroy)
self.assertIn('/proc/[0-9]*/mountinfo', script)
self.assertIn('nsenter -t "$pid" -m -- umount', script)
self.assertIn('dataset is busy', script)
self.assertIn('umount -l', script)
def test_release_unit_can_be_stopped_promptly(self):
# A long-running driver must not be a oneshot: systemd then refuses to
# deliver SIGTERM until TimeoutStopSec, so a stop looks ignored for minutes.