Use Raw Partition Image in VirtualBox?

Solution 1:

Access disk image via loop device

(Found here: http://blog.mybox.ro/2010/11/03/how-to-use-a-raw-disk-image-file-in-virtualbox/)

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

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.

Solution 2:

Both of the answers here get you most of the way there, but here's what I ended up finding helpful:

  • First of all, note that a fixed-size VHD file only contains 1 sector of metadata at the end of the file, which is a lot easier to deal with than a file with metadata the beginning. For booting Linux partition images, a VHD would work just fine. There used to be a tool called VHDTool by Microsoft which could instantly append the extra sector of metadata to turn a raw image into a VHD, but it's hard to find a copy online now. There are probably other tools that could do the same, or you could make a different image of the same size and transfer the appended sector (wasteful, but gets the job done).

  • Otherwise, a VMDK file will do what you need, because it's just a text file (with LF line endings at least in my case, but maybe CRLF will work too) that can reference other files to use as the chunk. Here's the format one of my VMDK files had (read more here):

      # Disk DescriptorFile
      version=1
      CID=YYYYYYYY
      parentCID=ffffffff
      createType="partitionedDevice"
    
      # Extent description
      RW 1234 FLAT "\\.\C:\Path\To\Image.raw" 5678
    
      # The disk Data Base 
      #DDB
    
      ddb.virtualHWVersion = "4"
      ddb.adapterType="ide"
      ddb.geometry.cylinders="16383"
      ddb.geometry.heads="16"
      ddb.geometry.sectors="63"
      ddb.uuid.image="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
      ddb.uuid.parent="00000000-0000-0000-0000-000000000000"
      ddb.uuid.modification="00000000-0000-0000-0000-000000000000"
      ddb.uuid.parentmodification="00000000-0000-0000-0000-000000000000"
      ddb.geometry.biosCylinders="1024"
      ddb.geometry.biosHeads="255"
      ddb.geometry.biosSectors="63"
    

    The important bits to fill in are the following:

    • YYYYYYYY: This is the content ID. When the virtual disk is created, it's a random hexadecimal 32-bit value as far as you're concerned. For details see the VMDK specification.

    • XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX is the ID of your disk. As far as I know, it can be any random GUID, but I'm not sure.

    • 1234 is the number of sectors in the Image.raw file that you want to map to the disk

    • 5678 is the sector offset inside the Image.raw file where you want the mapping to begin

    The other bits about the disk geometry seem irrelevant as far as I can tell.

Note that you don't need any particular commands to make a VMDK, but as others have stated, VBoxManage internalcommands createrawvmdk can create one for you as well, which you may find easier or harder depending on what you're trying to do.

Also note that VMDKs can point to actual partitions or disks as well (\\.\PhysicalDriveN on Windows, etc.), but the caveat is that VirtualBox doesn't lock the volumes using those disks, so you'll get write errors and/or corruption depending on your OS and whether the volume is mounted.

Solution 3:

This may not be a complete solution to what you're looking for, but you can boot to raw physical disks - if you can mount/masquerade your partition file as a disk, I could see this working. Otherwise, if booting to a physical disk is an option, these are the steps for doing so on a Windows 7 host:

  1. Create a 'mock' VMDK file that points to the physical disk you wish to boot to in your VM (http://www.virtualbox.org/manual/ch09.html#rawdisk). Note that if you are running windows you need to run both the VirtualBox management interface and command prompt as administrator for this command to succeed.
  2. Attach the mock VMDK to a VM.
  3. Offline the disk in disk management and clear the read-only flag for it using DISKPART (https://forums.virtualbox.org/viewtopic.php?f=6&t=38914). This ensures the disk is read/write accessible for VirtualBox, but nothing else on your host.

You then should be able to use the physical drive as your guest OS. Super handy.

Solution 4:

in Linux: create mbr file:

sudo install-mbr --force WinXP.mbr

after that create a vmdk file like so:

sudo VBoxManage internalcommands createrawvmdk -filename myUSB.vmdk -rawdisk /dev/sdc  -partitions 1 -relative -mbr WinXP.mbr

/dev/sdc should be the partiton of the device/usb the raw .img was taken from. Thus we will create a .vmdk conf file with correct configuration.

open the created myUSB.vmdk. Replace /dev/sdc with the path to the img file. Remove ralative row

need to look something like this:

# Disk DescriptorFile
version=1
CID=557ec7f5
parentCID=ffffffff
createType="partitionedDevice"

# Extent description
RW 1 FLAT "myUSB-pt.vmdk" 0
RW 7812500 FLAT "new.img" 0
RW 22497899 ZERO

# The disk Data Base 
#DDB

ddb.virtualHWVersion = "4"
ddb.adapterType="ide"
ddb.geometry.cylinders="16383"
ddb.geometry.heads="16"
ddb.geometry.sectors="63"
ddb.uuid.image="76bb94c9-64f5-4602-a65a-2aca02ce814f"
ddb.uuid.parent="00000000-0000-0000-0000-000000000000"
ddb.uuid.modification="00000000-0000-0000-0000-000000000000"
ddb.uuid.parentmodification="00000000-0000-0000-0000-000000000000"
ddb.geometry.biosCylinders="1024"
ddb.geometry.biosHeads="255"
ddb.geometry.biosSectors="63"