qemu-img diff

This patch I posted yesterday needs some reviews! It adds a qemu-img “diff” subcommand. One use for this is for when you’ve copied/cloned a guest, and you want to go back to a thin-provisioned guest over a base image.

To show it in action, we can use virt-ls. Firstly I’ll start with a Windows 2003 guest which I’ll copy:

$ xzcat /mnt/media/guest-library/win2003.img.xz > base.img
$ cp base.img copy.img

At this point, I boot copy.img, log in and install the Windows updates, just so the guest has some changes over the base image.

Now I have two 6 GB files**, but I know that they’re very similar to each other:

$ ll -h
total 6.9G
-rw-rw-r--. 1 rjones rjones 6.0G May 18 11:26 base.img
-rw-rw-r--. 1 rjones rjones 6.0G May 18 11:42 copy.img

I could save some space here using qemu-img diff!

$ ~/d/qemu/qemu-img diff -b base.img copy.img diff.qcow2
Formatting 'diff.qcow2', fmt=qcow2 size=6442450944 backing_file='base.img' encryption=off cluster_size=65536 
$ ll -h
total 7.2G
-rw-rw-r--. 1 rjones rjones 6.0G May 18 11:26 base.img
-rw-rw-r--. 1 rjones rjones 6.0G May 18 11:42 copy.img
-rw-r--r--. 1 rjones rjones 308M May 18 11:44 diff.qcow2

copy.img is no longer needed because base.img + diff.qcow2 contains the same data. Before we delete it, let’s check that using virt-ls*:

$ virt-ls -lR -a base.img --checksum / > base.files
$ virt-ls -lR -a copy.img --checksum / > copy.files
$ virt-ls -lR -a diff.qcow2 --checksum / > diff.files

Note we expect that “base” is different from the other two, but “copy” and “diff” should be identical if all went well:

$ md5sum *.files
bef46650ff6f2d3f22f562c2a820230f  base.files
fc723e43a9c16bffd6a9fb7dfd701752  copy.files
fc723e43a9c16bffd6a9fb7dfd701752  diff.files

So we can now in confidence delete copy.img.

* = Note there is a bug which stops --checksum from working in released versions of virt-ls.

** = The eagle-eyed will spot that I don’t in fact have two 6 GB files, because cp has cleverly made the copy sparse, Nevertheless qemu-img diff still saves space.

1 Comment

Filed under Uncategorized

One response to “qemu-img diff

  1. pixelbeat1

    It’s worth noting cp –reflink too which will auto CoW the diff blocks on BTRFS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s