USB device in an LXC container with libvirt

Passing USB to an unprivileged container in libvirt quite straightforward, but somewhat difficult to find documentation for it. It is done with a <hostdev> tag in the domain XML file of the container. Use lsusb to find out the vendor and product IDs of the device you want to pass through to the container and insert them in the definition of the container. On the next start the container will have the device in /dev/bus/….

<domain type='lxc'>
  …
  <devices>
    …
    <hostdev mode='subsystem' type='usb' managed='yes'>
      <source>
        <vendor id='0x1234'/>
        <product id='0xaffe'/>
      </source>
    </hostdev>
  </devices>
</domain>

The device in the container then may need changing of the user privileges, this can be done the usual way using udev rules..