July 16, 2012

VirtualBox USB from the command line

Credit: http://richardhorwood.com/108/virtualbox-usb-from-the-command-line/

How to add a USB device using vboxmanage


  1. Ensure you actually have USB support for your target VM:
    # VBoxManage showvminfo "somevm" | grep USB
    USB:             enabled
  2. If it’s not set to “enabled” you’ll have to add USB support to your VM.  You’ll need to power off the VM to do this:

    # VBoxManage modifyvm "somevm" --usb on --usbehci on
  3. To attach a device that’s plugged into the same system as your VM (in my case, a Sony USB memory stick), grab its UID as follows:
    # VBoxManage list usbhost
    Sun VirtualBox Command Line Management Interface Version 3.1.4
    (C) 2005-2010 Sun Microsystems, Inc.
    All rights reserved.
    
    Host USB Devices:
    [...]
    UUID:               2a2c7255-3b90-448e-aa7a-b1c5710ddd79
    VendorId:           0x054c (054C)
    ProductId:          0x0243 (0243)
    Revision:           1.0 (0100)
    Manufacturer:       Sony
    Product:            Storage Media
    SerialNumber:       6A08102832911
    Address:            0x54c:0x243:256:/pci@0,0/pci108e,5347@2,1
    Current State:      Busy
    
  4. Create a usb filter which will tell VirtualBox to provide the USB device to your virtual machine when it’s detected as plugged in on the host:
    # VBoxManage usbfilter add 0 --target "somevm" --name usbstick \
                   --vendorid 054C --productid 0243
  5. Go ahead and power on your Virtual Machine.  You’ll notice that the USB device (if it’s currently plugged in) immediately becomes unavailable on the host.  You can confirm that it’s attached and that you didn’t make a typo with the vendor and/or product IDs:
    # VBoxManage showvminfo "somevm"
    [...]
    Currently Attached USB Devices:
    
    UUID:               582313d4-1d51-41ea-a053-ba5ac552d2e5
    VendorId:           0x054c (054C)
    ProductId:          0x0243 (0243)
    Revision:           1.0 (0100)
    Manufacturer:       Sony
    Product:            Storage Media
    SerialNumber:       6A08102832911
    Address:            0x54c:0x243:256:/pci@0,0/pci108e,5347@2,1
That’s it.  You can mount and unmount this device now inside your VM.

1 comment:

  1. from squirrel:

    Hi! Great article, thank you. Old, but this mostly all still worked. One thing that was missing, which it looks like you started to do, was the actual "attachusb" command with the UUID you get from the "list usbhost" command. Like so:

    VBoxManage controlvm "somevm" usbattach UUIDHERE

    Once I did that the drive showed up in my guest.
    Thanks again!

    ReplyDelete