qemu-img

On this page 7

qemu-img is the command-line utility shipped with QEMU for creating, converting, inspecting, resizing, checking and snapshotting virtual disk images while they are offline. In Linux virtualization, it is the standard tool for preparing the storage that KVM guests use, whether those guests are started directly with QEMU, managed through libvirt and virsh, or run on platforms such as Proxmox VE and oVirt.

The tool understands the block layer of QEMU, so it reads and writes every image format that QEMU itself supports. That makes qemu-img the common translator between hypervisors: it can turn a VMware VMDK or a Hyper-V VHDX into qcow2 or raw in a single command, and it exposes format features such as backing chains, compression and encryption that ordinary file tools cannot see.

How qemu-img works with disk image formats

qemu-img works by opening an image through QEMU block drivers, which translate a guest-visible sequence of sectors into the layout of a specific file format. A raw image stores sectors one-to-one, while qcow2 keeps a two-level lookup table that maps guest clusters to host file offsets and allocates space only when data is written.

Because the same drivers serve running virtual machines, qemu-img commands interpret metadata exactly as QEMU will. The utility also honors image locking: by default it refuses to open an image for writing while a running guest holds it, which prevents the metadata corruption that concurrent writers cause. The table below compares the formats administrators encounter most often.

FormatOriginMain featuresTypical use
rawGenericNo metadata, sparse if the filesystem allowsMaximum speed, LVM or Ceph volumes
qcow2QEMUThin provisioning, snapshots, backing files, compressionDefault for KVM and libvirt
vmdkVMwareMonolithic or split extentsImport from or export to VMware
vhdxMicrosoft Hyper-VLarge disks, log-protected metadataExchange with Hyper-V and Azure
vpcMicrosoft Virtual PCLegacy VHD layoutOlder Azure and Virtual PC images
vdiVirtualBoxDynamic or fixed allocationMigration from VirtualBox
luksLinux disk encryptionLUKS header around raw dataEncrypted standalone images
Common image formats handled by qemu-img

Core qemu-img subcommands

qemu-img is organized around subcommands, each of which performs one operation on one or more images. The first argument names the subcommand, followed by options such as -f for the input format and -O for the output format.

SubcommandPurposeExample
createMakes a new empty or overlay imageqemu-img create -f qcow2 vm.qcow2 40G
infoShows format, virtual size, disk usage and backing chainqemu-img info --backing-chain vm.qcow2
convertCopies an image into another format or fileqemu-img convert -p -O qcow2 in.vmdk out.qcow2
checkVerifies qcow2 metadata and can repair leaksqemu-img check -r leaks vm.qcow2
resizeChanges the virtual disk sizeqemu-img resize vm.qcow2 +20G
snapshotLists, creates, applies or deletes internal snapshotsqemu-img snapshot -l vm.qcow2
commitMerges an overlay into its backing fileqemu-img commit overlay.qcow2
rebaseChanges the backing file of an overlayqemu-img rebase -b new.qcow2 -F qcow2 top.qcow2
measureCalculates the space a conversion will needqemu-img measure -O qcow2 in.raw
compareChecks whether two images have identical contentqemu-img compare a.qcow2 b.raw
Essential subcommands and their purpose

Two lesser-known subcommands help with diagnosis. qemu-img map prints which guest ranges are allocated, zero or backed by a lower layer, and qemu-img bench measures raw I/O throughput of an image to separate storage problems from guest problems.

Backing files, overlays and snapshots

A backing file lets one qcow2 image record only the differences from a read-only base image, which is the foundation of copy-on-write cloning. Creating an overlay with qemu-img create -f qcow2 -b base.qcow2 -F qcow2 vm1.qcow2 produces a nearly empty file that reads unchanged clusters from the base and stores new writes locally.

Recent QEMU releases require the backing format through -F, because probing an unknown backing file is a security risk: a malicious raw image could contain a qcow2 header that points at arbitrary host files. Internal snapshots, by contrast, live inside a single qcow2 file and are managed with qemu-img snapshot, which is convenient for quick rollback but makes the file grow and cannot be used safely while the guest runs.

ApproachWhere data livesStrengthWeakness
Internal snapshotInside one qcow2 fileSingle file to manageOffline only with qemu-img, slower to delete
External overlaySeparate qcow2 per layerFast creation, shareable baseChains must be tracked and merged
Storage-level snapshotLVM, ZFS or CephIndependent of image formatRequires a capable storage backend
Snapshot approaches compared

Performance and allocation options

The -o creation options control how qcow2 allocates space, and they have a measurable effect on guest I/O. The default cluster size of 64 KiB balances metadata size against allocation granularity; larger clusters reduce lookup overhead for big sequential workloads, while smaller clusters waste less space with many small writes.

SettingValuesEffect
preallocationoff, metadata, falloc, fullTrades initial disk usage for fewer allocation stalls
cluster_size512 B to 2 MiBChanges metadata overhead and space granularity
lazy_refcountson or offFaster writes, but requires repair after a crash
compression_typezlib or zstdApplies to clusters written with convert -c
extended_l2on or offEnables subclusters to reduce copy-on-write cost
Allocation settings and their trade-offs

For conversions, -p shows progress, -m sets the number of parallel coroutines and -W allows out-of-order writes, which can shorten large copies considerably on fast storage. By default convert also detects zeroed regions and leaves them unallocated in the target, so a 100 GB guest with 15 GB of data usually produces a file close to 15 GB.

Common mistakes and safe practice

The most damaging qemu-img mistake is modifying an image that a running guest is using. Image locking blocks most such attempts, but forcing access or working on copies outside QEMU can still corrupt qcow2 metadata beyond repair.

  • Shrinking without care: resize --shrink cuts data at the end of the disk, so the guest filesystem and partition must be reduced first.
  • Growing without guest steps: enlarging the image does not enlarge the partition or filesystem inside it; tools such as growpart and resize2fs are still needed.
  • Committing into a shared base: commit writes changes into the backing file and silently alters every other overlay that depends on it.
  • Reading a live image without `-U`: qemu-img info on an attached disk needs --force-share, and the reported values may be slightly out of date.
  • Skipping `check` after a host crash: qcow2 images with lazy refcounts or interrupted writes should be verified before the guest starts again.

A non-technical view

A virtual machine keeps its whole hard drive inside one big file. qemu-img is the toolbox for those drive files. It can make a new empty drive, tell you how large a drive is and how much space it really uses, copy it into a format another program understands, or make it bigger.

It works a bit like a converter for document formats, except the documents are entire computers. The key point is simple: use it while the virtual machine is switched off, and the drive files stay healthy.

A concrete example

A small company is moving twelve virtual servers from VMware to a Linux host running KVM and libvirt. For each server, the administrator exports the VMDK disk, runs qemu-img measure to confirm the target volume has enough room, and then converts it with qemu-img convert -p -m 8 -O qcow2 server.vmdk server.qcow2.

The largest server has a 200 GB virtual disk with about 38 GB of real data. Because empty regions stay unallocated, the resulting qcow2 file occupies roughly 39 GB, and the conversion finishes in under ten minutes on solid-state storage. After qemu-img check reports no errors, the administrator defines the guest with virt-install, switches its disk bus to Virtio and boots it. All twelve servers are migrated over one weekend without buying conversion software.