1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00

fix(instances): a VM starts only with host devices the host can give now

A device is checked when it is given to a VM, but a start later did not
check it again: a PCI device bound back to a host driver for the host's
own network or disks, or a USB device the host no longer has as named,
was taken from the host or failed in libvirt. Power On and Power Cycle now
check the definition the VM starts from (its saved state's when restored)
as a snapshot revert does, and refuse with the reasons; Power Cycle checks
before it stops the VM.
This commit is contained in:
catborise 2026-11-11 16:16:55 +03:00
parent 4045819b8d
commit a36ffcd3ba
4 changed files with 59 additions and 1 deletions

View file

@ -48,6 +48,17 @@ class VMRowTests(TestCase):
self.proxy.start.assert_called_once_with(libvirt.VIR_DOMAIN_START_FORCE_BOOT)
self.assertEqual(self.rows(), [("VM vm: power on without the saved state", True)])
def test_a_start_the_host_cannot_give_the_devices_of_does_not_stop_or_start_the_vm(self):
self.proxy.check_start_hostdevs.side_effect = OperationError("PCI device 0000:00:0a.0 holds the host's eth0")
for view, restore in (("poweron", True), ("powercycle", False)):
self.post(view)
with self.subTest(view=view):
self.assertEqual(self.proxy.check_start_hostdevs.call_args.kwargs.get("restore", True), restore)
self.proxy.force_shutdown.assert_not_called()
self.proxy.start.assert_not_called()
self.assertEqual(self.rows(), [("VM vm: power on: PCI device 0000:00:0a.0 holds the host's eth0", False),
("VM vm: power cycle: PCI device 0000:00:0a.0 holds the host's eth0", False)])
def test_a_power_cycle_that_does_not_start_again_says_the_vm_stays_off(self):
self.proxy.start.side_effect = libvirtError("Cannot access storage file")
response = self.post("powercycle")

View file

@ -811,9 +811,11 @@ def poweron(request, pk):
messages.warning(request, _("Templates cannot be started."))
elif options.validated_data["fresh"]: # the saved state is discarded: the VM boots anew
with audit_vm(request, instance, "power on without the saved state"):
instance.proxy.check_start_hostdevs(restore=False)
instance.proxy.start(VIR_DOMAIN_START_FORCE_BOOT)
else:
with audit_vm(request, instance, "power on"):
instance.proxy.check_start_hostdevs()
instance.proxy.start()
return get_safe_redirect(
@ -829,6 +831,7 @@ def powercycle(request, pk):
messages.warning(request, _("Templates cannot be started."))
else:
with audit_vm(request, instance, "power cycle") as entry:
instance.proxy.check_start_hostdevs(restore=False) # before it stops
instance.proxy.force_shutdown()
try:
instance.proxy.start()

View file

@ -72,7 +72,7 @@ from lxml import etree
from vrtManager import util
from vrtManager.connection import NVRAM_DIR, wvmConnect
from vrtManager.nodedevs import pci_address, usb_selector
from vrtManager.nodedevs import check_hostdevs, pci_address, usb_selector
from vrtManager.storage import wvmStorage, wvmStorages
logger = logging.getLogger(__name__)
@ -652,6 +652,17 @@ class wvmInstance(wvmConnect):
def start(self, flags=0):
self.instance.createWithFlags(flags)
def check_start_hostdevs(self, restore=True):
"""Refuses a start whose host devices the host cannot give now
(check_hostdevs): a device given while free may since be another
VM's or the host's, and libvirt takes a managed one from its host
driver as the VM starts. The definition checked is the one the VM
starts from: its saved state's when restored."""
dom = self.instance
restore = restore and dom.hasManagedSaveImage(0)
xml = dom.managedSaveGetXMLDesc(0) if restore else dom.XMLDesc(VIR_DOMAIN_XML_INACTIVE)
check_hostdevs(self, dom.name(), [etree.fromstring(xml)], restore)
def shutdown(self):
self.instance.shutdown()

View file

@ -82,6 +82,39 @@ class UsbUnmatchedTests(SimpleTestCase):
check_hostdevs(conn, "kiosk-1", [tree])
class StartCheckTests(SimpleTestCase):
"""A start checks the host devices of the definition it starts from."""
def proxy(self, persistent, saved=None):
from vrtManager.instance import wvmInstance
proxy = wvmInstance.__new__(wvmInstance)
proxy.wvm = MagicMock(**{"listAllDevices.return_value": [node(*n) for n in NODES], "listAllDomains.return_value": []})
proxy.instance = MagicMock(**{"name.return_value": "kiosk-1", "hasManagedSaveImage.return_value": int(saved is not None),
"XMLDesc.return_value": f"<domain><devices>{persistent}</devices></domain>",
"managedSaveGetXMLDesc.return_value": f"<domain><devices>{saved}</devices></domain>"})
return proxy
def test_the_host_s_own_nic_refuses_the_start(self):
own_nic = PCI_01.replace("0x01", "0x06")
with self.assertRaisesRegex(util.OperationError, "holds the host's eth0"):
self.proxy(own_nic).check_start_hostdevs()
self.proxy(PCI_0A).check_start_hostdevs() # bound to vfio-pci, alone in its group
def test_a_restore_checks_the_saved_state_a_fresh_boot_the_definition(self):
proxy = self.proxy(PCI_0A, saved=USB_MODEL)
with self.assertRaisesRegex(util.OperationError, "the host has 2 of"):
proxy.check_start_hostdevs()
proxy.check_start_hostdevs(restore=False)
def test_a_restore_drops_a_missing_requisite_device_a_boot_does_not(self):
gone = USB_MODEL.replace("<source>", "<source startupPolicy='requisite'>")
gone = gone.replace("</source>", "<address bus='1' device='9'/></source>")
self.proxy(gone, saved=gone).check_start_hostdevs()
with self.assertRaisesRegex(util.OperationError, "is not on the host"):
self.proxy(gone, saved=gone).check_start_hostdevs(restore=False)
class NodeDevicesTests(SimpleTestCase):
def test_pci_devices_by_group_with_what_the_host_has_on_them(self):
pci_devices, _ = devices()