Skip to contentSkip to conversation

qemu-img: Implement commit like QMP

PATCH Superseded Max Reitz 4 participants 26 messages Apr 8, 2014 – Apr 11, 2014 Block layer

v2 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

v2 cover qemu-img: Implement commit like QMP

qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. For the "commit" command, this is relatively easy, so implement it first (in the hope that indeed others will follow).

As qemu-img does not have access to QMP (due to QMP being intertwined with basically everything in qemu), we cannot directly use QMP, but at least use the functions the corresponding QMP commands are using (which would be "block-commit", in this case).

v2 (mostly following Eric's comments):
- Patch 1:
- allow giving the granularity as 0 in order to enforce the default value
- use is_power_of_2() instead of manually inlining it
- use "if (!has_x) { x = DEFAULT_VALUE; }" (which is the common pattern in blockdev.c) instead of "x = has_x ? x : DEFAULT_VALUE;"
- Patch 2:
- "if (!has_x) { x = DEFAULT_VALUE; }" with a fixed condition
- Patch 3:
- don't use obsolete error_is_set()
- use bs->backing_hd instead of bdrv_find_base(), as the latter actually finds the very base of the backing chain which does not correspond to the current qemu-img commit behavior (which is to commit into the first backing file)
- Added patches 5 and 6

Max Reitz (6):
block-commit: Expose granularity
block-commit: speed is an optional parameter
qemu-img: Implement commit like QMP
qemu-img: Enable progress output for commit
qemu-img: Specify backing file for commit
iotests: Commit tests for two-layer backing chains

 block/Makefile.objs        |    2 +-
 block/commit.c             |   16 +-
 block/mirror.c             |    4 +-
 blockdev.c                 |   26 +-
 include/block/block_int.h  |    6 +-
 qapi-schema.json           |    6 +-
 qemu-img-cmds.hx           |    4 +-
 qemu-img.c                 |  113 ++-
 qemu-img.texi              |    8 +-
 tests/qemu-iotests/020     |   57 +-
 tests/qemu-iotests/020.out | 2168 ++++++++++++++++++++++++++++++++++++++++++++
 11 files changed, 2368 insertions(+), 42 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v2 1/6 block-commit: Expose granularity

Allow QMP users to manipulate the granularity used in the block-commit command.

---
 block/commit.c            | 16 +++++++++++++---
 block/mirror.c            |  4 ++--
 blockdev.c                | 23 +++++++++++++++++------
 include/block/block_int.h |  6 ++++--
 qapi-schema.json          |  6 +++++-
 5 files changed, 41 insertions(+), 14 deletions(-)
block/commit.c +13 -3
index acec4ac..cf0e500 100644
@@ -37,6 +37,7 @@ typedef struct CommitBlockJob {
     BlockdevOnError on_error;
     int base_flags;
     int orig_overlay_flags;
+    int64_t granularity;
 } CommitBlockJob;
 
 static int coroutine_fn commit_populate(BlockDriverState *bs,
@@ -93,7 +94,7 @@ static void coroutine_fn commit_run(void *opaque)
     }
 
     end = s->common.len >> BDRV_SECTOR_BITS;
-    buf = qemu_blockalign(top, COMMIT_BUFFER_SIZE);
+    buf = qemu_blockalign(top, s->granularity);
 
     for (sector_num = 0; sector_num < end; sector_num += n) {
         uint64_t delay_ns = 0;
@@ -109,7 +110,7 @@ wait:
         }
         /* Copy if allocated above the base */
         ret = bdrv_is_allocated_above(top, base, sector_num,
-                                      COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
+                                      s->granularity / BDRV_SECTOR_SIZE,
                                       &n);
         copy = (ret == 1);
         trace_commit_one_iteration(s, sector_num, n, ret);
@@ -180,7 +181,7 @@ static const BlockJobDriver commit_job_driver = {
 };
 
 void commit_start(BlockDriverState *bs, BlockDriverState *base,
-                  BlockDriverState *top, int64_t speed,
+                  BlockDriverState *top, int64_t speed, int64_t granularity,
                   BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
                   void *opaque, Error **errp)
 {
@@ -214,6 +215,13 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
     orig_base_flags    = bdrv_get_flags(base);
     orig_overlay_flags = bdrv_get_flags(overlay_bs);
 
+    if (!granularity) {
+        granularity = COMMIT_BUFFER_SIZE;
+    }
+
+    assert(granularity >= BDRV_SECTOR_SIZE);
+    assert(is_power_of_2(granularity));
+
     /* convert base & overlay_bs to r/w, if necessary */
     if (!(orig_base_flags & BDRV_O_RDWR)) {
         reopen_queue = bdrv_reopen_queue(reopen_queue, base,
@@ -244,6 +252,8 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
     s->base_flags          = orig_base_flags;
     s->orig_overlay_flags  = orig_overlay_flags;
 
+    s->granularity = granularity;
+
     s->on_error = on_error;
     s->common.co = qemu_coroutine_create(commit_run);
 
block/mirror.c +2 -2
index 0ef41f9..5b1ebb2 100644
@@ -632,7 +632,7 @@ void mirror_start(BlockDriverState *bs, BlockDriverState *target,
 }
 
 void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
-                         int64_t speed,
+                         int64_t speed, int64_t granularity,
                          BlockdevOnError on_error,
                          BlockDriverCompletionFunc *cb,
                          void *opaque, Error **errp)
@@ -674,7 +674,7 @@ void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
     }
 
     bdrv_ref(base);
-    mirror_start_job(bs, base, speed, 0, 0,
+    mirror_start_job(bs, base, speed, granularity, 0,
                      on_error, on_error, cb, opaque, &local_err,
                      &commit_active_job_driver, false, base);
     if (error_is_set(&local_err)) {
blockdev.c +17 -6
index c3422a1..b988cc5 100644
@@ -1865,8 +1865,8 @@ void qmp_block_stream(const char *device, bool has_base,
 
 void qmp_block_commit(const char *device,
                       bool has_base, const char *base, const char *top,
-                      bool has_speed, int64_t speed,
-                      Error **errp)
+                      bool has_speed, int64_t speed, bool has_granularity,
+                      int64_t granularity, Error **errp)
 {
     BlockDriverState *bs;
     BlockDriverState *base_bs, *top_bs;
@@ -1876,6 +1876,17 @@ void qmp_block_commit(const char *device,
      */
     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
 
+    if (!has_granularity) {
+        granularity = 0;
+    }
+
+    if (granularity &&
+        (granularity < BDRV_SECTOR_SIZE || !is_power_of_2(granularity)))
+    {
+        error_set(errp, QERR_INVALID_PARAMETER, "granularity");
+        return;
+    }
+
     /* drain all i/o before commits */
     bdrv_drain_all();
 
@@ -1911,11 +1922,11 @@ void qmp_block_commit(const char *device,
     }
 
     if (top_bs == bs) {
-        commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
-                            bs, &local_err);
+        commit_active_start(bs, base_bs, speed, granularity, on_error,
+                            block_job_cb, bs, &local_err);
     } else {
-        commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
-                    &local_err);
+        commit_start(bs, base_bs, top_bs, speed, granularity, on_error,
+                     block_job_cb, bs, &local_err);
     }
     if (local_err != NULL) {
         error_propagate(errp, local_err);
include/block/block_int.h +4 -2
index cd5bc73..2e4b470 100644
@@ -426,6 +426,7 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
  * @top: Top block device to be committed.
  * @base: Block device that will be written into, and become the new top.
  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
+ * @granularity: The granularity, in bytes, or 0 for a default value.
  * @on_error: The action to take upon error.
  * @cb: Completion function for the job.
  * @opaque: Opaque pointer value passed to @cb.
@@ -433,7 +434,7 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
  *
  */
 void commit_start(BlockDriverState *bs, BlockDriverState *base,
-                 BlockDriverState *top, int64_t speed,
+                 BlockDriverState *top, int64_t speed, int64_t granularity,
                  BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
                  void *opaque, Error **errp);
 /**
@@ -441,6 +442,7 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
  * @bs: Active block device to be committed.
  * @base: Block device that will be written into, and become the new top.
  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
+ * @granularity: The granularity, in bytes, or 0 for cluster size.
  * @on_error: The action to take upon error.
  * @cb: Completion function for the job.
  * @opaque: Opaque pointer value passed to @cb.
@@ -448,7 +450,7 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
  *
  */
 void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
-                         int64_t speed,
+                         int64_t speed, int64_t granularity,
                          BlockdevOnError on_error,
                          BlockDriverCompletionFunc *cb,
                          void *opaque, Error **errp);
qapi-schema.json +5 -1
index 391356f..151fad3 100644
@@ -2112,6 +2112,10 @@
 #
 # @speed:  #optional the maximum speed, in bytes per second
 #
+# @granularity: #optional the granularity to be used for the operation, in
+#               bytes; has to be a power of two and at least 512, or 0 for the
+#               default value (since 2.1)
+#
 # Returns: Nothing on success
 #          If commit or stream is already active on this device, DeviceInUse
 #          If @device does not exist, DeviceNotFound
@@ -2124,7 +2128,7 @@
 ##
 { 'command': 'block-commit',
   'data': { 'device': 'str', '*base': 'str', 'top': 'str',
-            '*speed': 'int' } }
+            '*speed': 'int', '*granularity': 'int' } }
 
 ##
 # @drive-backup
Signature · 1 line
1.9.1
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/6] block-commit: Expose granularity

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 4 lines

Allow QMP users to manipulate the granularity used in the block-commit command.

Granularity is a strange name for live commit, especially on non-active layers where it's really just the buffer size. Not that I have a better suggestion, though...

Kevin

Reviewed-by on v2 1/6
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/6] block-commit: Expose granularity

