mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-10-20 01:43:25 +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:
@@ -21,6 +21,14 @@ class Runner:
|
||||
return []
|
||||
|
||||
|
||||
class Unmountable(Runner):
|
||||
def __call__(self, command: list[str]) -> list[str]:
|
||||
result = super().__call__(command)
|
||||
if command[0] == "mount":
|
||||
raise BackupError("permission denied")
|
||||
return result
|
||||
|
||||
|
||||
class TestBtrfs(unittest.TestCase):
|
||||
def test_it_creates_a_read_only_snapshot_inside_the_subject(self) -> None:
|
||||
run = Runner()
|
||||
@@ -95,14 +103,57 @@ class TestZfs(unittest.TestCase):
|
||||
run.calls[-1], ["zfs", "destroy", "tank/docker@baudolo-20260731"]
|
||||
)
|
||||
|
||||
def test_it_maps_a_volume_path_through_the_dot_zfs_directory(self) -> None:
|
||||
def test_it_mounts_the_snapshot_inside_the_subject(self) -> None:
|
||||
run = self._run()
|
||||
with volume_snapshot("zfs", "/var/lib/docker", "20260731", run=run):
|
||||
pass
|
||||
self.assertIn(
|
||||
[
|
||||
"mount",
|
||||
"-t",
|
||||
"zfs",
|
||||
"tank/docker@baudolo-20260731",
|
||||
"/var/lib/docker/.baudolo-20260731",
|
||||
],
|
||||
run.calls,
|
||||
)
|
||||
|
||||
def test_it_maps_a_volume_path_into_the_mounted_snapshot(self) -> None:
|
||||
run = self._run()
|
||||
with volume_snapshot("zfs", "/var/lib/docker", "20260731", run=run) as resolve:
|
||||
self.assertEqual(
|
||||
resolve("/var/lib/docker/volumes/postgres_data/_data"),
|
||||
"/var/lib/docker/.zfs/snapshot/baudolo-20260731/volumes/postgres_data/_data",
|
||||
"/var/lib/docker/.baudolo-20260731/volumes/postgres_data/_data",
|
||||
)
|
||||
|
||||
def test_it_unmounts_before_it_destroys(self) -> None:
|
||||
run = self._run()
|
||||
with volume_snapshot("zfs", "/var/lib/docker", "20260731", run=run):
|
||||
pass
|
||||
self.assertEqual(
|
||||
run.calls[-3:],
|
||||
[
|
||||
["umount", "/var/lib/docker/.baudolo-20260731"],
|
||||
["rmdir", "/var/lib/docker/.baudolo-20260731"],
|
||||
["zfs", "destroy", "tank/docker@baudolo-20260731"],
|
||||
],
|
||||
)
|
||||
|
||||
def test_a_failed_mount_leaves_no_snapshot_behind(self) -> None:
|
||||
run = Unmountable({"zfs list": ["tank/docker"]})
|
||||
with (
|
||||
self.assertRaises(BackupError),
|
||||
volume_snapshot("zfs", "/var/lib/docker", "20260731", run=run),
|
||||
):
|
||||
pass
|
||||
self.assertEqual(
|
||||
run.calls[-2:],
|
||||
[
|
||||
["rmdir", "/var/lib/docker/.baudolo-20260731"],
|
||||
["zfs", "destroy", "tank/docker@baudolo-20260731"],
|
||||
],
|
||||
)
|
||||
|
||||
def test_an_unmounted_dataset_is_an_error(self) -> None:
|
||||
run = Runner({"zfs list": [""]})
|
||||
with (
|
||||
|
||||
Reference in New Issue
Block a user