![启动带有 USB 串行设备的 QEMU VM 失败,权限被拒绝,但在 VM 启动后附加它可以正常工作](https://linux22.com/image/220922/%E5%90%AF%E5%8A%A8%E5%B8%A6%E6%9C%89%20USB%20%E4%B8%B2%E8%A1%8C%E8%AE%BE%E5%A4%87%E7%9A%84%20QEMU%20VM%20%E5%A4%B1%E8%B4%A5%EF%BC%8C%E6%9D%83%E9%99%90%E8%A2%AB%E6%8B%92%E7%BB%9D%EF%BC%8C%E4%BD%86%E5%9C%A8%20VM%20%E5%90%AF%E5%8A%A8%E5%90%8E%E9%99%84%E5%8A%A0%E5%AE%83%E5%8F%AF%E4%BB%A5%E6%AD%A3%E5%B8%B8%E5%B7%A5%E4%BD%9C.png)
我在以下位置定义了一个 USB 设备virt/usb-serial.xml
:
<serial type="dev">
<source path="/dev/serial/by-id/usb-dresden_elektronik_ingenieurtechnik_GmbH_ConBee_II_DE2403534-if00"/>
<target type="usb-serial" port="1">
<model name="usb-serial"/>
</target>
<address type="usb" bus="0" port="5"/>
</serial>
我成功地附加了它,如下所示:
virsh # detach-device hass4 virt/usb-serial.xml --persistent
Device detached successfully
VM 能够完美地访问 USB 设备。但是,在重新启动主机设备后,由于此 USB 串行设备,虚拟机无法启动:
virsh # start hass4
error: Failed to start domain 'hass4'
error: internal error: process exited while connecting to monitor: 2023-06-01T20:28:52.153257Z qemu-system-x86_64: -chardev serial,id=charserial1,path=/dev/serial/by-id/usb-dresden_elektronik_ingenieurtechnik_GmbH_ConBee_II_DE2403534-if00: Could not open '/dev/serial/by-id/usb-dresden_elektronik_ingenieurtechnik_GmbH_ConBee_II_DE2403534-if00': Permission denied
分离设备可以让虚拟机正常启动,但奇怪的是在启动后我可以再次成功连接设备:
virsh # detach-device hass4 virt/usb-serial.xml --persistent
Device detached successfully
virsh # start hass4
Domain 'hass4' started
virsh # attach-device hass4 virt/usb-serial.xml
Device attached successfully
为什么它在首次启动时失败?如何解决权限问题?对于上下文,我在中定义了这一点/lib/udev/rules.d/51-qemu-usb-passthrough.rules
:
SUBSYSTEM=="usb", ATTRS{idVendor}=="1cf1" ATTRS{idProduct}=="0030" GROUP="libvirt"
这些是该设备相应的供应商和产品 ID:
$ lsusb -d 1cf1:0030
Bus 003 Device 010: ID 1cf1:0030 Dresden Elektronik ZigBee gateway [ConBee II]
在/etc/libvirt/qemu.conf
我已经设置:
user = "henrik"
group = "libvirt"
作为henrik
我运行这些命令的用户。