Recently I was setting up a Xen guest which needed access to a USB port on a CentOS 5.1 host. I searched the Internet for instructions on how to use USB forwarding or USB pass-through but I was not able to find anything which would work on the Xen 3.0.3 I’m using. I decided to set up PCI pass-through instead.
To set it up, I followed instructions written by Jón Fairbairn at xensource.com. The pciback module in the CentOS 5.1 kernel doesn’t support the hide parameter so I left that out from my setup. It doesn’t seem to be necessary, anyway, for the driver for the USB controller in the host can be unbound from the PCI slot used by the controller.
To make sure the PCI slot is exported to Xen guests when the host system boots I created a xenpciexport script and placed it in the /etc/init.d/ directory. You can download the code from here xenpciexport.txt
The script xenpciexport is set up to start before xend and xendomains to make sure the PCI forwarding is working before any guest is running. It stops after xend and xendomains. That’s controlled by the line:
# chkconfig: 2345 97 02
you can see in the script header.
After copying the file to /etc/init.d you need to run
chkconfig --add xenpciexport
to create the appropriate links in the /etc/rc*.d/ directories.
To find out the slot number for the USB controller use lspci and lsusb commands. If the script is set up correctly you can plug in the USB device and run
service xenpciexport stop
After that the results of lsusb should include the USB device you want to export. Then run
service xenpciexport start
and now lsusb won’t show this device anymore – it’s hidden from the host and visible to Xen guests.
Before you start the Xen guest which is supposed to have access to the exported device add this line to its configuration file:
pci = [ '0000:00:1d.1' ]
The number in quotes must be the same as the SLOT variable in the xenpciexport script.
After that you need to patch your Xen installation. The version installed in CentOS 5.1 contains a bug which makes it impossible to use PCI pass-through. The fix can be found at CentOS Bug Tracker. Make sure you also fix the file pciif.py as specified in the note. In total there are three files which need to be edited: iopif.py, irqif.py and pciif.py.
Now you can finally start the Xen guest. When it finishes booting log into it and run lsusb there to confirm that the device is visible to the guest. lspci in the guest should also show you the USB controller. It is important to run the xenpciexport script before you start your guest, otherwise the USB device will not be visible to the guest OS.
The Xen guest has now full access to the USB device.
Here you can read more about PCI passthrough on Xen.
Update:
It turns out that this setup was causing the Xen guest to crash. You can read more in a new post.
Image may be NSFW.
Clik here to view.
