Patchwork [v3] qemu-img: Implement 'diff' operation.

login
register
mail settings
Submitter Richard W.M. Jones
Date May 21, 2012, 1:59 p.m.
Message ID <20120521135926.GM15276@amd.home.annexia.org>
Download mbox | patch
Permalink /patch/160366/
State New
Headers show

Comments

Richard W.M. Jones - May 21, 2012, 1:59 p.m.
On Mon, May 21, 2012 at 03:29:22PM +0200, Kevin Wolf wrote:
> Am 17.05.2012 17:34, schrieb Richard W.M. Jones:
> > From: "Richard W.M. Jones" <rjones@redhat.com>
> > 
> > This produces a qcow2 file which is the difference between
> > two disk images.  ie, if:
> > 
> >   base.img     - is a disk image (in any format)
> >   modified.img - is base.img, copied and modified
> > 
> > then:
> > 
> >   qemu-img diff -b base.img modified.img diff.qcow2
> > 
> > creates 'diff.qcow2' which contains the differences between 'base.img'
> > and 'modified.img'.  Note that 'diff.qcow2' has 'base.img' as its
> > backing file.
> > 
> > Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> > Cc: Matthew Booth <mbooth@redhat.com>
> > Cc: Pablo Iranzo Gómez <Pablo.Iranzo@redhat.com>
> > Cc: Tomas Von Veschler <tvvcox@redhat.com>
> 
> Hm, I'm wondering... If I have a command line like this:
> 
>   qemu-img diff -b base.img modified.img diff.qcow2
> 
> Would this be equivalent to this sequence?
> 
>   qemu-img create -f qcow2 -b modified.img diff.qcow2
>   qemu-img rebase -b base.img diff.qcow2
> 
> Or is there some detail that I'm missing? If it is equivalent, this
> would suggest that either the new command isn't necessary at all or at
> least that it should reuse the qemu-img rebase code.

Yes.  I tried for a while to work out the sequence of commands that
could make a diff using 'qemu-img rebase', but it wasn't obvious and I
gave up.  It should at least be documented.  How about the attached
patch?

Rich.
Kevin Wolf - May 21, 2012, 2:06 p.m.
Am 21.05.2012 15:59, schrieb Richard W.M. Jones:
> On Mon, May 21, 2012 at 03:29:22PM +0200, Kevin Wolf wrote:
>> Am 17.05.2012 17:34, schrieb Richard W.M. Jones:
>>> From: "Richard W.M. Jones" <rjones@redhat.com>
>>>
>>> This produces a qcow2 file which is the difference between
>>> two disk images.  ie, if:
>>>
>>>   base.img     - is a disk image (in any format)
>>>   modified.img - is base.img, copied and modified
>>>
>>> then:
>>>
>>>   qemu-img diff -b base.img modified.img diff.qcow2
>>>
>>> creates 'diff.qcow2' which contains the differences between 'base.img'
>>> and 'modified.img'.  Note that 'diff.qcow2' has 'base.img' as its
>>> backing file.
>>>
>>> Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
>>> Cc: Matthew Booth <mbooth@redhat.com>
>>> Cc: Pablo Iranzo Gómez <Pablo.Iranzo@redhat.com>
>>> Cc: Tomas Von Veschler <tvvcox@redhat.com>
>>
>> Hm, I'm wondering... If I have a command line like this:
>>
>>   qemu-img diff -b base.img modified.img diff.qcow2
>>
>> Would this be equivalent to this sequence?
>>
>>   qemu-img create -f qcow2 -b modified.img diff.qcow2
>>   qemu-img rebase -b base.img diff.qcow2
>>
>> Or is there some detail that I'm missing? If it is equivalent, this
>> would suggest that either the new command isn't necessary at all or at
>> least that it should reuse the qemu-img rebase code.
> 
> Yes.  I tried for a while to work out the sequence of commands that
> could make a diff using 'qemu-img rebase', but it wasn't obvious and I
> gave up.  It should at least be documented.  How about the attached
> patch?

