I sometimes need to boot a disk image (usually, a USB stick image) to check it out – for example, I want to check that my method of booting multiple CDs from a USB stick works ok. Rebooting my computer every time I do changes is annoying, so I decided to use VirtualBox to boot the “USB stick”. I also didn’t want to use the real device, because of speed considerations.
Reading the VirtualBox manual and googling around I found out that one can give VirtualBox access to a physical hard disk or raw partition. Also, VirtualBox has some tools to convert its own disk image format to and from raw disk images. So I identified two methods of doing what I wanted: to use a raw disk image in VirtualBox.
Method 1: Access disk image via loop device.
We have a disk image, we just need to convince VirtualBox to access it as a raw block device. This can be done with losetup.
Step 1: Associate the disk image with a loop device.
$ losetup /dev/loop0 /path/to/usb.img
Step 2: Create a virtual disk and register it with VirtualBox.
$ VBoxManage internalcommands createrawvmdk -filename /path/to/usb.vmdk -rawdisk /dev/loop0 -register
Step 3: Attach the virtual disk to a virtual machine and start it. The virtual machine will access the virtual disk, which now is linked to /dev/loop0, which in turn is linked to the disk image file.
Warning: In order for this to work, VirtualBox needs to be able to access the loop device you created. This means either adding your user to a group that has access to disks (on my Ubuntu machine, this is group “disk”), or you need to run VirtualBox as root. Since I’m messing with disks a lot, I used the first option (that’s why my commands start with $ not #), but I think running just VirtualBox as root is the safer/better solution for most users.
Method 2: Convert the disk image to a format VirtualBox understands.
If you don’t want to or can’t use the loop device method, you can run some commands to convert the image file to a format that VirtualBox understands. I didn’t test, but disk access should be faster with this method, because it has less layers and I suppose the disk formats are optimized to maximize access speed.
Step 1: Convert the raw image file to VirtualBox’s disk formats:
$ VBoxManage convertfromraw /path/to/usb.img /path/to/usb.vdi --format vdi
You can also use vmdk or vdh as valid values to the –format parameter. I don’t know which one is the best, but vdi is the default in my VirtualBox setup so I’ll stick to that.
Step 2: Add the vdi image to your virtual machine and start it.
The obvious disadvantages to this method is that all the data in the image file will be copied to the virtual disk image. This takes some time and you will need, at least temporarily, twice the disk space.
Step 3 (optional): Convert the virtual disk image back to raw format. In case you do changes to the virtual disk and you want to have it in a raw image so you can write it back to a real device, there is a corresponding command:
$ VBoxManage internalcommands converttoraw /path/to/usb.vdi /path/to/usb.img
There you have it! Two methods of using raw disk images in VirtualBox. I tested both with the downloadable USB image of DragonFly BSD and with a SysRescueCD image created from a physical USB drive. Both methods worked without a glitch, so enjoy and let me know if it works for you, in the comments section!
Image credit: Mousetail123.