将 USB 加密狗连接到 KVM 虚拟机

csc*_*sch 8 ubuntu libvirt qemu kvm-virtualization

我正在努力将主机正确检测到的 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
Run Code Online (Sandbox Code Playgroud)

安装成功后virsh dumpxml test01返回

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
Run Code Online (Sandbox Code Playgroud)

我现在想连接一个 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
    
    Run Code Online (Sandbox Code Playgroud)
  3. 通过添加新的配置片段到 vm virsh edit 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>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 重启虚拟机

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

一些说明建议关闭 apparmor,但/etc/init.d/apparmor stop没有任何改变。

这让我抓狂,因为我没有收到任何错误或日志消息,而且我不知道如何找出问题所在。关于如何将 USB 连接到 vm 的任何想法,或者至少,如何进一步分析它?

小智 8

要向运行管理程序的用户授予对原始 USB 设备节点的永久访问权限,您需要创建一个 udev 规则;基于 chown 的答案只会在下次重新启动之前有效。

在 中/lib/udev/rules.d,创建一个文件,如51-usb_passthrough.rules

SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTRS{busnum}=="2" ATTRS{devpath}=="1" GROUP="kvm"
Run Code Online (Sandbox Code Playgroud)

在这里,我使用物理总线和端口号来定位设备(因为无论插入什么设备,我都喜欢通过物理端口,而不是每次连接新设备时重新配置我的 VM),但当然你可以使用任何属性你要; 该GROUP参数确定将拥有设备节点的组,这应该是运行虚拟机的任何用户。

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


amo*_*ebe 4

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

chown libvirt-qemu /dev/bus/usb/ -R
Run Code Online (Sandbox Code Playgroud)

或您的 KVM 以哪个用户身份运行。这应该可以解决问题。