PCIDevice and USBDevice include respective comparison logic. Signed-off-by: Lin Ma <lma@xxxxxxxx> --- virtManager/details.py | 47 ++--------------------------------------- virtinst/nodedev.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 45 deletions(-) diff --git a/virtManager/details.py b/virtManager/details.py index 4cb1baf..0875e4e 100644 --- a/virtManager/details.py +++ b/virtManager/details.py @@ -42,6 +42,7 @@ from .graphwidgets import Sparkline import virtinst from virtinst import util from virtinst import VirtualRNGDevice +from virtinst import NodeDevice # Parameters that can be edited in the details window @@ -255,61 +256,17 @@ def _build_hostdev_label(hostdev): def lookup_nodedev(vmmconn, hostdev): - def intify(val, do_hex=False): - try: - if do_hex: - return int(val or '0x00', 16) - else: - return int(val) - except: - return -1 - - def attrVal(node, attr): - if not hasattr(node, attr): - return None - return getattr(node, attr) - devtype = hostdev.type found_dev = None - vendor_id = product_id = bus = device = domain = slot = func = None - # For USB we want a device, not a bus if devtype == 'usb': devtype = 'usb_device' - vendor_id = hostdev.vendor or -1 - product_id = hostdev.product or -1 - bus = intify(hostdev.bus) - device = intify(hostdev.device) - - elif devtype == 'pci': - domain = intify(hostdev.domain, True) - bus = intify(hostdev.bus, True) - slot = intify(hostdev.slot, True) - func = intify(hostdev.function, True) devs = vmmconn.get_nodedevs(devtype, None) for dev in devs: - # Try to match with product_id|vendor_id|bus|device - if ((attrVal(dev, "product_id") == product_id or product_id == -1) and - (attrVal(dev, "vendor_id") == vendor_id or vendor_id == -1) and - (attrVal(dev, "bus") == bus or bus == -1) and - (attrVal(dev, "device") == device or device == -1)): + if NodeDevice.compare_to_nodedev(hostdev, dev): found_dev = dev - else: - # Try to get info from bus/addr - dev_id = intify(attrVal(dev, "device")) - bus_id = intify(attrVal(dev, "bus")) - dom_id = intify(attrVal(dev, "domain")) - func_id = intify(attrVal(dev, "function")) - slot_id = intify(attrVal(dev, "slot")) - - if ((dev_id == device and bus_id == bus) or - (dom_id == domain and func_id == func and - bus_id == bus and slot_id == slot)): - found_dev = dev - - if found_dev: break return found_dev diff --git a/virtinst/nodedev.py b/virtinst/nodedev.py index 9c990b3..fcbd49b 100644 --- a/virtinst/nodedev.py +++ b/virtinst/nodedev.py @@ -47,6 +47,22 @@ def _lookupNodeName(conn, name): return NodeDevice.parse(conn, xml) +def _intify(val, do_hex=False): + try: + if do_hex: + return int(val or '0x00', 16) + else: + return int(val) + except: + return -1 + + +def _attrVal(node, attr): + if not hasattr(node, attr): + return None + return getattr(node, attr) + + class NodeDevice(XMLBuilder): CAPABILITY_TYPE_SYSTEM = "system" CAPABILITY_TYPE_NET = "net" @@ -121,6 +137,15 @@ class NodeDevice(XMLBuilder): """ return self.name + @staticmethod + def compare_to_nodedev(hostdev, nodedev): + devtype = hostdev.type + if devtype == "usb": + devtype = "usb_device" + if devtype == nodedev.device_type: + return nodedev.compare_to_hostdev(hostdev) + return False + class SystemDevice(NodeDevice): hw_vendor = XMLProperty("./capability/hardware/vendor") @@ -176,6 +201,24 @@ class PCIDevice(NodeDevice): return "%s %s %s" % (devstr, self.vendor_name, self.product_name) + def compare_to_hostdev(self, hostdev): + h_bus = h_dev = h_dom = h_slot = h_func = None + + h_dom = _intify(hostdev.domain, True) + h_bus = _intify(hostdev.bus, True) + h_slot = _intify(hostdev.slot, True) + h_func = _intify(hostdev.function, True) + n_dev = _intify(_attrVal(self, "device")) + n_bus = _intify(_attrVal(self, "bus")) + n_dom = _intify(_attrVal(self, "domain")) + n_func = _intify(_attrVal(self, "function")) + n_slot = _intify(_attrVal(self, "slot")) + + if ((n_dev == h_dev and n_bus == h_bus) or + (n_dom == h_dom and n_func == h_func and + n_bus == h_bus and n_slot == h_slot)): + return True + class USBDevice(NodeDevice): bus = XMLProperty("./capability/bus") @@ -219,6 +262,20 @@ class USBDevice(NodeDevice): desc = "%s %s" % (busstr, devstr) return desc + def compare_to_hostdev(self, hostdev): + h_vid = h_pid = h_bus = h_dev = None + + h_vid = hostdev.vendor or -1 + h_pid = hostdev.product or -1 + h_bus = _intify(hostdev.bus) + h_dev = _intify(hostdev.device) + + if ((_attrVal(self, "product_id") == h_pid or h_pid == -1) and + (_attrVal(self, "vendor_id") == h_vid or h_vid == -1) and + (_intify(_attrVal(self, "bus")) == h_bus or h_bus == -1) and + (_intify(_attrVal(self, "device")) == h_dev or h_dev == -1)): + return True + class StorageDevice(NodeDevice): block = XMLProperty("./capability/block") -- 1.8.4 _______________________________________________ virt-tools-list mailing list virt-tools-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/virt-tools-list