Skip to main content

Usb Stick CF Card Booting With VirtualBox

·4 mins

Yesterday I was attempting to upgrade my FreeNAS server to the latest version.  The trick is that I built a custom box that boots from a CF card so I could leave all the drive bays free for real disk drives, and it doesn’t have a CD-ROM drive on it either.  The only option here was to use a virtual machine emulator to boot the install CD and have it install the new version onto a CF card via USB adapter.

The first thing I did was download the install ISO, boot using VirtualBox, and install to the CF card plugged into a USB port on my Mac desktop.  Tricking VirtualBox into hijacking the USB device is an easy parlor trick explained in the VirtualBox manual and other places on the webs: basically, you plug in the drive, then add a filter to map that device into the VM using the menu on the Devices dialog of the VM itself (not the main VirtualBox window).

When I plugged in the CF card back into the NAS box but it just sat there. Not having a handy dandy monitor either, I was stumped… until I figured “why not just boot the USB CF card adapter in VirtualBox?”

Here is how I did it.

First, plug in the USB card reader with the CF card inserted.  MacOS will warn you it is an unformatted drive.  Click the “ignore” option (if it is a fresh CF card that is recognized, when you open Disk Utility, click the “unmount” button, not the “eject” button).  Now open Disk Utility, select the drive and view the info for that volume.  Find the “Disk Identifier” for that drive, something like disk2.  This is basically the same way you find the device for using to do the “dd” image copy of embedded usb bootable systems.

Once the image is written to the USB device, make a VirtualBox virtual drive to reference it.  This doesn’t copy the image, but arranges for the raw device to be usable to VirtualBox:

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

Remember to use the diskN device you discovered above.  This will register a virtual drive right into VirtualBox.  Now simply enter the configuration settings for your VM and go into the storage settings, and add a disk to the disk controller.  Select this virtual device and remove any other devices that may boot.

When you turn on the VM it will now boot from the USB stick attached to your host computer and you can pre-configure it as you wish before installing it on your actual embedded device.

What I discovered with my FreeNAS installation was that something botched the disk write and the boot loader was unable to find the boot volume.  Simply re-flashing directly on the Mac using the raw image file from the FreeNAS web site let me get it to work.  I think something with the VirtualBox USB connection was breaking it.

As a final trick of awesomeness, I cloned the MAC address of the file server onto the VirtualBox VM and treated it as the real thing (this allowed it to get the right IP address via DHCP server), uploaded my saved config file, and did a shutdown. Then when I plugged it into the real server it fired up straight away and was ready to run.

Finally, to clean up, I went into the virtual media manager in VirtualBox, deleted this new temporary vmdk file, and deleted the temporary VM I created for this purpose. None of it is needed anymore.

I plan to update my pfSense embedded installation this way as well.  Newer versions allow self-upgrade, but to get to that version I have to do a full install one last time. This is a simple way to prepare the CF card with the existing configuration to minimize the downtime.