mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-11-05 02:37:49 +00:00
fix(backup): mount the zfs snapshot itself so a container can read it
The zfs snapshot was read through <subject>/.zfs/snapshot/<name>. The kernel automounts that directory through a usermode helper that runs in the host's mount namespace, so a containerised baudolo never sees the mount and finds no snapshot. The snapshot is now mounted explicitly at <subject>/.baudolo-<tag>, beside where btrfs carves its own, and removed in order: umount, rmdir, zfs destroy. A failed mount destroys the snapshot at once instead of leaking one per run. The e2e suite could not reach this code at all: zpool create on the /img file failed with "no such pool or dataset", because the kernel opens vdevs from a taskq thread that resolves paths in the host's root, where the container's /img does not exist. The pool now sits on a loop device, whose /dev/loopN node the kernel creates on the host as well, and it is destroyed on exit once this script created it, since a pool is kernel-wide. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
389b418f42
commit
0735e5fa7b
3 files changed
+90
-15
No files matched your search
@@ -58,27 +58,50 @@ def _resolver(subject: str, root: str) -> Callable[[str], str]:
|
||||
return resolve
|
||||
|
||||
|
||||
def _remove(
|
||||
root: str, commands: list[list[str]], run: Callable[[list[str]], list[str]]
|
||||
) -> None:
|
||||
try:
|
||||
for command in commands:
|
||||
run(command)
|
||||
except BackupError as error:
|
||||
# Raising here would also mask whatever the body raised.
|
||||
print(f"WARNING: {root} could not be removed: {error}", flush=True)
|
||||
|
||||
|
||||
def _btrfs(
|
||||
subject: str, name: str, run: Callable[[list[str]], list[str]]
|
||||
) -> tuple[str, list[str]]:
|
||||
) -> tuple[str, list[list[str]]]:
|
||||
# The snapshot goes inside the subject, never beside it: the kernel rejects
|
||||
# a snapshot whose destination is on another filesystem, which is exactly
|
||||
# what the parent directory is when the subject is a mountpoint of its own.
|
||||
target = str(Path(os.path.abspath(subject)) / f".{name}") # noqa: PTH100 - see _resolver
|
||||
run(["btrfs", "subvolume", "snapshot", "-r", subject, target])
|
||||
return target, ["btrfs", "subvolume", "delete", target]
|
||||
return target, [["btrfs", "subvolume", "delete", target]]
|
||||
|
||||
|
||||
def _zfs(
|
||||
subject: str, name: str, run: Callable[[list[str]], list[str]]
|
||||
) -> tuple[str, list[str]]:
|
||||
) -> tuple[str, list[list[str]]]:
|
||||
output = run(["zfs", "list", "-H", "-o", "name", subject])
|
||||
dataset = (output[0] if output else "").strip()
|
||||
if not dataset:
|
||||
raise SnapshotError(f"no zfs dataset is mounted at {subject}")
|
||||
run(["zfs", "snapshot", f"{dataset}@{name}"])
|
||||
root = str(Path(subject) / ".zfs" / "snapshot" / name)
|
||||
return root, ["zfs", "destroy", f"{dataset}@{name}"]
|
||||
snapshot = f"{dataset}@{name}"
|
||||
# Exception: mounted here rather than read through .zfs/snapshot, which the
|
||||
# kernel automounts via a usermode helper in the host's mount namespace,
|
||||
# so a containerised run never sees it.
|
||||
root = str(Path(os.path.abspath(subject)) / f".{name}") # noqa: PTH100 - see _resolver
|
||||
run(["zfs", "snapshot", snapshot])
|
||||
remove = [["zfs", "destroy", snapshot]]
|
||||
try:
|
||||
run(["mkdir", root])
|
||||
remove.insert(0, ["rmdir", root])
|
||||
run(["mount", "-t", "zfs", snapshot, root])
|
||||
except BaseException:
|
||||
_remove(root, remove, run)
|
||||
raise
|
||||
return root, [["umount", root], *remove]
|
||||
|
||||
|
||||
_CREATE = {"btrfs": _btrfs, "zfs": _zfs}
|
||||
@@ -169,8 +192,4 @@ def volume_snapshot(
|
||||
try:
|
||||
yield _resolver(subject, root)
|
||||
finally:
|
||||
try:
|
||||
run(remove)
|
||||
except BackupError as error:
|
||||
# Raising here would also mask whatever the body raised.
|
||||
print(f"WARNING: {root} could not be removed: {error}", flush=True)
|
||||
_remove(root, remove, run)
|
||||
Reference in new issue
Block a user