How do I convert an .img file to vhd?

45,341

Solution 1

VhdTool should be able to do the conversion.

vhdtool.exe /convert myimage.raw

Solution 2

I'm not sure for how long this has been the case, but since this is the #1 search result for this question, I'll answer it currently. VHD is currently supported by qemu-img. The argument for VHD is vpc. This was found here http://docs.openstack.org/image-guide/content/ch_converting.html

In case link breaks, here's a copy/past

Converting images from one format to another is generally straightforward. qemu-img convert: raw, qcow2, VDI, VMDK

The qemu-img convert command can do conversion between multiple formats, including raw, qcow2, VDI (VirtualBox), VMDK (VMWare) and VHD (Hyper-V). Table 7.1. qemu-img format strings

**Image format**    **Argument to qemu-img**
raw                     raw
qcow2                   qcow2
VDI (VirtualBox)        vdi
VMDK (VMWare)           vmdk
VHD (Hyper-V)           vpc

This example will convert a raw image file named centos63.dsk to a qcow2 image file.

$ qemu-img convert -f raw -O qcow2 centos64.dsk centos64.qcow2

To convert from vmdk to raw, you would do:

$ qemu-img convert -f vmdk -O raw centos64.vmdk centos64.img

Note

The -f format flag is optional. If omitted, qemu-img will try to infer the image format.

VBoxManage: VDI (VirtualBox) to raw

If you've created a VDI image using VirtualBox, you can convert it to raw format using the VBoxManage command-line tool that ships with VirtualBox. On Mac OS X, VirtualBox stores images by default in the ~/VirtualBox VMs/ directory. The following example creates a raw image in the current directory from a VirtualBox VDI image.

$ VBoxManage clonehd ~/VirtualBox\ VMs/fedora18.vdi fedora18.img --format raw

Solution 3

if anyone is still interested of converting raw disk image to vhd, use the following command:

qemu>qemu-img.exe convert e:\src.img -O vpc -o subformat=dynamic f:\dst.vhd

Don't forget to change presented filenames to your ones.

Solution 4

As indicated above by apple16 there is a quite similar question here that circumvents the problem that VhdTool seems not available anymore. Virtualbox comes with a tool called "VBoxManage" which works just perfect. Provided you've got a plain image file of your disk using e.g. dd, you can do this:

VBoxManage convertfromraw myfile.dd myfile.vhd --format VHD

Share:
45,341

Related videos on Youtube

user1060517
Author by

user1060517

Updated on September 18, 2022

Comments

  • user1060517
    user1060517 over 1 year

    I have an image file (linux based image) and want to convert it to VHD so that it can be used to create hyper-V VMs.

    I used "qemu-img convert" to convert raw to vpc format, but vpc doesnt work for hyper-V and vhd is not supported by "qemu-img convert".

    What would be the right way to do this?

    The image only has one partition:

    Partition Table: msdos
    
    Number  Start   End     Size    Type     File system  Flags
     1      32.8kB  4295MB  4295MB  primary  ext3         boot
    
  • user1060517
    user1060517 almost 12 years
    Thanks that worked. However the command doesnt create a new vhd, but appends a footer at the end of the existing image file. I just have to rename the file to .vhd so that it is recognized by the hyper-v vm creation wizard.
  • user1060517
    user1060517 almost 12 years
    Also, vhdtool.exe has a prerequisite of windows server OS/NTFS. I was wondering if there is any similar tool (raw to vhd) that can be used on a linux machine?
  • Michael Hampton
    Michael Hampton almost 12 years
    I'm not aware of one. But since the destination hypervisor is Hyper-V, this shouldn't be much of a problem. :)
  • Michael Hampton
    Michael Hampton almost 10 years
    @apple16 Yep, it's dead. Microsoft yanked it for some reason. Though copies can still be found with a careful web search, I think it best not to link to any such URLs as they may also change over time, and this one can also be fed to the Wayback Machine if necessary.
  • apple16
    apple16 almost 10 years
  • Admin
    Admin about 2 years
    This worked for me. However the device that I was using required the VHD image to be created with -o subformat=fixed instead of dynamic. dynamic is the default.