LinuxCommandLibrary

qemu-img

Manage virtual machine disk images

TLDR

Create disk image with a specific size (in gigabytes)

$ qemu-img create [path/to/image_file.img] [gigabytes]G
copy

Show information about a disk image
$ qemu-img info [path/to/image_file.img]
copy

Increase or decrease image size
$ qemu-img resize [path/to/image_file.img] [gigabytes]G
copy

Dump the allocation state of every sector of the specified disk image
$ qemu-img map [path/to/image_file.img]
copy

Convert a VMware .vmdk disk image to a KVM .qcow2 disk image and display [p]rogress
$ qemu-img convert -f vmdk -O qcow2 -p [path/to/image_file.vmdk] [path/to/image_file.qcow2]
copy

[c]reate an internal snapshot of a KVM .qcow2 disk image
$ qemu-img snapshot -c [snapshot_tag_name] [path/to/image_file.qcow2]
copy

[a]pply an internal snapshot to a KVM .qcow2 disk image
$ qemu-img snapshot -a [snapshot_tag_name] [path/to/image_file.qcow2]
copy

SYNOPSIS

qemu-img command [command_options] arguments

PARAMETERS

create
    
Creates a new disk image of a specified size and format. It supports various options like preallocation, backing files, and cluster size.

convert
    
Converts a disk image from one format to another. This is highly useful for migrating virtual disks between different hypervisors or optimizing storage.

check
    
Checks the consistency and integrity of a disk image, particularly useful for QCOW2 images, identifying and reporting potential corruption.

info
    
Displays detailed information about a disk image, including its format, virtual size, actual size on disk, backing file (if any), and snapshot details.

snapshot
    
Manages snapshots within a disk image. This subcommand allows creating, applying, deleting, and listing snapshots, useful for saving and restoring VM states.

rebase
    
Changes the backing file of a disk image. This is useful for consolidating chained images or changing the base image for a differencing disk.

resize
    
Resizes a disk image to a new specified size. It can expand or shrink images (though shrinking often requires filesystem changes within the guest).

amend
    
Amends specific properties of an existing disk image without modifying its content.

measure
    
Measures the required space for certain operations, like converting an image to a new format.

map
    
Dumps the virtual to physical page mapping of an image.

compare
    
Compares two disk images to identify differences.

dd
    
Copies data from an input path/image to an output path/image, similar to the dd command but for qemu-img supported formats.

sign
    
Signs a disk image, useful for verifying integrity and authenticity.

verify
    
Verifies the signature of a signed disk image.

DESCRIPTION

qemu-img is a powerful and versatile command-line utility included with the QEMU virtualization suite. It is indispensable for managing and manipulating disk images used by QEMU virtual machines.

This tool supports a wide array of disk image formats, including QCOW2 (QEMU Copy-On-Write), RAW, VMDK (VMware), VDI (VirtualBox), VHD (Microsoft Virtual PC/Hyper-V), VHDX (Hyper-V's enhanced VHD), and others.

With qemu-img, users can perform essential operations such as creating new disk images, converting images between different formats, checking the integrity of existing images, gathering detailed information about them, managing snapshots, rebasing images on different backing files, and resizing image capacities. Its flexibility makes it a crucial component in various virtualization workflows, from setting up development environments and migrating virtual machines between different hypervisors to analyzing disk contents and optimizing storage usage in cloud or bare-metal virtualization platforms. It handles both fixed-size and sparse (growable) disk images, offering efficient storage management capabilities.

CAVEATS

Data Loss Risk:
Operations like resize, convert, and rebase can potentially lead to data loss if interrupted or performed incorrectly. Always back up critical images before proceeding.
Performance:
Converting or resizing large disk images can be a time-consuming and resource-intensive process. Ensure sufficient disk space and system resources are available.
Filesystem Management:
While qemu-img resize changes the disk image size, it does not automatically resize the filesystem within the guest OS. This often requires separate guest OS tools (e.g., parted, resize2fs) to expand or shrink the filesystem to match the new disk size.
Format Compatibility:
Not all disk image formats support all qemu-img features (e.g., snapshots are primarily a QCOW2 feature). Specific format limitations might apply.

SUPPORTED IMAGE FORMATS

qemu-img boasts extensive support for numerous disk image formats, making it highly versatile for various virtualization environments. Key formats include:
RAW (simple byte-for-byte copy, often sparse),
QCOW2 (QEMU Copy-On-Write, advanced features like snapshots, compression, encryption, and backing files),
QCOW (older QEMU format),
VMDK (VMware's Virtual Machine Disk format),
VDI (VirtualBox Disk Image),
VHD (Microsoft Virtual Hard Disk),
VHDX (Microsoft's enhanced VHD format),
ISO (CD/DVD images), and others like cloop, host_device, parallels, vpc, bochs.

BACKING FILES AND SNAPSHOTS

A powerful feature of qemu-img, especially with QCOW2, is the concept of backing files. This allows creating a new image that only stores the differences from an existing base (backing) image. This is highly efficient for creating multiple virtual machines from a single master image or for managing disk snapshots. Snapshots, managed via qemu-img snapshot, capture the state of a disk at a specific point in time, enabling easy rollback and experimentation without affecting the base image.

HISTORY

qemu-img has been an integral part of the QEMU project since its early days, evolving alongside the main QEMU emulator. Its development has closely mirrored the increasing sophistication of virtualization technologies and the need for robust disk image management. Initially supporting basic RAW and QCOW formats, it has continuously expanded its capabilities to include a vast array of disk image formats (like VMDK, VHD, VDI, VHDX) and advanced features such as sophisticated snapshot management, backing files, and efficient copy-on-write mechanisms (e.g., QCOW2 improvements). Its ongoing development focuses on performance enhancements, broader format support, and improved integrity checks, making it a cornerstone utility for modern virtualization and cloud environments.

SEE ALSO

qemu(1), kvm(1), virsh(1), dd(1)

Copied to clipboard