'from QEMU to libvirt(virsh) - add USB-port

I would like to have following line translated from qemu optiones into libvirt xml.

-qemu-system-x86_64
-...
-usb -device usb-host,bus=usb-bus.0,hostbus=<bus>,hostport=<port> 

This adds a physical usb plug to a virtual machine. Most examples are shown and well documented for usb-bus/deviceID, not for this solution.

Edit: The tool virsh domxml-from-native qemu-argv MyArgV.sh has the following solution:

<qemu:commandline>
  <qemu:arg value='-device'/>
  <qemu:arg value='usb-host,bus=usb-bus.0,hostbus=1,hostport=10'/>
</qemu:commandline>

But this is not really what I wanted, because it is bypassing the libvirt system. So if the native tool can't find any solution, is there a general libvirt solution for passing USB-Ports?

I've also tried the virt-manager GUI for adding a USB-Port, but I was not able to find any possebilety to do so.

Is there maybe a possebilety to make a snapshot of a running qemu machine and replicate it with libvirt on the fly?

I found this webpage. But this one is describing how to assemble the usb-port hierarchy in the VM, not forwarding a host port to vm.



Solution 1:[1]

Unfortunately it isn't documented, but you can assign a USB device based on bus + device number with this syntax:

<hostdev mode='subsystem' type='usb' managed='no'>
  <source>
    <address bus='1' device='NNN'/>
  </source>
</hostdev>

unfortunately the device number here is the /dev/usb/bus/NNN number which changes every time you plug it in. There's not yet any support for picking the device based on hostport which is stable.

Solution 2:[2]

You can also find out the vendor and product ids from your USB device (by using lsusb) and then use this information to attach your USB device to your KVM:

$ lsusb
...
Bus 002 Device 018: ID 03f0:4217 Hewlett-Packard EWS CM1015
...
<hostdev mode='subsystem' type='usb'>
  <source>
    <vendor id='0x03f0'/>
    <product id='0x4217'/>
  </source>
</hostdev>

Found on https://rolandtapken.de/blog/2011-04/how-auto-hotplug-usb-devices-libvirt-vms-update-1.

However this will not work if you have more than one of such USB devices connected to the host.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DanielB
Solution 2 Claudio Kuenzler