Skip to content

Commit

Permalink
test: Overwriting snapshot from which microvm is loaded
Browse files Browse the repository at this point in the history
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 <roypat@amazon.co.uk>
  • Loading branch information
roypat committed Dec 7, 2023
1 parent fde4760 commit 4d66562
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/integration_tests/functional/test_snapshot_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,43 @@ def test_diff_snapshot_overlay(guest_kernel, rootfs, microvm_factory):
# Run some command to check that the restored VM works
rc, _, stderr = new_vm.ssh.run("true")
assert rc == 0, stderr


def test_snapshot_overwrite_self(guest_kernel, rootfs, microvm_factory):
"""Tests that if we try to take a snapshot that would overwrite the
very file from which the current VM is stored, nothing happens.
Note that even though we map the file as MAP_PRIVATE, the documentation
of mmap does not specify what should happen if the file is changed after being
mmap'd (https://man7.org/linux/man-pages/man2/mmap.2.html). It seems that
these changes can propagate to the mmap'd memory region."""
base_vm = microvm_factory.build(guest_kernel, rootfs)
base_vm.spawn()
base_vm.basic_config()
base_vm.add_net_iface()
base_vm.start()

# Wait for microvm to be booted
rc, _, stderr = base_vm.ssh.run("true")
assert rc == 0, stderr

snapshot = base_vm.snapshot_full()
base_vm.kill()

vm = microvm_factory.build()
vm.spawn()
vm.restore_from_snapshot(snapshot, resume=True)

# When restoring a snapshot, vm.restore_from_snapshot first copies
# the memory file (inside of the jailer) to /mem.src
currently_loaded = Path(vm.chroot()) / "mem.src"

assert currently_loaded.exists()

vm.snapshot_full(mem_path="mem.src")
vm.resume()

# Check the overwriting the snapshot file from which this microvm was originally
# restored, with a new snapshot of this vm, does not break the VM
rc, _, stderr = vm.ssh.run("true")
assert rc == 0, stderr

0 comments on commit 4d66562

Please sign in to comment.