How to Create and Convert Disks with qemu-img in Linux
The qemu-img command-line utility is a foundational
component of the Linux virtualization stack, primarily used to create,
convert, inspect, and modify virtual machine disk images offline.
Supporting an extensive variety of formats—such as QCOW2, RAW, VMDK,
VDI, and VHDX—it bridges the gap between different hypervisors and
storage architectures. This utility enables administrators to provision
new storage, adapt foreign disks for native Linux KVM/QEMU environments,
resize virtual storage, and inspect image metadata without running a
virtual machine.
What is qemu-img?
qemu-img is a standalone storage management tool
distributed as part of the QEMU package. In modern Linux hypervisors
like KVM, Proxmox VE, and OpenStack, it handles the underlying storage
primitives. Because it operates directly on disk files without mounting
or booting them, it provides a safe, scriptable, and resource-efficient
method for managing storage backends.
Creating Virtual Disk Images
One of the primary roles of qemu-img is provisioning
clean, unformatted virtual drives for new virtual machines. The utility
supports several formats, most notably:
- QCOW2 (QEMU Copy-On-Write): The native format for QEMU/KVM. It allocates space dynamically, supports zlib or zstd compression, and allows for internal snapshots.
- RAW: A simple binary image containing no metadata. It provides maximum I/O performance but allocates all specified space upfront unless created on a filesystem that supports sparse files.
To create an empty 20 GB disk image using the QCOW2 format, run:
qemu-img create -f qcow2 /var/lib/libvirt/images/disk.qcow2 20GPreallocation policies can also be defined during creation (such as
metadata or falloc) to balance allocation
speed and runtime performance.
Converting Between Disk Formats
The conversion capability of qemu-img is vital for
multi-hypervisor environments and cloud migrations. It allows
administrators to import VMware (VMDK), VirtualBox (VDI), or Hyper-V
(VHD/VHDX) disks into a KVM environment, or export KVM disks to other
platforms.
The basic syntax defines the source format (-f), output
format (-O), source file, and destination file:
qemu-img convert -f vmdk -O qcow2 source.vmdk target.qcow2During conversion, qemu-img can also compress the output
image to reduce network transfer times and storage consumption using the
-c flag:
qemu-img convert -c -O qcow2 source.raw compressed_target.qcow2The tool automatically strips out contiguous blocks of zeroes, functioning as an offline disk defragmenter and sparsifier during the conversion process.
Inspecting and Modifying Images
Beyond creation and conversion, qemu-img provides
crucial maintenance capabilities:
- Image Inspection (
info): Displays low-level metadata, including format, virtual size, actual disk usage, cluster size, and backing file chains.qemu-img info disk.qcow2 - Disk Resizing (
resize): Increases or shrinks the virtual capacity of an image offline before modifying partitions inside the guest operating system.qemu-img resize disk.qcow2 +10G - Consistency Checks (
check): Scans QCOW2 images for internal inconsistencies, cluster leaks, or file corruption.qemu-img check disk.qcow2
Through these combined roles, qemu-img functions as the
standard storage engine for virtual machine lifecycle management across
enterprise Linux infrastructures.