Qemu: Merge snapshot and backing file into standalone disk

Praveen Kumar
2 min readNov 29, 2018

--

Today we found an interesting problem to solve and impressed that qemu have required tools for do the job.

Problem: We have one disk image which have a backing image and we want to merge them into a standalone disk.

# qemu-img info --backing-chain test1-master-0 
image: test1-master-0
file format: qcow2
virtual size: 16G (17179869184 bytes)
disk size: 4.6G
cluster_size: 65536
backing file: /var/lib/libvirt/images/test1-base
backing file format: qcow2
Format specific information:
compat: 0.10
refcount bits: 16
image: /var/lib/libvirt/images/test1-base ***
file format: qcow2
virtual size: 16G (17179869184 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false

In above snip you can see test1-master-0 have test1-base as backing image and this is we want to merge it with the test1-master-0 itself.

# cp test1-base new-master
# qemu-img rebase -b new-master test1-master-0
# qemu-img commit test1-master-0
Image committed.
# qemu-img info new-master
image: new-master
file format: qcow2
virtual size: 16G (17179869184 bytes)
disk size: 6.1G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false

What we did here?

  1. We copied our backing file test1-base to a new-master pretty easy :)
  2. Then rebase the image file that was backed off the original file so that it uses the new file i.e. new-master
  3. Finally you can commit those changes back to original file test1-master-0 back into the new base new-master

Now you can edit your vm xml to use this one and see if that works as expected (for me atleast it does work).

Happy hacking!!

Resource: https://stackoverflow.com/questions/22913384/transforming-qcows2-snapshot-plus-backing-file-into-standalone-image-file

--

--

Praveen Kumar

Runner, Cyclist, Open-source lover, MiniShift Developer !!