qemu下的USB直通功能介绍

文章目录

  • 1. 查看linux usb设备
    • 1.1.通过/sys/kernel/debug/usb/devices文件
    • 1.2. 通过lsusb命令
  • 2.启动带usb设备的虚拟机
    • 2.1.根据Bus, Dev来识别
    • 2.2. 根据Bus,Port来识别
  • 3.动态删除usb设备
    • 3.1.列出qemu的虚拟usb设备
    • 3.2.删除指定设备
    • 3.3.动态添加设备

1. 查看linux usb设备

1.1.通过/sys/kernel/debug/usb/devices文件

[root@192 usb-test]# cat /sys/kernel/debug/usb/devices

T:  Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#=  1 Spd=480  MxCh= 6
B:  Alloc=  0/800 us ( 0%), #Int=  0, #Iso=  0
D:  Ver= 2.00 Cls=09(hub  ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=1d6b ProdID=0002 Rev= 3.10
S:  Manufacturer=Linux 3.10.0-693.21.1.el7.x86_64.debug ehci_hcd
S:  Product=EHCI Host Controller
S:  SerialNumber=0000:02:03.0
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=  0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
E:  Ad=81(I) Atr=03(Int.) MxPS=   4 Ivl=256ms

T:  Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#=  1 Spd=12   MxCh= 2
B:  Alloc=  0/900 us ( 0%), #Int=  1, #Iso=  0
D:  Ver= 1.10 Cls=09(hub  ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=1d6b ProdID=0001 Rev= 3.10
S:  Manufacturer=Linux 3.10.0-693.21.1.el7.x86_64.debug uhci_hcd
S:  Product=UHCI Host Controller
S:  SerialNumber=0000:02:00.0
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=  0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
E:  Ad=81(I) Atr=03(Int.) MxPS=   2 Ivl=255ms

T:  Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=12   MxCh= 0
D:  Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
P:  Vendor=0e0f ProdID=0003 Rev= 1.03
S:  Manufacturer=VMware
S:  Product=VMware Virtual USB Mouse
C:* #Ifs= 1 Cfg#= 1 Atr=c0 MxPwr=  0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=01 Prot=02 Driver=usbhid
E:  Ad=81(I) Atr=03(Int.) MxPS=   8 Ivl=1ms

T:  Bus=02 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#=  3 Spd=12   MxCh= 7
D:  Ver= 1.10 Cls=09(hub  ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
P:  Vendor=0e0f ProdID=0002 Rev= 1.00
S:  Product=VMware Virtual USB Hub
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=  0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub  ) Sub=00 Prot=00 Driver=hub
E:  Ad=81(I) Atr=03(Int.) MxPS=   1 Ivl=255ms

T:  Bus=02 Lev=02 Prnt=03 Port=00 Cnt=01 Dev#=  4 Spd=12   MxCh= 0
D:  Ver= 2.00 Cls=e0(wlcon) Sub=01 Prot=01 MxPS=64 #Cfgs=  1
P:  Vendor=0e0f ProdID=0008 Rev= 1.00
S:  Manufacturer=VMware
S:  Product=Virtual Bluetooth Adapter
S:  SerialNumber=000650268328
C:* #Ifs= 2 Cfg#= 1 Atr=c0 MxPwr=  0mA
I:* If#= 0 Alt= 0 #EPs= 3 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb
E:  Ad=81(I) Atr=03(Int.) MxPS=  16 Ivl=1ms
E:  Ad=02(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=82(I) Atr=02(Bulk) MxPS=  64 Ivl=0ms
I:* If#= 1 Alt= 0 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb
E:  Ad=03(O) Atr=01(Isoc) MxPS=   0 Ivl=1ms
E:  Ad=83(I) Atr=01(Isoc) MxPS=   0 Ivl=1ms
I:  If#= 1 Alt= 1 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb
E:  Ad=03(O) Atr=01(Isoc) MxPS=   9 Ivl=1ms
E:  Ad=83(I) Atr=01(Isoc) MxPS=   9 Ivl=1ms
I:  If#= 1 Alt= 2 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb
E:  Ad=03(O) Atr=01(Isoc) MxPS=  17 Ivl=1ms
E:  Ad=83(I) Atr=01(Isoc) MxPS=  17 Ivl=1ms
I:  If#= 1 Alt= 3 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb
E:  Ad=03(O) Atr=01(Isoc) MxPS=  25 Ivl=1ms
E:  Ad=83(I) Atr=01(Isoc) MxPS=  25 Ivl=1ms
I:  If#= 1 Alt= 4 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb
E:  Ad=03(O) Atr=01(Isoc) MxPS=  33 Ivl=1ms
E:  Ad=83(I) Atr=01(Isoc) MxPS=  33 Ivl=1ms
I:  If#= 1 Alt= 5 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb
E:  Ad=03(O) Atr=01(Isoc) MxPS=  49 Ivl=1ms
E:  Ad=83(I) Atr=01(Isoc) MxPS=  49 Ivl=1ms

T:  Bus=02 Lev=02 Prnt=03 Port=01 Cnt=02 Dev#=  6 Spd=12   MxCh= 0
D:  Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=16 #Cfgs=  1
P:  Vendor=0ef5 ProdID=2202 Rev= 1.00
S:  Manufacturer=USB Mass Storage
S:  Product=USB Mass Storage
S:  SerialNumber=2005102616390601
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=100mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E:  Ad=01(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=82(I) Atr=02(Bulk) MxPS=  64 Ivl=0ms

1.2. 通过lsusb命令

[root@localhost ~]# lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
    |__ Port 1: Dev 2, If 0, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 2: Dev 3, If 0, Class=Hub, Driver=hub/7p, 12M
        |__ Port 1: Dev 4, If 0, Class=Wireless, Driver=btusb, 12M
        |__ Port 1: Dev 4, If 1, Class=Wireless, Driver=btusb, 12M
        |__ Port 2: Dev 5, If 0, Class=Mass Storage, Driver=usb-storage, 12M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M

2.启动带usb设备的虚拟机

注意,识别出host上usb设备的方式有很多种,以下的并不完整

2.1.根据Bus, Dev来识别

  • qemu命令行方式
 -device usb-host,hostbus=2,hostaddr=6,id=hostdev0 

这种方式优点在于Dev号是唯一的,如1.2节所示,每个层级的设备都是统一编号

  • libvirt xml方式
'subsystem' type='usb' managed='yes'>
   
       '0x0ef5'/>
       '0x2202'/>
       
'2' device='6' />

这里效果和上面一样,但是不知道vendor和product的id是否是必须的???

2.2. 根据Bus,Port来识别

  • qemu命令行方式
-device usb-host,hostbus=2,hostport=2.2,id=hostdev0 \

这里要注意hostport的2.2,代表是Bus=02,Port2的Port2,或者是level2的Port2???

  • libvirt xml方式
    赞不支持,需要开发

3.动态删除usb设备

3.1.列出qemu的虚拟usb设备

(qemu) info usb
  Device 0.2, Port 1, Speed 12 Mb/s, Product host:1.2, ID: hostdev0
  Device 0.3, Port 2, Speed 12 Mb/s, Product QEMU USB Hub
  Device 0.4, Port 2.1, Speed 12 Mb/s, Product USB Mass Storage, ID: hostdev1

qemu下的USB直通功能介绍_第1张图片图3-1

字段含义
ID: hostdev0这个就是qemu启动参数里面的id
Device 0.2指的是guest里面看到的Dev 2
Port 1指的是Bus 01下面的Port1
Port 2.2指的是Bus 01下面的Port2,再下面的Port1
Product指的是产品名字,没任何意义
由此可见,真正是对host可见的只有ID,因此要删除一个设备,只能通过ID号

[root@192 usb-test]# lsusb  -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
    |__ Port 1: Dev 2, If 0, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 2: Dev 3, If 0, Class=Hub, Driver=hub/7p, 12M
        |__ Port 1: Dev 4, If 0, Class=Wireless, Driver=btusb, 12M
        |__ Port 1: Dev 4, If 1, Class=Wireless, Driver=btusb, 12M
        |__ Port 2: Dev 5, If 0, Class=Mass Storage, Driver=usb-storage, 12M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M
    |__ Port 1: Dev 2, If 0, Class=Mass Storage, Driver=usb-storage, 480M

从host上U盘信息也可以看到,与图3-1显示的信息无关

3.2.删除指定设备

(qemu) device_del hostdev1
(qemu) info usb
  Device 0.2, Port 1, Speed 12 Mb/s, Product host:1.2, ID: hostdev0
  Device 0.3, Port 2, Speed 12 Mb/s, Product QEMU USB Hub

可以看到,ID为hostdev1的设备被删除了

-libvirt删除
[root@localhost usb-test]# virsh detach-device vm1-usb --file usb_2.0.xml

 <hostdev mode='subsystem' type='usb' managed='yes'>
    <source>
        <address bus='1' port='1' />
     </source>
 </hostdev>

3.3.动态添加设备

[root@localhost usb-test]# virsh attach-device vm1-usb --file usb_2.0.xml
Device attached successfully

[1]USB device & USB controller & USB passthrough
[2]Linux下,查看USB设备信息
[3]KVM usb passthrough配置
[4]虚机支持虚拟USB存储设备
[5]qemu: usb存储设备仿真

你可能感兴趣的:(虚拟化,qemu,qemu,libvirt,虚拟化)