Making USB Work in VirtualBox Guests



 

 


Note: The information in this document is no longer correct. recent versions of VirtualBox and updates to the UDEV subsystem have made this information obsolete. USB devices are now available to guest operating systems and can be hot-plugged at any time.

For example my Apple iPod can be plugged in after the Windows XP guest machine is booted and I have logged in.

If you still have older versions of UDEV and VirtualBox, this information may still be valid. For that reason I will not delete this document. In addition, this document provides a useful example for writing udev rules.

Making USB devices available to VirtualBox guest operating systems, regardless of the type, seems to be quite problematic. I have tried many of the solutions that I found using Google. I tried many things that were not on the Internet during my lengthy experimentation.  None of them worked for me in Fedora 11.

Note that the Open Source version of VirtualBox does not support USB devices. Only the VirtualBox Personal Use and Evaluation License (PUEL) versions provide USB support at this time.

The Key

The real key to this is setting the access permissions to 777 on the entire branch of the USB directory tree to the device being added. All other permissions, ownership combinations and group memberships have failed to achieve results in my experiments. This introduces security issues but, so far at least, is the only working option.

This could be accomplished simply using a chmod -R 777 /proc/usb command to change all of the directories and end nodes in the /proc/usb tree. But we a bit want to be more targeted.

Device Identifier

In order to provide a more targeted response to plugging in a specific device, it is necessary to know some identifying attribute. This is a little more difficult for non-storage devices, but can be obtained in the following manner. Locate the device in the /dev/bus/usb directory. This can be done by plugging it in to the USB port and using the following find command to locate the device just plugged in.

#find /dev/bus/usb -mmin -1
/dev/bus/usb/001
/dev/bus/usb/001/015

The above command finds all files in the /dev/bus/usb direcrory that have been added or updated less than one minute ago. Now that you have located the new device node in the /dev tree use the following command to obtain a list of all the device attributes. In this case the device is located at /dev/bus/usb/001/015/.

udevinfo -a -p $(udevinfo -q path -n /dev/bus/usb/001/015)

The output will look like that below, in this case I have used a printer as an example:

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

looking at device ‘/devices/pci0000:00/0000:00:1d.7/usb1/1-2′:
KERNEL==”1-2″
SUBSYSTEM==”usb”
DRIVER==”usb”
ATTR{configuration}==”"
ATTR{bNumInterfaces}==” 4″
ATTR{bConfigurationValue}==”1″
ATTR{bmAttributes}==”c0″
ATTR{bMaxPower}==”  2mA”
ATTR{urbnum}==”2966701″
ATTR{idVendor}==”03f0″
ATTR{idProduct}==”3812″
ATTR{bcdDevice}==”0100″
ATTR{bDeviceClass}==”00″
ATTR{bDeviceSubClass}==”00″
ATTR{bDeviceProtocol}==”00″
ATTR{bNumConfigurations}==”1″
ATTR{bMaxPacketSize0}==”64″
ATTR{speed}==”480″
ATTR{busnum}==”1″
ATTR{devnum}==”15″
ATTR{version}==” 2.00″
ATTR{maxchild}==”0″
ATTR{quirks}==”0×0″
ATTR{authorized}==”1″
ATTR{manufacturer}==”HP”
ATTR{product}==”Officejet Pro 8500 A909a”
ATTR{serial}==”MY95K3215P”

The complete tree of the output above has been pruned for brevity.

Choose one of the unique attributes such as manufacturer, product or vendor ID, serial number or device name and use it in the udev rules file in the next step.

Scripts for Setting up USB bus for use inside VirtualBox Guest

This rules file is triggered when the specific USB device is plugged in. It runs the /usr/local/bin/biocomusbsetup.sh shell script. In this case I was trying to allow access by a specific hardware device which is why I use the ATTRS{manufacturer}==”Biocom” filter. You can eliminate this filter and it won’t matter what type of device is plugged in; the rule would then be triggered any time any USB device at all is plugged in. That may not be what you want.

File: /etc/udev/rules.d/10-davids.rules

KERNEL==”vboxdrv”, NAME=”vboxdrv”, OWNER=”root”, GROUP=”root”, MODE=”0600″ SUBSYSTEM==”usb”, ENV{DEVTYPE}==”usb_device”, ATTRS{manufacturer}==”Biocom”, GROUP=”vboxusers”, MODE=”0777″, RUN=”/usr/local/bin/biocomusbsetup.sh”

This quick and dirty BASH script sets the permissions for the entire USB bus directory tree down to the node in the specific branch containing the device in question. A little more work with this script would make it a bit more selective in the /proc/bus/usb/ directory.

File: /usr/local/bin/biocomusbsetup.sh

#!/bin/bash
# Set up USB access so that all users can access the Biocom hardware.
# This makes it possible to use the USB device in VirtualBox
# first make the base dirs 777
chmod 777 /proc
chmod 777 /proc/bus
chmod 777 /proc/bus/usb
chmod 777 /proc/bus/usb/*
find /proc/bus/usb/0* -type f -mmin -0.5 -exec chmod -R 777 {} \;

# Adds line to log file for debugging
find /proc/bus/usb/0* -type f -mmin -0.5 -exec ls -l {} \; >> /tmp/BiocomUdev.log

This may not work for you but it does work for me. Of course you may have to make minor changes to the script and rules file to adjust to your specific circumstances.


References:

  1. Writing udev Rules:  http://www.reactivated.net/writing_udev_rules.html
  2. Linux Journal, Painless Thumb Drive Backups, http://www.linuxjournal.com/article/9311