How to use a raw disk image file in VirtualBox

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.

Share

7 comments

  1. lopezavila69 says:

    Hi,

    I have tried both method using a 10 GB img file (ext4 format) which contains a Linux From Scratch 6.8 fully built.

    I have even installed GRUB. However, when I try to boot the machine I find this message:

    “Fatal: Could not read from the boot medium! System halted”

    What can I do? Maybe the img file should have an ext2/ext3 format?

  2. Ovidiu says:

    This seems like a VirtualBox error, not an operating system error. If you are using VirtualBox, are you sure you run it as a user who can access the file?

  3. [...] from a single partitioned USB stick. Also, while creating the USB images, I preffer to test the raw disk image files in VirtualBox – it’s way faster and I don’t need to reboot my machine. There are some caveats [...]

  4. anonymous says:

    Current (4.1.2, possibly older as well) VirtualBox can deal with raw HDD image files: just rename the file to .hdd and open it. Although I don’t know if it can do snapshots on raw images…

  5. chepe263 says:

    When converting from VDI to RAW is it posible to write the image to a device (USB Device) instead to file?

Leave a Reply

Your email address will not be published. Required fields are marked *


− 4 = three

CommentLuv badge