In reply to Max Reitz
Quoted 17 lines · Max Reitz

Allow QMP users to manipulate the granularity used in the block-commit command.

---

+++ b/include/block/block_int.h

include/block/block_int.h +1 -0
@@ -426,6 +426,7 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
  * @top: Top block device to be committed.
  * @base: Block device that will be written into, and become the new top.
  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
+ * @granularity: The granularity, in bytes, or 0 for a default value.
  * @on_error: The action to take upon error.
  * @cb: Completion function for the job.
  * @opaque: Opaque pointer value passed to @cb.

include/block/block_int.h void stream_start(BlockDriverState *bs, BlockDriverState *base, · line 439

Quoted 7 lines
include/block/block_int.h +1 -1
@@ -433,7 +434,7 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
  *
  */
 void commit_start(BlockDriverState *bs, BlockDriverState *base,
-                 BlockDriverState *top, int64_t speed,
+                 BlockDriverState *top, int64_t speed, int64_t granularity,
                  BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
                  void *opaque, Error **errp);

Worth fixing the indentation while you are touching this?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/6] block-commit: Expose granularity

Quoted 17 lines · Eric Blake
Quoted 16 lines · Max Reitz

Allow QMP users to manipulate the granularity used in the block-commit command.

---

+++ b/include/block/block_int.h

include/block/block_int.h +1 -0
@@ -426,6 +426,7 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
   * @top: Top block device to be committed.
   * @base: Block device that will be written into, and become the new top.
   * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
+ * @granularity: The granularity, in bytes, or 0 for a default value.
   * @on_error: The action to take upon error.
   * @cb: Completion function for the job.
   * @opaque: Opaque pointer value passed to @cb.
@@ -433,7 +434,7 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
Quoted 8 lines

