Difference between revisions of "Linux/Virtualization/Usb"

From Iveze
Jump to: navigation, search
Line 37: Line 37:
 
== 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.
 
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.
 +
 +
=== Events ===
 +
Udev gives access to the events of plugging in and pulling out devices. It comes pre-installed with the OS. Just create a rules file that redirects all usb events to a script.
 +
 +
/etc/udev/rules.d/95-libvirt-usb.rules
 +
SUBSYSTEMS=="usb", \<br>
 +
RUN+="/var/scripts/usb-guest"
 +
All usb events now go to the script usb-quest, where we are going to decide if anything needs to be done. Udev sets several environment variables for the script, enabling us to make decisions. These are the most interesting variables and values.
 +
{| class="wikitable"
 +
|'''Variable'''
 +
|'''Values'''
 +
|'''Remark'''
 +
|-
 +
|SUBSYSTEM
 +
|usb or block
 +
|Block can be used to identify that it is an external disk
 +
|-
 +
|ACTION
 +
|add or remove
 +
|
 +
|-
 +
|ID_SERIAL
 +
|Serial number
 +
|A good match for symlinks in /dev/disk/by-id/
 +
|-
 +
|ID_VENDOR_ID
 +
|Vendor id
 +
|The same as in lsusb and the hostdev xml
 +
|-
 +
|ID_MODEL_ID
 +
|Product id
 +
|The same as in lsusb and the hostdev xml
 +
|}
  
 
[[Category:Linux]]
 
[[Category:Linux]]
 
[[Category:Hardware]]
 
[[Category:Hardware]]

Revision as of 14:08, 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.

Events

Udev gives access to the events of plugging in and pulling out devices. It comes pre-installed with the OS. Just create a rules file that redirects all usb events to a script.

/etc/udev/rules.d/95-libvirt-usb.rules

SUBSYSTEMS=="usb", \
RUN+="/var/scripts/usb-guest"

All usb events now go to the script usb-quest, where we are going to decide if anything needs to be done. Udev sets several environment variables for the script, enabling us to make decisions. These are the most interesting variables and values.

Variable Values Remark
SUBSYSTEM usb or block Block can be used to identify that it is an external disk
ACTION add or remove
ID_SERIAL Serial number A good match for symlinks in /dev/disk/by-id/
ID_VENDOR_ID Vendor id The same as in lsusb and the hostdev xml
ID_MODEL_ID Product id The same as in lsusb and the hostdev xml