diff --git a/instances/tests/test_vm_audit_rows.py b/instances/tests/test_vm_audit_rows.py
index 7b1ea5a5..66ab2530 100644
--- a/instances/tests/test_vm_audit_rows.py
+++ b/instances/tests/test_vm_audit_rows.py
@@ -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")
diff --git a/instances/views.py b/instances/views.py
index 7e72ef9a..510c2cf6 100755
--- a/instances/views.py
+++ b/instances/views.py
@@ -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()
diff --git a/vrtManager/instance.py b/vrtManager/instance.py
index ab625690..67b8e0c7 100644
--- a/vrtManager/instance.py
+++ b/vrtManager/instance.py
@@ -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()
diff --git a/vrtManager/tests/test_nodedevs.py b/vrtManager/tests/test_nodedevs.py
index 65ff5c1e..410f80bd 100644
--- a/vrtManager/tests/test_nodedevs.py
+++ b/vrtManager/tests/test_nodedevs.py
@@ -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"{persistent}",
+ "managedSaveGetXMLDesc.return_value": f"{saved}"})
+ 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("", "")
+ gone = gone.replace("", "")
+ 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()