Converting a QCOW2 image to a VHD image

Last Updated : Mar 21, 2024 |
Prolog information
Depending on the type of VM you are using, you might need to convert a QCOW2 image to a VHD image. Use this procedure to do that conversion.
  1. Log on as root to the Linux server.
  2. Copy the QCOW2 image file to a temporary directory.
  3. Run the following command convert the QCOW2 image to a raw file format:
    ADDITIONAL INFORMATION:
    qemu-img convert -f qcow2 -O raw ASBCE.qcow2 ASBCE.raw
    MB=$((1024 * 1024))
  4. Verify that the size of the raw image is aligned with 1 MB. If it is not, use the following commands to round it up to 1 MB:
    ADDITIONAL INFORMATION:
    size=$(qemu-img info -f raw --output json "ASBCE.raw" | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}')
    rounded_size=$((($size/$MB + 1)*$MB))
    qemu-img resize ASBCE.raw $rounded_size
    Note:
    The qemu-img command sometimes displays the following message, but you can ignore the warning:
    WARNING: Image format was not specified for 'ASBCE.raw' and probing guessed raw. 
    Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted. 
    Specify the 'raw' format explicitly to remove the restrictions. Image resized.
  5. Run the following command to convert the raw file to a fixed-size VHD image:
    ADDITIONAL INFORMATION:
    • If you are using QEMU Version 2.6 or later:
      qemu-img convert -f raw -o subformat=fixed,force_size -O vpc ASBCE.raw ASBCE.vhd
    • If you are using QEMU version earlier than 2.6:
      qemu-img convert -f raw -o subformat=fixed -O vpc ASBCE.raw ASBCE.vhd