Partition Virtual Machine – Convert Partition Image to Disk Image with Partition Table

hard-diskpartitionvirtual machinevirtualbox

I have an image of an existing partition generated with dd if=/dev/sdXN of=image.bin. Now I want to use this image as the basis for a virtual machine. I know how to convert the image into a format that VirtualBox can use.

The problem is that the "disk" image is really just the image of one partition and thus does not contain an MBR or a partition table. This makes it very hard to boot the VM.

Is there a simple way, given an image of a partition, to create a proper disk image, including a partition table?

Best Answer

You can do this on the host machine. Most tools like fdisk will operate on files, and kpartx gives you access to partitions in a file.

  1. Create a new empty 100GiB sparse image (make this slightly bigger than the size of the partition image)

    dd if=/dev/zero of=myvm.img bs=1G count=0 seek=100
    
  2. Partition the image file with fdisk

    fdisk myvm.img
    
  3. Make the partitions in the image file available is individual devices

    sudo kpartx -a myvm.img
    
  4. Copy the partition image into the partition

    sudo cp image.bin /dev/mapper/loop0p1
    
  5. Extend the filesystem to fill the entire partition

    sudo resize2fs /dev/mapper/loop0p1
    
  6. Close the partitions

    sudo kpartx -d myvm.img
    
  7. Dismantle the loopback device

    sudo losetup -D
    
Related Question