[virt-tools-list] [PATCH] virtinst: throw error when --host-device can't specify unique device

Guannan Ren gren at redhat.com
Wed Apr 24 10:05:08 UTC 2013


When there are mutiple USB devices with identical vendorId and
productId, with --host-device <vendorId:productId>, virt-install
fails to specify one.
Currently the virt-install add the first node device with
these vendorId and productId.
This fix will throw out an error when argument to --host-device
can't specify unique node device.

For example:
virt-install ${other args} \
             --host-device <vendorId:productId>
ERROR    15e1:2007 corresponds to multiple node devices
---
 tests/clitest.py             |  3 ++-
 virtinst/NodeDeviceParser.py | 22 ++++++++++++++++------
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/tests/clitest.py b/tests/clitest.py
index c7d7b05..650b51b 100644
--- a/tests/clitest.py
+++ b/tests/clitest.py
@@ -640,7 +640,8 @@ vinst.add_invalid("redirdev", "--redirdev usb,type=tcp,server=:399")  # Missing
 
 vinst.add_category("hostdev", "--noautoconsole --nographics --nodisks --pxe")
 vinst.add_valid("hostdev", "--host-device usb_device_781_5151_2004453082054CA1BEEE")  # Host dev by libvirt name
-vinst.add_valid("hostdev", "--host-device 001.003 --host-device 15:0.1 --host-device 2:15:0.2 --host-device 0:15:0.3 --host-device 0x0781:0x5151 --host-device 1d6b:2")  # Many hostdev parsing types
+vinst.add_valid("hostdev", "--host-device 001.003 --host-device 15:0.1 --host-device 2:15:0.2 --host-device 0:15:0.3 --host-device 0x0781:0x5151")  # Many hostdev parsing types
+vinst.add_invalid("hostdev", "--host-device 1d6b:2")  # multiple USB devices with identical vendorId and productId
 vinst.add_invalid("hostdev", "--host-device pci_8086_2850_scsi_host_scsi_host")  # Unsupported hostdev type
 vinst.add_invalid("hostdev", "--host-device foobarhostdev")  # Unknown hostdev
 vinst.add_invalid("hostdev", "--host-device 300:400")  # Parseable hostdev, but unknown digits
diff --git a/virtinst/NodeDeviceParser.py b/virtinst/NodeDeviceParser.py
index 5ad7465..5a184b3 100644
--- a/virtinst/NodeDeviceParser.py
+++ b/virtinst/NodeDeviceParser.py
@@ -527,14 +527,24 @@ def devAddressToNodedev(conn, addrstr):
     cmp_func, devtype = ret
 
     # Iterate over node devices and compare
+    count = 0
+    nodedev = None
+
     nodenames = conn.listDevices(devtype, 0)
     for name in nodenames:
-        nodedev = _lookupNodeName(conn, name)
-        if cmp_func(nodedev):
-            return nodedev
-
-    raise ValueError(_("Did not find a matching node device for '%s'") %
-                     addrstr)
+        tmpnode = _lookupNodeName(conn, name)
+        if cmp_func(tmpnode):
+            nodedev = tmpnode
+            count += 1
+
+    if count == 1:
+        return nodedev
+    elif count > 1:
+        raise ValueError(_("%s corresponds to multiple node devices") %
+                         addrstr)
+    elif count < 1:
+        raise ValueError(_("Did not find a matching node device for '%s'") %
+                         addrstr)
 
 
 def parse(xml):
-- 
1.8.1.4




More information about the virt-tools-list mailing list