Skip to content

Commit 4d66562

Browse files
committed
test: Overwriting snapshot from which microvm is loaded
This commit adds a test that has a microvm overwrite the snapshot file from which it was loaded with a new snapshot (e.g. a sort of "refresh" operation on the snapshot to write any modifications done since loading back to disk). Prior to this patch series, this caused both the snapshot and the microvm to become corrupted, as the zeroing done by the truncate/set_len pair would be reflected in the mmap'd memory region. Signed-off-by: Patrick Roy <[email protected]>
1 parent fde4760 commit 4d66562

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/integration_tests/functional/test_snapshot_basic.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,43 @@ def test_diff_snapshot_overlay(guest_kernel, rootfs, microvm_factory):
458458
# Run some command to check that the restored VM works
459459
rc, _, stderr = new_vm.ssh.run("true")
460460
assert rc == 0, stderr
461+
462+
463+
def test_snapshot_overwrite_self(guest_kernel, rootfs, microvm_factory):
464+
"""Tests that if we try to take a snapshot that would overwrite the
465+
very file from which the current VM is stored, nothing happens.
466+
467+
Note that even though we map the file as MAP_PRIVATE, the documentation
468+
of mmap does not specify what should happen if the file is changed after being
469+
mmap'd (https://man7.org/linux/man-pages/man2/mmap.2.html). It seems that
470+
these changes can propagate to the mmap'd memory region."""
471+
base_vm = microvm_factory.build(guest_kernel, rootfs)
472+
base_vm.spawn()
473+
base_vm.basic_config()
474+
base_vm.add_net_iface()
475+
base_vm.start()
476+
477+
# Wait for microvm to be booted
478+
rc, _, stderr = base_vm.ssh.run("true")
479+
assert rc == 0, stderr
480+
481+
snapshot = base_vm.snapshot_full()
482+
base_vm.kill()
483+
484+
vm = microvm_factory.build()
485+
vm.spawn()
486+
vm.restore_from_snapshot(snapshot, resume=True)
487+
488+
# When restoring a snapshot, vm.restore_from_snapshot first copies
489+
# the memory file (inside of the jailer) to /mem.src
490+
currently_loaded = Path(vm.chroot()) / "mem.src"
491+
492+
assert currently_loaded.exists()
493+
494+
vm.snapshot_full(mem_path="mem.src")
495+
vm.resume()
496+
497+
# Check the overwriting the snapshot file from which this microvm was originally
498+
# restored, with a new snapshot of this vm, does not break the VM
499+
rc, _, stderr = vm.ssh.run("true")
500+
assert rc == 0, stderr

0 commit comments

Comments
 (0)