All Products
Search
Document Center

Elastic Compute Service:Convert the format of an image

Last Updated:Mar 22, 2024

Only images in the RAW, VHD, and QCOW2 formats can be imported to Elastic Compute Service (ECS). If your image file is in another format, you must convert the file to a supported format before you can import the file to ECS. This topic describes how to use the open source conversion tool qemu-img to convert the format of an image.

Limitations

  • The qemu-img tool can convert images between formats including RAW, QCOW2, QCOW1, VMDK, VDI, VHD, VHDX, and QED.

  • The qemu-img tool cannot directly convert images from the ISO format to the RAW, VHD, or QCOW2 format.

    To directly convert an image from the ISO format to the RAW, VHD, or QCOW2 format, create a virtual machine and use the ISO image to boot the virtual machine and the operating system. Then install the operating system on the virtual machine and export the disk of the virtual machine as a virtual disk image in the RAW, VHD, or QCOW2 format. For more information, see Create a virtual machine and install an operating system or Create a virtual machine and install an operating system.

  • When you convert an image from VHD to another format or from another format to VHD, use vpc instead of vhd as the file name extension of the image file in VHD format. Otherwise, the conversion may fail because the qemu-img tool fails to recognize the VHD image format. An error message similar to the following information is returned:

    image.png

Procedure

Install the qemu-img tool on your Windows instance and convert the format of an image

Perform the following steps to install the qemu-img tool on Windows and convert the image format.

Note

This section describes how to install qemu-img in a Windows Server 2016 operating system. The operations may differ if your instance uses other operating system versions.

  1. Install the qemu-img tool.

    Download and install the qemu-img tool. In the following example, the installation path is C:\Program Files\qemu.

  2. Configure environment variables for qemu-img.

    1. On the Windows Server desktop, right-click the image.png icon, and then click System.

    2. In the left-side navigation pane, click Advanced System Settings.

    3. In the System Properties dialog box, click the Advanced tab and click Environment Variables.

    4. In the Environment Variables dialog box, find the Path variable in the System variables section and click Edit. If the Path variable does not exist, click New to create the variable.

    5. Create the Path variable or add a value to the Path variable.

      • Edit the Path variable

        Click New and add the C:\Program Files\qemu variable value.

      • Create the Path variable

        Enter Path in the Variable name field and C:\Program Files\qemu in the Variable value field.

    6. Check whether the environment variable is properly configured.

      1. Open the Windows command prompt.

      2. Run the qemu-img --help command.

        If the version information of the qemu-img tool is returned, the environment variable is properly configured.

  3. Convert the image format.

    1. In the Command Prompt window, run the following command to switch to the directory where the source image file is located, for example, C:\Program Files\ConvertImage.

      cd C:\Program Files\ConvertImage
    2. Run the following command to convert the format of the image:

      In the following examples, an image is converted from QCOW2 to RAW, and another image is converted from VHD to RAW. Replace the image format in the command lines based on your requirement.

      Run the following command to convert an image from QCOW2 to RAW

      qemu-img convert -f qcow2 -O raw centos.qcow2 centos.raw

      The relevant parameters are:

      • -f: This parameter is followed by the format of the source image. A value of qcow2 indicates that the format of the source image file is QCOW2. This parameter is optional.

        Note

        You can run the qemu-img convert -O raw centos.qcow2 centos.raw command and omit the-f parameter and its value to convert the image format.

      • -O: This parameter must be in uppercase and is followed by the image format to which the source image is converted, the name of the source image file, and the name of the destination image file in sequence. This command converts the centos.qcow2 image file to the centos.raw image file in the RAW format.

      Run the following command to convert an image from VHD to RAW

      qemu-img convert -f vpc -O raw centos.vhd centos.raw

      The relevant parameters are:

      • -f: This parameter is followed by the format of the source image. A value of vpc indicates that the format of the source image file is VHD. This parameter is optional.

        Note

        You can run the qemu-img convert -O raw centos.vhd centos.raw command and omit the -f parameter and its value to convert the image format.

      • -O: This parameter must be in uppercase and is followed by the image format to which the source image is converted, the name of the source image file, and the name of the destination image file in sequence. This command converts the centos.vhd image file to the centos.raw image file in the RAW format.

        Important

        If you are converting other formats to VHD, use vpc instead of vhd as the file name extension of the destination image file. For example, to convert an image from RAW to VHD run the following command: qemu-img convert -f raw -O vpc centos.raw centos.vhd.

    3. Obtain the converted image file.

      • The converted image file is saved in the directory where the source image file is located. In this example, the converted image file is saved in C:\Program Files\ConvertImage.

      • You can also run the qemu-img info <destination file name> command to view the details of the converted image file.

        image.png