include/block/block_int.h void stream_start(BlockDriverState *bs, BlockDriverState *base, · line 439

Quoted 7 lines
include/block/block_int.h +1 -1
   *
   */
  void commit_start(BlockDriverState *bs, BlockDriverState *base,
-                 BlockDriverState *top, int64_t speed,
+                 BlockDriverState *top, int64_t speed, int64_t granularity,
                   BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
                   void *opaque, Error **errp);

Worth fixing the indentation while you are touching this?

I'm always in favor of code style clean-up. ;-)

Max

Quoted 1 line
author
#5 lore raw

PATCH v2 2/6 block-commit: speed is an optional parameter

In reply to Max Reitz

As speed is an optional parameter for the QMP block-commit command, it should be set to 0 if not given (as it is undefined if has_speed is false), that is, the speed should not be limited.

---
 blockdev.c | 3 +++
 1 file changed, 3 insertions(+)
blockdev.c +3 -0
index b988cc5..9d7bd04 100644
@@ -1876,6 +1876,9 @@ void qmp_block_commit(const char *device,
      */
     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
 
+    if (!has_speed) {
+        speed = 0;
+    }
     if (!has_granularity) {
         granularity = 0;
     }
Signature · 1 line
1.9.1
#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] block-commit: speed is an optional parameter

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 5 lines

As speed is an optional parameter for the QMP block-commit command, it should be set to 0 if not given (as it is undefined if has_speed is false), that is, the speed should not be limited.

Should this be Cc: [email protected]?

Kevin

Quoted 20 lines
 blockdev.c | 3 +++
 1 file changed, 3 insertions(+)
blockdev.c +3 -0
index b988cc5..9d7bd04 100644
@@ -1876,6 +1876,9 @@ void qmp_block_commit(const char *device,
      */
     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
 
+    if (!has_speed) {
+        speed = 0;
+    }
     if (!has_granularity) {
         granularity = 0;
     }
Signature · 1 line
1.9.1
author
#7 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] block-commit: speed is an optional parameter

Quoted 7 lines · Kevin Wolf

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 5 lines

As speed is an optional parameter for the QMP block-commit command, it should be set to 0 if not given (as it is undefined if has_speed is false), that is, the speed should not be limited.

Should this be Cc: [email protected]?

Probably, yes. I'll swap this and the first patch for this, so there are no contextual conflicts; or even better, I'll exclude this patch from this series.

Max

Quoted 22 lines

Kevin

Quoted 20 lines
  blockdev.c | 3 +++
  1 file changed, 3 insertions(+)
blockdev.c +3 -0
index b988cc5..9d7bd04 100644
@@ -1876,6 +1876,9 @@ void qmp_block_commit(const char *device,
       */
      BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
 
+    if (!has_speed) {
+        speed = 0;
+    }
      if (!has_granularity) {
          granularity = 0;
      }
Signature · 1 line
1.9.1
Reviewed-by on v2 2/6
#8 lore raw

Re: [Qemu-devel] [PATCH v2 2/6] block-commit: speed is an optional parameter

In reply to Max Reitz
Quoted 8 lines · Max Reitz

As speed is an optional parameter for the QMP block-commit command, it should be set to 0 if not given (as it is undefined if has_speed is false), that is, the speed should not be limited.

---
 blockdev.c | 3 +++
 1 file changed, 3 insertions(+)
Quoted 16 lines
blockdev.c +3 -0
index b988cc5..9d7bd04 100644
@@ -1876,6 +1876,9 @@ void qmp_block_commit(const char *device,
      */
     BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT;
 
+    if (!has_speed) {
+        speed = 0;
+    }
     if (!has_granularity) {
         granularity = 0;
     }
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#9 lore raw

PATCH v2 3/6 qemu-img: Implement commit like QMP

In reply to Max Reitz

qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. As qemu-img itself has no access to QMP (since this would basically require just everything being linked into qemu-img), imitate QMP's implementation of block-commit by using commit_active_start() and then waiting for the block job to finish.

This new implementation does not empty the snapshot image, as opposed to the old implementation using bdrv_commit(). However, as QMP's block-commit apparently never did this and as qcow2 (which is probably qemu's standard image format) does not even implement the required function (bdrv_make_empty()), it does not seem necessary.

---
 block/Makefile.objs |  2 +-
 qemu-img.c          | 70 ++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 52 insertions(+), 20 deletions(-)
block/Makefile.objs +1 -1
index fd88c03..2c37e80 100644
@@ -9,6 +9,7 @@ block-obj-y += snapshot.o qapi.o
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += raw-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
+block-obj-y += mirror.o
 
 ifeq ($(CONFIG_POSIX),y)
 block-obj-y += nbd.o nbd-client.o sheepdog.o
@@ -22,7 +23,6 @@ endif
 
 common-obj-y += stream.o
 common-obj-y += commit.o
-common-obj-y += mirror.o
 common-obj-y += backup.o
 
 iscsi.o-cflags     := $(LIBISCSI_CFLAGS)
qemu-img.c +51 -19

98 lines not on this page · raw message

Signature · 1 line
1.9.1
#10 lore raw

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 6 lines

qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. As qemu-img itself has no access to QMP (since this would basically require just everything being linked into qemu-img), imitate QMP's implementation of block-commit by using commit_active_start() and then waiting for the block job to finish.

Leaves us with the HMP commit command that uses the old bdrv_commit() function. I wonder if we can get rid of it by letting the HMP command stop the VM, do a live commit, and then restart the VM.

Quoted 5 lines

This new implementation does not empty the snapshot image, as opposed to the old implementation using bdrv_commit(). However, as QMP's block-commit apparently never did this and as qcow2 (which is probably qemu's standard image format) does not even implement the required function (bdrv_make_empty()), it does not seem necessary.

In fact, I think since qcow2 has discard support it would actually be possible to write a sensible implementation of bdrv_make_empty(). That's a separate feature, though, and can go in a different patch series.

Quoted 39 lines
---
 block/Makefile.objs |  2 +-
 qemu-img.c          | 70 ++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 52 insertions(+), 20 deletions(-)
block/Makefile.objs +1 -1
index fd88c03..2c37e80 100644
@@ -9,6 +9,7 @@ block-obj-y += snapshot.o qapi.o
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += raw-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
+block-obj-y += mirror.o
 
 ifeq ($(CONFIG_POSIX),y)
 block-obj-y += nbd.o nbd-client.o sheepdog.o
@@ -22,7 +23,6 @@ endif
 
 common-obj-y += stream.o
 common-obj-y += commit.o
-common-obj-y += mirror.o
 common-obj-y += backup.o
 
 iscsi.o-cflags     := $(LIBISCSI_CFLAGS)
qemu-img.c +1 -0
index 8455994..e86911f 100644
@@ -30,6 +30,7 @@
 #include "qemu/osdep.h"
 #include "sysemu/sysemu.h"
 #include "block/block_int.h"
+#include "block/blockjob.h"
 #include "block/qapi.h"
 #include <getopt.h>
 

qemu-img.c fail: · line 688

Quoted 6 lines
qemu-img.c +3 -0
@@ -682,12 +683,37 @@ fail:
     return ret;
 }
 
+static void dummy_block_job_cb(void *opaque, int ret)
+{
+}

Why don't we need to check the return value?

Kevin

#11 lore raw
Quoted 11 lines · Kevin Wolf

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 6 lines

qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. As qemu-img itself has no access to QMP (since this would basically require just everything being linked into qemu-img), imitate QMP's implementation of block-commit by using commit_active_start() and then waiting for the block job to finish.

Leaves us with the HMP commit command that uses the old bdrv_commit() function. I wonder if we can get rid of it by letting the HMP command stop the VM, do a live commit, and then restart the VM.

Not a bad idea, actually. Of course, as additional patches.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#12 lore raw
In reply to Kevin Wolf
Quoted 58 lines · Kevin Wolf

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 6 lines

qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. As qemu-img itself has no access to QMP (since this would basically require just everything being linked into qemu-img), imitate QMP's implementation of block-commit by using commit_active_start() and then waiting for the block job to finish.

Leaves us with the HMP commit command that uses the old bdrv_commit() function. I wonder if we can get rid of it by letting the HMP command stop the VM, do a live commit, and then restart the VM.

Quoted 5 lines

This new implementation does not empty the snapshot image, as opposed to the old implementation using bdrv_commit(). However, as QMP's block-commit apparently never did this and as qcow2 (which is probably qemu's standard image format) does not even implement the required function (bdrv_make_empty()), it does not seem necessary.

In fact, I think since qcow2 has discard support it would actually be possible to write a sensible implementation of bdrv_make_empty(). That's a separate feature, though, and can go in a different patch series.

Quoted 38 lines
---
  block/Makefile.objs |  2 +-
  qemu-img.c          | 70 ++++++++++++++++++++++++++++++++++++++---------------
  2 files changed, 52 insertions(+), 20 deletions(-)
block/Makefile.objs +1 -1
index fd88c03..2c37e80 100644
@@ -9,6 +9,7 @@ block-obj-y += snapshot.o qapi.o
  block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
  block-obj-$(CONFIG_POSIX) += raw-posix.o
  block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
+block-obj-y += mirror.o
 
  ifeq ($(CONFIG_POSIX),y)
  block-obj-y += nbd.o nbd-client.o sheepdog.o
@@ -22,7 +23,6 @@ endif
 
  common-obj-y += stream.o
  common-obj-y += commit.o
-common-obj-y += mirror.o
  common-obj-y += backup.o
 
  iscsi.o-cflags     := $(LIBISCSI_CFLAGS)
qemu-img.c +1 -0
index 8455994..e86911f 100644
@@ -30,6 +30,7 @@
  #include "qemu/osdep.h"
  #include "sysemu/sysemu.h"
  #include "block/block_int.h"
+#include "block/blockjob.h"
  #include "block/qapi.h"
  #include <getopt.h>
 
Quoted 8 lines

qemu-img.c fail: · line 688

Quoted 7 lines
qemu-img.c +3 -0
@@ -682,12 +683,37 @@ fail:
      return ret;
  }
 