Looks good to me. As it already has a SoB, I've applied it to the block
branch. Let me know if I should dequeue it again.

Kevin
Eric Blake - May 21, 2012, 3:30 p.m.
On 05/21/2012 07:59 AM, Richard W.M. Jones wrote:

>>
>> Hm, I'm wondering... If I have a command line like this:
>>
>>   qemu-img diff -b base.img modified.img diff.qcow2
>>
>> Would this be equivalent to this sequence?
>>
>>   qemu-img create -f qcow2 -b modified.img diff.qcow2
>>   qemu-img rebase -b base.img diff.qcow2

So 'qemu-img rebase' is able to rebase to any other image, even if
base.img is nowhere in the backing chain of modified.img?  That means it
is more powerful than runtime 'block-stream' monitor command under qemu,
which can only do a block pull from an earlier point in the backing chain.

> Yes.  I tried for a while to work out the sequence of commands that
> could make a diff using 'qemu-img rebase', but it wasn't obvious and I
> gave up.  It should at least be documented.  How about the attached
> patch?

Seems reasonable to me.  A two-command sequence of qemu-img to
manipulate offline images isn't all that bad; it's different from the
case of a disk image in active use by qemu.
Kevin Wolf - May 21, 2012, 3:40 p.m.
Am 21.05.2012 17:30, schrieb Eric Blake:
> On 05/21/2012 07:59 AM, Richard W.M. Jones wrote:
> 
>>>
>>> Hm, I'm wondering... If I have a command line like this:
>>>
>>>   qemu-img diff -b base.img modified.img diff.qcow2
>>>
>>> Would this be equivalent to this sequence?
>>>
>>>   qemu-img create -f qcow2 -b modified.img diff.qcow2
>>>   qemu-img rebase -b base.img diff.qcow2
> 
> So 'qemu-img rebase' is able to rebase to any other image, even if
> base.img is nowhere in the backing chain of modified.img?  That means it
> is more powerful than runtime 'block-stream' monitor command under qemu,
> which can only do a block pull from an earlier point in the backing chain.

Correct. 'qemu-img rebase' compares the old and the new backing file for
each unallocated cluster of the overlay, and if they differ, it copies
the data from the old backing file into the overlay.

Not sure if there would be any real use for a real live rebase, but even
if there was, implementing it probably wouldn't be a priority for most
of us.

>> Yes.  I tried for a while to work out the sequence of commands that
>> could make a diff using 'qemu-img rebase', but it wasn't obvious and I
>> gave up.  It should at least be documented.  How about the attached
>> patch?
> 
> Seems reasonable to me.  A two-command sequence of qemu-img to
> manipulate offline images isn't all that bad; it's different from the
> case of a disk image in active use by qemu.

Even there you use a two-command sequence when you do an external
'qemu-img create' and then use the 'existing' mode of snapshots or
mirroring. :-)

Kevin

Patch

diff --git a/qemu-img.texi b/qemu-img.texi
index b2ca3a5..6fc3c28 100644
--- a/qemu-img.texi
+++ b/qemu-img.texi
@@ -159,6 +159,24 @@  It can be used without an accessible old backing file, i.e. you can use it to
 fix an image whose backing file has already been moved/renamed.
 @end table
 
+You can use @code{rebase} to perform a ``diff'' operation on two
+disk images.  This can be useful when you have copied or cloned
+a guest, and you want to get back to a thin image on top of a
+template or base image.
+
+Say that @code{base.img} has been cloned as @code{modified.img} by
+copying it, and that the @code{modified.img} guest has run so there
+are now some changes compared to @code{base.img}.  To construct a thin
+image called @code{diff.qcow2} that contains just the differences, do:
+
+@example
+qemu-img create -f qcow2 -b modified.img diff.qcow2
+qemu-img rebase -b base.img diff.qcow2
+@end example
+
+At this point, @code{modified.img} can be discarded, since
+@code{base.img + diff.qcow2} contains the same information.
+
 @item resize @var{filename} [+ | -]@var{size}
 
 Change the disk image as if it had been created with @var{size}.