Install the qemu-img tool on your Linux instance and convert the format of an image

Perform the following steps to install the qemu-img tool on Linux and convert the image format.

Note

This section describes how to install qemu-img in a Alibaba Cloud Linux 3 or Ubuntu operating system. The operations may differ if your instance uses other operating system versions.

  1. Run the following command to install the qemu-img tool.

    • Alibaba Cloud Linux 3

      sudo yum install qemu-img
      Note

      If an error message is returned and does not indicate which dependency libraries are missing, you can run the sudo pip install -r requirements.txt command to install all dependency libraries contained in the requirements.txt file of cloud-init.

    • Ubuntu

      sudo apt-get install qemu-utils
      Note

      If the error message Unable to locate package qemu-utils is returned during the installation process, run the following commands to install the qemu-img tool.

      sudo apt-get update # Update the package list.
      sudo apt-get install qemu-utils # Install the qemu-img tool. 
  2. Run the following command to convert the format of the image:

    In the following examples, an image is converted from QCOW2 to RAW, and another image is converted from VHD to RAW. Replace the image format in the command lines based on your requirement.

    Run the following command to convert an image from QCOW2 to RAW

    qemu-img convert -f qcow2 -O raw centos.qcow2 centos.raw

    The relevant parameters are:

    • -f: This parameter is followed by the format of the source image. A value of qcow2 indicates that the format of the source image file is QCOW2. This parameter is optional.

      Note

      You can run the qemu-img convert -O raw centos.qcow2 centos.raw command and omit the-f parameter and its value to convert the image format.

    • -O: This parameter must be in uppercase and is followed by the image format to which the source image is converted, the name of the source image file, and the name of the destination image file in sequence. This command converts the centos.qcow2 image file to the centos.raw image file in the RAW format.

    Run the following command to convert an image from VHD to RAW

    qemu-img convert -f vpc -O raw centos.vhd centos.raw

    The relevant parameters are:

    • -f: This parameter is followed by the format of the source image. A value of vpc indicates that the format of the source image file is VHD. This parameter is optional.

      Note

      You can run the qemu-img convert -O raw centos.vhd centos.raw command and omit the -f parameter and its value to convert the image format.

    • -O: This parameter must be in uppercase and is followed by the image format to which the source image is converted, the name of the source image file, and the name of the destination image file in sequence. This command converts the centos.vhd image file to the centos.raw image file in the RAW format.

      Important

      If you are converting other formats to VHD, use vpc instead of vhd as the file name extension of the destination image file. For example, to convert an image from RAW to VHD run the following command: qemu-img convert -f raw -O vpc centos.raw centos.vhd.

  3. Obtain the converted image file.

    • The converted image file is saved in the directory where the source image file is located.

    • You can also run the qemu-img info <destination file name> command to view the details of the converted image file.

      image.png

What to do next

After you convert the image file format, you need to upload the image file to an OSS bucket to import the custom image. For more information, see Upload image files to OSS.

References

  • qemu-img is a command-line interface (CLI) for creating, converting, and managing virtual machine images. It is part of the Quick EMUlator (QEMU) project. In addition to converting image formats, qemu-img also supports other features such as creating images and resizing virtual machine images. For more information, visit the QEMU official website.

  • The qemu-img tool does not support the Open Virtualization Appliance (OVA) and Open Virtualization Format (OVF) formats. Because the qemu-img tool mainly creates, converts and manages disk image files. Image files in the OVA and OVF formats are virtualization packages, which include configuration files, description files and disk images. The OVA and OVF formats are maintained by the Distributed Management Task Force (DMTF). For more information, visit the DMTF official website.