VMmanager Knowledge Base

How to connect a USB device to a VM?

For the virtual machine (VM) to work with external USB devices, configure port redirection on the cluster node:

  1. Connect the USB device to the cluster node where the VM is located.
  2. Connect to the cluster node via SSH.
  3. Define the full name of the VM (Name column):

    virsh list --all
    Example output
      Id    Name         State
    ---------------------------
     145   115_name1    running
     159   2218_name2   running
     223   1736_name3   running
     523   2117_name4   running
  4. Determine the type of controllers added to the VM: get the xml description of the VM and filter by USB controllers:

    virsh dumpxml <vm_name> | grep -i -E 'uhci|ehci|xhci'
    Example output
    <controller type='usb' index='0' model='piix3-uhci'>
    <controller type='usb' index='0' model='ehci'>
    <controller type='usb' index='1' model='nec-xhci'>
    Comments
  5. Get the bus_id and device_id of your USB device by getting a list of connected USB devices:

    lsusb -v
    Example output
    Bus <bus_ID> Device <device_ID>: ID 0cd2:1402 Netac Technology Co., Ltd OnlyDisk
  6. Create a file /root/usb_device.xml with the following contents:

    <hostdev mode='subsystem' type='usb'>
    <source>
    <address bus='<bus_ID>' device='<device_ID>'/>
    </source>
    <address type='usb' bus='1' port='0'/>
    </hostdev>
    Comments
  7. Enable port forwarding: 

    virsh attach-device <vm_name> --file /root/usb_device.xml --config --persistent
    Comments