diff options
author | Cole Robinson <crobinso@redhat.com> | 2022-01-21 12:58:50 -0500 |
---|---|---|
committer | Cole Robinson <crobinso@redhat.com> | 2022-01-21 13:20:46 -0500 |
commit | 7e1f886aa990ee462962b264cb412b749a23a9cb (patch) | |
tree | eb3df336a056de175ed5a3c979be687a4c203e9b | |
parent | 7986ffa80b01b699d75bd9d1724e9abe6f80de12 (diff) | |
download | virt-manager-7e1f886aa990ee462962b264cb412b749a23a9cb.tar.gz |
cli: Clean up nodedev address functions a bit
Signed-off-by: Cole Robinson <crobinso@redhat.com>
-rw-r--r-- | virtinst/cli.py | 101 |
1 files changed, 47 insertions, 54 deletions
diff --git a/virtinst/cli.py b/virtinst/cli.py index 752f9df4..480e0b1e 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -22,7 +22,7 @@ from . import xmlutil from .buildconfig import BuildConfig from .connection import VirtinstConnection from .devices import (Device, DeviceController, DeviceDisk, DeviceGraphics, - DeviceInterface, DevicePanic) + DeviceHostdev, DeviceInterface, DevicePanic) from .guest import Guest from .logger import log, reset_logging from .nodedev import NodeDevice @@ -4559,53 +4559,61 @@ class ParserSound(VirtCLIParser): ##################### def _AddressStringToHostdev(conn, addrstr): - from .devices import DeviceHostdev + """ + Decompose common USB and PCI address string formats into a DeviceHostdev + instance. The 3 expected formats are: + + - bus.addr (ex. 001.003 for a usb device) + - vendor:product (ex. 0x1234:0x5678 for a usb device + - (domain:)bus:slot.func (ex. 00:10.0 for a pci device) + """ hostdev = DeviceHostdev(conn) - try: - # Determine addrstr type - if addrstr.count(":") in [1, 2] and "." in addrstr: - addrstr, func = addrstr.split(".", 1) - addrstr, slot = addrstr.rsplit(":", 1) - domain = "0" - if ":" in addrstr: - domain, bus = addrstr.split(":", 1) - else: - bus = addrstr + if addrstr.count(":") in [1, 2] and "." in addrstr: + # PCI address + addrstr, func = addrstr.split(".", 1) + addrstr, slot = addrstr.rsplit(":", 1) + domain = "0" + if ":" in addrstr: + domain, bus = addrstr.split(":", 1) + else: + bus = addrstr - hostdev.type = "pci" - hostdev.domain = "0x%.4X" % int(domain, 16) - hostdev.function = "0x%.2X" % int(func, 16) - hostdev.slot = "0x%.2X" % int(slot, 16) - hostdev.bus = "0x%.2X" % int(bus, 16) + hostdev.type = "pci" + hostdev.domain = "0x%.4X" % int(domain, 16) + hostdev.function = "0x%.2X" % int(func, 16) + hostdev.slot = "0x%.2X" % int(slot, 16) + hostdev.bus = "0x%.2X" % int(bus, 16) - elif ":" in addrstr: - vendor, product = addrstr.split(":") + elif ":" in addrstr: + # USB product:vendor + vendor, product = addrstr.split(":") - hostdev.type = "usb" - hostdev.vendor = "0x%.4X" % int(vendor, 16) - hostdev.product = "0x%.4X" % int(product, 16) + hostdev.type = "usb" + hostdev.vendor = "0x%.4X" % int(vendor, 16) + hostdev.product = "0x%.4X" % int(product, 16) - elif "." in addrstr: - bus, device = addrstr.split(".", 1) + elif "." in addrstr: + # USB bus.device + bus, device = addrstr.split(".", 1) - hostdev.type = "usb" - hostdev.bus = bus - hostdev.device = device - else: - raise RuntimeError( - "Unknown hostdev address string format '%s'" % addrstr) - except Exception: - log.debug("Error parsing node device string.", exc_info=True) - raise + hostdev.type = "usb" + hostdev.bus = bus + hostdev.device = device + else: + raise RuntimeError( + "Unknown hostdev address string format '%s'" % addrstr) return hostdev def _AddressStringToNodedev(conn, addrstr): - hostdev = _AddressStringToHostdev(conn, addrstr) + try: + hostdev = _AddressStringToHostdev(conn, addrstr) + except Exception: + log.debug("Error parsing node device string.", exc_info=True) + raise - # Iterate over node devices and compare count = 0 nodedev = None @@ -4614,37 +4622,22 @@ def _AddressStringToNodedev(conn, addrstr): nodedev = xmlobj count += 1 - if count == 1: - return nodedev - elif count > 1: + if count > 1: raise ValueError(_("%s corresponds to multiple node devices") % addrstr) - elif count < 1: + if count < 1: raise ValueError(_("Did not find a matching node device for '%s'") % addrstr) + return nodedev def _lookupNodedevFromString(conn, idstring): - """ - Convert the passed libvirt node device name to a NodeDevice - instance, with proper error reporting. If the name is name is not - found, we will attempt to parse the name as would be passed to - devAddressToNodeDev - - :param conn: libvirt.virConnect instance to perform the lookup on - :param idstring: libvirt node device name to lookup, or address - of the form: - - bus.addr (ex. 001.003 for a usb device) - - vendor:product (ex. 0x1234:0x5678 for a usb device - - (domain:)bus:slot.func (ex. 00:10.0 for a pci device) - - :returns: NodeDevice instance - """ # First try and see if this is a libvirt nodedev name nodedev = NodeDevice.lookupNodedevByName(conn, idstring) if nodedev: return nodedev + # If not it must be a special CLI format that we need to parse try: return _AddressStringToNodedev(conn, idstring) except Exception: |