[virt-tools-list] [PATCH 2/2] addhardware: use bus, device to distingush usb products

Guannan Ren gren at redhat.com
Mon Apr 22 17:45:32 UTC 2013


When the usb device being attached is unique, the
vendorId and productId is being used in XML.
When there are multiple usb devices with same vendorId
and productId in the host device list, the bus and device
number is being used to attach the device for virt-manager.
---
 virtManager/addhardware.py    | 21 +++++++++++++++++++++
 virtManager/connection.py     | 20 ++++++++++++++++++++
 virtinst/VirtualHostDevice.py |  5 ++---
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/virtManager/addhardware.py b/virtManager/addhardware.py
index 4c1d4ac..572b04c 100644
--- a/virtManager/addhardware.py
+++ b/virtManager/addhardware.py
@@ -1415,6 +1415,27 @@ class vmmAddHardware(vmmGObjectUI):
         except Exception, e:
             return self.err.val_err(_("Host device parameter error"), e)
 
+        devtype = ret[2]
+        if devtype == "usb_device":
+            vendor = self._dev.vendor
+            product = self._dev.product
+            count = self.conn.get_nodedev_numbers(devtype, vendor, product)
+            if count > 1:
+                self._dev.vendor = None
+                self._dev.product = None
+                return
+
+            if count == 1:
+                self._dev.bus = None
+                self._dev.device = None
+                return
+
+            if not count:
+                raise RuntimeError(_("Could not find USB device "
+                                     "(vendorId: %s, productId: %s) "
+                                     % (vendor, product)))
+
+
     def validate_page_char(self):
         chartype = self.get_char_type()
         modebox = self.widget("char-mode")
diff --git a/virtManager/connection.py b/virtManager/connection.py
index cd340ac..c8767b7 100644
--- a/virtManager/connection.py
+++ b/virtManager/connection.py
@@ -705,6 +705,26 @@ class vmmConnection(vmmGObject):
 
         return retdevs
 
+    def get_nodedev_numbers(self, devtype, vendor, product):
+        count = 0
+        self._ticklock.acquire()
+
+        for dev in self.nodedevs.values():
+            vdev = dev.get_virtinst_obj()
+            if devtype and vdev.device_type != devtype:
+                continue
+
+            if vendor == vdev.vendor_id and \
+                product == vdev.product_id:
+                count += 1
+
+        self._ticklock.release()
+        logging.debug("There are %d node devices with "
+                      "vendorId: %s, productId: %s",
+                       count, vendor, product)
+
+        return count
+
     def get_net_by_name(self, name):
         for net in self.nets.values():
             if net.get_name() == name:
diff --git a/virtinst/VirtualHostDevice.py b/virtinst/VirtualHostDevice.py
index f5740d0..ee38242 100644
--- a/virtinst/VirtualHostDevice.py
+++ b/virtinst/VirtualHostDevice.py
@@ -215,9 +215,8 @@ class VirtualHostDeviceUSB(VirtualHostDevice):
         self.vendor = nodedev.vendor_id
         self.product = nodedev.product_id
 
-        if not (self.vendor or self.product):
-            self.bus = nodedev.bus
-            self.device = nodedev.device
+        self.bus = nodedev.bus
+        self.device = nodedev.device
 
     def _get_source_xml(self):
         xml = ""
-- 
1.8.1.4




More information about the virt-tools-list mailing list