Converting VHD to raw disk images, or even VMDK.
VHD is a nice format for exchanging disks. It doesn’t take up a lot of bytes with the empty space of a filesystem. But if you want to access the data without a virtual machine it can be a bit of a pain.
The attached program will expand most VHD files into a sparse image of the disk. You can then do whatever you wish with that, such as:
- Use it with a virtual machine that takes raw images.
- Convert it to a VMDK with qemu-img from the qemu folk. (Note: some people say qemu-img can read VHD format already. It didn’t work for me and the man page doesn’t mention it.)
Mount it on your Linux box, though you have to skip some bytes to get over the MBR and down to the first partition. Go look at Creating and using disk images mini-howto | Marc’s Realm and find “The dirty way”. Basically it boils down to trying all the plausible offsets until you find the right one:
for ((i=0 ; $i < 10000 ; i=$i + 1)) ; do mount -o loop,offset=$(($i * 512)) image.bootable /mnt && breakdone
Give it a sound grepping.
Note: I don’t support this program or even intend to run it ever again. I converted my data. If you need to convert your data, then enjoy.
This looks like just what the doctor ordered but it failed to compile for me:
Did I do something wrong or do you have any suggestions to help fix it?
Thanks much,
Patrick -> patrickkirchner AT yahoo . com
Current Directory = /tmp/vhd2img
-->make
cc -g -MMD -Wstrict-prototypes -Wall -Werror vhd2img.c -o vhd2img
vhd2img.c:38:38: error: missing terminating ' character
vhd2img.c:59:37: error: missing terminating ' character
vhd2img.c:67:67: error: missing terminating ' character
make: *** [vhd2img] Error 1
Well, err, uh, duh! I poked around in the .c file and removed the whole license preamble then it compiled just fine. Sorry about that. It worked great but the resulting .raw and converted .qcow2 file give me a BSOD when run with kvm.
Thanks,
Patrick.
http://oss.netfarm.it/download/vhd2img-64bit-aware.tar.bz2
(use wget or direct link, a link from a page will cause 503)
contact me if interested (sherpya@netfarm.it)
from sourceforge (disktype.sourceforge.net). That program is also included in your favorite package manager. It'll make it a bit easier to figure out where your
partitions are located. This is a sample output, using my newly converted image file. "disktype myimage.img"
Regular file, size 31.00 GiB (33286000640 bytes)
DOS/MBR partition map
Partition 1: 29.31 GiB (31473008640 bytes, 61470720 sectors from 2048, bootable)
Type 0x83 (Linux)
Ext3 file system
UUID 09DE2E1E-2C2F-4378-A8E6-59C8723865C7 (DCE, v4)
Volume size 29.31 GiB (31473008640 bytes, 7683840 blocks of 4 KiB)
Partition 2: 2.687 GiB (2884632576 bytes, 5634048 sectors from 61472768)
Type 0x82 (Linux swap / Solaris)
Linux swap, version 2, subversion 1, 4 KiB pages, little-endian
Swap size 2.687 GiB (2884624384 bytes, 704254 pages of 4 KiB)