Boot VirtualBox machine from a USB flash drive

VirtualBox
Image via Wikipedia

While preparing my new workstation I stumbled upon a problem on how to boot VirtualBox machine from a USB fash drive. Pretty soon I discovered that VirtualBox has no support for booting from USB devices. Something with their BIOS not supporting it and that implementing this wouldn’t bring a lot to the whole package1.

Read on if you want to know more.

I am using Ubuntu 9.0.4 and I installed VirtualBox from Ubuntu repositories. Accessing physical devices in Linux is restricted to root user only, so you’ll have to run your VirtualBox as root2, which is rather inconvenient, however this is the only way I got things to work.

The trick is to tell VirtualBox that your USB drive is just an ordinary IDE drive.

sudo -s
virtualbox &
mkdir /root/.VirtualBox/HardDisks
VBoxManage internalcommands createrawvmdk \
   -filename /root/.VirtualBox/HardDisks/usbdisk.vmdk \
   -rawdisk /dev/sdi -register

First you switch to root shell and then run VirtualBox in the background. If you did this before and you have virtual machines already running as root then you can probably skip creating HardDisks directory. With VBoxManage you create vmdk file that is connected to /dev/sdi device and register it with internal registry of hard disks.

Two important things to note: First, path to image file must be absolute starting with / and second, raw disk device must be your actual device. It might not be called /dev/sdi, but something completely different. Plug in your USB drive and then run dmesg in console. Output will be similar to this:

... snip snip ...
[181685.693389] usb-storage: device found at 5
[181685.693392] usb-storage: waiting for device to settle before scanning
[181690.692200] usb-storage: device scan complete
[181690.692918] scsi 12:0:0:0: Direct-Access                               0.00 PQ: 0 ANSI: 2
[181690.694539] sd 12:0:0:0: [sdi] 7897088 512-byte hardware sectors: (4.04 GB/3.76 GiB)
[181690.695037] sd 12:0:0:0: [sdi] Write Protect is off
[181690.695040] sd 12:0:0:0: [sdi] Mode Sense: 00 00 00 00
[181690.695043] sd 12:0:0:0: [sdi] Assuming drive cache: write through
[181690.702414] sd 12:0:0:0: [sdi] 7897088 512-byte hardware sectors: (4.04 GB/3.76 GiB)
[181690.702910] sd 12:0:0:0: [sdi] Write Protect is off
[181690.702913] sd 12:0:0:0: [sdi] Mode Sense: 00 00 00 00
[181690.702916] sd 12:0:0:0: [sdi] Assuming drive cache: write through
[181690.702922]  sdi: sdi1
[181690.830974] sd 12:0:0:0: [sdi] Attached SCSI removable disk

This is your newly plugged USB drive. If you are not sure about it, you can open file manager and mount your drive then check the output of mount command to see which device was just mounted.

After VBoxManage has completed check if the file was created.

ls -l /root/.VirtualBox/HardDisks
total 4
-rw------- 1 root root 624 2009-09-08 12:50 usbdisk.vmdk

If all seems to be in order and you got output similar to this, then you can go create a virtual machine using your USB drive. When you get to the disk image creation you’ll be able to select usbdisk.vmdk.

Addding physical drive

Before you start the virtual machine make sure you add one more disk if you plan to install something from your USB drive. This way you can also add other physical storage devices to Virtual Box.

Reblog this post [with Zemanta]


Footnotes:
  1. I gathered this from few forum posts on their website, things today might be different. []
  2. This could probably be done with udev configuration, giving a group of users read/write access to disk devices, but I didn’t bother with it. []


  • [...] (you can also try VirtualBox) [...]

  • On June 7, 2010 at 23:18 Sam Hodgkinson said:

    Thanks for this ,

    it worked great for me in virtual box unbuntu 10.4 .

  • Nice to hear that this solution still works. :)

  • Tested on Linux Mint 9 still work’s great!

    Thank you for shearing :)

  • Hi, thanks…

    work for me on Debian and VirtualBox 3.2.6

    found next things…
    do not need to run VirtualBox as root, need only create usbdisk.vmdk file as root and chmod it…

  • I’m trying to get VirtualBox to boot from an external hard drive. I’m running Ubuntu 10.10. I followed all of your instructions and the new vdi, located on the external, is attached to the guest vm in VirtualBox, but when I try to boot it up it says “Missing operating system.”

    Any idea what might be wrong?

  • [...] Three Wise Men http://www.twm-kd.com/software/boot-virtualbox-machine-from-a-usb-flash-drive/ Category: [...]

  • On May 21, 2011 at 21:14 Thomas said:

    If you don’t want to do this as root:

    # sudo adduser yourusername disk

    That way you get raw disk access for that user. That’s not too good either, but better than running as root.

  • On June 8, 2011 at 13:39 Gohst said:

    I am getting an error when I try to do this. Here is the error “The medium usb.vmdk can’t be used as the requested device type” RESULT_CODE NS_ERROR_FAILURE 0×80004005
    I am running Ubuntu 10.10 VirtualBox 4.0.8 and have my user in the disk group

    • On June 8, 2011 at 13:43 Gohst said:

      Ok tried again running VirtualBox as root, and it works as expected. Is there anyway to make it work without running VirtualBox as root?

  • Tried this udev rules, works for me. Create a rule file under /etc/udev (this is Fedora) called 99-vbox-usb.rules with the following two lines:

    KERNEL==”sd[c-f]“, OWNER=”rich”,GROUP=”rich”,MODE=”660″
    KERNEL==”sd[c-f][0-9a-f]“, OWNER=”rich”,GROUP=”rich”,MODE=”660″

    sda and sdb are my hard drives, so my usb devices start at sdc. Your device names may be different. Anyway, this works for me, allowing my userid (rich) read/write access the usb device without having to run VirtualBox as root or to give global access to the device file.

  • Well I know others have followed this how to but I came across the command “-register”. The man page data that described the error indicated that “-relative” is the correct command. I tried it and the process continued as indicated above. I’m having trouble getting my usb key to act like a disk drive and boot. Perhaps b/c its a “live usb”? ger

Leave a comment