9

Is it possible to somehow make VirtualBox directly use a raw partition image for a virtual machine?

By raw partition image, I mean a file that contains a byte-by-byte copy of the partition that I would like to boot from.

I'm not looking to boot from a raw disk image, nor am I looking for a way to boot from a VHD (which isn't raw because it contains other metadata).

11

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.

4
  • 1
    This is a decent answer but is Linux-centric. A Windows solution is probably the more common use case. Not sure if there's an equivalent technique to associate a disk, partition and volume with an image file. Otherwise, the usb.img has to be raw-written to disk, and then step 2 but with \\.\PhysicalDrive# where # is the disk number from diskmgmt.msc, but step 3 requires the USB to be first, before optical or other hdd devices/images, otherwise it won't boot from USB, unless you F12 and manually select it.
    – user587061
    Jun 7 '17 at 0:56
  • 3
    Be aware that adding a user to the disk group will allow this user and any malicious software he might be running to circumvent each and every security measure by direct access to the system's hard disk, including destruction of data, stealing of data and becoming root.
    – Bachsau
    Nov 4 '18 at 2:18
  • I think this does not answer the question, since it is again about disk images, not partition images. Jun 7 at 9:03
  • 1
    Thanks for this, super useful for doing some upgrades on a disk removed from an old laptop, that would take forever to do the laptop itself. Jul 5 at 19:41
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.

2
  • Works like a charm. For calculating the number of sectors, I just did a fake fdisk -l Image.raw which gives the sector numbes (in the standard-assumption 512 byte/ sector). The sector offset is canonically 0, if your Image.raw is a partition image, since you want to use the partition image from the very beginning if it has not been created with some specialities making it offsetted. Jun 7 at 9:54
  • This answer from 2019-12-30 shows a variant of this when the disk containing the partition to be used is still accessible, and how then automatically create the .vmdk-file. Jun 7 at 11:22
1

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.

1
  • 1
    This doesn't use an image of a partition, but rather the actual(!) partition on the disc!
    – TJJ
    Feb 7 '17 at 18:19
1

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"
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.