This patch add shrinking of the image file for qcow2. As a result, this allows us to reduce the virtual image size and free up space on the disk without copying the image. Image can be fragmented and shrink is done by punching holes in the image file.
# ./qemu-img create -f qcow2 -o size=4G image.qcow2 Formatting 'image.qcow2', fmt=qcow2 size=4294967296 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16 # ./qemu-io -c "write -P 0x22 0 1G" image.qcow2 wrote 1073741824/1073741824 bytes at offset 0 1 GiB, 1 ops; 0:00:02.59 (395.180 MiB/sec and 0.3859 ops/sec) # ./qemu-img resize image.qcow2 512M Warning: shrinking of the image can lead to data loss. Before performing shrink operation you must make sure that the shrink part of image doesn't contain important data. If you don't want to see this message use --shrink option. Image resized. # ./qemu-img resize --shrink image.qcow2 128M Image resized. # ./qemu-img info image.qcow2 image: image.qcow2 file format: qcow2 virtual size: 128M (134217728 bytes) disk size: 128M cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false # du -h ./image.qcow2 129M ./image.qcow2 Changes from v1: - add --shrink flag for qemu-img resize - add qcow2_cache_discard - simplify qcow2_shrink_l1_table() to reduce the likelihood of image corruption - add new qemu-iotests for shrinking images Pavel Butsykin (4): qemu-img: add --shrink flag for resize qcow2: add qcow2_cache_discard qcow2: add shrink image support qemu-iotests: add shrinking image test block/qcow2-cache.c | 21 +++++++++ block/qcow2-cluster.c | 42 +++++++++++++++++ block/qcow2-refcount.c | 70 ++++++++++++++++++++++++++++ block/qcow2.c | 40 ++++++++++++---- block/qcow2.h | 3 ++ qapi/block-core.json | 3 +- qemu-img-cmds.hx | 4 +- qemu-img.c | 15 ++++++ qemu-img.texi | 5 +- tests/qemu-iotests/102 | 4 +- tests/qemu-iotests/163 | 113 +++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/163.out | 5 ++ tests/qemu-iotests/group | 1 + 13 files changed, 310 insertions(+), 16 deletions(-) create mode 100644 tests/qemu-iotests/163 create mode 100644 tests/qemu-iotests/163.out -- 2.13.0