PCIDevice and USBDevice include respective comparison logic. Signed-off-by: Lin Ma <lma@xxxxxxxx> --- virtinst/nodedev.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/virtinst/nodedev.py b/virtinst/nodedev.py index 17524dc..39ccc0f 100644 --- a/virtinst/nodedev.py +++ b/virtinst/nodedev.py @@ -121,6 +121,68 @@ class NodeDevice(XMLBuilder): """ return self.name + @staticmethod + def compare_to_nodedev(vmmconn, new_nodedev): + 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) + + ret = [] + conn = vmmconn.get_backend() + vms = conn.fetch_all_guests() + cls = _typeToDeviceClass(new_nodedev.device_type) + for vm in vms: + for hostdev in vm.get_devices("hostdev"): + devtype = hostdev.type + found_dev = None + vid = pid = None + bus = device = domain = slot = func = None + if devtype == "usb": + devtype = "usb_device" + vid = hostdev.vendor or -1 + pid = 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: + if ((attrVal(dev, "product_id") == pid or pid == -1) and + (attrVal(dev, "vendor_id") == vid or vid == -1) and + (attrVal(dev, "bus") == bus or bus == -1) and + (attrVal(dev, "device") == device or device == -1)): + found_dev = dev + else: + 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 + if found_dev: + if new_nodedev.device_type == found_dev.device_type: + if cls.compare_to_nodedev(new_nodedev, found_dev): + ret.append(vm.name) + return ret class SystemDevice(NodeDevice): hw_vendor = XMLProperty("./capability/hardware/vendor") @@ -176,6 +238,14 @@ class PCIDevice(NodeDevice): return "%s %s %s" % (devstr, self.vendor_name, self.product_name) + @staticmethod + def compare_to_nodedev(new_nodedev, found_dev): + if new_nodedev.domain == found_dev.domain and \ + new_nodedev.bus == found_dev.bus and \ + new_nodedev.slot == found_dev.slot and \ + new_nodedev.function == found_dev.function: + return True + class USBDevice(NodeDevice): bus = XMLProperty("./capability/bus") @@ -192,6 +262,14 @@ class USBDevice(NodeDevice): str(self.product_name)) return desc + @staticmethod + def compare_to_nodedev(new_nodedev, found_dev): + if new_nodedev.device == found_dev.device and \ + new_nodedev.bus == found_dev.bus and \ + new_nodedev.vendor_id == found_dev.vendor_id and \ + new_nodedev.product_id == found_dev.product_id: + 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