9

I have two USB Redirector devices in my KVM, created using virt-manager:

<redirdev bus="usb" type="spicevmc">
  <alias name="redir0"/>
  <address type="usb" bus="0" port="2"/>
</redirdev>
<redirdev bus="usb" type="spicevmc">
  <alias name="redir1"/>
  <address type="usb" bus="0" port="5"/>
</redirdev>

I can use these to redirect a USB device at runtime, in virt-manager by going to Virtual Machine > Redirect USB device, I can then pick which host devices to pass through the Spice channel: virt-manager choose usb

How do I do the same with virsh, or some other terminal tool? I would like to be able to switch it off and on, basically to move a device from one VM to another.

1
  • So I guess this won't be possible with virsh because it's running on Spice and it's spice client responsibility to set this up. So depending on the client used (e.g. Boxes, virt-manager, virt-viewer), it will be managing the USB redirection using channel added to the VM. Commented Aug 26, 2022 at 16:35

1 Answer 1

7

Turns out, you can do it.

The way it works for me: identify vendor and product IDs. You can see those in the "Select USB devices for redirection" or using lsusb. For me, for SteelSeries Arctis 7, vendor is 1038 and product is 12ad.

Create an XML file with the following contents, in my example I saved it as usb.xml:

   <hostdev mode='subsystem' type='usb' managed='yes'>
      <source>
        <vendor id='0x1038'/>
        <product id='0x12ad'/>
      </source>
    </hostdev>

Now attach it using virsh by using:

$ virsh attach-device <VMName> --file usb.xml --current

--current makes it hotplug the device while the VM is running. Unfortunately it doesn't show up in the Redirect window as connected, because it's not using a Spice channel, but it works nonetheless.

Detach with detach-device.

1
  • For clarity, this attaches the USB device to the VM, it doesn't redirect it as your question originally asked. The end-effect is similar of course, but USB redirection allows the user to control it, whereas this requires the credentials of the user the VM is running under (which may be different). Commented Jan 11, 2024 at 23:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.