Use USB installation media with a Virtualbox VM

So.
You want to create a Windows 7 virtual machine, on virtualbox, on your Mac.
Don’t ask why. The answer might include Visual Studio.
You have a sysprep’d Windows 7 USB drive full of installation extras, custom wim files… But the virtualbox GUI gives you no “boot from USB” option.

The workaround: Create a “stub” virtual disk file somewhere Virtualbox can read it that references the USB drive as a “raw” disk.

First, see what OSX thinks USB drive is called:
:~ username$ diskutil list
/dev/disk0
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *1000.1 GB disk0
1: EFI 209.7 MB disk0s1
2: Apple_HFS osx 999.7 GB disk0s2
/dev/disk1
#: TYPE NAME SIZE IDENTIFIER
0: NO NAME *26.0 MB disk1
/dev/disk2
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *8.1 GB disk2

/dev/disk2

then create the vmdk:

VBoxManage internalcommands createrawvmdk -filename ~/test_usb_stub_disk2.vmdk -rawdisk /dev/disk2

Now attach the new stub disk to the same disk controller as the virtual machine’s main hard drive, using the next¬†available SATA port (SATA Port 1 assuming the main drive is on SATA port 0 – if you don’t do this, removing your flash drive will make your VM unbootable and annoying to fix):

Screen shot 2013-07-10 at 18.37.49

…then start the virtual machine, clicking its pre-boot window immediately and using Function+F12 to open the boot menu which will give show you the virtual USB drive.

You should see the usual Windows installation prompts…

“Next >>”.