Difference between revisions of "Linux/Virtualization/Usb"
|Line 36:||Line 36:|
== Plug and Play ==
== Plug and Play ==
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?
Before making USB plug-and-play, we need to explore the options virsh offers us.
Forward as USB device
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 SupplyIn 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">
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
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
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.