Add reduce image for qcow2
PATCH 2/2 qemu-iotests: add reducing image test in 025
In reply to Pavel Butsykin--- tests/qemu-iotests/025 | 19 +++++++++++++++++-- tests/qemu-iotests/025.out | 12 +++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-)
tests/qemu-iotests/025 +17 -2
@@ -38,7 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter . ./common.pattern -_supported_fmt raw qcow2 qed +_supported_fmt raw qcow2 _supported_proto file sheepdog rbd nfs _supported_os Linux
@@ -46,6 +46,7 @@ echo "=== Creating image" echo small_size=$((128 * 1024 * 1024)) big_size=$((384 * 1024 * 1024)) +bigger_size=$((512 * 1024 * 1024)) _make_test_img $small_size echo
@@ -54,7 +55,20 @@ io_pattern write 0 $small_size 0 1 0xc5 _check_test_img echo -echo "=== Resizing image" +echo "=== Growing image" +$QEMU_IO "$TEST_IMG" <<EOF | _filter_qemu_io +length +truncate $bigger_size +length +EOF +_check_test_img + +echo +echo "=== Verifying image size after reopen" +$QEMU_IO -c "length" "$TEST_IMG" + +echo +echo "=== Reducing image" $QEMU_IO "$TEST_IMG" <<EOF | _filter_qemu_io length truncate $big_size
@@ -70,6 +84,7 @@ echo echo "=== Verifying resized image" io_pattern read 0 $small_size 0 1 0xc5 io_pattern read $small_size $(($big_size - $small_size)) 0 1 0 +io_pattern read $big_size $(($bigger_size - $big_size)) 0 1 0 # success, all done echo "*** done"
tests/qemu-iotests/025.out +11 -1
@@ -9,8 +9,16 @@ wrote 134217728/134217728 bytes at offset 0 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) No errors were found on the image. -=== Resizing image +=== Growing image 128 MiB +512 MiB +No errors were found on the image. + +=== Verifying image size after reopen +512 MiB + +=== Reducing image +512 MiB 384 MiB No errors were found on the image.
@@ -24,4 +32,6 @@ read 134217728/134217728 bytes at offset 0 === IO: pattern 0 read 268435456/268435456 bytes at offset 134217728 256 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +=== IO: pattern 0 +read failed: Input/output error *** done
Signature · 1 line
2.11.0
PATCH 1/2 qcow2: add reduce image support
In reply to Pavel ButsykinThis patch adds the reduction 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 reduction is done by punching holes in the image file.
--- block/qcow2-cache.c | 8 +++++ block/qcow2-cluster.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2-refcount.c | 65 +++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 40 ++++++++++++++++++------ block/qcow2.h | 4 +++ qapi/block-core.json | 4 ++- 6 files changed, 193 insertions(+), 11 deletions(-)
block/qcow2-cache.c +8 -0
@@ -411,3 +411,11 @@ void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,assert (c->entries[i].offset !=0 ); c->entries[i].dirty = true; } + +void qcow2_cache_entry_mark_clean (BlockDriverState *bs,Qcow2Cache *c, +void *table) +{ +int i =qcow2_cache_get_table_idx (bs, c, table); +assert (c->entries[i].offset !=0 ); + c->entries[i].dirty = false; +}
block/qcow2-cluster.c +83 -0
89 lines not on this page · raw message
block/qcow2-refcount.c +65 -0
74 lines not on this page · raw message
block/qcow2.c +30 -10
@@ -2545,6 +2545,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, Error **errp) {BDRVQcow2State *s = bs->opaque;int64_t new_l1_size; +uint64_t total_size;int ret;if (offset &511 ) {
@@ -2558,17 +2559,36 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset, Error **errp)return -ENOTSUP ; } -/* shrinking is currently not supported */ -if (offset < bs->total_sectors *512 ) { -error_setg (errp,"qcow2 doesn't support shrinking images yet" ); -return -ENOTSUP ; - } - new_l1_size =size_to_l1 (s, offset); - ret =qcow2_grow_l1_table (bs, new_l1_size, true); -if (ret <0 ) { -error_setg_errno (errp, -ret,"Failed to grow the L1 table" ); -return ret; + total_size = bs->total_sectors <<BDRV_SECTOR_BITS ; + +if (offset < total_size) { + ret =qcow2_cluster_discard (bs,ROUND_UP (offset, s->cluster_size), + total_size -ROUND_UP (offset, + s->cluster_size), +QCOW2_DISCARD_ALWAYS , true); +if (ret <0 ) { +error_setg_errno (errp, -ret,"Failed to discard reduced clasters" ); +return ret; + } + + ret =qcow2_reduce_l1_table (bs, new_l1_size); +if (ret <0 ) { +error_setg_errno (errp, -ret,"Failed to reduce the L1 table" ); +return ret; + } + + ret =qcow2_reftable_shrink (bs); +if (ret <0 ) { +error_setg_errno (errp, -ret,"Failed to shrink the refcount table" ); +return ret; + } + }else { + ret =qcow2_grow_l1_table (bs, new_l1_size, true); +if (ret <0 ) { +error_setg_errno (errp, -ret,"Failed to grow the L1 table" ); +return ret; + } }/* write updated header.size */
block/qcow2.h +4 -0
@@ -531,10 +531,12 @@ int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,int qcow2_change_refcount_order (BlockDriverState *bs,int refcount_order,BlockDriverAmendStatusCB *status_cb,void *cb_opaque,Error **errp); +int qcow2_reftable_shrink (BlockDriverState *bs);/* qcow2-cluster.c functions */ int qcow2_grow_l1_table (BlockDriverState *bs,uint64_t min_size,bool exact_size); +int qcow2_reduce_l1_table (BlockDriverState *bs,uint64_t max_size);int qcow2_write_l1_entry (BlockDriverState *bs,int l1_index);int qcow2_decompress_cluster (BlockDriverState *bs,uint64_t cluster_offset); int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
@@ -583,6 +585,8 @@ int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);void qcow2_cache_entry_mark_dirty (BlockDriverState *bs,Qcow2Cache *c,void *table); +void qcow2_cache_entry_mark_clean (BlockDriverState *bs,Qcow2Cache *c, +void *table);int qcow2_cache_flush (BlockDriverState *bs,Qcow2Cache *c);int qcow2_cache_write (BlockDriverState *bs,Qcow2Cache *c); int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
qapi/block-core.json +3 -1
@@ -2371,7 +2371,9 @@ 'cluster_alloc_bytes', 'cluster_free', 'flush_to_os', 'flush_to_disk', 'pwritev_rmw_head', 'pwritev_rmw_after_head', 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', - 'pwritev_zero', 'pwritev_done', 'empty_image_prepare' ] } + 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', + 'l1_reduce_write_table', 'l1_reduce_activate_table', + 'l1_reduce_free_l2_clusters', 'l1_reduce_free_l1_clusters' ] } ## # @BlkdebugInjectErrorOptions:
Signature · 1 line
2.11.0
cover Add reduce image for qcow2
This patch adds the reduction 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 reduction 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 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
Pavel Butsykin (2):
qcow2: add reduce image support
qemu-iotests: add reducing image test in 025
block/qcow2-cache.c | 8 +++++ block/qcow2-cluster.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2-refcount.c | 65 ++++++++++++++++++++++++++++++++++++ block/qcow2.c | 40 ++++++++++++++++------ block/qcow2.h | 4 +++ qapi/block-core.json | 4 ++- tests/qemu-iotests/025 | 19 +++++++++-- tests/qemu-iotests/025.out | 12 ++++++- 8 files changed, 221 insertions(+), 14 deletions(-)
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 2/2] qemu-iotests: add reducing image test in 025
In reply to Pavel ButsykinQuoted 11 lines · Pavel Butsykin
Signed-off-by Pavel Butsykin <[email protected]>--- tests/qemu-iotests/025 | 19 +++++++++++++++++-- tests/qemu-iotests/025.out | 12 +++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-)tests/qemu-iotests/025 +0
-0
tests/qemu-iotests/025 trap "_cleanup; exit \$status" 0 1 2 3 15 · line 41
Quoted 5 lines
tests/qemu-iotests/025 +1
-1@@ -38,7 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter . ./common.pattern -_supported_fmt raw qcow2 qed +_supported_fmt raw qcow2
I'm not sure, can I so blatantly drop QED here. But this place is very suitable for reduce image case. Perhaps the alternative would be adding a new test, I just didn't want to copy the tests, which are testing almost the same thing.
Quoted 71 lines
tests/qemu-iotests/025 +16
-1_supported_proto file sheepdog rbd nfs _supported_os Linux@@ -46,6 +46,7 @@ echo "=== Creating image" echo small_size=$((128 * 1024 * 1024)) big_size=$((384 * 1024 * 1024)) +bigger_size=$((512 * 1024 * 1024)) _make_test_img $small_size echo@@ -54,7 +55,20 @@ io_pattern write 0 $small_size 0 1 0xc5 _check_test_img echo -echo "=== Resizing image" +echo "=== Growing image" +$QEMU_IO "$TEST_IMG" <<EOF | _filter_qemu_io +length +truncate $bigger_size +length +EOF +_check_test_img + +echo +echo "=== Verifying image size after reopen" +$QEMU_IO -c "length" "$TEST_IMG" + +echo +echo "=== Reducing image" $QEMU_IO "$TEST_IMG" <<EOF | _filter_qemu_io length truncate $big_size@@ -70,6 +84,7 @@ echo echo "=== Verifying resized image" io_pattern read 0 $small_size 0 1 0xc5 io_pattern read $small_size $(($big_size - $small_size)) 0 1 0 +io_pattern read $big_size $(($bigger_size - $big_size)) 0 1 0 # success, all done echo "*** done"tests/qemu-iotests/025.out +11
-1@@ -9,8 +9,16 @@ wrote 134217728/134217728 bytes at offset 0 128 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) No errors were found on the image. -=== Resizing image +=== Growing image 128 MiB +512 MiB +No errors were found on the image. + +=== Verifying image size after reopen +512 MiB + +=== Reducing image +512 MiB 384 MiB No errors were found on the image.@@ -24,4 +32,6 @@ read 134217728/134217728 bytes at offset 0 === IO: pattern 0 read 268435456/268435456 bytes at offset 134217728 256 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +=== IO: pattern 0 +read failed: Input/output error *** done
Quoted 9 lines · Pavel Butsykin
This patch adds the reduction 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 reduction 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
So this is 1G of guest-visible data...
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized.
...and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 13 lines · Eric Blake
Quoted 9 lines · Pavel Butsykin
This patch adds the reduction 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 reduction 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.qcow2So this is 1G of guest-visible data...
Quoted 8 lines
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized....and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
It is assumed that the user has already made a preparatory with the image:
1. freeing space at the end of the image
2. reducing the last partition on the disk
3. rebuilding fs
Only after these steps, the user can reduce the image by qemu-img.
I think it's not so rare case, sometimes people run out of disk space and this is another way to solve the problem (along with the use of trim).
We already have all the interfaces, left them only to support :)
Quoted 5 lines
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
Yes.
# ./qemu-img create -f qcow2 -o size=4G image.qcow2
# ./qemu-io -c "write -P 0x22 0 1G" image.qcow2 # ./qemu-io -c "write -P 0x22 1G 1G" image.qcow2
# qemu-img map ./image.qcow2 Offset Length Mapped to File 0 0x20000000 0x50000 ./image.qcow2 0x20000000 0x20000000 0x20060000 ./image.qcow2 0x40000000 0x20000000 0x40070000 ./image.qcow2 0x60000000 0x20000000 0x60080000 ./image.qcow2
# ./qemu-io -c "discard 1G 1G" ./image.qcow2
# qemu-img map ./image.qcow2 Offset Length Mapped to File0 0x20000000 0x50000 ./image.qcow2 0x20000000 0x20000000 0x20060000 ./image.qcow2
Quoted 27 lines · Pavel Butsykin
Quoted 17 lines · Eric Blake
Quoted 8 lines · Pavel Butsykin
This patch adds the reduction 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 reduction is done by punching
holes in the image file.Quoted 5 lines
# ./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.qcow2So this is 1G of guest-visible data...
Quoted 8 lines
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized....and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
Quoted 6 lines
It is assumed that the user has already made a preparatory with the image:
1. freeing space at the end of the image
2. reducing the last partition on the disk
3. rebuilding fs
Only after these steps, the user can reduce the image by qemu-img.
But your implementation allows the user to reduce it anyway, even if these steps have not been performed.
I very much agree that we have to be careful because otherwise you might ruin all your data if you hand-type a resize command and drop a digit.
Quoted 26 lines
I think it's not so rare case, sometimes people run out of disk space and this is another way to solve the problem (along with the use of trim).
We already have all the interfaces, left them only to support :)
Quoted 5 lines
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
Yes.
# ./qemu-img create -f qcow2 -o size=4G image.qcow2# ./qemu-io -c "write -P 0x22 0 1G" image.qcow2 # ./qemu-io -c "write -P 0x22 1G 1G" image.qcow2# qemu-img map ./image.qcow2 Offset Length Mapped to File 0 0x20000000 0x50000 ./image.qcow2 0x20000000 0x20000000 0x20060000 ./image.qcow2 0x40000000 0x20000000 0x40070000 ./image.qcow2 0x60000000 0x20000000 0x60080000 ./image.qcow2
Quoted 6 lines
# ./qemu-io -c "discard 1G 1G" ./image.qcow2# qemu-img map ./image.qcow2 Offset Length Mapped to File0 0x20000000 0x50000 ./image.qcow2 0x20000000 0x20000000 0x20060000 ./image.qcow2
No, it isn't, because qemu-io is a debugging tool and a debugging tool only.
We could require the user to perform a trim operation or something in the guest instead -- but I'd prefer a plain new flag for qemu-img resize that says the user is OK with shrinking the image and thus throwing data way.
I think it's fine to have this flag only as part of the qemu-img interface, not e.g. for the block-resize QMP command. I think it's reasonable to assume someone sending a QMP command (i.e. usually the management layer) to know exactly what they are doing. OTOH, I wouldn't oppose a flag there, though, I just don't think it's absolutely necessary.
Max
Quoted 6 lines · Pavel Butsykin
It is assumed that the user has already made a preparatory with the image:
1. freeing space at the end of the image
2. reducing the last partition on the disk
3. rebuilding fs
Only after these steps, the user can reduce the image by qemu-img.
It's tricky with GPT, as GPT puts a second copy of the header in the last few sectors of the disk. I always advise people to use trim (or virt-sparsify) instead of trying to adjust virtual disk size.
Nevertheless I don't think we should prevent shrinking qcow2 virtual size. It's likely useful to someone.
Rich.
Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
Quoted 35 lines · Max Reitz
Quoted 27 lines · Pavel Butsykin
Quoted 17 lines · Eric Blake
Quoted 8 lines · Pavel Butsykin
This patch adds the reduction 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 reduction is done by punching
holes in the image file.Quoted 5 lines
# ./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.qcow2So this is 1G of guest-visible data...
Quoted 8 lines
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized....and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
Quoted 6 lines
It is assumed that the user has already made a preparatory with the image:
1. freeing space at the end of the image
2. reducing the last partition on the disk
3. rebuilding fs
Only after these steps, the user can reduce the image by qemu-img.
Quoted 6 lines
But your implementation allows the user to reduce it anyway, even if these steps have not been performed.
I very much agree that we have to be careful because otherwise you might ruin all your data if you hand-type a resize command and drop a digit.
We could check that the shrinking part of the image doesn't contain non-zero clusters and print just a warning. But on the other hand, if the user has not performed trim, at the end of the disk will still be dirty cluster and we can't force users to do trim :)
We can add a flag --force and without flag just print a warning.
Quoted 40 lines
Quoted 26 lines
I think it's not so rare case, sometimes people run out of disk space and this is another way to solve the problem (along with the use of trim).
We already have all the interfaces, left them only to support :)
Quoted 5 lines
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
Yes.
# ./qemu-img create -f qcow2 -o size=4G image.qcow2# ./qemu-io -c "write -P 0x22 0 1G" image.qcow2 # ./qemu-io -c "write -P 0x22 1G 1G" image.qcow2# qemu-img map ./image.qcow2 Offset Length Mapped to File 0 0x20000000 0x50000 ./image.qcow2 0x20000000 0x20000000 0x20060000 ./image.qcow2 0x40000000 0x20000000 0x40070000 ./image.qcow2 0x60000000 0x20000000 0x60080000 ./image.qcow2Quoted 6 lines
# ./qemu-io -c "discard 1G 1G" ./image.qcow2# qemu-img map ./image.qcow2 Offset Length Mapped to File0 0x20000000 0x50000 ./image.qcow2 0x20000000 0x20000000 0x20060000 ./image.qcow2No, it isn't, because qemu-io is a debugging tool and a debugging tool only.
We could require the user to perform a trim operation or something in the guest instead -- but I'd prefer a plain new flag for qemu-img resize that says the user is OK with shrinking the image and thus throwing data way.
Quoted 6 lines
I think it's fine to have this flag only as part of the qemu-img interface, not e.g. for the block-resize QMP command. I think it's reasonable to assume someone sending a QMP command (i.e. usually the management layer) to know exactly what they are doing. OTOH, I wouldn't oppose a flag there, though, I just don't think it's absolutely necessary.
I agree that the flag can be only as protection from the human factor.
Quoted 2 lines
Max
Quoted 11 lines · Richard W.M. Jones
Quoted 6 lines · Pavel Butsykin
It is assumed that the user has already made a preparatory with the image:
1. freeing space at the end of the image
2. reducing the last partition on the disk
3. rebuilding fs
Only after these steps, the user can reduce the image by qemu-img.It's tricky with GPT, as GPT puts a second copy of the header in the last few sectors of the disk. I always advise people to use trim (or virt-sparsify) instead of trying to adjust virtual disk size.
virt-sparsify is a very useful utility, I use it too :) I agree that the trim is much better if the goal is only to free up space on the host disk. But there's another goal is to limit further growth of the image.
Quoted 6 lines
Nevertheless I don't think we should prevent shrinking qcow2 virtual size. It's likely useful to someone.
Rich.
Am 31.05.2017 um 17:03 hat Eric Blake geschrieben:
Quoted 22 lines · Pavel Butsykin
Quoted 9 lines
This patch adds the reduction 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 reduction 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.qcow2So this is 1G of guest-visible data...
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized....and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
Quoted 4 lines
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
I think that ship has sailed long ago because raw images have always supported shrinking images without any special checks or options. We want consistency between raw and qcow2, and we also need to provide backwards compatibility.
The only thing I can imagine we could do now is to introduce a --shrink option in qemu-img, print a deprecation warning for shrinking without this option, and then make it mandatory in a few years.
If we want to distinguish based on the block driver, so that we can require --shrink for all drivers that didn't support shrinking until now, we'd have to check the .bdrv_truncate() implementations of all drivers to see whether it already allowed shrinking.
Kevin
Re: [Qemu-devel] [PATCH 2/2] qemu-iotests: add reducing image test in 025
In reply to Pavel ButsykinAm 31.05.2017 um 16:54 hat Pavel Butsykin geschrieben:
Quoted 7 lines · Pavel Butsykin
Quoted 6 lines
Signed-off-by Pavel Butsykin <[email protected]>--- tests/qemu-iotests/025 | 19 +++++++++++++++++-- tests/qemu-iotests/025.out | 12 +++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-)
Quoted 14 lines
tests/qemu-iotests/025 trap "_cleanup; exit \$status" 0 1 2 3 15 · line 40
Quoted 9 lines
tests/qemu-iotests/025 +1
-1@@ -38,7 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter . ./common.pattern -_supported_fmt raw qcow2 qed +_supported_fmt raw qcow2I'm not sure, can I so blatantly drop QED here. But this place is very suitable for reduce image case. Perhaps the alternative would be adding a new test, I just didn't want to copy the tests, which are testing almost the same thing.
I think it's better to have a separate test case for shrinking, so that we don't damage the test coverage of qed. The resulting new patch would be a very small one, but that's okay.
Kevin
Quoted 34 lines · Kevin Wolf
Am 31.05.2017 um 17:03 hat Eric Blake geschrieben:
Quoted 20 lines · Pavel Butsykin
Quoted 9 lines
This patch adds the reduction 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 reduction 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.qcow2So this is 1G of guest-visible data...
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized....and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
Quoted 4 lines
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
I think that ship has sailed long ago because raw images have always supported shrinking images without any special checks or options. We want consistency between raw and qcow2, and we also need to provide backwards compatibility.
The only thing I can imagine we could do now is to introduce a --shrink option in qemu-img, print a deprecation warning for shrinking without this option, and then make it mandatory in a few years.
Quoted 6 lines
If we want to distinguish based on the block driver, so that we can require --shrink for all drivers that didn't support shrinking until now, we'd have to check the .bdrv_truncate() implementations of all drivers to see whether it already allowed shrinking.
Kevin
Will the solution proposed by Pavel in the answer to Max work:
- if there are no data blocks in the truncated space, truncate is allowed without additional options
- if there are data blocks in the truncated space, truncate is allowed with the flag --force or error is generated
Den
Am 01.06.2017 um 13:11 hat Denis V. Lunev geschrieben:
Quoted 40 lines · Kevin Wolf
Quoted 39 lines
Am 31.05.2017 um 17:03 hat Eric Blake geschrieben:
Quoted 20 lines · Pavel Butsykin
Quoted 9 lines
This patch adds the reduction 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 reduction 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.qcow2So this is 1G of guest-visible data...
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized....and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
Quoted 4 lines
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
I think that ship has sailed long ago because raw images have always supported shrinking images without any special checks or options. We want consistency between raw and qcow2, and we also need to provide backwards compatibility.
The only thing I can imagine we could do now is to introduce a --shrink option in qemu-img, print a deprecation warning for shrinking without this option, and then make it mandatory in a few years.
If we want to distinguish based on the block driver, so that we can require --shrink for all drivers that didn't support shrinking until now, we'd have to check the .bdrv_truncate() implementations of all drivers to see whether it already allowed shrinking.
Quoted 6 lines
Quoted 1 line
Kevin
Will the solution proposed by Pavel in the answer to Max work:
- if there are no data blocks in the truncated space, truncate is allowed without additional options
- if there are data blocks in the truncated space, truncate is allowed with the flag --force or error is generated
I don't think that's a great user interface because it means that you can only shrink images without --force if you use a guest device that supports discard (i.e. not virtio-blk) and your guest OS supports it, too. In fact, even with a SCSI device and Linux, I wouldn't be sure offhand how to discard unused space after the last partition. I'd rather call the switch --shrink and require it always.
In any case, however, I think 'qemu-img resize' should work consistently across different formats. That is, if we require that there be no data blocks in the truncated space for qcow2, we must require the same for raw. And that's where we come back to backwards compatibility, so it's the same case as I mentioned above: We would have to print a deprecation warning for some years and only then we could make it mandatory.
Kevin
Re: [Qemu-devel] [PATCH 1/2] qcow2: add reduce image support
In reply to Pavel ButsykinAm 31.05.2017 um 16:43 hat Pavel Butsykin geschrieben:
Quoted 21 lines
This patch adds the reduction 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 reduction is done by punching holes in the image file.
Signed-off-by Pavel Butsykin <[email protected]>--- block/qcow2-cache.c | 8 +++++ block/qcow2-cluster.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2-refcount.c | 65 +++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 40 ++++++++++++++++++------ block/qcow2.h | 4 +++ qapi/block-core.json | 4 ++- 6 files changed, 193 insertions(+), 11 deletions(-)block/qcow2-cache.c +0
-0@@ -411,3 +411,11 @@ void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,assert (c->entries[i].offset !=0 );
block/qcow2-cache.c void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c, · line 421
Quoted 10 lines
block/qcow2-cache.c +8
-0c->entries[i].dirty = true; } + +void qcow2_cache_entry_mark_clean (BlockDriverState *bs,Qcow2Cache *c, +void *table) +{ +int i =qcow2_cache_get_table_idx (bs, c, table); +assert (c->entries[i].offset !=0 ); + c->entries[i].dirty = false; +}
This is an interesting function. We can use it whenever we're not interested in the content of the table any more. However, we still keep that data in the cache and may even evict other tables before this one. The data in the cache also becomes inconsistent with the data in the file, which should not be a problem in theory (because nobody should be using it), but it surely could be confusing when debugging something in the cache.
We can easily improve this a little: Make it qcow2_cache_discard(), a function that gets a cluster offset, asserts that a table at this offset isn't in use (not cached or ref == 0), and then just directly drops it from the cache. This can be called from update_refcount() whenever a refcount goes to 0, immediately before or after calling update_refcount_discard() - those two are closely related. Then this would automatically also be used for L2 tables.
Adding this mechanism could be a patch of its own.
block/qcow2-cluster.c line 35
Quoted 9 lines
block/qcow2-cluster.c +1
-0@@ -32,6 +32,89 @@#include "qemu/bswap.h" #include "trace.h" +int qcow2_reduce_l1_table (BlockDriverState *bs,uint64_t max_size)
Let's call this shrink, it's easier to understand and consistent with qcow2_reftable_shrink() that this patch adds, too.
Quoted 23 lines
block/qcow2-cluster.c +23
-0+{ +BDRVQcow2State *s = bs->opaque; +int64_t new_l1_size_bytes, free_l1_clusters; +uint64_t *new_l1_table; +int new_l1_size, i, ret; + +if (max_size >= s->l1_size) { +return 0 ; + } + + new_l1_size = max_size; + +#ifdef DEBUG_ALLOC2 +fprintf (stderr,"reduce l1_table from %d to %" PRId64"\n" , + s->l1_size, new_l1_size); +#endif + + ret =qcow2_cache_flush (bs, s->l2_table_cache); +if (ret <0 ) { +return ret; + } + +BLKDBG_EVENT (bs->file,BLKDBG_L1_REDUCE_FREE_L2_CLUSTERS );
block/qcow2-cluster.c line 68
Quoted 10 lines
block/qcow2-cluster.c +10
-0+for (i = s->l1_size -1 ; i > new_l1_size -1 ; i--) { +if ((s->l1_table[i] &L1E_OFFSET_MASK ) ==0 ) { +continue ; + } +qcow2_free_clusters (bs, s->l1_table[i] &L1E_OFFSET_MASK , + s->l2_size *sizeof (uint64_t ), +QCOW2_DISCARD_ALWAYS ); + } + + new_l1_size_bytes =sizeof (uint64_t ) * new_l1_size;
On 32 bit hosts, this does a 32 bit calculation and assigns it to a 64 bit value. I think this is still correct because new_l1_size_bytes is limited by QCOW_MAX_L1_SIZE (0x2000000).
If this is the intention, maybe it would be more obvious to use a normal int for new_l1_size_bytes, or to assert(new_l1_size <= QCOW_MAX_L1_SIZE / sizeof(uint64_t)) before this line.
block/qcow2-cluster.c line 73
Quoted 4 lines
block/qcow2-cluster.c +4
-0+BLKDBG_EVENT (bs->file,BLKDBG_L1_REDUCE_WRITE_TABLE ); + ret =bdrv_pwrite_zeroes (bs->file, s->l1_table_offset + new_l1_size_bytes, + s->l1_size *sizeof (uint64_t ) - new_l1_size_bytes, +0 );
s->l1_table and the on-disk content are out of sync now. Error paths must bring them back into sync from now on.
This is easier with the approach of qcow2_grow_l1_table(), which creates a completely new L1 table and then atomically switches from old to new with a header update.
block/qcow2-cluster.c line 81
Quoted 8 lines
block/qcow2-cluster.c +8
-0+if (ret <0 ) { +return ret; + } + + ret =bdrv_flush (bs->file->bs); +if (ret <0 ) { +return ret; + }
In both of these error cases, we don't know the actual state of the L1 table on disk.
block/qcow2-cluster.c line 91
Quoted 9 lines
block/qcow2-cluster.c +9
-0+/* set new table size */ +BLKDBG_EVENT (bs->file,BLKDBG_L1_REDUCE_ACTIVATE_TABLE ); + new_l1_size =cpu_to_be32 (new_l1_size); + ret =bdrv_pwrite_sync (bs->file, offsetof(QCowHeader , l1_size), + &new_l1_size,sizeof (new_l1_size)); + new_l1_size =be32_to_cpu (new_l1_size); +if (ret <0 ) { +return ret; + }
Maybe we can salvage the error handling if we move this first. If this update fails, we simply have to keep using the old L1 table. If it succeeds, we have successfully shrunk the L1 table and the contents of the old entries doesn't strictly matter. We can zero it out just to be nice, but correctness isn't affected by it.
Quoted 5 lines
block/qcow2-cluster.c +5
-0+BLKDBG_EVENT (bs->file,BLKDBG_L1_REDUCE_FREE_L1_CLUSTERS ); + free_l1_clusters = +DIV_ROUND_UP (s->l1_size *sizeof (uint64_t ), s->cluster_size) - +DIV_ROUND_UP (new_l1_size_bytes, s->cluster_size); +if (free_l1_clusters) {
block/qcow2-cluster.c line 107
Quoted 10 lines
block/qcow2-cluster.c +10
-0+qcow2_free_clusters (bs, s->l1_table_offset + +ROUND_UP (new_l1_size_bytes, s->cluster_size), + free_l1_clusters << s->cluster_bits, +QCOW2_DISCARD_ALWAYS ); + } + + new_l1_table =qemu_try_blockalign (bs->file->bs, +align_offset (new_l1_size_bytes,512 )); +if (new_l1_table ==NULL ) { +return -ENOMEM ;
Now the disk has a shortened L1 size, but our in-memory representation still has the old size. This will cause corruption when the L1 table is grown again.
block/qcow2-cluster.c line 116
Quoted 9 lines
block/qcow2-cluster.c +9
-0+ } +memcpy (new_l1_table, s->l1_table, new_l1_size_bytes); + +qemu_vfree (s->l1_table); + s->l1_table = new_l1_table; + s->l1_size = new_l1_size; + +return 0 ; +}
Another thought: Is resizing the L1 table actually worth it given how easy it is to get the error paths wrong?
With 64k clusters, you get another L1 table cluster every 4 TB. So leaving 64k in the image file uselessly allocated for resizing an image from 8 TB to 4 TB sounds like a waste that is totally acceptable if we use it to get some more confidence that we won't corrupt images in error cases.
We could just free the now unused L2 tables and overwrite their L1 entries with 0 without actually resizing the L1 table.
block/qcow2-refcount.c
Quoted 4 lines
block/qcow2-refcount.c +0
-0
Skipping the review for refcount tables for now. The same argument as for L1 tables applies, except that each cluster of the refcount table covers 16 TB instead of 4 TB here.
qapi/block-core.json line 2376
Quoted 12 lines
qapi/block-core.json +3
-1@@ -2371,7 +2371,9 @@ 'cluster_alloc_bytes', 'cluster_free', 'flush_to_os', 'flush_to_disk', 'pwritev_rmw_head', 'pwritev_rmw_after_head', 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', - 'pwritev_zero', 'pwritev_done', 'empty_image_prepare' ] } + 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', + 'l1_reduce_write_table', 'l1_reduce_activate_table', + 'l1_reduce_free_l2_clusters', 'l1_reduce_free_l1_clusters' ] }
If you rename the function above, s/reduce/shrink/ here as well.
Kevin
Re: [Qemu-devel] [PATCH 1/2] qcow2: add reduce image support
Quoted 33 lines · Kevin Wolf
Am 31.05.2017 um 16:43 hat Pavel Butsykin geschrieben:
Quoted 21 lines
This patch adds the reduction 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 reduction is done by punching holes in the image file.
Signed-off-by Pavel Butsykin <[email protected]>--- block/qcow2-cache.c | 8 +++++ block/qcow2-cluster.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2-refcount.c | 65 +++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 40 ++++++++++++++++++------ block/qcow2.h | 4 +++ qapi/block-core.json | 4 ++- 6 files changed, 193 insertions(+), 11 deletions(-)block/qcow2-cache.c +0
-0@@ -411,3 +411,11 @@ void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,assert (c->entries[i].offset !=0 );block/qcow2-cache.c void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c, · line 421
Quoted 10 lines
block/qcow2-cache.c +8
-0c->entries[i].dirty = true; } + +void qcow2_cache_entry_mark_clean (BlockDriverState *bs,Qcow2Cache *c, +void *table) +{ +int i =qcow2_cache_get_table_idx (bs, c, table); +assert (c->entries[i].offset !=0 ); + c->entries[i].dirty = false; +}
Quoted 8 lines
This is an interesting function. We can use it whenever we're not interested in the content of the table any more. However, we still keep that data in the cache and may even evict other tables before this one. The data in the cache also becomes inconsistent with the data in the file, which should not be a problem in theory (because nobody should be using it), but it surely could be confusing when debugging something in the cache.
Good idea!
Quoted 8 lines
We can easily improve this a little: Make it
qcow2_cache_discard(), a function that gets a cluster offset, asserts that a table at this offset isn't in use (not cached or ref == 0), and then just directly drops it from the cache. This can be called fromupdate_refcount()whenever a refcount goes to 0, immediately before or after callingupdate_refcount_discard()- those two are closely related. Then this would automatically also be used for L2 tables.
Did I understand correctly? Every time we need to check the incoming offset to make sure it is offset to L2/refcount table (not to the guest data) ?
Quoted 1 line
Adding this mechanism could be a patch of its own
...
Quoted 3 lines
Kevin
Re: [Qemu-devel] [PATCH 1/2] qcow2: add reduce image support
Am 02.06.2017 um 11:53 hat Pavel Butsykin geschrieben:
Quoted 54 lines · Kevin Wolf
Quoted 33 lines
Am 31.05.2017 um 16:43 hat Pavel Butsykin geschrieben:
Quoted 21 lines
This patch adds the reduction 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 reduction is done by punching holes in the image file.
Signed-off-by Pavel Butsykin <[email protected]>--- block/qcow2-cache.c | 8 +++++ block/qcow2-cluster.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2-refcount.c | 65 +++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 40 ++++++++++++++++++------ block/qcow2.h | 4 +++ qapi/block-core.json | 4 ++- 6 files changed, 193 insertions(+), 11 deletions(-)block/qcow2-cache.c +0
-0@@ -411,3 +411,11 @@ void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,assert (c->entries[i].offset !=0 );block/qcow2-cache.c void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c, · line 421
Quoted 10 lines
block/qcow2-cache.c +8
-0c->entries[i].dirty = true; } + +void qcow2_cache_entry_mark_clean (BlockDriverState *bs,Qcow2Cache *c, +void *table) +{ +int i =qcow2_cache_get_table_idx (bs, c, table); +assert (c->entries[i].offset !=0 ); + c->entries[i].dirty = false; +}Quoted 8 lines
This is an interesting function. We can use it whenever we're not interested in the content of the table any more. However, we still keep that data in the cache and may even evict other tables before this one. The data in the cache also becomes inconsistent with the data in the file, which should not be a problem in theory (because nobody should be using it), but it surely could be confusing when debugging something in the cache.
Good idea!
Quoted 8 lines
We can easily improve this a little: Make it
qcow2_cache_discard(), a function that gets a cluster offset, asserts that a table at this offset isn't in use (not cached or ref == 0), and then just directly drops it from the cache. This can be called fromupdate_refcount()whenever a refcount goes to 0, immediately before or after callingupdate_refcount_discard()- those two are closely related. Then this would automatically also be used for L2 tables.
Quoted 3 lines
Did I understand correctly? Every time we need to check the incoming offset to make sure it is offset to L2/refcount table (not to the guest data) ?
Yes. Basically, whenever the refcount of a cluster becomes 0 and it is in a cache, remove it from the cache.
Kevin
Quoted 26 lines · Denis V. Lunev
Quoted 25 lines · Kevin Wolf
Am 31.05.2017 um 17:03 hat Eric Blake geschrieben:
Quoted 24 lines · Pavel Butsykin
Quoted 9 lines
This patch adds the reduction 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 reduction 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.qcow2So this is 1G of guest-visible data...
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized....and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
Quoted 8 lines
Quoted 8 lines
I think that ship has sailed long ago because raw images have always supported shrinking images without any special checks or options. We want consistency between raw and qcow2, and we also need to provide backwards compatibility.
The only thing I can imagine we could do now is to introduce a --shrink option in qemu-img, print a deprecation warning for shrinking without this option, and then make it mandatory in a few years.
Do I hear a "3.0"?
Quoted 4 lines
Quoted 4 lines
If we want to distinguish based on the block driver, so that we can require --shrink for all drivers that didn't support shrinking until now, we'd have to check the .bdrv_truncate() implementations of all drivers to see whether it already allowed shrinking.
We could have an ugly raw-only hack directly in qemu-img (and qmp_block_resize) until 3.0.
I'm really concerned about someone mistyping something and accidentally dropping a digit.
Quoted 6 lines
Quoted 1 line
Kevin
Will the solution proposed by Pavel in the answer to Max work:
- if there are no data blocks in the truncated space, truncate is allowed without additional options
- if there are data blocks in the truncated space, truncate is allowed with the flag --force or error is generated
I'd be OK with that, but I'm not sure we really need it. It would be nice to have for convenience, but I don't think it solves the backwards-compatibility problem (as said by Kevin), and I'm not sure it makes things that much more convenient: Just specifying --force is easier than to manually trim the device in the guest (and then maybe having to specify --force anyway because you didn't do it right).
Max
Am 07.06.2017 um 15:37 hat Max Reitz geschrieben:
Quoted 32 lines · Denis V. Lunev
Quoted 31 lines · Kevin Wolf
Quoted 30 lines
Am 31.05.2017 um 17:03 hat Eric Blake geschrieben:
Quoted 20 lines · Pavel Butsykin
Quoted 9 lines
This patch adds the reduction 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 reduction 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.qcow2So this is 1G of guest-visible data...
Quoted 2 lines
# ./qemu-img resize image.qcow2 128M Image resized....and you are truncating the image by throwing away guest-visible content, with no warning or double-checking (such as requiring a -f force parameter or something) about the data loss. Shrinking images is something we should allow, but it feels like is a rare enough operation that you don't want it to be casually easy to throw away data.
Quoted 4 lines
Is it feasible to require that a shrink operation will not be performed unless all clusters being eliminated have been previously discarded (or maybe written to zero), as an assurance that the guest did not care about the tail of the image?
I think that ship has sailed long ago because raw images have always supported shrinking images without any special checks or options. We want consistency between raw and qcow2, and we also need to provide backwards compatibility.
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
The only thing I can imagine we could do now is to introduce a --shrink option in qemu-img, print a deprecation warning for shrinking without this option, and then make it mandatory in a few years.
Do I hear a "3.0"?
You do.
Quoted 10 lines
Quoted 4 lines
Quoted 4 lines
If we want to distinguish based on the block driver, so that we can require --shrink for all drivers that didn't support shrinking until now, we'd have to check the .bdrv_truncate() implementations of all drivers to see whether it already allowed shrinking.
We could have an ugly raw-only hack directly in qemu-img (and
qmp_block_resize) until 3.0.I'm really concerned about someone mistyping something and accidentally dropping a digit.
Me too. But still we can't break working command lines (at least before 3.0).
I'm okay with temporary hacks in qemu-img, but did you check whether it's really raw-only? We know that raw can shrink currently and qcow2 can't, but there are 12 drivers implementing .bdrv_truncate, not only two.
Quoted 7 lines
Quoted 5 lines
Will the solution proposed by Pavel in the answer to Max work:
- if there are no data blocks in the truncated space, truncate is allowed without additional options
- if there are data blocks in the truncated space, truncate is allowed with the flag --force or error is generatedI'd be OK with that, but I'm not sure we really need it.
Agreed. It would add a lot of complexity for little use. As I wrote in a previous mail: I don't even know how I would discard the unpartitioned space after shrinking my guest filesystem.
Quoted 6 lines
It would be nice to have for convenience, but I don't think it solves the backwards-compatibility problem (as said by Kevin), and I'm not sure it makes things that much more convenient: Just specifying --force is easier than to manually trim the device in the guest (and then maybe having to specify --force anyway because you didn't do it right).
I think it should be specifically --shrink rather than an unspecific --force that could mean anything.
Kevin