+static void dummy_block_job_cb(void *opaque, int ret)
+{
+}

Why don't we need to check the return value?

I didn't check it, because it was not called – which apparently didn't sound strange to me at all. I checked and the much more interesting fact is that I assumed block_job_complete() would actually complete the block job without any further need for aio_poll(); but it doesn't. I'll fix both things (calling aio_poll() until the CB is called and checking the return value).

Max

Quoted 1 line

Kevin

#13 lore raw

Am 10.04.2014 um 16:32 hat Max Reitz geschrieben:

Quoted 63 lines · Kevin Wolf
Quoted 53 lines

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 6 lines

qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. As qemu-img itself has no access to QMP (since this would basically require just everything being linked into qemu-img), imitate QMP's implementation of block-commit by using commit_active_start() and then waiting for the block job to finish.

Leaves us with the HMP commit command that uses the old bdrv_commit() function. I wonder if we can get rid of it by letting the HMP command stop the VM, do a live commit, and then restart the VM.

Quoted 5 lines

This new implementation does not empty the snapshot image, as opposed to the old implementation using bdrv_commit(). However, as QMP's block-commit apparently never did this and as qcow2 (which is probably qemu's standard image format) does not even implement the required function (bdrv_make_empty()), it does not seem necessary.

In fact, I think since qcow2 has discard support it would actually be possible to write a sensible implementation of bdrv_make_empty(). That's a separate feature, though, and can go in a different patch series.

Quoted 33 lines
---
 block/Makefile.objs |  2 +-
 qemu-img.c          | 70 ++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 52 insertions(+), 20 deletions(-)
block/Makefile.objs +1 -1
index fd88c03..2c37e80 100644
@@ -9,6 +9,7 @@ block-obj-y += snapshot.o qapi.o
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += raw-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
+block-obj-y += mirror.o
 ifeq ($(CONFIG_POSIX),y)
 block-obj-y += nbd.o nbd-client.o sheepdog.o
@@ -22,7 +23,6 @@ endif
 common-obj-y += stream.o
 common-obj-y += commit.o
-common-obj-y += mirror.o
 common-obj-y += backup.o
 iscsi.o-cflags     := $(LIBISCSI_CFLAGS)
qemu-img.c +1 -0
index 8455994..e86911f 100644
@@ -30,6 +30,7 @@
 #include "qemu/osdep.h"
 #include "sysemu/sysemu.h"
 #include "block/block_int.h"
+#include "block/blockjob.h"
 #include "block/qapi.h"
Quoted 8 lines

qemu-img.c fail: · line 687

Quoted 7 lines
qemu-img.c +3 -0
 #include <getopt.h>
@@ -682,12 +683,37 @@ fail:
     return ret;
 }
+static void dummy_block_job_cb(void *opaque, int ret)
+{
+}

Why don't we need to check the return value?

Quoted 6 lines

I didn't check it, because it was not called – which apparently didn't sound strange to me at all. I checked and the much more interesting fact is that I assumed block_job_complete() would actually complete the block job without any further need for aio_poll(); but it doesn't. I'll fix both things (calling aio_poll() until the CB is called and checking the return value).

There is a block_job_cancel_sync(). Should we add a new block_job_complete_sync() instead of adding the polling logic to qemu-img?

Kevin

author
#14 lore raw

PATCH v2 4/6 qemu-img: Enable progress output for commit

In reply to Max Reitz

Implement progress output for the commit command by querying the progress of the block job.

---
 qemu-img-cmds.hx |  4 ++--
 qemu-img.c       | 33 +++++++++++++++++++++++++++++++--
 qemu-img.texi    |  2 +-
 3 files changed, 34 insertions(+), 5 deletions(-)
