Mounting a USB stick with VirtualBox

I used VirtualBox to create a bootable USB key with FreeBSD using VirtualBox on my Mac.

Booting from a IMG in Virtual Box
FreeBSD is distributed as a IMG file, which is a normal format for USB disks. It is not possible to tell Virtual Box you want to boot from a USB disk. So you either have to tell Virtual Box it is a floppy, regular hard disk, or CD. While it should not matter (you have a file which contains a bootable system), in practise I only got it to work using a bootable hard disk.

The IMG files distributed by FreeBSD contain a raw UFS file system; raw meaning without overhead (metadata), compression or partition schema. This is unlike ISO distributions, which do contain a partition table (even though their content is also not compressed).

Hard disk booting
In order to tell Virtual Box that the file is a hard disk, you must first convert the IMG file to a VDI file. VDI is the native Virtual Box disk format.

VBoxManage convertfromraw -format VDI FreeBSD-9.0-RELEASE-i386-memstick.img FreeBSD-9.0-RELEASE-i386-memstick.vdi

(VBoxManage is a tool installed by VirtualBox. Usually, Virtual Box creates an alias from /usr/bin/VBoxManage to /Applications/VirtualBox.app/Contents/MacOS/VBoxManage.)

In the storage settings, select the IDE controller, and "add hard disk" using the buttons at the bottom of the screen. "Choose existing disk" and select the vdi disk image.

Either make the bootable disk the "IDE Primary Master", or immediately after booting press F12 and select the boot disk. Otherwise, Virtual Box will try to boot from the (still empty) default disk, and you get the error "FATAL: No bootable medium found! System halted."

Floppy booting
In the storage settings, there is by default only a IDE controller. You first need to "Add Floppy Controller" with on of the small buttons at the bottom of the screen. Now use the disk icon to add a new floppy. You will be asked: "You are about to add a new floppy drive to controller Floppy Controller. Would you ike to choose a virtual floppy to put in the drive or to leave it empty for now?" Select "Choose disk" and select the appropriate IMG file.

If you get an error "Could not get the storage format of the medium 'FreeBSD-9.0-RELEASE-memstick.img' (VERR_NOT_SUPPORTED)", Virtual Box can't read the IMG file. In that case, choose one of the other methods (hard disk booting or CD booting).

It seems that the IMG files distributed by FreeBSD are not suitable (perhaps Virtual Box expects an image with a partition table? I tried converting with hdiutils convert but to no avail.)

CD booting
In order to tell Virtual Box that the file is a CD, you must first convert the IMG file to a ISO file. ISO 9660 is a common CD disk format.

To convert the raw disk image to ISO, run:

hdiutil convert -format UDTO -o FreeBSD-9.0-RELEASE-i386-memstick.cdr FreeBSD-9.0-RELEASE-i386-memstick.img

(Similarly, use hdiutil with -format UDRW to convert from IMG or ISO to DMG (actually: UDIF read/write image) which can be used with dd. Use -format UDZO  for zlib compressed DMG files. For IMG there seems less conventions, but you may want to try -format RdWr to convert to a NDIF read/write image).

Unfortunately, this does not seem to create a partition. If I try to boot from this newly created image, Virtual Box gives an error "FATAL: No bootable medium found! System halted.". hdutils imageinfo lists "partition-scheme: none", while it should have been "partition-scheme: ISO9660</tt>".

I expected the following command to create the partition: hdiutil makehybrid -iso -joliet -o FreeBSD-9.0-RELEASE-i386-memstick.iso FreeBSD-9.0-RELEASE-i386-memstick.cdr

Unfortunately, this gives an error hdiutil: makehybrid: attach of disk image failed: 111</tt>. So it seems I'm at loss about the details of these file formats and how to deal with them. I gave up (Suggestion as how to fix this are most welcome!)

Issues
Here are a few issues I ran into.

USB2 is not supported by VirtualBox
VirtualBox does not support USB2 by default. If you try to mount a USB device, you will get a warning that it requires 'Oracle VM VirtualBox Extension Pack'. Download from the VirtualBox website and install extension pack by simply dragging it to the VirtualBox application.

Failed to attach the USB device
Problem: SanDisk Ultra Fit [0100] to the virtual machine FreeBSD install test. Failed to create a proxy device for the USB device. (Error: VERR_PDM_NO_USB_PORTS). Result Code: NS_ERROR_FAILURE (0x80004005) Component: ConsoleWrap Interface: IConsole {872da645-4a9b-1727-bee2-5585105b9eed}

Solution: In the USB settings of the VM, changed from the USB 2.0 (EHCI) controller to the USB 2.0 (xHCI) controller.

Determine the mount location of the disk
The mount location of the USB disk is determined by your operating system and presently available hardware. The names typically take these forms:


 * on FreeBSD
 * on Mac OS X
 * on Linux

If the USB key is already mounted, these tools list the mounted disks:


 * on Linux and FreeBSD
 * on Mac OS X

Mac does not fully unmount USB stick
Problem: Ejecting a disk in the Finder only ejects the partition, but does not unmount the whole disk.

Solution: determine the disk, and unmount using diskutil: diskutil list diskutil unmountDisk /dev/disk2

No USB devices found
Deattach USB disk from host computer, and attach to to Virtual Box.


 * Problem:Installer shows up, but I can't select a USB disk. Even after rescanning, and checking the fixit menu, it gaves an error "No USB devices found".


 * Solution: It seems that VirtualBox has some limitations when it comes to accessing USB devices with the guest OS. I wanted a bootable USB disk with some modifications. In the end, I simply copied the default memstick distribution to a USB disk using dd (not using VirtualBox), than booted a installed FreeBSD on virtual disk (instead of USB disk) in Virtual Box, and mounted the USB disk in that guest OS to make the desired changes. (I probably could have used a "Live CD" in Virtual Box as well)

USB_ERR_TIMEOUT
Booted FreeBSD in VirtualBox, and attached USB disk. Error: uhub_reattach_port: port 1 reset failed, error=USB_ERR_TIMEOUT uhub_reattach_port: device problem (USB_ERR_TIMEOUT), disabling port 1

Seems a bug in VirtualBox. See issue #8182

In VirtualBox settings (under ports > USB) Disable USB 2.0 (EHCI) Controller.

Reboot the Virtual box.

Now the USB key works fine in FreeBSD, and is made available at ..