qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Richard W.M. Jones" <rjones@redhat.com>
To: Max Reitz <mreitz@redhat.com>
Cc: ptoscano@redhat.com, sgarzare@redhat.com, qemu-devel@nongnu.org,
	qemu-block@nongnu.org, mlevitsk@redhat.com
Subject: Re: Bug? qemu-img convert to preallocated image makes it sparse
Date: Thu, 16 Jan 2020 14:53:59 +0000	[thread overview]
Message-ID: <20200116145359.GR3888@redhat.com> (raw)
In-Reply-To: <962aa54b-f6e5-bb43-50a0-c4cad59cd22e@redhat.com>

On Thu, Jan 16, 2020 at 03:47:30PM +0100, Max Reitz wrote:
> On 16.01.20 15:13, Richard W.M. Jones wrote:
> > I'm not necessarily saying this is a bug, but a change in behaviour in
> > qemu has caused virt-v2v to fail.  The reproducer is quite simple.
> > 
> > Create sparse and preallocated qcow2 files of the same size:
> > 
> >   $ qemu-img create -f qcow2 sparse.qcow2 50M
> >   Formatting 'sparse.qcow2', fmt=qcow2 size=52428800 cluster_size=65536 lazy_refcounts=off refcount_bits=16
> > 
> >   $ qemu-img create -f qcow2 prealloc.qcow2 50M -o preallocation=falloc,compat=1.1
> >   Formatting 'prealloc.qcow2', fmt=qcow2 size=52428800 compat=1.1 cluster_size=65536 preallocation=falloc lazy_refcounts=off refcount_bits=16
> > 
> >   $ du -m sparse.qcow2 prealloc.qcow2 
> >   1 sparse.qcow2
> >   51	prealloc.qcow2
> > 
> > Now copy the sparse file into the preallocated file using the -n
> > option so qemu-img doesn't create the target:
> > 
> >   $ qemu-img convert -p -n -f qcow2 -O qcow2 sparse.qcow2 prealloc.qcow2
> >       (100.00/100%)
> > 
> > In new qemu that makes the target file sparse:
> > 
> >   $ du -m sparse.qcow2 prealloc.qcow2 
> >   1 sparse.qcow2
> >   1 prealloc.qcow2         <-- should still be 51
> > 
> > In old qemu the target file remained preallocated, which is what
> > I and virt-v2v are expecting.
> > 
> > I bisected this to the following commit:
> > 
> > 4d7c487eac1652dfe4498fe84f32900ad461d61b is the first bad commit
> > commit 4d7c487eac1652dfe4498fe84f32900ad461d61b
> > Author: Max Reitz <mreitz@redhat.com>
> > Date:   Wed Jul 24 19:12:29 2019 +0200
> > 
> >     qemu-img: Fix bdrv_has_zero_init() use in convert
> >     
> >     bdrv_has_zero_init() only has meaning for newly created images or image
> >     areas.  If qemu-img convert did not create the image itself, it cannot
> >     rely on bdrv_has_zero_init()'s result to carry any meaning.
> >     
> >     Signed-off-by: Max Reitz <mreitz@redhat.com>
> >     Message-id: 20190724171239.8764-2-mreitz@redhat.com
> >     Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
> >     Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
> >     Signed-off-by: Max Reitz <mreitz@redhat.com>
> > 
> >  qemu-img.c | 11 ++++++++---
> >  1 file changed, 8 insertions(+), 3 deletions(-)
> > 
> > Reverting this commit on the current master branch restores the
> > expected behaviour.
> 
> So what this commit changed was that when you take an existing image as
> the destination, you can’t assume anything about its contents.  Before
> this commit, we assumed it’s zero.  That’s clearly wrong, because it can
> be anything.
> 
> So when you convert to the target image, you have to make sure all areas
> that are zero in the source are zero in the target, too.  The way we do
> that is to write zeroes to the target.  The problem is that this
> operation disregards the previous preallocation and discards the
> preallocated space.
> 
> As for fixing the bug...  Can we fix it in qemu(-img)?
> 
> We could try to detect whether areas that are zero in the source are
> zero in the (preallocated) target image, too.  But doing so what require
> reading the data from those areas and comparing it to zero.  That would
> take time and it isn’t trivial.  So that’s something I’d rather avoid.
> 
> Off the top of my head, the only thing that comes to my mind would be to
> add a flag to qemu-img convert with which you can let it know that you
> guarantee the target image is zero.  I suppose we could document it also
> to imply that given this flag, areas that are zero in the source will
> then not be changed in the target image; i.e. that preallocation stays
> intact in those areas.
> 
> 
> OTOH, can it be fixed in virt-v2v?  Is there already a safe way to call
> qemu-img convert -n and keeping the target’s preallocation intact?
> Unfortunately, I don’t think so.  I don’t think we ever guaranteed it
> would, and well, now it broke.

From the fixing virt-v2v point of view, it's a bit tricky since the
code has to deal with all kinds of output targets.  (For example we
sometimes qemu-img convert into an NBD target.)

However we do know when the target contains zeroes - in fact it always
contains zeroes, so:

> So would you be OK with a --target-is-zero flag?  (I think we could let
> this flag guarantee that your use case works, so it should be future-safe.)

this one should work.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW



  reply	other threads:[~2020-01-16 14:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-16 14:13 Bug? qemu-img convert to preallocated image makes it sparse Richard W.M. Jones
2020-01-16 14:37 ` Max Reitz
2020-01-16 14:50   ` Kevin Wolf
2020-01-16 14:55     ` Max Reitz
2020-01-16 15:38       ` Maxim Levitsky
2020-01-16 15:56         ` Max Reitz
2020-01-16 16:00         ` Richard W.M. Jones
2020-01-16 16:02           ` Max Reitz
2020-01-17 10:28   ` David Edmondson
2020-01-16 14:47 ` Max Reitz
2020-01-16 14:53   ` Richard W.M. Jones [this message]
2020-01-16 14:57   ` Eric Blake
2020-01-16 15:03     ` Max Reitz

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=20200116145359.GR3888@redhat.com \
    --to=rjones@redhat.com \
    --cc=mlevitsk@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=ptoscano@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sgarzare@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).