How to install and use qemu-img on Windows and Linux?

STORDIS-qemu-img-banner-3200x1200.png

[stordis-toc]

Introduction

qemu-img is a free command-line tool that converts virtual disk images between formats. Use it, for example, to start the same SONiC VS image in KVM, VirtualBox, VMware or Hyper-V.

Format Platform Name in qemu-img
QCOW2 KVM, Proxmox, GNS3, Cisco Modeling Labs qcow2
VMDK VMware Workstation, VMware ESXi vmdk
VDI VirtualBox vdi
VHDX Hyper-V vhdx
RAW (.img) All platforms, written directly to a disk raw

Prerequisites

  1. A computer with Windows 10/11 or Linux.
  2. Administrator rights to install the tool.
  3. Free disk space: at least the full size of the disk image. A converted image can be larger than the source file.

Step 1: Install qemu-img

Windows Linux
  1. Go to https://qemu.weilnetz.de/w64/ and download the latest installer, for example qemu-w64-setup-<date>.exe.
  2. Run the installer. The default directory is C:\Program Files\qemu.
  3. You do not need to change the PATH variable. Use the full path to qemu-img.exe (refer to Step 4).

Step 2: Find the format of the source image

The file extension does not always show the real format. For example, sonic-vs.img is a QCOW2 file. Examine the file:

qemu-img info sonic-vs.img

The line file format shows the format. Use it as the input format in Step 3.


Step 3: Convert the image

General command:

qemu-img convert -p -f <input_format> -O <output_format> <source> <destination>
Option Description
-p Shows the progress.
-f The format of the source image (from Step 2). If you do not set it, qemu-img tries to find it.
-O The format of the new image (capital letter O).

Examples:

From → To Command
QCOW2 → VMDK
(KVM → VMware Workstation)
qemu-img convert -p -f qcow2 -O vmdk sonic-vs.img sonic-vs.vmdk
QCOW2 → VDI
(KVM → VirtualBox)
qemu-img convert -p -f qcow2 -O vdi sonic-vs.img sonic-vs.vdi
QCOW2 → VHDX
(KVM → Hyper-V)
qemu-img convert -p -f qcow2 -O vhdx sonic-vs.img sonic-vs.vhdx
VMDK → QCOW2
(VMware → KVM)
qemu-img convert -p -f vmdk -O qcow2 sonic-vs.vmdk sonic-vs.qcow2
VHDX → VDI
(Hyper-V → VirtualBox)
qemu-img convert -p -f vhdx -O vdi sonic-vs.vhdx sonic-vs.vdi
VDI → VHDX
(VirtualBox → Hyper-V)
qemu-img convert -p -f vdi -O vhdx sonic-vs.vdi sonic-vs.vhdx

 NOTE

VMware ESXi does not start a standard VMDK file directly. Convert the image to a VMDK for ESXi:

qemu-img convert -p -f qcow2 -O vmdk -o adapter_type=lsilogic,subformat=streamOptimized,compat6 sonic-vs.img sonic-vs-esxi.vmdk

Then upload the file to the datastore and attach it to the VM. For the full procedure, refer to the links at the end of this guide.


Step 4: Use qemu-img on Windows

Use the full path to qemu-img.exe. Put paths with spaces in quotation marks.

PowerShell Command Prompt

In PowerShell, type & before the path. Without it, PowerShell shows an error.

& "C:\Program Files\qemu\qemu-img.exe" info "C:\images\sonic-vs.img"

& "C:\Program Files\qemu\qemu-img.exe" convert -p -f qcow2 -O vdi "C:\images\sonic-vs.img" "C:\images\sonic-vs.vdi"

Troubleshooting

PowerShell: Unexpected token 'convert'

Type & and a space before the path in quotation marks (refer to Step 4).

Could not open the file / the file is locked

A virtual machine uses the image. Stop the VM, then convert the image again.

The new VM does not start from the converted disk
  • Make sure that the input format (-f) is correct (Step 2).
  • Make sure that the VM uses the same firmware as the source VM (BIOS or UEFI).
  • Make sure that the disk is the first device in the boot order.

Procedure completion

Congratulations!

You have successfully installed qemu-img and converted a virtual disk image. Now you can use the same image on different virtualization platforms.

Happy networking!

 

Useful links:

Where to download the SONiC Virtual Switch (VS) community pre-built image?

How to run SONiC VS in VirtualBox?

How to run SONiC VS in VMWare ESXi?

How to import VMDK image to Proxmox?

 

For more free resources, visit: https://stordis.com/free-resources/

Comments 0

Articles in this section