1

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Cutton Eye
  • 3,207
  • 3
  • 20
  • 39

3 Answers3

3

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.

DanielB
  • 2,461
  • 1
  • 10
  • 13
  • This is the workarround. Finding a NO to this question is really valuable. Do you know if libvirt does not support it, or does only virsh not support it? In this context, what does "managed=yes/no" change? – Cutton Eye Sep 26 '17 at 14:17
  • The 'managed' attribute doesn't do anything interesting for USB devices - only PCI. The lack of support is in libvirt itself - we would willing accept patches from anyone with skills & interest to work on supporting this.... – DanielB Oct 03 '17 at 09:31
  • 1
    I've just discovered someone already supplied a patch for this feature, but we dropped the ball and it never got reviewed/merged https://www.redhat.com/archives/libvir-list/2016-July/msg00127.html I'll chase this up... – DanielB Oct 03 '17 at 09:35
  • This sounds cool! I can support this in testing the feature with several OS like XP / 7 / 8 / 10 / 14.4 / 16.04 on common and special USB-hardware if still necessary. – Cutton Eye Oct 03 '17 at 10:08
  • push: According to the commit and a diff to version 4.4.0 of libvirt released 2018-06, this feature is not implemented. If I can be of any help in deploying this feature, lease let me know. – Cutton Eye Jun 22 '18 at 08:57
0

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.

Claudio Kuenzler
  • 772
  • 7
  • 12
0

lsusb will reveal all your devices find some snippets below how I solved attching multiple devices of same vendor without knowing which device it will be assigned to (due to reboot or reattching the device)

# Discover relevant USB devices and write xml files

# How many numbers are returned per lsusb device listing
POINTS_PER_DEVICE=6
# Command to grep for USB devices from vendor Cygnal
DEVICES=$(lsusb | grep Cygnal)
# Trimming all numbers out
NUMBERS=$( echo "$DEVICES" | sed -e 's/[^0-9]/ /g' | tr -s ' ')

ARRAY=()
NUMBER=0
for num in $NUMBERS; do
   NUMBER=$((NUMBER + 1));
   #echo "number: $NUMBER value: $((10#$num))"
   ARRAY[$NUMBER]=$((10#$num));
done

NUM_DEVICES=$(( NUMBER / POINTS_PER_DEVICE ));
echo "Found $NUM_DEVICES devices matching."

for (( devices=1; devices <= $NUM_DEVICES; devices++ )); do
   INDEX=$(( devices * POINTS_PER_DEVICE - POINTS_PER_DEVICE + 1));
   echo "<hostdev mode='subsystem' type='usb'>
         <source startupPolicy='optional'>
         <address bus='${ARRAY[$INDEX]}' device='${ARRAY[$INDEX + 1]}'/>
         </source>
         </hostdev>" > $DIR/Automount_${devices}_usb.xml
   echo "Created Automount_${devices}_usb.xml file."
   # Then mount USB
   /QVS/usr/bin/virsh attach-device <your-uuid> $DIR/Automount_${devices}_usb.xml
done