This is a cheatsheet/reference for all the qemu-img related commands.
Get Output in JSON
If you are writing code that interacts with qemu, then you'll be pleased to know they added the following optional parameter to their commands:
Get Information About A File
qemu-img info $FILENAME
For a Qcow2 file this may output:
image: test-guest.qcow2 file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 196K cluster_size: 65536 backing file: /media/ssd_storage2/kvm/vms/templates/template-ubuntu-docker2/head.qcow2 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false
For a raw file it may output:
image: images.programster.org.img file format: raw virtual size: 30G (32217432064 bytes) disk size: 30G
Get Full Chain
Any number of images can be backing a qcow2 file you are looking at. In order to find out where these files are and other information about them you can run:
qemu-img info --backing-chain $FILENAME
image: test-guest.qcow2 file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 196K cluster_size: 65536 backing file: /media/ssd_storage2/kvm/vms/templates/template-ubuntu-docker2/head.qcow2 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false image: /media/ssd_storage2/kvm/vms/templates/template-ubuntu-docker2/head.qcow2 file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 1.1G cluster_size: 65536 backing file: /media/ssd_storage2/kvm/vms/templates/template-ubuntu-docker2/base.qcow2 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false image: /media/ssd_storage2/kvm/vms/templates/template-ubuntu-docker2/base.qcow2 file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 8.8G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true refcount bits: 16 corrupt: false
Convert Raw Image to Qcow2
qemu-img convert \ -f raw \ -O qcow2 \ $INPUT_FILE \ $OUTPUT_FILE
Resize/Expand Disk Image
Merge Image Into Its Backing File - Commit
Let's say that you have two files called head.qcow2 and base.qcow2 that backs it. If you wish to merge the two, then use the qemu-img commit which will merge the provided file into its backing image.
qemu-img commit $FILEPATH
qemu-img commit head.qcow2
Then you can manually delete
head.qcow2 if you like. All you really need now is base.qcow2.
Change Path Of Backing File
If you have moved the backing file to a new location, then you will need to run this command on the file(s) that were referencing it. This will just update them to point to the new location:
sudo qemu-img rebase \ -f qcow2 \ -u \ -b $NEW_BACKING_FILE_LOCATION \ $QCOW2_FILE_TO_CHANGE
-u stands for unsafe and results in a very quick operation of changing the pointer. If you don't specify it, then you will be doing the next section.
Changing Backing File (Advanced)
If you wish to wish to change to use a different backing file which may be different from the current backing file, then you want to use:
sudo qemu-img rebase \ -f qcow2 \ -b $NEW_BACKING_FILE \ $QCOW2_FILE_TO_CHANGE
qemu-img: Could not open '908793e8-8f85-4644-ad5c-6dc3afcbf9a2.qcow2': Failed to get "write" lock
Is another process using the image [908793e8-8f85-4644-ad5c-6dc3afcbf9a2.qcow2]?
This will be a slower operation as any differences between the original backing file and the new one are merged into
The result is that the "guest-visible content" remains the same and neither of the backing files are changed. The only file that changes is the "head" or "top" file (
Obviously, this requires you to still have the original backing file.
This command is actually quite neat/useful as it allows you do do the following:
Collapse/Flatten All Images
Let's say your happy with how your guest's data is, but you're tired of managing all your backing images and just want to start fresh with one image (head.qcow2). The easiest way to do that is to run a block pull
virsh blockpull $GUEST_ID vda --wait
--delete parameter if you wish for the old images to be removed and you know that no other images might be referencing them.
qemu-img snapshot -l $DISK_IMAGE.qcow2
qemu-img info command.
qemu-img snapshot -c $SNAPSHOT_NAME $DISK_IMAGE
Restore (Apply) Snapshot
qemu-img snapshot -a $SNAPSHOT_NAME $DISK_IMAGE
qemu-img snapshot -d \ $SNAPSHOT_NAME \ $DISK_IMAGE
- systutorials.com - qemu-img (1) - Linux Man Pages
- Fedora Docs - 9.2. Using qemu-img
- Stack Overflow - Transforming qcows2 snapshot plus backing file into standalone image file
- Unix & Linux - Qcow2 snapshots - snapshot for disk vda unsupported for storage type raw
- Kashyap Chamarthy - Snapshotting with libvirt for qcow2 images
- Azertech.net - KVM-QEMU, QCOW2, QEMU-IMG and Snapshots
First published: 16th August 2018