Difference between revisions of "Linux/Virtualization/Usb"

From Iveze
Jump to: navigation, search
Line 1: Line 1:
 
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?
 
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?
  
=== Forward USB ===
+
=== Forward as USB ===
 
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.
 
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.
  
Line 12: Line 12:
 
or
 
or
 
  virsh detach-device guestname ups.xml
 
  virsh detach-device guestname ups.xml
 +
 +
=== 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
 
[[Category:Linux]]
 
[[Category:Linux]]
 
[[Category:Hardware]]
 
[[Category:Hardware]]

Revision as of 16:14, 13 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?

Forward as USB

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

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