|
| 1 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | +# |
| 3 | +# virsh_memonly_snapshot.py |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: GPL-2.0 |
| 6 | +# |
| 7 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | +""" |
| 9 | +Avocado-VT test: create a *memory-only* snapshot for a running guest. |
| 10 | +
|
| 11 | +Two scenarios are supported: |
| 12 | + - xml_diskspec: set <disk snapshot='no'> in domain XML for all disks |
| 13 | + - cli_diskspec: leave XML intact, pass --diskspec <dev>,snapshot=no via CLI |
| 14 | +
|
| 15 | +If 'mem_file' is not provided, the test will create a unique path in tmp. |
| 16 | +""" |
| 17 | + |
| 18 | +import time |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from virttest import virsh, data_dir |
| 22 | +from virttest.libvirt_xml import vm_xml |
| 23 | +from virttest.utils_libvirt import libvirt_vmxml, libvirt_disk |
| 24 | + |
| 25 | + |
| 26 | +def run(test, params, env): |
| 27 | + """ |
| 28 | + Entry point for Avocado-VT. |
| 29 | +
|
| 30 | + Params (from .cfg): |
| 31 | + main_vm: VM name under test (required) |
| 32 | + scenario: 'xml_diskspec' (default) or 'cli_diskspec' |
| 33 | + mem_file: Optional path to store memory dump created by --memspec |
| 34 | +
|
| 35 | + The test ensures the VM is running, applies disk snapshot=no either via |
| 36 | + XML or CLI, then runs: |
| 37 | + virsh snapshot-create-as --no-metadata --memspec file=<mem> --live |
| 38 | + Finally, it restores the original domain XML. |
| 39 | + """ |
| 40 | + vm_name = params.get("main_vm") |
| 41 | + if not vm_name: |
| 42 | + test.error("Missing 'main_vm'") |
| 43 | + |
| 44 | + default_mem = Path(data_dir.get_tmp_dir()) / f"{vm_name}-{int(time.time())}.mem" |
| 45 | + mem_file = Path(params.get("mem_file", str(default_mem))) |
| 46 | + |
| 47 | + case = params.get("scenario", "") |
| 48 | + cli_diskspec = case == "cli_diskspec" |
| 49 | + |
| 50 | + vm = env.get_vm(vm_name) |
| 51 | + if vm is None: |
| 52 | + test.error(f"VM '{vm_name}' not found") |
| 53 | + |
| 54 | + backup_xml = None |
| 55 | + disk_targets = [] |
| 56 | + |
| 57 | + def _ensure_running(): |
| 58 | + """Start the VM if it is not already running.""" |
| 59 | + if not vm.is_alive(): |
| 60 | + vm.start() |
| 61 | + |
| 62 | + def _snapshot_create(): |
| 63 | + """Invoke virsh to create a live memory-only snapshot.""" |
| 64 | + mem_file.parent.mkdir(parents=True, exist_ok=True) |
| 65 | + opts = f"--no-metadata --memspec file={mem_file} --live" |
| 66 | + if cli_diskspec: |
| 67 | + for dev in disk_targets: |
| 68 | + opts += f" --diskspec {dev},snapshot=no" |
| 69 | + virsh.snapshot_create_as(vm_name, options=opts, ignore_status=False, debug=True) |
| 70 | + |
| 71 | + def setup_test(): |
| 72 | + """Backup XML, collect disk targets and prepare the scenario.""" |
| 73 | + nonlocal backup_xml, disk_targets |
| 74 | + backup_xml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) |
| 75 | + disk_targets = libvirt_disk.all_disk_targets(vm_name) |
| 76 | + test.log.debug(f"scenario: {case}") |
| 77 | + test.log.debug(f"mem_file: {mem_file}") |
| 78 | + test.log.debug(f"disk_targets: {disk_targets}") |
| 79 | + if not cli_diskspec: |
| 80 | + vmx = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) |
| 81 | + for dev in disk_targets: |
| 82 | + disk_dict = {"target": {"dev": dev}, "snapshot": "no"} |
| 83 | + libvirt_vmxml.modify_vm_device(vmx, "disk", disk_dict) |
| 84 | + test.log.debug(vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)) |
| 85 | + _ensure_running() |
| 86 | + |
| 87 | + def teardown_test(): |
| 88 | + """Restore original domain XML (and implicit cleanup).""" |
| 89 | + if backup_xml is not None: |
| 90 | + backup_xml.sync() |
| 91 | + |
| 92 | + try: |
| 93 | + setup_test() |
| 94 | + _snapshot_create() |
| 95 | + finally: |
| 96 | + teardown_test() |
0 commit comments