目录
实现直通
查看USB相关信息:
# lsusb
Bus 002 Device 003: ID 058f:6387 Alcor Micro Corp. Flash Drive
Bus 002 Device 002: ID 8087:8002 Intel Corp.
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 005: ID 413c:a001 Dell Computer Corp. Hub
Bus 001 Device 004: ID 17ef:602d Lenovo
Bus 001 Device 003: ID 17ef:602e Lenovo
Bus 001 Device 002: ID 8087:800a Intel Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
查看USB的树状信息:
# lsusb -t
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
|__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
|__ Port 2: Dev 3, If 0, Class=Mass Storage, Driver=usbfs, 480M
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
|__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
|__ Port 1: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
|__ Port 5: Dev 4, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
|__ Port 5: Dev 4, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
|__ Port 6: Dev 5, If 0, Class=Hub, Driver=hub/6p, 480M
根据上面的信息,可以获取到将要直通的USB设备的如下相关信息:
Vendor id : 058f
Product id :6387
Driver :ehci
新建一个xml文件:
# vim /etc/libvirt/W10_usb.xml
写入如下内容:
<hostdev mode='subsystem' type='usb' managed='yes'>
<source>
<vendor id='0x058f'/>
<product id='0x6387'/>
</source>
</hostdev>
修改对应虚拟机的xml配置文件:
# vim /etc/libvirt/qemu/W10.xml
添加一行:
其中的 ehci 是在 lsusb -t 命令下获取的信息
<controller type='usb' index='1' model='ehci' />
执行设备追加:
其中第二个参数 W10 是虚拟机名。
如果后面跟随 --persistent 参数则表示永久连接,如果没有此参数则只连接到本次关闭虚拟机为止。
# virsh attach-device W10 /etc/libvirt/W10_usb.xml --persistent
支持热插拨
该方法通过物理机的 udev 实现。
新增udev规则文件:
# vim /etc/udev/rules.d/vim 90-libvirt-usb.rules
内容如下:
ACTION=="add", \
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="058f", \
ATTRS{idProduct}=="6387", \
RUN+="/usr/bin/virsh attach-device W10 /etc/libvirt/W10_usb.xml"
ACTION=="remove", \
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="058f",\
ATTRS{idProduct}=="6387", \
RUN+="/usr/bin/virsh detach-device W10 /etc/libvirt/W10_usb.xml"
常用命令备忘
查看硬件详细信息:
# udevadm info -a -n /dev/sdb
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2':
KERNELS=="2-1.2"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{authorized}=="1"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{bConfigurationValue}=="1"
ATTRS{bDeviceClass}=="00"
ATTRS{bDeviceProtocol}=="00"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{bMaxPower}=="200mA"
ATTRS{bNumConfigurations}=="1"
ATTRS{bNumInterfaces}==" 1"
ATTRS{bcdDevice}=="0100"
ATTRS{bmAttributes}=="80"
ATTRS{busnum}=="2"
ATTRS{configuration}==""
ATTRS{devnum}=="5"
ATTRS{devpath}=="1.2"
ATTRS{idProduct}=="6387"
ATTRS{idVendor}=="058f"
ATTRS{ltm_capable}=="no"
ATTRS{manufacturer}=="Generic"
ATTRS{maxchild}=="0"
ATTRS{product}=="Mass Storage"
ATTRS{quirks}=="0x0"
ATTRS{removable}=="removable"
ATTRS{rx_lanes}=="1"
ATTRS{serial}=="8BCBA39315F5"
ATTRS{speed}=="480"
ATTRS{tx_lanes}=="1"
ATTRS{urbnum}=="515"
ATTRS{version}==" 2.00"
查看USB设备描述:
# lsusb -v
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x058f Alcor Micro Corp.
idProduct 0x6387 Flash Drive
bcdDevice 1.00
iManufacturer 1 Generic
iProduct 2 Mass Storage
iSerial 3 8BCBA39315F5
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 0x0020
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0x80
(Bus Powered)
MaxPower 200mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 2
bInterfaceClass 8 Mass Storage
bInterfaceSubClass 6 SCSI
bInterfaceProtocol 80 Bulk-Only
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x01 EP 1 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x82 EP 2 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
OVER