qemu-img-cmds.hx +2 -2
index d029609..8bc55cd 100644
@@ -22,9 +22,9 @@ STEXI
 ETEXI
 
 DEF("commit", img_commit,
-    "commit [-q] [-f fmt] [-t cache] filename")
+    "commit [-q] [-f fmt] [-t cache] [-p] filename")
 STEXI
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
 ETEXI
 
 DEF("compare", img_compare,
qemu-img.c +31 -2
index e86911f..0a9eff7 100644
@@ -690,12 +690,27 @@ static void dummy_block_job_cb(void *opaque, int ret)
 static void run_block_job(BlockJob *job, Error **errp)
 {
     BlockJobInfo *info;
+    uint64_t mod_offset = 0;
 
     do {
         aio_poll(qemu_get_aio_context(), true);
 
         info = block_job_query(job);
 
+        if (info->offset) {
+            if (!mod_offset) {
+                /* Some block jobs (at least "commit") will only work on a
+                 * subset of the image file and therefore basically skip many
+                 * sectors at the start (processing them apparently
+                 * instantaneously). These sectors should be ignored when
+                 * calculating the progress. */
+                mod_offset = info->offset;
+            }
+
+            qemu_progress_print((float)(info->offset - mod_offset) /
+                                (info->len - mod_offset) * 100.f, 0);
+        }
+
         if (!info->busy && info->offset < info->len) {
             block_job_resume(job);
         }
@@ -712,13 +727,13 @@ static int img_commit(int argc, char **argv)
     int c, ret, flags;
     const char *filename, *fmt, *cache;
     BlockDriverState *bs, *base_bs;
-    bool quiet = false;
+    bool progress = false, quiet = false;
     Error *local_err = NULL;
 
     fmt = NULL;
     cache = BDRV_DEFAULT_CACHE;
     for(;;) {
-        c = getopt(argc, argv, "f:ht:q");
+        c = getopt(argc, argv, "f:ht:qp");
         if (c == -1) {
             break;
         }
@@ -733,11 +748,20 @@ static int img_commit(int argc, char **argv)
         case 't':
             cache = optarg;
             break;
+        case 'p':
+            progress = true;
+            break;
         case 'q':
             quiet = true;
             break;
         }
     }
+
+    /* Progress is not shown in Quiet mode */
+    if (quiet) {
+        progress = false;
+    }
+
     if (optind != argc - 1) {
         help();
     }
@@ -755,6 +779,9 @@ static int img_commit(int argc, char **argv)
         return 1;
     }
 
+    qemu_progress_init(progress, 1.f);
+    qemu_progress_print(0.f, 100);
+
     /* This is different from QMP, which by default uses the deepest file in the
      * backing chain (i.e., the very base); however, the traditional behavior of
      * qemu-img commit is using the immediate backing file. */
@@ -774,6 +801,8 @@ static int img_commit(int argc, char **argv)
     run_block_job(bs->job, &local_err);
 
 done:
+    qemu_progress_end();
+
     bdrv_unref(bs);
 
     if (local_err) {
qemu-img.texi +1 -1
index f84590e..1a9c08f 100644
@@ -140,7 +140,7 @@ this case. @var{backing_file} will never be modified unless you use the
 The size can also be specified using the @var{size} option with @code{-o},
 it doesn't need to be specified separately in this case.
 
-@item commit [-f @var{fmt}] [-t @var{cache}] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
 
 Commit the changes recorded in @var{filename} in its base image or backing file.
 If the backing file is smaller than the snapshot, then the backing file will be
Signature · 1 line
1.9.1
#15 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] qemu-img: Enable progress output for commit

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 34 lines

Implement progress output for the commit command by querying the progress of the block job.

---
 qemu-img-cmds.hx |  4 ++--
 qemu-img.c       | 33 +++++++++++++++++++++++++++++++--
 qemu-img.texi    |  2 +-
 3 files changed, 34 insertions(+), 5 deletions(-)
qemu-img-cmds.hx +2 -2
index d029609..8bc55cd 100644
@@ -22,9 +22,9 @@ STEXI
 ETEXI
 
 DEF("commit", img_commit,
-    "commit [-q] [-f fmt] [-t cache] filename")
+    "commit [-q] [-f fmt] [-t cache] [-p] filename")
 STEXI
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
 ETEXI
 
 DEF("compare", img_compare,
qemu-img.c +0 -0
index e86911f..0a9eff7 100644
@@ -690,12 +690,27 @@ static void dummy_block_job_cb(void *opaque, int ret)
 static void run_block_job(BlockJob *job, Error **errp)
 {

qemu-img.c static void dummy_block_job_cb(void *opaque, int ret) · line 701

Quoted 10 lines
qemu-img.c +3 -0
     BlockJobInfo *info;
+    uint64_t mod_offset = 0;
 
     do {
         aio_poll(qemu_get_aio_context(), true);
 
         info = block_job_query(job);
 
+        if (info->offset) {
+            if (!mod_offset) {

On a fully populated image this doesn't look entirely right. I think the first 2 MB (or whatever the buffer size is) will be disregarded in the calculation, even though they are real work that is done.

Quoted 5 lines
qemu-img.c +5 -0
+                /* Some block jobs (at least "commit") will only work on a
+                 * subset of the image file and therefore basically skip many
+                 * sectors at the start (processing them apparently
+                 * instantaneously). These sectors should be ignored when
+                 * calculating the progress. */

qemu-img.c static void dummy_block_job_cb(void *opaque, int ret) · line 716

Quoted 10 lines
qemu-img.c +7 -0
+                mod_offset = info->offset;
+            }
+
+            qemu_progress_print((float)(info->offset - mod_offset) /
+                                (info->len - mod_offset) * 100.f, 0);
+        }
+
         if (!info->busy && info->offset < info->len) {
             block_job_resume(job);
         }

Kevin

#16 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] qemu-img: Enable progress output for commit

Quoted 9 lines · Kevin Wolf

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 8 lines

Implement progress output for the commit command by querying the progress of the block job.

---

Quoted 8 lines

qemu-img.c static void dummy_block_job_cb(void *opaque, int ret) · line 701

Quoted 4 lines
         info = block_job_query(job);
qemu-img.c +2 -0
+        if (info->offset) {
+            if (!mod_offset) {

On a fully populated image this doesn't look entirely right. I think the first 2 MB (or whatever the buffer size is) will be disregarded in the calculation, even though they are real work that is done.

Is there any (easy) way to calculate how many dirty sectors remain to be committed, to use that rather than the image size as the job percentage remaining?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#17 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] qemu-img: Enable progress output for commit

Am 08.04.2014 um 18:53 hat Eric Blake geschrieben:

Quoted 15 lines · Kevin Wolf
Quoted 14 lines

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 12 lines

Implement progress output for the commit command by querying the progress of the block job.

---

         info = block_job_query(job);
qemu-img.c +2 -0
+        if (info->offset) {
+            if (!mod_offset) {
Quoted 7 lines
Quoted 3 lines

On a fully populated image this doesn't look entirely right. I think the first 2 MB (or whatever the buffer size is) will be disregarded in the calculation, even though they are real work that is done.

Is there any (easy) way to calculate how many dirty sectors remain to be committed, to use that rather than the image size as the job percentage remaining?

The very first thing that mirror_run() does is building the dirty bitmap depending on whether clusters are allocated or not. After this loop we have the necessary information, but we don't really communicate it to the caller. Max tries to guess the point at which the loop completed by checking when we make progress for the first time, but that means that we ignore the first actual buffer.

We could probably simply change mirror_run() to update s->common.len by subtracting the parts that haven't been marked dirty. Then the caller could use the values as they are, and query-block-jobs would get more accurate values for mirroring jobs, too.

commit_run(), for a commit somewhere down in the backing chain, we don't have this initialising loop yet, but we could add it without adding too much overhead, I think. qemu-img convert was changed in December to do the same and noone has complained yet (though it's not in a release yet, perhaps people will complain once 2.0 is out).

Kevin

author
#18 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] qemu-img: Enable progress output for commit

In reply to Kevin Wolf
Quoted 41 lines · Kevin Wolf

Am 08.04.2014 um 14:50 hat Max Reitz geschrieben:

Quoted 40 lines

Implement progress output for the commit command by querying the progress of the block job.

---
  qemu-img-cmds.hx |  4 ++--
  qemu-img.c       | 33 +++++++++++++++++++++++++++++++--
  qemu-img.texi    |  2 +-
  3 files changed, 34 insertions(+), 5 deletions(-)
qemu-img-cmds.hx +2 -2
index d029609..8bc55cd 100644
@@ -22,9 +22,9 @@ STEXI
  ETEXI
 
  DEF("commit", img_commit,
-    "commit [-q] [-f fmt] [-t cache] filename")
+    "commit [-q] [-f fmt] [-t cache] [-p] filename")
  STEXI
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
  ETEXI
 
  DEF("compare", img_compare,
qemu-img.c +1 -0
index e86911f..0a9eff7 100644
@@ -690,12 +690,27 @@ static void dummy_block_job_cb(void *opaque, int ret)
  static void run_block_job(BlockJob *job, Error **errp)
  {
      BlockJobInfo *info;
+    uint64_t mod_offset = 0;
 
      do {
          aio_poll(qemu_get_aio_context(), true);
 
Quoted 7 lines

qemu-img.c static void dummy_block_job_cb(void *opaque, int ret) · line 701

Quoted 4 lines
qemu-img.c +2 -0
          info = block_job_query(job);
 
+        if (info->offset) {
+            if (!mod_offset) {

On a fully populated image this doesn't look entirely right. I think the first 2 MB (or whatever the buffer size is) will be disregarded in the calculation, even though they are real work that is done.

Hm, right. I'll see how I get it included into this supposedly common function.

Max

Quoted 16 lines
Quoted 5 lines
qemu-img.c +5 -0
+                /* Some block jobs (at least "commit") will only work on a
+                 * subset of the image file and therefore basically skip many
+                 * sectors at the start (processing them apparently
+                 * instantaneously). These sectors should be ignored when
+                 * calculating the progress. */

qemu-img.c static void dummy_block_job_cb(void *opaque, int ret) · line 716

Quoted 10 lines
qemu-img.c +7 -0
+                mod_offset = info->offset;
+            }
+
+            qemu_progress_print((float)(info->offset - mod_offset) /
+                                (info->len - mod_offset) * 100.f, 0);
+        }
+
          if (!info->busy && info->offset < info->len) {
              block_job_resume(job);
          }

Kevin

author
#19 lore raw

PATCH v2 5/6 qemu-img: Specify backing file for commit

In reply to Max Reitz

Introduce a new parameter for qemu-img commit which may be used to explicitly specify the backing file unto which an image should be committed if the backing chain has more than a single layer.

---
 qemu-img-cmds.hx |  4 ++--
 qemu-img.c       | 22 +++++++++++++++-------
 qemu-img.texi    |  8 +++++++-
 3 files changed, 24 insertions(+), 10 deletions(-)
qemu-img-cmds.hx +2 -2
index 8bc55cd..7f62f6d 100644
@@ -22,9 +22,9 @@ STEXI
 ETEXI
 
 DEF("commit", img_commit,
-    "commit [-q] [-f fmt] [-t cache] [-p] filename")
+    "commit [-q] [-f fmt] [-t cache] [-b backing_file] [-p] filename")
 STEXI
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{backing_file}] [-p] @var{filename}
 ETEXI
 
 DEF("compare", img_compare,
qemu-img.c +15 -7
index 0a9eff7..9d4bdbc 100644
@@ -725,15 +725,16 @@ static void run_block_job(BlockJob *job, Error **errp)
 static int img_commit(int argc, char **argv)
 {
     int c, ret, flags;
-    const char *filename, *fmt, *cache;
+    const char *filename, *fmt, *cache, *base;
     BlockDriverState *bs, *base_bs;
     bool progress = false, quiet = false;
     Error *local_err = NULL;
 
     fmt = NULL;
     cache = BDRV_DEFAULT_CACHE;
+    base = NULL;
     for(;;) {
-        c = getopt(argc, argv, "f:ht:qp");
+        c = getopt(argc, argv, "f:ht:b:qp");
         if (c == -1) {
             break;
         }
@@ -748,6 +749,9 @@ static int img_commit(int argc, char **argv)
         case 't':
             cache = optarg;
             break;
+        case 'b':
+            base = optarg;
+            break;
         case 'p':
             progress = true;
             break;
@@ -782,12 +786,16 @@ static int img_commit(int argc, char **argv)
     qemu_progress_init(progress, 1.f);
     qemu_progress_print(0.f, 100);
 
-    /* This is different from QMP, which by default uses the deepest file in the
-     * backing chain (i.e., the very base); however, the traditional behavior of
-     * qemu-img commit is using the immediate backing file. */
-    base_bs = bs->backing_hd;
+    if (base) {
+        base_bs = bdrv_find_backing_image(bs, base);
+    } else {
+        /* This is different from QMP, which by default uses the deepest file in
+         * the backing chain (i.e., the very base); however, the traditional
+         * behavior of qemu-img commit is using the immediate backing file. */
+        base_bs = bs->backing_hd;
+    }
     if (!base_bs) {
-        error_set(&local_err, QERR_BASE_NOT_FOUND, "NULL");
+        error_set(&local_err, QERR_BASE_NOT_FOUND, base ?: "NULL");
         goto done;
     }
 
qemu-img.texi +7 -1
index 1a9c08f..4a9f493 100644
@@ -140,7 +140,7 @@ this case. @var{backing_file} will never be modified unless you use the
 The size can also be specified using the @var{size} option with @code{-o},
 it doesn't need to be specified separately in this case.
 
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{backing_file}] [-p] @var{filename}
 
 Commit the changes recorded in @var{filename} in its base image or backing file.
 If the backing file is smaller than the snapshot, then the backing file will be
@@ -149,6 +149,12 @@ the backing file, the backing file will not be truncated.  If you want the
 backing file to match the size of the smaller snapshot, you can safely truncate
 it yourself once the commit operation successfully completes.
 
+If the backing chain of the given image file @var{filename} has more than one
+layer, the backing file unto which the changes shall be committed may be
+specified as @var{backing_file} (which has to be part of @var{filename}'s
+backing chain). If @var{filename} is not specified, the immediate backing file
+of the top image (which is @var{filename}) will be used.
+
 @item compare [-f @var{fmt}] [-F @var{fmt}] [-p] [-s] [-q] @var{filename1} @var{filename2}
 
 Check if two images have the same content. You can compare images with
Signature · 1 line
1.9.1
Reviewed-by on v2 5/6
#20 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] qemu-img: Specify backing file for commit

Quoted 2 lines · Max Reitz

Introduce a new parameter for qemu-img commit which may be used to explicitly specify the backing file unto which an image should be

s/unto/into/

qemu-img.texi the backing file, the backing file will not be truncated. If you want the · line 153

Quoted 12 lines

committed if the backing chain has more than a single layer.

---
 qemu-img-cmds.hx |  4 ++--
 qemu-img.c       | 22 +++++++++++++++-------
 qemu-img.texi    |  8 +++++++-
 3 files changed, 24 insertions(+), 10 deletions(-)
qemu-img.texi +2 -0
+If the backing chain of the given image file @var{filename} has more than one
+layer, the backing file unto which the changes shall be committed may be

s/unto/into/
s/shall/will/

qemu-img.texi the backing file, the backing file will not be truncated. If you want the · line 157

Quoted 4 lines
qemu-img.texi +4 -0
+specified as @var{backing_file} (which has to be part of @var{filename}'s
+backing chain). If @var{filename} is not specified, the immediate backing file
+of the top image (which is @var{filename}) will be used.
+

With those changes,

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#21 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] qemu-img: Specify backing file for commit

Quoted 4 lines · Eric Blake
Quoted 2 lines · Max Reitz

Introduce a new parameter for qemu-img commit which may be used to explicitly specify the backing file unto which an image should be

s/unto/into/

I was wondering about that and asked someone about it (German as well, however), who said “unto” would be correct. And since you did not complain in v1… ;-)

I'll fix it.

Quoted 9 lines
Quoted 8 lines

committed if the backing chain has more than a single layer.

---
  qemu-img-cmds.hx |  4 ++--
  qemu-img.c       | 22 +++++++++++++++-------
  qemu-img.texi    |  8 +++++++-
  3 files changed, 24 insertions(+), 10 deletions(-)
Quoted 4 lines

qemu-img.texi the backing file, the backing file will not be truncated. If you want the · line 153

Quoted 2 lines
qemu-img.texi +2 -0
+If the backing chain of the given image file @var{filename} has more than one
+layer, the backing file unto which the changes shall be committed may be

s/unto/into/
s/shall/will/

As long as there are no bugs, right. *g*

Quoted 7 lines

qemu-img.texi the backing file, the backing file will not be truncated. If you want the · line 157

Quoted 4 lines
qemu-img.texi +4 -0
+specified as @var{backing_file} (which has to be part of @var{filename}'s
+backing chain). If @var{filename} is not specified, the immediate backing file
+of the top image (which is @var{filename}) will be used.
+

With those changes,

Reviewed-by on v2 5/6
#22 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] qemu-img: Specify backing file for commit

In reply to Max Reitz
Quoted 96 lines · Max Reitz

Introduce a new parameter for qemu-img commit which may be used to explicitly specify the backing file unto which an image should be committed if the backing chain has more than a single layer.

---
 qemu-img-cmds.hx |  4 ++--
 qemu-img.c       | 22 +++++++++++++++-------
 qemu-img.texi    |  8 +++++++-
 3 files changed, 24 insertions(+), 10 deletions(-)
qemu-img-cmds.hx +2 -2
index 8bc55cd..7f62f6d 100644
@@ -22,9 +22,9 @@ STEXI
 ETEXI
 
 DEF("commit", img_commit,
-    "commit [-q] [-f fmt] [-t cache] [-p] filename")
+    "commit [-q] [-f fmt] [-t cache] [-b backing_file] [-p] filename")
 STEXI
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{backing_file}] [-p] @var{filename}
 ETEXI
 
 DEF("compare", img_compare,
qemu-img.c +15 -7
index 0a9eff7..9d4bdbc 100644
@@ -725,15 +725,16 @@ static void run_block_job(BlockJob *job, Error **errp)
 static int img_commit(int argc, char **argv)
 {
     int c, ret, flags;
-    const char *filename, *fmt, *cache;
+    const char *filename, *fmt, *cache, *base;
     BlockDriverState *bs, *base_bs;
     bool progress = false, quiet = false;
     Error *local_err = NULL;
 
     fmt = NULL;
     cache = BDRV_DEFAULT_CACHE;
+    base = NULL;
     for(;;) {
-        c = getopt(argc, argv, "f:ht:qp");
+        c = getopt(argc, argv, "f:ht:b:qp");
         if (c == -1) {
             break;
         }
@@ -748,6 +749,9 @@ static int img_commit(int argc, char **argv)
         case 't':
             cache = optarg;
             break;
+        case 'b':
+            base = optarg;
+            break;
         case 'p':
             progress = true;
             break;
@@ -782,12 +786,16 @@ static int img_commit(int argc, char **argv)
     qemu_progress_init(progress, 1.f);
     qemu_progress_print(0.f, 100);
 
-    /* This is different from QMP, which by default uses the deepest file in the
-     * backing chain (i.e., the very base); however, the traditional behavior of
-     * qemu-img commit is using the immediate backing file. */
-    base_bs = bs->backing_hd;
+    if (base) {
+        base_bs = bdrv_find_backing_image(bs, base);
+    } else {
+        /* This is different from QMP, which by default uses the deepest file in
+         * the backing chain (i.e., the very base); however, the traditional
+         * behavior of qemu-img commit is using the immediate backing file. */
+        base_bs = bs->backing_hd;
+    }
     if (!base_bs) {
-        error_set(&local_err, QERR_BASE_NOT_FOUND, "NULL");
+        error_set(&local_err, QERR_BASE_NOT_FOUND, base ?: "NULL");
         goto done;
     }
 
qemu-img.texi +1 -1
index 1a9c08f..4a9f493 100644
@@ -140,7 +140,7 @@ this case. @var{backing_file} will never be modified unless you use the
 The size can also be specified using the @var{size} option with @code{-o},
 it doesn't need to be specified separately in this case.
 
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{backing_file}] [-p] @var{filename}
 
 Commit the changes recorded in @var{filename} in its base image or backing file.
 If the backing file is smaller than the snapshot, then the backing file will be

qemu-img.texi the backing file, the backing file will not be truncated. If you want the · line 155

Quoted 7 lines
qemu-img.texi +4 -0
@@ -149,6 +149,12 @@ the backing file, the backing file will not be truncated.  If you want the
 backing file to match the size of the smaller snapshot, you can safely truncate
 it yourself once the commit operation successfully completes.
 
+If the backing chain of the given image file @var{filename} has more than one
+layer, the backing file unto which the changes shall be committed may be
+specified as @var{backing_file} (which has to be part of @var{filename}'s
+backing chain). If @var{filename} is not specified, the immediate backing file

s/@var{filename}/@var{backing_file}/ ?

BTW how about just calling it 'base' as in qmp commands, because backing_file has usages in (slightly) different context of create.

Other than the two questions,

Quoted 9 lines
qemu-img.texi +2 -0
+of the top image (which is @var{filename}) will be used.
+
 @item compare [-f @var{fmt}] [-F @var{fmt}] [-p] [-s] [-q] @var{filename1} @var{filename2}
 
 Check if two images have the same content. You can compare images with
Signature · 1 line
1.9.1
author
#23 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] qemu-img: Specify backing file for commit

Quoted 100 lines · Fam Zheng
Quoted 99 lines · Max Reitz

Introduce a new parameter for qemu-img commit which may be used to explicitly specify the backing file unto which an image should be committed if the backing chain has more than a single layer.

---
  qemu-img-cmds.hx |  4 ++--
  qemu-img.c       | 22 +++++++++++++++-------
  qemu-img.texi    |  8 +++++++-
  3 files changed, 24 insertions(+), 10 deletions(-)
qemu-img-cmds.hx +2 -2
index 8bc55cd..7f62f6d 100644
@@ -22,9 +22,9 @@ STEXI
  ETEXI
 
  DEF("commit", img_commit,
-    "commit [-q] [-f fmt] [-t cache] [-p] filename")
+    "commit [-q] [-f fmt] [-t cache] [-b backing_file] [-p] filename")
  STEXI
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{backing_file}] [-p] @var{filename}
  ETEXI
 
  DEF("compare", img_compare,
qemu-img.c +15 -7
index 0a9eff7..9d4bdbc 100644
@@ -725,15 +725,16 @@ static void run_block_job(BlockJob *job, Error **errp)
  static int img_commit(int argc, char **argv)
  {
      int c, ret, flags;
-    const char *filename, *fmt, *cache;
+    const char *filename, *fmt, *cache, *base;
      BlockDriverState *bs, *base_bs;
      bool progress = false, quiet = false;
      Error *local_err = NULL;
 
      fmt = NULL;
      cache = BDRV_DEFAULT_CACHE;
+    base = NULL;
      for(;;) {
-        c = getopt(argc, argv, "f:ht:qp");
+        c = getopt(argc, argv, "f:ht:b:qp");
          if (c == -1) {
              break;
          }
@@ -748,6 +749,9 @@ static int img_commit(int argc, char **argv)
          case 't':
              cache = optarg;
              break;
+        case 'b':
+            base = optarg;
+            break;
          case 'p':
              progress = true;
              break;
@@ -782,12 +786,16 @@ static int img_commit(int argc, char **argv)
      qemu_progress_init(progress, 1.f);
      qemu_progress_print(0.f, 100);
 
-    /* This is different from QMP, which by default uses the deepest file in the
-     * backing chain (i.e., the very base); however, the traditional behavior of
-     * qemu-img commit is using the immediate backing file. */
-    base_bs = bs->backing_hd;
+    if (base) {
+        base_bs = bdrv_find_backing_image(bs, base);
+    } else {
+        /* This is different from QMP, which by default uses the deepest file in
+         * the backing chain (i.e., the very base); however, the traditional
+         * behavior of qemu-img commit is using the immediate backing file. */
+        base_bs = bs->backing_hd;
+    }
      if (!base_bs) {
-        error_set(&local_err, QERR_BASE_NOT_FOUND, "NULL");
+        error_set(&local_err, QERR_BASE_NOT_FOUND, base ?: "NULL");
          goto done;
      }
 
qemu-img.texi +1 -1
index 1a9c08f..4a9f493 100644
@@ -140,7 +140,7 @@ this case. @var{backing_file} will never be modified unless you use the
  The size can also be specified using the @var{size} option with @code{-o},
  it doesn't need to be specified separately in this case.
 
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{backing_file}] [-p] @var{filename}
 
  Commit the changes recorded in @var{filename} in its base image or backing file.
  If the backing file is smaller than the snapshot, then the backing file will be
@@ -149,6 +149,12 @@ the backing file, the backing file will not be truncated.  If you want the
  backing file to match the size of the smaller snapshot, you can safely truncate
  it yourself once the commit operation successfully completes.
 
Quoted 5 lines

qemu-img.texi the backing file, the backing file will not be truncated. If you want the · line 155

Quoted 4 lines
qemu-img.texi +4 -0
+If the backing chain of the given image file @var{filename} has more than one
+layer, the backing file unto which the changes shall be committed may be
+specified as @var{backing_file} (which has to be part of @var{filename}'s
+backing chain). If @var{filename} is not specified, the immediate backing file

s/@var{filename}/@var{backing_file}/ ?

Right.

Quoted 2 lines

BTW how about just calling it 'base' as in qmp commands, because backing_file has usages in (slightly) different context of create.

I just called it “backing_file”, as there are currently no qemu-img commands with a “base” parameter; then again, there aren't any qemu-img commands for which a filename of the backing chain may be specified, so you're correct.

Max

Quoted 13 lines

Other than the two questions,

Quoted 9 lines
qemu-img.texi +2 -0
+of the top image (which is @var{filename}) will be used.
+
  @item compare [-f @var{fmt}] [-F @var{fmt}] [-p] [-s] [-q] @var{filename1} @var{filename2}
 
  Check if two images have the same content. You can compare images with
Signature · 1 line
1.9.1
author
#24 lore raw

PATCH v2 6/6 iotests: Commit tests for two-layer backing chains

In reply to Max Reitz

Add tests to qemu-iotest 020 for committing through a two-layer backing file chain with qemu-img commit utilizing the -b option.

The newly added output in 020.out is basically the same as for the already existing single-layer pass, but with another image created and additional qemu-img map output.

---
 tests/qemu-iotests/020     |   57 +-
 tests/qemu-iotests/020.out | 2168 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 2223 insertions(+), 2 deletions(-)
tests/qemu-iotests/020 +55 -2
index 2f258dc..1e7327c 100755
@@ -32,6 +32,7 @@ _cleanup()
 {
 	_cleanup_test_img
     rm -f "$TEST_IMG.base"
+    rm -f "$TEST_IMG.itmd"
     rm -f "$TEST_IMG.orig"
 }
 trap "_cleanup; exit \$status" 0 1 2 3 15
@@ -56,6 +57,16 @@ fi
 
 TEST_OFFSETS="0 4294967296"
 
+# Three passes:
+#  0: Single-layer backing chain
+#  1: Two-layer backing chain, commit to upper backing file (default)
+#  2: Two-layer backing chain, commit to lower backing file
+for i in 0 1 2
+do
+
+echo "=== Test pass $i ==="
+echo
+
 _make_test_img 6G
 
 echo "Filling base image"
@@ -76,6 +87,13 @@ echo
 mv "$TEST_IMG" "$TEST_IMG.base"
 _make_test_img -b "$TEST_IMG.base" 6G
 
+if [ $i -gt 0 ]
+then
+    # Create intermediate image
+    mv "$TEST_IMG" "$TEST_IMG.itmd"
+    _make_test_img -b "$TEST_IMG.itmd" 6G
+fi
+
 echo "Filling test image"
 echo
 
@@ -88,8 +106,24 @@ for offset in $TEST_OFFSETS; do
 done
 _check_test_img
 
-$QEMU_IMG commit "$TEST_IMG"
-mv "$TEST_IMG.base" "$TEST_IMG"
+case $i in
+    0)
+        $QEMU_IMG commit "$TEST_IMG"
+        mv "$TEST_IMG.base" "$TEST_IMG"
+        ;;
+
+    1)
+        # -b "$TEST_IMG.itmd" should be the default (that is, committing to the
+        # first backing file in the chain)
+        $QEMU_IMG commit "$TEST_IMG"
+        mv "$TEST_IMG.itmd" "$TEST_IMG"
+        ;;
+
+    2)
+        $QEMU_IMG commit -b "$TEST_IMG.base" "$TEST_IMG"
+        mv "$TEST_IMG.base" "$TEST_IMG"
+        ;;
+esac
 
 echo "Reading from the backing file"
 echo
@@ -108,6 +142,25 @@ for offset in $TEST_OFFSETS; do
 done
 _check_test_img
 
+if [ $i -eq 0 ]
+then
+    continue
+fi
+
+# For two-layer chains, make sure the other image in the chain is unmodified
+case $i in
+    1)
+        $QEMU_IMG map "$TEST_IMG.base"
+        ;;
+
+    2)
+        mv "$TEST_IMG" "$TEST_IMG.base"
+        $QEMU_IMG map "$TEST_IMG.itmd"
+        ;;
+esac
+
+done
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
tests/qemu-iotests/020.out +2168 -0

2,182 lines not on this page · raw message

Signature · 1 line
1.9.1
Reviewed-by on v2 6/6
#25 lore raw

Re: [Qemu-devel] [PATCH v2 6/6] iotests: Commit tests for two-layer backing chains

Quoted 7 lines · Max Reitz

Add tests to qemu-iotest 020 for committing through a two-layer backing file chain with qemu-img commit utilizing the -b option.

The newly added output in 020.out is basically the same as for the already existing single-layer pass, but with another image created and additional qemu-img map output.

Quoted 5 lines
---
 tests/qemu-iotests/020     |   57 +-
 tests/qemu-iotests/020.out | 2168 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 2223 insertions(+), 2 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org