Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Qemu-img Cheatsheet

This is a cheatsheet/reference for all the qemu-img related 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

Example 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

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 

Merge Image Into Its Backing File - Commit


7ZlLc5swEMc/jY/JWLxMjrGbtId0JjOZaZujDALUCESE/OqnrwQSIMApk4dDM/HF6C9pJXZ/Wq3HM3uV7r8ymCffaYjIzJqH+5n9ZWZZ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0p+YaIRzebvhIqg5j8b++ov">
base.qcow2
[Not supported by viewer]
head.qcow2
[Not supported by viewer]
snapshot-1523781533
[Not supported by viewer]
Commit head.qcow2
Commit head.qcow2

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

e.g.

qemu-img commit head.qcow2

Then you can manually delete head.qcow2 if you like. All you really need now is base.qcow2.

Don't forget to either set up some symlinks or run virsh edit $VM_ID to update the guest to point back to the base.qcow2 rather than head.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

The -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

This will be a slower operation as any differences between the original backing file and the new one are merged into $QCOW2_FILE_TO_CHANGE. 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 ($QCOW2_FILE_TO_CHANGE) Obviously, this requires you to still have the original backing file.

Collapse 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:

  1. create a new empty backing file (new.base.qcow2)
  2. Rebase your head.qcow2 image onto that new empty backing file. Thus, all data will be considered different and merged into head.qcow2.
  3. Run a commit on the head.qcow2 to merge head.qcow2 into new.base.qcow2.
  4. Delete head.qcow2 before renaming new.base.qcow2 to head.qcow2.
  5. Delete all images other than head.qcow2.

References