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]
  • Digg
  • StumbleUpon
  • Twitter
  • Facebook
  • MySpace
  • Google Bookmarks
  • del.icio.us
  • Reddit
  • Technorati
  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 follow any responses to this entry through the RSS 2.0 feed.


Support Us