Creating a bootable USB flash drive in VirtualBox

Its always a good idea to have a bootable recovery system laying around. You never know when you will need to use it. It might come in handy when retrieving a forgotten password or recovering from a butchered upgrade, deleted user /home folders, etc. Having a Linux live CD is not practical since most modern laptops and netbooks do not come with optical drives. In addition a bootable USB drive can be kept upgraded and customised by installing new software, cant do that with live CD. Following overview describes the process of creating a fully functional operating system that resides on a USB drive and can be booted on any hardware capable of being started from USB. The last section describes the process of booting the USB drive as a client in a new VirtualBox machine without the need to restart the hardware.

Preparation. It is assumed that you have the latest Virtualbox with Oracle Virtualbox extension pack installed. You also need to have a spare USB flash drive. Make sure to format the disk to Ext4 file-system, with the "boot" flag turned on. Also install grub on the disk using the following set of instructions. If your USB drive does not have Grub installed to its MBR, the LiveCD installer might fail.

sudo mkdir /media/USB/boot #Create boot folder on USB drive
sudo cp -r /boot/grub /media/USB/boot/ #Copy grub files from local hard-drive to USB
sudo grub #Start the grub program
find /boot/grub/stage1 #List all disks with grub folders
geometry (hd1) #Look for device that corresponds to your USB /dev/sdX
root (hd1,0) #Set root partition to the first partition on USB drive
setup (hd1) #Write grub to the master boot record of the USB drive
quit #Exit the grub program

VirtualBox Setup. Open VirtualBox as a regular user and create a new machine. Set it to boot from a Linux Live CD ISO image. Enable a bridged network adapter to allow LiveCD access the Internet. Finally enable a USB controller and add a new USB filter for your USB flash storage device. Start the machine.

Installation. This step is very straightforward but takes the most time. After VB Linux client boots, proceed to install from the Live CD, and set the USB flash drive as a target. Let the installer overwrite everything on the disk, including Grub that we have set up previously.

Booting. Now you can try booting your computer from the USB flash drive that we just prepared. If the device is not bootable, then your grub installer failed and you need to reinstall grub manually using the above instructions starting from sudo grub line. Once you have a functional USB installation, you have an option to boot your new USB drive inside VirtualBox. This way you do not have to reboot your main system to make sure the USB disk works. VirtualBox is not limited to booting virtual disks. The name can be deceiving, but the fact is that VirtualBox can also boot real tangible hardware. Follow these steps to boot your USB flash drive. Do these as root:

df -h #Verify the /dev/sdXn disk partition that has your new installation
VBoxManage internalcommands createrawvmdk -filename usb.vmdk -rawdisk /dev/sdX
#Create a virtual VMDK disk linked to a tangible hardware device in the current folder
sudo virtualbox #Start VirtualBox as root to be able to boot hardware devices

Now that you have a root VirtualBox instance, create a new machine and supply it with the existing usb.vmdk drive we just created. Give it a bridged network adapter to enable Internet access. Starting our virtual machine will boot the OS installed on USB flash drive inside VirtualBox. This setup gives us more flexibility for testing and upgrading USB installations without the need to reboot the hardware.

Back to main index