将USBencryption狗连接到KVM虚拟机

我正在努力将主机正确检测到的USB设备连接到kvm虚拟机。

我有一个新安装的Ubuntu Server 14.10作为KVM / QEMU主机。 我使用这个命令设置了一个Ubuntu虚拟机:

virt-install --connect qemu:///system \ -n test01 \ -r 1024 \ --vcpus=2 \ --disk path=/vmstorage/01/test01.img,size=5 \ --vnc \ --noautoconsole \ --os-variant=ubuntuutopic \ --hvm \ --cdrom /path/to/ubuntu-14.10-server-i386.iso 

成功安装后, virsh dumpxml test01返回

 <domain type='kvm' id='16'> <name>test01</name> <uuid>f58ca825-c999-4168-9f5a-616057d9955d</uuid> <memory unit='KiB'>1048576</memory> <currentMemory unit='KiB'>1048576</currentMemory> <vcpu placement='static'>2</vcpu> <resource> <partition>/machine</partition> </resource> <os> <type arch='x86_64' machine='pc-i440fx-utopic'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> <apic/> <pae/> </features> <cpu mode='custom' match='exact'> <model fallback='allow'>SandyBridge</model> </cpu> <clock offset='utc'> <timer name='rtc' tickpolicy='catchup'/> <timer name='pit' tickpolicy='delay'/> <timer name='hpet' present='no'/> </clock> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/bin/kvm-spice</emulator> <disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/vmstorage/01/test01.img'/> <backingStore/> <target dev='vda' bus='virtio'/> <alias name='virtio-disk0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </disk> <disk type='block' device='cdrom'> <driver name='qemu' type='raw'/> <backingStore/> <target dev='hda' bus='ide'/> <readonly/> <alias name='ide0-0-0'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <controller type='usb' index='0' model='ich9-ehci1'> <alias name='usb0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/> </controller> <controller type='usb' index='0' model='ich9-uhci1'> <alias name='usb0'/> <master startport='0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/> </controller> <controller type='usb' index='0' model='ich9-uhci2'> <alias name='usb0'/> <master startport='2'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/> </controller> <controller type='usb' index='0' model='ich9-uhci3'> <alias name='usb0'/> <master startport='4'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/> </controller> <controller type='pci' index='0' model='pci-root'> <alias name='pci.0'/> </controller> <controller type='ide' index='0'> <alias name='ide0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <interface type='bridge'> <mac address='52:54:00:11:b2:c1'/> <source bridge='br0'/> <target dev='vnet0'/> <model type='virtio'/> <alias name='net0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <serial type='pty'> <source path='/dev/pts/0'/> <target port='0'/> <alias name='serial0'/> </serial> <console type='pty' tty='/dev/pts/0'> <source path='/dev/pts/0'/> <target type='serial' port='0'/> <alias name='serial0'/> </console> <input type='mouse' bus='ps2'/> <input type='keyboard' bus='ps2'/> <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0'> <listen type='address' address='0.0.0.0'/> </graphics> <video> <model type='cirrus' vram='9216' heads='1'/> <alias name='video0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <alias name='balloon0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </memballoon> </devices> <seclabel type='dynamic' model='apparmor' relabel='yes'> <label>libvirt-f58ca825-c999-4168-9f5a-616057d9955d</label> <imagelabel>libvirt-f58ca825-c999-4168-9f5a-616057d9955d</imagelabel> </seclabel> </domain> 

我现在要附加一个USB棒。 经过一番googleing,我发现 一些 来源 ,基本上都提出了以下方法:

  1. 把棍子附加到主机上
  2. 获取供应商和产品ID

     root@host01:~# lsusb Bus 002 Device 004: ID 13fe:5100 Kingston Technology Company Inc. Flash Drive Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 004: ID 0624:0249 Avocent Corp. Bus 001 Device 003: ID 0624:0248 Avocent Corp. Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 
  3. 通过virsh edit test01添加新的config-snippet到vm

     <devices> <!- ... -> <hostdev mode='subsystem' type='usb' managed='yes'> <source> <vendor id='0x13fe'/> <product id='0x5100'/> </source> </hostdev> </devices/> 
  4. 重启虚拟机

之后,vm应该通过lsusb看到这个棍子,但没有任何变化。 如果我尝试将设备连接到第二个virsh start <othervm>virsh start <othervm>失败, error: Requested operation is not valid: USB device 002:003 is in use by driver QEMU, domain test01 但是仍然可以挂载和访问坚持在主机上。 我尝试了不同的棒和不同的虚拟机操作系统(Ubuntu和Windows),没有成功。

一些说明build议closuresapparmor,但是/etc/init.d/apparmor stop没有改变任何东西。

这使我疯狂,因为我没有得到任何错误或日志消息,我不知道如何找出什么是错的。 有关如何让vm连接到vm的任何想法,或者至less,如何进一步分析它?

这可能是访问权限的问题。 您的QEMU deamon不允许访问USB设备。 尝试:

 chown libvirt-qemu /dev/bus/usb/ -R 

或者KVM运行的用户。 这应该做的伎俩。

为了永久访问pipe理程序所运行的用户的原始USB设备节点,您需要创build一个udev规则; 基于chown的答案只有在下一次重新启动时才会起作用。

/lib/udev/rules.d ,创build一个像51-usb_passthrough.rules的文件:

 SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{busnum}=="2" ATTRS{devpath}=="1" GROUP="kvm" 

在这里,我使用了物理总线和端口号来定位设备(不pipe每次连接新设备时,无论插入什么设备,而是重新configuration虚拟机),我更愿意通过物理端口传递,但是当然,您可以使用任何属性你要; GROUP参数确定将拥有设备节点的组,这应该是用户运行虚拟机的任何内容。

运行udevadm control --reload-rules使新规则立即生效(您仍然需要断开/重新连接USB设备),或者重启主机。

简单的解决scheme:连接

 nc -U socket-file 

到您的QEMU监视器,其中套接字文件是监视的path。 如何find:ps -FA | grep qemu。 你会在命令行find那里。

然后在监视器上运行命令:

 device_add usb-host,id=<ANY string>,hostbus=<BUS>,hostport=<PORT> 

这是通过端口号传递。 您可以通过任何设备与任何VID / PID。 您也可以使用特定的设备

 device_add usb-host,id=<ANY string>,vendorid=0x0461,productid= 0x0010 

他们可以插入到你的主机的任何端口。

有用的命令也:

 info usbhost info usb