(Solved) Automount of USB in Guest

Discussions related to using VirtualBox on Linux hosts.

(Solved) Automount of USB in Guest

Postby ksarkies » 25. Feb 2011, 13:31

Hi All

I started this as a question but seem to have solved it. I'll post the solution nevertheless as it may be useful, and someone may have some helpful comments or corrections to add. I'm using VB 4.0.4 on Ubuntu 10.10 with extpack installed. I'm interested in having USB sticks generally automount in a Windows guest so that a hardened non-geek Windows user can feel at home. With a general USB filter defined the USB devices can only be manually mounted from within the running guest. With a specific filter defined they do automount, but a filter would need to be added for each device likely to be used.

I've found a couple of past posts over 2 years old that offer udev scripts to do this. This previous post is the more elegant but this is also claimed to work.

The trouble with these is that they rely on the existence of /proc/bus/usb which was deprecated and has now disappeared in modern distros, notably Ubuntu. The USB attach command used in these scripts:

VBoxManage controlvm <vm-name> usbattach <UUID|address>

used an "address" that came from/proc/bus/usb. The command:

VBoxManage list usbhost

gives information about USB devices on the host, for example for a device not captured by the guest and mounted or unmounted on the host showed:

Code: Select all   Expand viewCollapse view
UUID:               453ded46-1c13-4f80-abf4-9de1cc2bb7d4
VendorId:           0x058f (058F)
ProductId:          0x6387 (6387)
Revision:           1.0 (0100)
Manufacturer:       JetFlash
Product:            Mass Storage Device
SerialNumber:       OM8GXQQ9
Address:            sysfs:/sys/devices/pci0000:00/0000:00:04.1/usb2/2-5//device:/dev/vboxusb/002/004
Current State:      Busy


The required address is actually given by this command, so we can massage the output of this to automount the device. The last two numbers in the address are in fact the bus/device numbers. Adapting the code by brandonheat from the earlier post, add the new rule into /etc/udev/rules.d into a file, for example vbox-automount-usd.rules

Code: Select all   Expand viewCollapse view
DRIVERS=="usb-storage", IMPORT{program}="usb_id --num-info --export %p", RUN+="/etc/udev/vbox-automount-usb.sh"


(run "reload udev" after creating or changing this). In the script /etc/udev/vbox-automount-usb.sh, which should be executable of course, add the adapted code:

Code: Select all   Expand viewCollapse view
#!/bin/bash

attach_storage()
{
  set `/usr/sbin/lsusb -d ${ID_VENDOR}:${ID_MODEL}| sed 's/:.*//g'`
  while [ ! -z "$1" ]; do
    case $1 in
      Bus) shift
      busdevice="$1"
      ;;
      Device) shift
      busdevice=${busdevice}"/$1"
      ;;
    esac
    shift
  done
  if [ ! -z "$busdevice" ]; then
    address=$(VBoxManage list usbhost | grep "Address:" | grep $busdevice | sed -e 's/Address://' -e 's/^[ \t]*//')
    if [ ! -z "$address" ]; then
      su - vbox_user -c "VBoxManage controlvm vm_name usbattach $address"
    fi
  fi
}

case $DEVNAME in
  /dev/sd[a-z])
  attach_storage;
  ;;
esac


This finds the bus/device combination of the newly inserted device, as in the earlier post, then matches it with the string on the end of the address given by the "VBoxManage list usbhost" command. The address can then be used to attach the device to the running guest. In the attach command, replace the vbox_user with the user running the guest and replace the vm_name with the relevant guest name (which may be derived from the command "VBoxManage list runningvms" if there is only one guest running).

This also worked with two devices inserted consecutively, although on the second insertion an error message appeared about being unable to attach the device, but it went ahead and attached it anyway. This needs a bit more investigation.

cheers, Ken
ksarkies
 
Posts: 8
Joined: 25. Feb 2011, 02:29
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: WinXP Win7

Re: (Solved) Automount of USB in Guest

Postby ksarkies » 26. Feb 2011, 01:11

Just a followup. The recognition of USB devices seems to be variable. Sometimes they are recognized by VB and other times they are shown greyed out. This is not the case if logged in as root (and the vb_user changed to root in the script), so there are some permissions issues. There are some other posts on this but I don't think the problem is yet corrected.
ksarkies
 
Posts: 8
Joined: 25. Feb 2011, 02:29
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: WinXP Win7

Re: (Solved) Automount of USB in Guest

Postby axlord » 10. Sep 2012, 15:52

have you gotten this to work in ubuntu 12.04 ?
axlord
 
Posts: 1
Joined: 10. Sep 2012, 15:52


Return to VirtualBox on Linux Hosts

Who is online

Users browsing this forum: No registered users and 14 guests