Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit 5ca920c

Browse files
committed
virt-install: Allow specifying --host-device by lsusb, lspci output
1 parent bea9f0a commit 5ca920c

6 files changed

Lines changed: 186 additions & 32 deletions

File tree

man/en/virt-install.1

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.07)
1+
.\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.07)
22
.\"
33
.\" Standard preamble:
44
.\" ========================================================================
5-
.de Sh \" Subsection heading
6-
.br
7-
.if t .Sp
8-
.ne 5
9-
.PP
10-
\fB\\$1\fR
11-
.PP
12-
..
135
.de Sp \" Vertical space (when we can't use .PP)
146
.if t .sp .5v
157
.if n .sp
@@ -53,7 +45,7 @@
5345
.el .ds Aq '
5446
.\"
5547
.\" If the F register is turned on, we'll generate index entries on stderr for
56-
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
48+
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
5749
.\" entries marked with X<> in POD. Of course, you'll have to process the
5850
.\" output yourself in some meaningful fashion.
5951
.ie \nF \{\
@@ -132,7 +124,7 @@
132124
.\" ========================================================================
133125
.\"
134126
.IX Title "VIRT-INSTALL 1"
135-
.TH VIRT-INSTALL 1 "2009-12-03" "" "Virtual Machine Install Tools"
127+
.TH VIRT-INSTALL 1 "2010-02-08" "" "Virtual Machine Install Tools"
136128
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
137129
.\" way too many mistakes in technical documents.
138130
.if n .ad l
@@ -189,7 +181,7 @@ prioritization is incorrect, eg if wanting to use \s-1QEMU\s0 while on a Xen ker
189181
.RE
190182
.RS 2
191183
.RE
192-
.Sh "General Options"
184+
.SS "General Options"
193185
.IX Subsection "General Options"
194186
General configuration parameters that apply to all types of guest installs.
195187
.IP "\-n \s-1NAME\s0, \-\-name=NAME" 2
@@ -440,9 +432,24 @@ No \s-1OS\s0 version specified (disables autodetect)
440432
.RE
441433
.IP "\-\-host\-device=HOSTDEV" 2
442434
.IX Item "--host-device=HOSTDEV"
443-
Attach a physical host device to the guest. \s-1HOSTDEV\s0 is a node device name
444-
as used by libvirt (as shown by 'virsh nodedev\-list').
445-
.Sh "Full Virtualization specific options"
435+
Attach a physical host device to the guest. Some example values for \s-1HOSTDEV:\s0
436+
.RS 2
437+
.IP "\fB\-\-host\-device pci_0000_00_1b_0\fR" 2
438+
.IX Item "--host-device pci_0000_00_1b_0"
439+
A node device name via libvirt, as shown by 'virsh nodedev\-list'
440+
.IP "\fB\-\-host\-device 001.003\fR" 2
441+
.IX Item "--host-device 001.003"
442+
\&\s-1USB\s0 by bus, device (via lsusb).
443+
.IP "\fB\-\-host\-device 0x1234:0x5678\fR" 2
444+
.IX Item "--host-device 0x1234:0x5678"
445+
\&\s-1USB\s0 by vendor, product (via lsusb).
446+
.IP "\fB\-\-host\-device 1f.01.02\fR" 2
447+
.IX Item "--host-device 1f.01.02"
448+
\&\s-1PCI\s0 device (via lspci).
449+
.RE
450+
.RS 2
451+
.RE
452+
.SS "Full Virtualization specific options"
446453
.IX Subsection "Full Virtualization specific options"
447454
Parameters specific only to fully virtualized guest installs.
448455
.IP "\-\-sound" 2
@@ -528,7 +535,7 @@ virtualized guest.
528535
.IX Item "--noacpi"
529536
Override the \s-1OS\s0 type / variant to disables the \s-1ACPI\s0 setting for fully
530537
virtualized guest.
531-
.Sh "Virtualization Type options"
538+
.SS "Virtualization Type options"
532539
.IX Subsection "Virtualization Type options"
533540
Options to override the default virtualization type choices.
534541
.IP "\-v, \-\-hvm" 2
@@ -551,7 +558,7 @@ Availabile options are listed via 'virsh capabilities' in the <domain> tags.
551558
Prefer \s-1KVM\s0 or \s-1KQEMU\s0 (in that order) if installing a \s-1QEMU\s0 guest. This behavior
552559
is now the default, and this option is deprecated. To install a plain \s-1QEMU\s0
553560
guest, use '\-\-virt\-type qemu'
554-
.Sh "Installation Method options"
561+
.SS "Installation Method options"
555562
.IX Subsection "Installation Method options"
556563
.IP "\-c \s-1CDROM\s0, \-\-cdrom=CDROM" 2
557564
.IX Item "-c CDROM, --cdrom=CDROM"
@@ -615,7 +622,7 @@ may be desirable to also use the \f(CW\*(C`\-\-nodisks\*(C'\fR flag in combinati
615622
.IX Item "-x EXTRA, --extra-args=EXTRA"
616623
Additional kernel command line arguments to pass to the installer when
617624
performing a guest install from \f(CW\*(C`\-\-location\*(C'\fR.
618-
.Sh "Storage Configuration"
625+
.SS "Storage Configuration"
619626
.IX Subsection "Storage Configuration"
620627
.IP "\-\-disk=DISKOPTS" 2
621628
.IX Item "--disk=DISKOPTS"
@@ -713,7 +720,7 @@ This option is deprecated in favor of \f(CW\*(C`\-\-disk ...,size=DISKSIZE,...\*
713720
.IP "\-\-nonsparse" 2
714721
.IX Item "--nonsparse"
715722
This option is deprecated in favor of \f(CW\*(C`\-\-disk ...,sparse=false,...\*(C'\fR
716-
.Sh "Networking Configuration"
723+
.SS "Networking Configuration"
717724
.IX Subsection "Networking Configuration"
718725
.IP "\-w \s-1NETWORK\s0, \-\-network=NETWORK,opt1=val1,opt2=val2" 2
719726
.IX Item "-w NETWORK, --network=NETWORK,opt1=val1,opt2=val2"
@@ -772,7 +779,7 @@ This parameter is deprecated in favour of
772779
.IP "\-m \s-1MAC\s0, \-\-mac=MAC" 2
773780
.IX Item "-m MAC, --mac=MAC"
774781
This parameter is deprecated in favour of \f(CW\*(C`\-\-network NETWORK,mac=12:34...\*(C'\fR
775-
.Sh "Graphics Configuration"
782+
.SS "Graphics Configuration"
776783
.IX Subsection "Graphics Configuration"
777784
If no graphics option is specified, \f(CW\*(C`virt\-install\*(C'\fR will default to \-\-vnc
778785
if the \s-1DISPLAY\s0 environment variable is set, otherwise \-\-nographics is used.
@@ -825,13 +832,17 @@ Don't automatically try to connect to the guest console. The default behaviour
825832
is to launch a \s-1VNC\s0 client to display the graphical console, or to run the
826833
\&\f(CW\*(C`virsh\*(C'\fR \f(CW\*(C`console\*(C'\fR command to display the text console. Use of this parameter
827834
will disable this behaviour.
828-
.Sh "Miscellaneous Options"
835+
.SS "Miscellaneous Options"
829836
.IX Subsection "Miscellaneous Options"
830837
.IP "\-d, \-\-debug" 2
831838
.IX Item "-d, --debug"
832839
Print debugging information to the terminal when running the install process.
833840
The debugging information is also stored in \f(CW\*(C`$HOME/.virtinst/virt\-install.log\*(C'\fR
834841
even if this parameter is omitted.
842+
.IP "\-\-autostart" 2
843+
.IX Item "--autostart"
844+
Set the autostart flag for a domain. This causes the domain to be started
845+
on host boot up.
835846
.IP "\-\-noreboot" 2
836847
.IX Item "--noreboot"
837848
Prevent the domain from automatically rebooting after the install has

man/en/virt-install.pod.in

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,27 @@ Valid values are:
147147

148148
=item --host-device=HOSTDEV
149149

150-
Attach a physical host device to the guest. HOSTDEV is a node device name
151-
as used by libvirt (as shown by 'virsh nodedev-list').
150+
Attach a physical host device to the guest. Some example values for HOSTDEV:
151+
152+
=over 2
153+
154+
=item B<--host-device pci_0000_00_1b_0>
155+
156+
A node device name via libvirt, as shown by 'virsh nodedev-list'
157+
158+
=item B<--host-device 001.003>
159+
160+
USB by bus, device (via lsusb).
161+
162+
=item B<--host-device 0x1234:0x5678>
163+
164+
USB by vendor, product (via lsusb).
165+
166+
=item B<--host-device 1f.01.02>
167+
168+
PCI device (via lspci).
169+
170+
=back
152171

153172
=back
154173

tests/clitest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,26 @@
397397

398398
}, # category "network"
399399

400+
"hostdev" : {
401+
"hostdev_args": "--noautoconsole --nographics --nodisks --pxe",
402+
403+
"valid" : [
404+
# Host dev by libvirt name
405+
"--host-device usb_device_781_5151_2004453082054CA1BEEE",
406+
# Many hostdev parsing types
407+
"--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",
408+
],
409+
410+
"invalid" : [
411+
# Unsupported hostdev type
412+
"--host-device pci_8086_2850_scsi_host_scsi_host",
413+
# Unknown hostdev
414+
"--host-device foobarhostdev",
415+
# Parseable hostdev, but unknown digits
416+
"--host-device 300:400",
417+
],
418+
}, # category "hostdev"
419+
400420
"remote" : {
401421
"remote_args": "--connect %(REMOTEURI)s --nographics --noautoconsole",
402422

tests/testdriver.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@
432432
<name>sdhci-pci</name>
433433
</driver>
434434
<capability type='pci'>
435-
<domain>0</domain>
435+
<domain>2</domain>
436436
<bus>21</bus>
437437
<slot>0</slot>
438438
<function>2</function>

virtinst/NodeDeviceParser.py

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from virtinst import _virtinst as _
2121
import support
2222
import _util
23+
import libvirt
24+
import logging
2325

2426
# class USBDevice
2527

@@ -405,25 +407,123 @@ def is_pci_detach_capable(conn):
405407
return support.check_conn_support(conn,
406408
support.SUPPORT_NODEDEV_PCI_DETACH)
407409

410+
def _lookupNodeName(conn, name):
411+
nodedev = conn.nodeDeviceLookupByName(name)
412+
xml = nodedev.XMLDesc(0)
413+
return parse(xml)
414+
408415
def lookupNodeName(conn, name):
409416
"""
410417
Convert the passed libvirt node device name to a NodeDevice
411-
instance, with proper error reporting.
418+
instance, with proper error reporting. If the name is name is not
419+
found, we will attempt to parse the name as would be passed to
420+
devAddressToNodeDev
412421
413422
@param conn: libvirt.virConnect instance to perform the lookup on
414-
@param name: libvirt node device name to lookup
423+
@param name: libvirt node device name to lookup, or address for
424+
devAddressToNodedev
415425
416426
@rtype: L{NodeDevice} instance
417427
"""
428+
if not is_nodedev_capable(conn):
429+
raise ValueError(_("Connection does not support host device "
430+
"enumeration."))
431+
432+
try:
433+
return _lookupNodeName(conn, name)
434+
except libvirt.libvirtError, e:
435+
ret = _isAddressStr(name)
436+
if not ret:
437+
raise e
438+
439+
return devAddressToNodedev(conn, name)
440+
441+
def _isAddressStr(addrstr):
442+
cmp_func = None
443+
444+
try:
445+
# Determine addrstr type
446+
if addrstr.count(":") in [1, 2] and addrstr.count("."):
447+
devtype = CAPABILITY_TYPE_PCI
448+
addrstr, func = addrstr.split(".", 1)
449+
addrstr, slot = addrstr.rsplit(":", 1)
450+
domain = "0"
451+
if addrstr.count(":"):
452+
domain, bus = addrstr.split(":", 1)
453+
else:
454+
bus = addrstr
455+
456+
func = int(func, 16)
457+
slot = int(slot, 16)
458+
domain = int(domain, 16)
459+
bus = int(bus, 16)
460+
461+
def pci_cmp(nodedev):
462+
return ((int(nodedev.domain) == domain) and
463+
(int(nodedev.function) == func) and
464+
(int(nodedev.bus) == bus) and
465+
(int(nodedev.slot) == slot))
466+
cmp_func = pci_cmp
467+
468+
elif addrstr.count(":"):
469+
devtype = CAPABILITY_TYPE_USBDEV
470+
vendor, product = addrstr.split(":")
471+
vendor = int(vendor, 16)
472+
product = int(product, 16)
473+
474+
def usbprod_cmp(nodedev):
475+
return ((int(nodedev.vendor_id, 16) == vendor) and
476+
(int(nodedev.product_id, 16) == product))
477+
cmp_func = usbprod_cmp
478+
479+
elif addrstr.count("."):
480+
devtype = CAPABILITY_TYPE_USBDEV
481+
bus, addr = addrstr.split(".", 1)
482+
bus = int(bus)
483+
addr = int(addr)
484+
485+
def usbaddr_cmp(nodedev):
486+
return ((int(nodedev.bus) == bus) and
487+
(int(nodedev.device) == addr))
488+
cmp_func = usbaddr_cmp
489+
except:
490+
logging.exception("Error parsing node device string.")
491+
return None
492+
493+
return cmp_func, devtype
418494

495+
def devAddressToNodedev(conn, addrstr):
496+
"""
497+
Look up the passed host device address string as a libvirt node device,
498+
parse its xml, and return a NodeDevice instance.
499+
500+
addrstr can be the following formats:
501+
bus.addr (ex. 001.003 for a usb device)
502+
vendor:product (ex. 0x1234:0x5678 for a usb device
503+
(domain:)bus:slot.func (ex. 00:10.0 for a pci device)
504+
505+
@param conn: libvirt.virConnect instance to perform the lookup on
506+
@param name: host device string to parse and lookup
507+
"""
419508
if not is_nodedev_capable(conn):
420509
raise ValueError(_("Connection does not support host device "
421510
"enumeration."))
422511

423-
nodedev = conn.nodeDeviceLookupByName(name)
424-
xml = nodedev.XMLDesc(0)
425-
return parse(xml)
512+
ret = _isAddressStr(addrstr)
513+
if not ret:
514+
raise ValueError(_("Could not determine format of '%s'") % addrstr)
515+
516+
cmp_func, devtype = ret
517+
518+
# Iterate over node devices and compare
519+
nodenames = conn.listDevices(devtype, 0)
520+
for name in nodenames:
521+
nodedev = _lookupNodeName(conn, name)
522+
if cmp_func(nodedev):
523+
return nodedev
426524

525+
raise ValueError(_("Did not find a matching node device for '%s'") %
526+
addrstr)
427527

428528
def parse(xml):
429529
"""

virtinst/VirtualHostDevice.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ class VirtualHostDevice(VirtualDevice.VirtualDevice):
3232

3333
def device_from_node(conn, name=None, nodedev=None):
3434
"""
35-
Convert the passed libvirt node device name to a VirtualHostDevice
36-
instance, with proper error reporting.
35+
Convert the passed device name to a VirtualHostDevice
36+
instance, with proper error reporting. Name can be any of the
37+
values accepted by NodeDeviceParser.lookupNodeName. If a node
38+
device name is not specified, a virtinst.NodeDevice instance can
39+
be passed in to create a dev from.
3740
3841
@param conn: libvirt.virConnect instance to perform the lookup on
39-
@param name: libvirt node device name to lookup
42+
@param name: optional libvirt node device name to lookup
43+
@param nodedev: optional L{virtinst.NodeDevice} instance to use
4044
4145
@rtype: L{virtinst.VirtualHostDevice} instance
4246
"""

0 commit comments

Comments
 (0)