[virt-tools-list] [PATCH virt-manager] addhardware: Deal with the conflict host device

Lin Ma lma at suse.com
Mon Aug 11 06:16:04 UTC 2014


If a host device is in use by an active guest, Doesn't append it to
list while adding hardware.

If a host device is in use by inactive guests, Then warn user and
let user make choice while adding hardware.

Signed-off-by: Lin Ma <lma at suse.com>
---
 virtManager/addhardware.py | 15 ++++++++++++++
 virtinst/devicehostdev.py  | 49 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/virtManager/addhardware.py b/virtManager/addhardware.py
index 02cff57..4eb4eed 100644
--- a/virtManager/addhardware.py
+++ b/virtManager/addhardware.py
@@ -835,6 +835,10 @@ class vmmAddHardware(vmmGObjectUI):
 
         devs = self.conn.get_nodedevs(devtype, devcap)
         for dev in devs:
+            if virtinst.VirtualHostDevice.is_using_by_active_vm(self.conn.get_backend(), devtype, dev):
+                # Doesn't append device which is in use by an active VM to list.
+                continue
+
             prettyname = dev.pretty_name()
 
             for subdev in subdevs:
@@ -1739,6 +1743,17 @@ class vmmAddHardware(vmmGObjectUI):
 
         try:
             dev = virtinst.VirtualHostDevice(self.conn.get_backend())
+            # Hostdev collision
+            names = virtinst.VirtualHostDevice.is_conflict_hostdev \
+                    (self.conn.get_backend(), devtype, nodedev)
+            if names:
+                res = self.err.yes_no(
+                        _('The device is already in use by other guests %s') %
+                         (names),
+                        _("Do you really want to use the device?"))
+                if not res:
+                    return False
+
             dev.set_from_nodedev(nodedev, use_full_usb=is_dup)
             self._dev = dev
         except Exception, e:
diff --git a/virtinst/devicehostdev.py b/virtinst/devicehostdev.py
index a5d1a2a..32510b7 100644
--- a/virtinst/devicehostdev.py
+++ b/virtinst/devicehostdev.py
@@ -89,5 +89,54 @@ class VirtualHostDevice(VirtualDevice):
     driver_name = XMLProperty("./driver/@name")
     rom_bar = XMLProperty("./rom/@bar", is_onoff=True)
 
+    @staticmethod
+    def is_using_by_active_vm(conn, devtype, dev):
+        vms = conn.fetch_all_guests()
+        for vm in vms:
+            _backend = vm.conn.lookupByName(vm.name)
+            if not _backend.isActive():
+                continue
+            for hostdev in vm.get_devices("hostdev"):
+                if devtype == NodeDevice.CAPABILITY_TYPE_USBDEV and \
+                        hostdev.type == "usb":
+                    if hostdev.bus == dev.bus and \
+                            hostdev.device == dev.device and \
+                            hostdev.vendor == dev.vendor_id and \
+                            hostdev.product == dev.product_id:
+                        return True
+                elif devtype == NodeDevice.CAPABILITY_TYPE_PCI and \
+                        hostdev.type == "pci":
+                    if str(int(hostdev.domain, 16)) == dev.domain and \
+                            str(int(hostdev.bus, 16)) == dev.bus and \
+                            str(int(hostdev.slot, 16)) == dev.slot and \
+                            str(int(hostdev.function, 16)) == dev.function:
+                        return True
+        return False
+
+    @staticmethod
+    def is_conflict_hostdev(conn, devtype, dev):
+        ret = []
+        vms = conn.fetch_all_guests()
+        for vm in vms:
+            _backend = vm.conn.lookupByName(vm.name)
+            if _backend.isActive():
+                continue
+            for hostdev in vm.get_devices("hostdev"):
+                if devtype == NodeDevice.CAPABILITY_TYPE_USBDEV and \
+                        hostdev.type == "usb":
+                    if hostdev.bus == dev.bus and \
+                            hostdev.device == dev.device and \
+                            hostdev.vendor == dev.vendor_id and \
+                            hostdev.product == dev.product_id:
+                        ret.append(vm.name)
+                elif devtype == NodeDevice.CAPABILITY_TYPE_PCI and \
+                        hostdev.type == "pci":
+                    if str(int(hostdev.domain, 16)) == dev.domain and \
+                            str(int(hostdev.bus, 16)) == dev.bus and \
+                            str(int(hostdev.slot, 16)) == dev.slot and \
+                            str(int(hostdev.function, 16)) == dev.function:
+                        ret.append(vm.name)
+        return ret
+
 
 VirtualHostDevice.register_type()
-- 
1.8.4




More information about the virt-tools-list mailing list