Difference between revisions of "Linux/Virtualization/Usb"

From Iveze
Jump to: navigation, search
Line 36: Line 36:
  
 
== Plug and Play ==
 
== Plug and Play ==
 +
If a USB device is attached with one of the above manners, it will not be plug and play. Remove and plugin the USB device, and you may have to type the attach statement again.
  
 
[[Category:Linux]]
 
[[Category:Linux]]
 
[[Category:Hardware]]
 
[[Category:Hardware]]

Revision as of 13:23, 14 June 2015

If a USB device is plugged in a host machine, it needs to know what to do with it. Should the host itself use it, or should it be forwarded to a guest, and if so to which guest?

Basics

Before making USB plug-and-play, we need to explore the options virsh offers us.

Forward as USB device

By vendor/product

A USB device can be forwarded to a guest by vendor id and product id. This works well if there is only one device of a certain model ever plugged into the host.

The vendor id and product id can be obtained with lsusb (if not available, install usbutils). There will be several lines like this:
Bus 002 Device 004: ID 051d:0002 American Power Conversion Uninterruptible Power Supply
In this line the vendor id is 051d and the product id is 0002. From the description you can make out which line you are looking for.

Now a sub-definition of the guest's definition must be written in a file, containing the vendor and product id.

<hostdev mode="subsystem" type="usb">
<source startupPolicy="optional">
<vendor id="0x051d"/>
<product id="0x0002"/>
</source>
</hostdev>

Assuming the file is called ups.xml, this file can be used to attach or detach the usb device.

virsh attach-device guestname ups.xml

or

virsh detach-device guestname ups.xml

By USB port

One could choose to use specific USB ports for specific guests. Lsusb also shows bus and device. These numbers can be used in the address element of the hostdev. The file ups.xml would then look like this.

  <hostdev mode="subsystem" type="usb">
    <source startupPolicy="optional">
      <address bus="002" device="004"/>
    </source>
  </hostdev>

We can not guarantee this to work well, because we have not tested it. We prefer to identify USB devices by attributes of themselves, so we stay free to use any USB port.

Forward as harddisk

If the USB device is an external harddisk it can be forwarded as such. USB is not always forwarded full speed. By leaving the USB part to the host, full speed is ensured. Another advantage is that no xml file is necessary.

Assuming the usb disk on the host is /dev/sdb and the guest has only /dev/vda, so vdb is free.

virsh attach-disk guestname /dev/sdb vdb

or

virsh detach-disk guestname vdb

Plug and Play

If a USB device is attached with one of the above manners, it will not be plug and play. Remove and plugin the USB device, and you may have to type the attach statement again.