mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 09:26:03 +00:00
fix(instances): the Host devices tab says when the host cannot give a VM's USB device
libvirt does not start a VM whose USB device the host does not have as the definition names it: a device replugged gets another address, and a second device of a model makes a model-only name ambiguous. The tab said nothing and the start failed with libvirt's words. The tab's form now says which device the next start cannot have and what to do (usb_unmatched), and a snapshot revert checks the same before it starts (check_hostdevs). The add form posts the device's IDs with its address: a device replugged since the page was loaded is not given in its place.
This commit is contained in:
parent
f0a9d2cf82
commit
4045819b8d
10 changed files with 453 additions and 271 deletions
|
|
@ -68,13 +68,24 @@ def host_resources(name, nodes, children):
|
|||
return found
|
||||
|
||||
|
||||
def usb_id(text):
|
||||
"""A USB vendor or product ID of a definition, read as libvirt reads it
|
||||
(C's strtoul: 0x hex, a leading 0 octal, else decimal) and written as a
|
||||
node device writes it (0x0627); empty when not given."""
|
||||
text = text.strip().removeprefix("+")
|
||||
if not text:
|
||||
return ""
|
||||
base = 16 if text.lower().startswith("0x") else 8 if text.startswith("0") and text != "0" else 10
|
||||
return f"0x{int(text, base):04x}"
|
||||
|
||||
|
||||
def usb_selector(source):
|
||||
"""(vendor, product, (bus, device) or None) of a USB <hostdev>'s <source>,
|
||||
the parts libvirt picks a device by. An address libvirt resolved for a
|
||||
running VM (autoAddress, in its migratable XML) is not one of them."""
|
||||
address = source.find("address")
|
||||
given = address is not None and source.get("autoAddress") != "yes"
|
||||
return (source.xpath("string(vendor/@id)").lower(), source.xpath("string(product/@id)").lower(),
|
||||
return (usb_id(source.xpath("string(vendor/@id)")), usb_id(source.xpath("string(product/@id)")),
|
||||
(int(address.get("bus")), int(address.get("device"))) if given else None)
|
||||
|
||||
|
||||
|
|
@ -177,6 +188,50 @@ def pci_problems(vm, device, by_address, managed):
|
|||
return problems
|
||||
|
||||
|
||||
USB_SOURCES = "devices/hostdev[@mode='subsystem'][@type='usb']/source"
|
||||
|
||||
|
||||
def usb_picked(source, usb):
|
||||
"""The selector (usb_selector) libvirt picks a device of the host's usb by
|
||||
for a USB <hostdev>'s <source>: an address it resolved before
|
||||
(autoAddress) while a device of the model is still there, else the model."""
|
||||
vendor, product, given = usb_selector(source)
|
||||
address = source.find("address")
|
||||
if given is None and address is not None:
|
||||
at = (int(address.get("bus")), int(address.get("device")))
|
||||
if any(usb_named((vendor, product, at), device["model"], (device["bus"], device["device"])) for device in usb):
|
||||
return vendor, product, at
|
||||
return vendor, product, given
|
||||
|
||||
|
||||
def usb_unmatched(trees, usb, restore=False):
|
||||
"""Why the host cannot give the USB devices the definitions trees name
|
||||
as they name them (libvirt then does not start the VM): a device it does
|
||||
not have, unless optional, or requisite on a restore (a saved state or a
|
||||
snapshot), as libvirt drops those; or a model it has more than one of,
|
||||
named without an address (usb_picked)."""
|
||||
dropped = ("optional", "requisite") if restore else ("optional",)
|
||||
problems = []
|
||||
for tree in trees:
|
||||
for source in tree.xpath(USB_SOURCES):
|
||||
picked = usb_picked(source, usb)
|
||||
matches = [device for device in usb if usb_named(picked, device["model"], (device["bus"], device["device"]))]
|
||||
vendor, product, given = usb_selector(source)
|
||||
ids = f"{vendor[2:]}:{product[2:]}"
|
||||
if given is None:
|
||||
label = [util.message(gettext_noop("USB device %(ids)s"), ids=ids)]
|
||||
else:
|
||||
label = [util.message(gettext_noop("USB device %(ids)s at %(bus)s/%(device)s"), ids=ids,
|
||||
bus=given[0], device=given[1])]
|
||||
if not matches and source.get("startupPolicy") not in dropped:
|
||||
problems.append(util.message(gettext_noop("%(device)s is not on the host"), device=label))
|
||||
elif len(matches) > 1:
|
||||
problems.append(util.message(gettext_noop(
|
||||
"the host has %(count)s of %(device)s: name the one to give by its address"),
|
||||
device=label, count=len(matches)))
|
||||
return problems
|
||||
|
||||
|
||||
def hostdev_problems(vm, pci, usb, wanted_pci, wanted_usb, managed=frozenset()):
|
||||
"""Why the host's devices pci and usb (get_node_devices) that a definition
|
||||
of the VM named vm wants (hostdev_users; managed_pci) cannot be given to
|
||||
|
|
@ -316,17 +371,19 @@ def device_names(conn):
|
|||
return names
|
||||
|
||||
|
||||
def check_hostdevs(conn, vm, trees):
|
||||
def check_hostdevs(conn, vm, trees, restore=False):
|
||||
"""Refuses the host devices the definitions trees give the VM named vm,
|
||||
on the host of conn, when they cannot be (hostdev_problems): each
|
||||
definition on its own, as the VM starts from one of them."""
|
||||
definition on its own, as the VM starts from one of them, restored from
|
||||
it when restore (usb_unmatched)."""
|
||||
wanted = [(tree, hostdev_users([tree])) for tree in trees]
|
||||
if not any(pci or usb for tree, (pci, usb) in wanted):
|
||||
return
|
||||
pci, usb = node_devices(conn)
|
||||
problems = []
|
||||
for tree, (wanted_pci, wanted_usb) in wanted:
|
||||
problems += hostdev_problems(vm, pci, usb, wanted_pci, wanted_usb, managed_pci(tree))
|
||||
problems = usb_unmatched(trees, usb, restore)
|
||||
for tree, (wanted_pci, _selectors) in wanted:
|
||||
picked = [usb_picked(source, usb) for source in tree.xpath(USB_SOURCES)]
|
||||
problems += hostdev_problems(vm, pci, usb, wanted_pci, picked, managed_pci(tree))
|
||||
if problems:
|
||||
unique = list({util.render(m["text"], m["values"], str): m for m in problems}.values())
|
||||
raise util.OperationError(gettext_noop("%(problems)s"), problems=unique)
|
||||
|
|
|
|||
|
|
@ -6,11 +6,12 @@ is a mock, libvirt's test driver has no node devices."""
|
|||
from unittest.mock import MagicMock
|
||||
|
||||
import libvirt
|
||||
from lxml import etree
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from vrtManager import util
|
||||
from vrtManager.connection import wvmConnect
|
||||
from vrtManager.nodedevs import hostdev_problems, node_devices, pci_address
|
||||
from vrtManager.nodedevs import check_hostdevs, hostdev_problems, node_devices, pci_address, usb_unmatched
|
||||
from vrtManager.tests.fakes import NODES, PCI_0A, USB_1_3, node, pci, vm
|
||||
|
||||
PCI_01 = "<hostdev mode='subsystem' type='pci'><source><address domain='0x0000' bus='0x00' slot='0x01' function='0x0'/></source></hostdev>"
|
||||
|
|
@ -26,6 +27,61 @@ def devices(nodes=NODES, domains=()):
|
|||
return node_devices(conn)
|
||||
|
||||
|
||||
class UsbUnmatchedTests(SimpleTestCase):
|
||||
"""libvirt does not start a VM whose USB device the host does not have
|
||||
as named: one replugged gets another address, a second of a model makes
|
||||
a model-only name ambiguous."""
|
||||
|
||||
def problems(self, source, restore=False):
|
||||
tree = etree.fromstring(f"<domain><devices>{source}</devices></domain>")
|
||||
return [util.render(m["text"], m["values"], str) for m in usb_unmatched([tree], devices()[1], restore)]
|
||||
|
||||
def test_a_device_named_as_the_host_has_it_is_fine(self):
|
||||
self.assertEqual(self.problems(USB_1_3), [])
|
||||
|
||||
def test_a_device_not_at_its_address_is_not_on_the_host(self):
|
||||
gone = USB_MODEL.replace("</source>", "<address bus='1' device='9'/></source>")
|
||||
self.assertEqual(self.problems(gone), ["USB device 0627:0001 at 1/9 is not on the host"])
|
||||
optional = gone.replace("<source>", "<source startupPolicy='optional'>")
|
||||
self.assertEqual(self.problems(optional), [])
|
||||
|
||||
def test_a_model_the_host_has_two_of_needs_an_address(self):
|
||||
self.assertEqual(self.problems(USB_MODEL),
|
||||
["the host has 2 of USB device 0627:0001: name the one to give by its address"])
|
||||
|
||||
def test_a_restore_drops_a_missing_requisite_device(self):
|
||||
requisite = USB_MODEL.replace("<source>", "<source startupPolicy='requisite'>")
|
||||
requisite = requisite.replace("</source>", "<address bus='1' device='9'/></source>")
|
||||
self.assertEqual(self.problems(requisite), ["USB device 0627:0001 at 1/9 is not on the host"])
|
||||
self.assertEqual(self.problems(requisite, restore=True), [])
|
||||
|
||||
def test_an_address_libvirt_found_is_tried_then_the_model(self):
|
||||
found = USB_MODEL.replace("<source>", "<source autoAddress='yes'>")
|
||||
self.assertEqual(self.problems(found.replace("</source>", "<address bus='1' device='3'/></source>")), [])
|
||||
self.assertEqual(self.problems(found.replace("</source>", "<address bus='1' device='9'/></source>")),
|
||||
["the host has 2 of USB device 0627:0001: name the one to give by its address"])
|
||||
|
||||
def test_a_found_address_still_there_is_the_device_checked(self):
|
||||
conn = wvmConnect.__new__(wvmConnect)
|
||||
conn.wvm = MagicMock(**{"listAllDevices.return_value": [node(*n) for n in NODES],
|
||||
"listAllDomains.return_value": [vm("db-1", USB_1_3)]})
|
||||
found = USB_MODEL.replace("<source>", "<source autoAddress='yes'>").replace("</source>", "<address bus='1' device='2'/></source>")
|
||||
check_hostdevs(conn, "kiosk-1", [etree.fromstring(f"<domain><devices>{found}</devices></domain>")], restore=True)
|
||||
|
||||
def test_ids_in_decimal_are_read_as_libvirt_reads_them(self):
|
||||
decimal = "<hostdev mode='subsystem' type='usb'><source><vendor id='1575'/><product id='1'/>" \
|
||||
"<address bus='1' device='3'/></source></hostdev>"
|
||||
self.assertEqual(self.problems(decimal), [])
|
||||
self.assertEqual(self.problems(decimal.replace("'1575'", "' +0x0627'")), [])
|
||||
|
||||
def test_a_revert_checks_them_too(self):
|
||||
conn = wvmConnect.__new__(wvmConnect)
|
||||
conn.wvm = MagicMock(**{"listAllDevices.return_value": [node(*n) for n in NODES], "listAllDomains.return_value": []})
|
||||
tree = etree.fromstring(f"<domain><devices>{USB_MODEL}</devices></domain>")
|
||||
with self.assertRaisesRegex(util.OperationError, "the host has 2 of USB device 0627:0001"):
|
||||
check_hostdevs(conn, "kiosk-1", [tree])
|
||||
|
||||
|
||||
class NodeDevicesTests(SimpleTestCase):
|
||||
def test_pci_devices_by_group_with_what_the_host_has_on_them(self):
|
||||
pci_devices, _ = devices()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue