mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
a4bfe77949
commit
a53a3623d2
7 changed files with 59 additions and 2 deletions
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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"},
|
||||
|
|
|
|||
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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"<disk type='file' device='disk'><source file='{d}'/></disk>" for d in disks)
|
||||
dom.XMLDesc.return_value = f"<domain><devices>{devices}</devices></domain>"
|
||||
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 = (
|
||||
"<domain><devices><disk type='file' device='disk'><source file='/pool/vm.qcow2'/></disk>"
|
||||
"<disk type='file' device='disk'><source file='/pool/data.qcow2'/></disk></devices></domain>")
|
||||
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"])],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue