From a53a3623d2172abd709b7d170707f900f65c5332 Mon Sep 17 00:00:00 2001 From: catborise Date: Wed, 11 Nov 2026 12:35:27 +0300 Subject: [PATCH] fix(instances): a VM's saved state keeps the disks it starts with A VM with a saved state (managed save, as libvirt-guests leaves VMs when its host shuts down) shows as shut off, and its start restores the definition it was saved with. The panel let the VM's Delete remove a disk of it, and the storage pool delete a volume detached since: the VM then did not start ("Cannot access storage file"), and the panel has no way to drop the saved state. A VM's Delete now refuses a disk while the VM has a saved state, and a volume in a domain's saved state is in use for every delete check. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- instances/tests/test_destroy.py | 1 + instances/tests/test_disk_isolation.py | 1 + instances/tests/test_pending_edits.py | 12 ++++++++++++ instances/tests/test_vm_audit_rows.py | 1 + instances/views.py | 4 ++++ vrtManager/connection.py | 17 +++++++++++++++-- vrtManager/tests/test_volume_in_use.py | 25 +++++++++++++++++++++++++ 7 files changed, 59 insertions(+), 2 deletions(-) diff --git a/instances/tests/test_destroy.py b/instances/tests/test_destroy.py index 9b91f5c0..10db5d54 100644 --- a/instances/tests/test_destroy.py +++ b/instances/tests/test_destroy.py @@ -209,6 +209,7 @@ class DestroyOrderTestCase(TransactionTestCase): with patch("instances.models.wvmInstance") as wvm, patch("instances.views.wvmStorage") as storage: proxy = wvm.return_value proxy.get_disk_devices.return_value = [disk] + proxy.get_managed_save_image.return_value = 0 proxy.paths_used_by_other_domains.return_value = set() proxy.wait_disk_detached.return_value = True storage.return_value.del_volume.side_effect = OperationError("Volume data.qcow2 is in use") diff --git a/instances/tests/test_disk_isolation.py b/instances/tests/test_disk_isolation.py index 7027adc7..792bf407 100644 --- a/instances/tests/test_disk_isolation.py +++ b/instances/tests/test_disk_isolation.py @@ -109,6 +109,7 @@ class DiskViewsTenantIsolationTestCase(TestCase): "instances.views.wvmStorage" ) as mock_storage: mock_wvm.return_value.get_disk_devices.return_value = [VM_DISK] + mock_wvm.return_value.get_managed_save_image.return_value = 0 res = self._post( "delete_vol", {"storage": "other-pool", "name": "victim.qcow2", "dev": "vda"}, diff --git a/instances/tests/test_pending_edits.py b/instances/tests/test_pending_edits.py index c3dc2d36..cff5a7be 100644 --- a/instances/tests/test_pending_edits.py +++ b/instances/tests/test_pending_edits.py @@ -205,6 +205,18 @@ class PendingEditsTestCase(TestCase): proxy.detach_disk.assert_not_called() storage.return_value.del_volume.assert_not_called() + def test_the_disk_of_a_saved_state_is_not_deleted(self): + # the saved state starts with it + with patch("instances.models.wvmInstance") as wvm, patch("instances.views.wvmStorage") as storage: + proxy = wvm.return_value + proxy.get_managed_save_image.return_value = 1 + proxy.get_disk_devices.side_effect = lambda **kw: [disk("vda", "default", "")] + response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda"}) + self.assertEqual([str(m) for m in get_messages(response.wsgi_request)], + ["The VM has a saved state; start it or remove the saved state first"]) + proxy.detach_disk.assert_not_called() + storage.return_value.del_volume.assert_not_called() + def test_the_options_and_cpu_forms_show_the_pending_definition(self): doc = self.page(get_vcpu=(2, 4), get_cur_vcpu=(1, 3), get_title=("old", "new"), get_description=("live text", "pending text")) diff --git a/instances/tests/test_vm_audit_rows.py b/instances/tests/test_vm_audit_rows.py index 173ffd9b..f29ec621 100644 --- a/instances/tests/test_vm_audit_rows.py +++ b/instances/tests/test_vm_audit_rows.py @@ -146,6 +146,7 @@ class VMRowTests(TestCase): def test_a_disk_delete_logs_the_detach_and_the_volume(self): disk = {"dev": "vdb", "path": "/pool/data.qcow2", "storage": "default", "image": "data.qcow2"} self.proxy.get_disk_devices.return_value = [disk] + self.proxy.get_managed_save_image.return_value = 0 self.proxy.paths_used_by_other_domains.return_value = set() with patch("instances.views.wvmStorage"): self.proxy.wait_disk_detached.return_value = False diff --git a/instances/views.py b/instances/views.py index 155e8b12..263af6e5 100755 --- a/instances/views.py +++ b/instances/views.py @@ -1508,6 +1508,10 @@ def delete_vol(request, pk): _("Disk %(dev)s is not a storage volume of this instance") % {"dev": dev}, ) return _back(request, pk, "disks") + # its saved state starts with the disk: the volume must stay until then + if instance.proxy.get_managed_save_image(): + messages.error(request, _("The VM has a saved state; start it or remove the saved state first")) + return _back(request, pk, "disks") # the page lists the persistent disks: delete only a volume both definitions use there if not any(d["dev"] == dev and d["path"] == disk["path"] for d in instance.config_disks): messages.error( diff --git a/vrtManager/connection.py b/vrtManager/connection.py index b2091051..7574a5cb 100644 --- a/vrtManager/connection.py +++ b/vrtManager/connection.py @@ -1123,8 +1123,9 @@ class wvmConnect(object): def paths_in_use(self, skip_uuid=None, own=()): """Paths (names for network disks) a domain uses as a disk or a - backing file of one, or as a file of its snapshots (overlays, memory, - the disks they were taken on); and the backing files of volumes. + backing file of one, in its saved state too, or as a file of its + snapshots (overlays, memory, the disks they were taken on); and the + backing files of volumes. skip_uuid leaves out one domain, own the volumes whose backing files do not count.""" used = set() @@ -1133,6 +1134,18 @@ class wvmConnect(object): continue for flags in (0, libvirt.VIR_DOMAIN_XML_INACTIVE): used.update(self._source_paths(etree.fromstring(dom.XMLDesc(flags)))) + # a saved state starts with the disks it was saved with, detached since or not + if dom.hasManagedSaveImage(0): + try: + saved = dom.managedSaveGetXMLDesc(0) + except libvirtError as err: + if err.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN: + continue + if err.get_error_code() != libvirt.VIR_ERR_OPERATION_INVALID: + raise + # the image went meanwhile: the VM may have started from it + saved = dom.XMLDesc(0) + used.update(self._source_paths(etree.fromstring(saved))) for snap in dom.listAllSnapshots(0): tree = etree.fromstring(snap.getXMLDesc(0)) used.update(tree.xpath("disks/disk/source/@file|memory/@file")) diff --git a/vrtManager/tests/test_volume_in_use.py b/vrtManager/tests/test_volume_in_use.py index 8a5ed983..fead4a54 100644 --- a/vrtManager/tests/test_volume_in_use.py +++ b/vrtManager/tests/test_volume_in_use.py @@ -15,6 +15,7 @@ from vrtManager.storage import wvmStorage def domain(uuid, disks=(), snapshots=()): dom = MagicMock() dom.UUIDString.return_value = uuid + dom.hasManagedSaveImage.return_value = 0 devices = "".join(f"" for d in disks) dom.XMLDesc.return_value = f"{devices}" snaps = [] @@ -66,6 +67,30 @@ class PathsInUseTestCase(unittest.TestCase): {"/pool/vm.now", "/pool/vm.s", "/pool/vm.s.mem", "/pool/vm.qcow2", "/pool/template.qcow2"}, ) + def test_a_disk_detached_since_the_vm_was_saved_is_in_use(self): + dom = domain("u1", ["/pool/vm.qcow2"]) + dom.hasManagedSaveImage.return_value = 1 + dom.managedSaveGetXMLDesc.return_value = ( + "" + "") + conn, _ = storage(domains=[dom]) + self.assertEqual(conn.paths_in_use(), {"/pool/vm.qcow2", "/pool/data.qcow2"}) + + def test_a_saved_state_gone_meanwhile_counts_the_disks_the_vm_started_with(self): + dom = domain("u1", ["/pool/vm.qcow2"]) + dom.hasManagedSaveImage.return_value = 1 + error = libvirtError("no saved state") + error.get_error_code = lambda: libvirt.VIR_ERR_OPERATION_INVALID + dom.managedSaveGetXMLDesc.side_effect = error + conn, _ = storage(domains=[dom]) + self.assertEqual(conn.paths_in_use(), {"/pool/vm.qcow2"}) + self.assertEqual(dom.XMLDesc.call_args_list[-1].args, (0,)) + # a VM removed meanwhile uses nothing + error.get_error_code = lambda: libvirt.VIR_ERR_NO_DOMAIN + dom.XMLDesc.reset_mock() + self.assertEqual(conn.paths_in_use(), {"/pool/vm.qcow2"}) + self.assertEqual(dom.XMLDesc.call_count, 2) + def test_one_domain_and_the_backing_files_of_own_volumes_can_be_left_out(self): conn, _ = storage( domains=[domain("u1", ["/pool/a.qcow2"]), domain("u2", ["/pool/b.qcow2"])],