From: Eric Blake <eblake@redhat.com>
To: Max Reitz <mreitz@redhat.com>, Jun Li <junmuzi@gmail.com>,
qemu-devel@nongnu.org
Cc: kwolf@redhat.com, juli@redhat.com, famz@redhat.com, stefanha@redhat.com
Subject: Re: [Qemu-devel] [PATCH v5 1/3] qcow2: Add qcow2_shrink_l1_and_l2_table for qcow2 shrinking
Date: Mon, 24 Nov 2014 10:49:10 -0700 [thread overview]
Message-ID: <54736F96.8030503@redhat.com> (raw)
In-Reply-To: <546F1A4D.50209@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 3214 bytes --]
On 11/21/2014 03:56 AM, Max Reitz wrote:
>
> Second, why are you truncating the file to offset if offset is smaller
> than actual_size? That is always wrong. You are blindly assuming:
> (1) that the image consists of only data and no metadata, because you're
> using the guest disk size (offset) to shrink the host file
> (2) that all that data is tightly packed at the beginning of the file
>
> (1) is obviously wrong. (2) is wrong as well, because clusters may be
> spread all over the host file. We would need defragmentation to solve
> that issue.
>
> Therefore, to shorten the host file, you'd need to walk through the
> image and find the highest *host* cluster actually in use. Then you can
> truncate to after that cluster. However, I'd strongly advise against
> that because it doesn't bring any actual benefit.
Well, there is a minor benefit - the 'query-blockstats' QMP command
reports wr_highest_offset, which IS the offset of the highest host
cluster already in use, but right now, we only populate that field on
the first allocating write, whereas I would like to be able to get at
that information even for a read-only file. See also Max's thread for
optimizing overlap checks, where I mention this same thing as a
side-effect we would get for free when improving overlap checks.
> So, as for what I think we do need to do when shrinking (and keep in
> mind: The offset given to qcow2_truncate() is the guest size! NOT the
> host image size!):
>
> (1) Determine the first L2 table and the first entry in the table which
> will lie beyond the new guest disk size.
> (2) Discard all clusters beginning from there.
> (3) Discard all L2 tables which are then completely empty.
You may want to also consider discarding refblocks when completely
empty, and truncating the size of the reftable if enough trailing
refblocks are dropped. On the other hand, just as Max argued that
shrinking the L1 table is going to be in the noise, you are also going
to be in the noise for trying to shrink the reftable.
> (4) Update the header size.
>
> And that's it. We can either speed up step (2) by implementing it
> manually, or we just use bdrv_discard() on the qcow2 BDS (in the
> simplest case: bdrv_discard(bs, DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE),
> bs->total_sectors - DIV_ROUND_UP(offset, BDRV_SECTOR_SIZE));.
>
> We can incorporate step (3) by extending qcow2_discard_clusters() to
> free L2 tables when they are empty after discard_single_l2(). But we
> don't even have to that now. It's an optimization we can go about later.
Same for trimming unused refblocks and/or shrinking the reftable. Also,
I think that a future addition of a defragmentation pass would be a more
ideal place for tightly packing an image, including trimming reftables
to a bare minimum.
>
> There is one advantage:
> - It's extremely simple. It's literally below ten lines of code.
>
> I think the advantage far outweighs the disadvantage. But I may be
> wrong. What do you think?
I also like the much simpler approach of leaving holes.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]
next prev parent reply other threads:[~2014-11-24 17:49 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-26 15:20 [Qemu-devel] [PATCH v5 0/3] qcow2: Patch for shrinking qcow2 disk image Jun Li
2014-10-26 15:20 ` [Qemu-devel] [PATCH v5 1/3] qcow2: Add qcow2_shrink_l1_and_l2_table for qcow2 shrinking Jun Li
2014-11-21 10:56 ` Max Reitz
2014-11-24 17:49 ` Eric Blake [this message]
2015-01-03 12:23 ` Jun Li
2015-01-15 18:47 ` Max Reitz
2015-01-19 13:16 ` Jun Li
2015-01-22 19:14 ` Max Reitz
2015-01-27 14:06 ` Jun Li
2014-10-26 15:20 ` [Qemu-devel] [PATCH v5 2/3] qcow2: add update refcount table realization for update_refcount Jun Li
2014-11-21 12:41 ` Max Reitz
2014-11-24 18:11 ` Eric Blake
2014-10-26 15:20 ` [Qemu-devel] [PATCH v5 3/3] qcow2: Add qemu-iotests for qcow2 shrinking Jun Li
2014-11-21 13:01 ` Max Reitz
2014-11-10 8:36 ` [Qemu-devel] [PATCH v5 0/3] qcow2: Patch for shrinking qcow2 disk image Jun Li
2014-11-10 9:17 ` Kevin Wolf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=54736F96.8030503@redhat.com \
--to=eblake@redhat.com \
--cc=famz@redhat.com \
--cc=juli@redhat.com \
--cc=junmuzi@gmail.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).