Cutton Eye
Cutton Eye

Reputation: 3569

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.

Upvotes: 2

Views: 7063

Answers (3)

tomwinkler
tomwinkler

Reputation: 1

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

Upvotes: 0

Claudio Kuenzler
Claudio Kuenzler

Reputation: 902

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.

Upvotes: 1

DanielB
DanielB

Reputation: 2836

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.

Upvotes: 4

Related Questions