Skip to contentSkip to conversation

Mirrored block writes

PATCH Applied Paolo Bonzini 4 participants 21 messages Mar 6, 2012 – Mar 14, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover Mirrored block writes

v4 includes Federico's drive-reopen (patch 10) command, fixes another small bug in Jeff's code (patch 2), and tweaks the union handling for older compilers.

v3 tested with the following scenarios, v4 only d/e:

a) mirror only

1) create base.qcow2 and starat QEMU with it

2) Execute the following QMP command

{ "execute": "qmp_capabilities" }
{ "execute": "transaction", "arguments":
{'actions': [

    { 'type': 'drive-mirror', 'data' :
      { 'device': 'ide0-hd0', 'target': '/home/pbonzini/mirror.qcow2' } } ] } }

{ "execute": "cont" }

3) hibernate the guest (this requires an IDE disk and -cpu kvm64,-kvmclock)

4) restart the guest with mirror.qcow2

b) Same as (a) with drive-mirror command.

c) streaming to new image

1) start QEMU with an existing image test.img

2) execute the following HMP commands

drive_mirror -s ide0-hd0 /home/pbonzini/mirror.qed qed block_stream ide0-hd0

3) shut down the guest (sorry, this took a looong time and I forgot to hibernate here)

4) move away the base image

5) restart the guest with mirror.qed

d) atomic snapshot+mirror (QMP only):

1) start QEMU with an existing image test.img

2) Execute the following QMP command

{ "execute": "qmp_capabilities" }
{ "execute": "transaction", "arguments":
{'actions': [

    { 'type': 'blockdev-snapshot-sync', 'data' :
      { 'device': 'ide0-hd0', 'snapshot-file': '/home/pbonzini/base.qcow2' } },
    { 'type': 'drive-mirror', 'data' :
      { 'device': 'ide0-hd0', 'target': '/home/pbonzini/mirror.qcow2' } } ] } }

{ "execute": "cont" }

3) hibernate the guest (this requires an IDE disk and -cpu kvm64,-kvmclock)

4) check that mirror.qcow2 has test.img as the base

5) restart the guest with base.qcow2

6) restart the guest with mirror.qcow2

e) simple snapshot with snapshot_blkdev and snapshot_blkdev -n.

v3->v4:
avoid literals with anonymous unions in them, add "void *data" to all unions (Mark Wu); add drive-reopen (Federico); new patch to fix use-after-free in group snapshots; patch splitting nits (Eric).

v2->v3:
replace reuse argument with NewImageMode enum and mode argument; renamed all commands and actions; implemented blockdev-snapshot-sync in terms of group snasphots; added new drive-mirror command and HMP equivalent.

Federico Simoncelli (1):
Add the drive-reopen command

Marcelo Tosatti (1):
Add blkmirror block driver

Paolo Bonzini (8):
use QSIMPLEQ_FOREACH_SAFE when freeing list elements
fix format name for backing file
qapi: complete implementation of unions
rename blockdev-group-snapshot-sync
add mode field to blockdev-snapshot-sync transaction item
qmp: convert blockdev-snapshot-sync to a wrapper around transactions add mirroring to transaction
add drive-mirror command and HMP equivalent

 Makefile.objs             |    2 +-
 block/blkmirror.c         |  239 +++++++++++++++++++++++++++++++++++++++++++++
 blockdev.c                |  202 +++++++++++++++++++++++++++-----------
 hmp-commands.hx           |   47 ++++++++-
 hmp.c                     |   45 ++++++++-
 hmp.h                     |    2 +
 qapi-schema-test.json     |   10 ++
 qapi-schema.json          |  143 +++++++++++++++++++++++----
 qmp-commands.hx           |  135 +++++++++++++++++++++-----
 scripts/qapi-types.py     |    6 +
 scripts/qapi-visit.py     |   31 ++++++-
 test-qmp-input-visitor.c  |   18 ++++
 test-qmp-output-visitor.c |   34 +++++++
 13 files changed, 804 insertions(+), 110 deletions(-)
 create mode 100644 block/blkmirror.c
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH v4 1/10 use QSIMPLEQ_FOREACH_SAFE when freeing list elements

---
 blockdev.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
blockdev.c +2 -2
index d78aa51..acdc72a 100644
@@ -733,7 +733,7 @@ void qmp_blockdev_group_snapshot_sync(SnapshotDevList *dev_list,
     int ret = 0;
     SnapshotDevList *dev_entry = dev_list;
     SnapshotDev *dev_info = NULL;
-    BlkGroupSnapshotStates *states;
+    BlkGroupSnapshotStates *states, *next;
     BlockDriver *proto_drv;
     BlockDriver *drv;
     int flags;
@@ -838,7 +838,7 @@ delete_and_fail:
         }
     }
 exit:
-    QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
+    QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
         g_free(states);
     }
     return;
Signature · 1 line
1.7.7.6
#2 lore raw

PATCH v4 2/10 fix format name for backing file

In reply to Paolo Bonzini
---
 blockdev.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
blockdev.c +2 -1
index acdc72a..6ed7d61 100644
@@ -800,7 +800,8 @@ void qmp_blockdev_group_snapshot_sync(SnapshotDevList *dev_list,
         /* create new image w/backing file */
         ret = bdrv_img_create(snapshot_file, format,
                               states->old_bs->filename,
-                              drv->format_name, NULL, -1, flags);
+                              states->old_bs->drv->format_name,
+                              NULL, -1, flags);
         if (ret) {
             error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file);
             goto delete_and_fail;
Signature · 1 line
1.7.7.6
#3 lore raw

PATCH v4 3/10 qapi: complete implementation of unions

In reply to Paolo Bonzini
---
 qapi-schema-test.json     |   10 ++++++++++
 scripts/qapi-types.py     |    6 ++++++
 scripts/qapi-visit.py     |   31 ++++++++++++++++++++++++++++++-
 test-qmp-input-visitor.c  |   18 ++++++++++++++++++
 test-qmp-output-visitor.c |   34 ++++++++++++++++++++++++++++++++++
 5 files changed, 98 insertions(+), 1 deletions(-)
qapi-schema-test.json +10 -0
index 2b38919..8c7f9f7 100644
@@ -22,6 +22,16 @@
                        'dict2': { 'userdef1': 'UserDefOne', 'string2': 'str' },
                        '*dict3': { 'userdef2': 'UserDefOne', 'string3': 'str' } } } }
 
+# for testing unions
+{ 'type': 'UserDefA',
+  'data': { 'boolean': 'bool' } }
+
+{ 'type': 'UserDefB',
+  'data': { 'integer': 'int' } }
+
+{ 'union': 'UserDefUnion',
+  'data': { 'a' : 'UserDefA', 'b' : 'UserDefB' } }
+
 # testing commands
 { 'command': 'user_def_cmd', 'data': {} }
 { 'command': 'user_def_cmd1', 'data': {'ud1a': 'UserDefOne'} }
scripts/qapi-types.py +6 -0
index b56225b..727fb77 100644
@@ -117,6 +117,7 @@ struct %(name)s
 {
     %(name)sKind kind;
     union {
+        void *data;
 ''',
                 name=name)
 
@@ -269,6 +270,7 @@ for expr in exprs:
     elif expr.has_key('union'):
         ret += generate_fwd_struct(expr['union'], expr['data']) + "\n"
         ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
+        fdef.write(generate_enum_lookup('%sKind' % expr['union'], expr['data'].keys()))
     else:
         continue
     fdecl.write(ret)
@@ -283,6 +285,10 @@ for expr in exprs:
         fdef.write(generate_type_cleanup(expr['type']) + "\n")
     elif expr.has_key('union'):
         ret += generate_union(expr['union'], expr['data'])
+        ret += generate_type_cleanup_decl(expr['union'] + "List")
+        fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
+        ret += generate_type_cleanup_decl(expr['union'])
+        fdef.write(generate_type_cleanup(expr['union']) + "\n")
     else:
         continue
     fdecl.write(ret)
scripts/qapi-visit.py +30 -1
index 5160d83..54117d4 100644
@@ -110,10 +110,38 @@ def generate_visit_union(name, members):
 
 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
 {
-}
+    Error *err = NULL;
+
+    visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
+    visit_type_%(name)sKind(m, &(*obj)->kind, "type", &err);
+    if (err) {
+        error_propagate(errp, err);
+        goto end;
+    }
+    switch ((*obj)->kind) {
 ''',
                  name=name)
 
+    for key in members:
+        ret += mcgen('''
+    case %(abbrev)s_KIND_%(enum)s:
+        visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", errp);
+        break;
+''',
+                abbrev = de_camel_case(name).upper(),
+                enum = de_camel_case(key).upper(),
+                c_type=members[key],
+                c_name=c_var(key))
+
+    ret += mcgen('''
+    default:
+        abort();
+    }
+end:
+    visit_end_struct(m, errp);
+}
+''')
+
     return ret
 
 def generate_declaration(name, members, genlist=True):
@@ -242,6 +270,7 @@ for expr in exprs:
         fdecl.write(ret)
     elif expr.has_key('union'):
         ret = generate_visit_union(expr['union'], expr['data'])
+        ret += generate_visit_list(expr['union'], expr['data'])
         fdef.write(ret)
 
         ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys())
test-qmp-input-visitor.c +18 -0
index 926db5c..1996e49 100644
@@ -234,6 +234,22 @@ static void test_visitor_in_list(TestInputVisitorData *data,
     qapi_free_UserDefOneList(head);
 }
 
+static void test_visitor_in_union(TestInputVisitorData *data,
+                                  const void *unused)
+{
+    Visitor *v;
+    Error *err = NULL;
+    UserDefUnion *tmp;
+
+    v = visitor_input_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
+
+    visit_type_UserDefUnion(v, &tmp, NULL, &err);
+    g_assert(err == NULL);
+    g_assert_cmpint(tmp->kind, ==, USER_DEF_UNION_KIND_B);
+    g_assert_cmpint(tmp->b->integer, ==, 42);
+    qapi_free_UserDefUnion(tmp);
+}
+
 static void input_visitor_test_add(const char *testpath,
                                    TestInputVisitorData *data,
                                    void (*test_func)(TestInputVisitorData *data, const void *user_data))
@@ -264,6 +280,8 @@ int main(int argc, char **argv)
                             &in_visitor_data, test_visitor_in_struct_nested);
     input_visitor_test_add("/visitor/input/list",
                             &in_visitor_data, test_visitor_in_list);
+    input_visitor_test_add("/visitor/input/union",
+                            &in_visitor_data, test_visitor_in_union);
 
     g_test_run();
 
test-qmp-output-visitor.c +34 -0
index c94c208..78e5f2d 100644
@@ -380,6 +380,38 @@ static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
     qapi_free_UserDefNestedList(head);
 }
 
+static void test_visitor_out_union(TestOutputVisitorData *data,
+                                   const void *unused)
+{
+    QObject *arg, *qvalue;
+    QDict *qdict, *value;
+
+    Error *err = NULL;
+
+    UserDefUnion *tmp = g_malloc0(sizeof(UserDefUnion));
+    tmp->kind = USER_DEF_UNION_KIND_A;
+    tmp->a = g_malloc0(sizeof(UserDefA));
+    tmp->a->boolean = true;
+
+    visit_type_UserDefUnion(data->ov, &tmp, NULL, &err);
+    g_assert(err == NULL);
+    arg = qmp_output_get_qobject(data->qov);
+
+    g_assert(qobject_type(arg) == QTYPE_QDICT);
+    qdict = qobject_to_qdict(arg);
+
+    g_assert_cmpstr(qdict_get_str(qdict, "type"), ==, "a");
+
+    qvalue = qdict_get(qdict, "data");
+    g_assert(data != NULL);
+    g_assert(qobject_type(qvalue) == QTYPE_QDICT);
+    value = qobject_to_qdict(qvalue);
+    g_assert_cmpint(qdict_get_bool(value, "boolean"), ==, true);
+
+    qapi_free_UserDefUnion(tmp);
+    QDECREF(qdict);
+}
+
 static void output_visitor_test_add(const char *testpath,
                                     TestOutputVisitorData *data,
                                     void (*test_func)(TestOutputVisitorData *data, const void *user_data))
@@ -416,6 +448,8 @@ int main(int argc, char **argv)
                             &out_visitor_data, test_visitor_out_list);
     output_visitor_test_add("/visitor/output/list-qapi-free",
                             &out_visitor_data, test_visitor_out_list_qapi_free);
+    output_visitor_test_add("/visitor/output/union",
+                            &out_visitor_data, test_visitor_out_union);
 
     g_test_run();
 
Signature · 1 line
1.7.7.6
#4 lore raw

PATCH v4 4/10 rename blockdev-group-snapshot-sync

In reply to Paolo Bonzini

We will add other kinds of operation. Prepare for this by adjusting the schema.

---
 blockdev.c       |   78 ++++++++++++++++++++++++++++-------------------------
 qapi-schema.json |   42 ++++++++++++++++++----------
 qmp-commands.hx  |   52 +++++++++++++++++++----------------
 3 files changed, 96 insertions(+), 76 deletions(-)
blockdev.c +41 -37
index 6ed7d61..df544a3 100644
@@ -716,31 +716,24 @@ void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
 
 
 /* New and old BlockDriverState structs for group snapshots */
-typedef struct BlkGroupSnapshotStates {
+typedef struct BlkTransactionStates {
     BlockDriverState *old_bs;
     BlockDriverState *new_bs;
-    QSIMPLEQ_ENTRY(BlkGroupSnapshotStates) entry;
-} BlkGroupSnapshotStates;
+    QSIMPLEQ_ENTRY(BlkTransactionStates) entry;
+} BlkTransactionStates;
 
 /*
  * 'Atomic' group snapshots.  The snapshots are taken as a set, and if any fail
  *  then we do not pivot any of the devices in the group, and abandon the
  *  snapshots
  */
-void qmp_blockdev_group_snapshot_sync(SnapshotDevList *dev_list,
-                                      Error **errp)
+void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
 {
     int ret = 0;
-    SnapshotDevList *dev_entry = dev_list;
-    SnapshotDev *dev_info = NULL;
-    BlkGroupSnapshotStates *states, *next;
-    BlockDriver *proto_drv;
-    BlockDriver *drv;
-    int flags;
-    const char *format;
-    const char *snapshot_file;
+    BlockdevActionList *dev_entry = dev_list;
+    BlkTransactionStates *states, *next;
 
-    QSIMPLEQ_HEAD(snap_bdrv_states, BlkGroupSnapshotStates) snap_bdrv_states;
+    QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
     QSIMPLEQ_INIT(&snap_bdrv_states);
 
     /* drain all i/o before any snapshots */
@@ -748,21 +741,46 @@ void qmp_blockdev_group_snapshot_sync(SnapshotDevList *dev_list,
 
     /* We don't do anything in this loop that commits us to the snapshot */
     while (NULL != dev_entry) {
+        BlockdevAction *dev_info = NULL;
+        BlockDriver *proto_drv;
+        BlockDriver *drv;
+        int flags;
+        const char *device;
+        const char *format = "qcow2";
+        const char *new_image_file = NULL;
+
         dev_info = dev_entry->value;
         dev_entry = dev_entry->next;
 
-        states = g_malloc0(sizeof(BlkGroupSnapshotStates));
+        states = g_malloc0(sizeof(BlkTransactionStates));
         QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry);
 
-        states->old_bs = bdrv_find(dev_info->device);
+        switch (dev_info->kind) {
+        case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
+            device = dev_info->blockdev_snapshot_sync->device;
+            if (dev_info->blockdev_snapshot_sync->has_format) {
+                format = dev_info->blockdev_snapshot_sync->format;
+            }
+            new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
+            break;
+        default:
+            abort();
+        }
+
+        drv = bdrv_find_format(format);
+        if (!drv) {
+            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
+            goto delete_and_fail;
+        }
 
+        states->old_bs = bdrv_find(device);
         if (!states->old_bs) {
-            error_set(errp, QERR_DEVICE_NOT_FOUND, dev_info->device);
+            error_set(errp, QERR_DEVICE_NOT_FOUND, device);
             goto delete_and_fail;
         }
 
         if (bdrv_in_use(states->old_bs)) {
-            error_set(errp, QERR_DEVICE_IN_USE, dev_info->device);
+            error_set(errp, QERR_DEVICE_IN_USE, device);
             goto delete_and_fail;
         }
 
@@ -775,44 +793,30 @@ void qmp_blockdev_group_snapshot_sync(SnapshotDevList *dev_list,
             }
         }
 
-        snapshot_file = dev_info->snapshot_file;
-
         flags = states->old_bs->open_flags;
 
-        if (!dev_info->has_format) {
-            format = "qcow2";
-        } else {
-            format = dev_info->format;
-        }
-
-        drv = bdrv_find_format(format);
-        if (!drv) {
-            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
-            goto delete_and_fail;
-        }
-
-        proto_drv = bdrv_find_protocol(snapshot_file);
+        proto_drv = bdrv_find_protocol(new_image_file);
         if (!proto_drv) {
             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
             goto delete_and_fail;
         }
 
         /* create new image w/backing file */
-        ret = bdrv_img_create(snapshot_file, format,
+        ret = bdrv_img_create(new_image_file, format,
                               states->old_bs->filename,
                               states->old_bs->drv->format_name,
                               NULL, -1, flags);
         if (ret) {
-            error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file);
+            error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
             goto delete_and_fail;
         }
 
         /* We will manually add the backing_hd field to the bs later */
         states->new_bs = bdrv_new("");
-        ret = bdrv_open(states->new_bs, snapshot_file,
+        ret = bdrv_open(states->new_bs, new_image_file,
                         flags | BDRV_O_NO_BACKING, drv);
         if (ret != 0) {
-            error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file);
+            error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
             goto delete_and_fail;
         }
     }
qapi-schema.json +27 -15

66 lines not on this page · raw message

qmp-commands.hx +28 -24

69 lines not on this page · raw message

Signature · 1 line
1.7.7.6
#5 lore raw

PATCH v4 5/10 add mode field to blockdev-snapshot-sync transaction item

In reply to Paolo Bonzini

The mode field lets a management application create the snapshot destination outside QEMU.

Right now, the only modes are "existing" and "absolute-paths". Mirroring introduces "no-backing-file". In the future "relative-paths" could be implemented too.

---
 blockdev.c       |   25 ++++++++++++++++---------
 qapi-schema.json |   21 ++++++++++++++++++++-
 qmp-commands.hx  |   10 ++++++++++
 3 files changed, 46 insertions(+), 10 deletions(-)
blockdev.c +16 -9
index df544a3..cb5bf03 100644
@@ -745,9 +745,10 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
         BlockDriver *proto_drv;
         BlockDriver *drv;
         int flags;
+        enum NewImageMode mode;
+        const char *new_image_file;
         const char *device;
         const char *format = "qcow2";
-        const char *new_image_file = NULL;
 
         dev_info = dev_entry->value;
         dev_entry = dev_entry->next;
@@ -758,10 +759,14 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
         switch (dev_info->kind) {
         case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC:
             device = dev_info->blockdev_snapshot_sync->device;
+            if (!dev_info->blockdev_snapshot_sync->has_mode) {
+                dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
+            }
+            new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
             if (dev_info->blockdev_snapshot_sync->has_format) {
                 format = dev_info->blockdev_snapshot_sync->format;
             }
-            new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file;
+            mode = dev_info->blockdev_snapshot_sync->mode;
             break;
         default:
             abort();
@@ -802,13 +807,15 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
         }
 
         /* create new image w/backing file */
-        ret = bdrv_img_create(new_image_file, format,
-                              states->old_bs->filename,
-                              states->old_bs->drv->format_name,
-                              NULL, -1, flags);
-        if (ret) {
-            error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
-            goto delete_and_fail;
+        if (mode != NEW_IMAGE_MODE_EXISTING) {
+            ret = bdrv_img_create(new_image_file, format,
+                                  states->old_bs->filename,
+                                  states->old_bs->drv->format_name,
+                                  NULL, -1, flags);
+            if (ret) {
+                error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
+                goto delete_and_fail;
+            }
         }
 
         /* We will manually add the backing_hd field to the bs later */
qapi-schema.json +18 -1
index 1d1ffa6..b062d01 100644
@@ -1118,6 +1118,22 @@
 { 'command': 'block_resize', 'data': { 'device': 'str', 'size': 'int' }}
 
 ##
+# @NewImageMode
+#
+# An enumeration that tells QEMU how to set the backing file path in
+# a new image file.
+#
+# @existing: QEMU should look for an existing image file.
+#
+# @absolute-paths: QEMU should create a new image with absolute paths
+# for the backing file.
+#
+# Since: 1.1
+##
+{ 'enum': 'NewImageMode'
+  'data': [ 'existing', 'absolute-paths' ] }
+
+##
 # @BlockdevSnapshot
 #
 # @device:  the name of the device to generate the snapshot from.
@@ -1127,7 +1145,8 @@
 # @format: #optional the format of the snapshot image, default is 'qcow2'.
 ##
 { 'type': 'BlockdevSnapshot',
-  'data': { 'device': 'str', 'snapshot-file': 'str', '*format': 'str' } }
+  'data': { 'device': 'str', 'snapshot-file': 'str', '*format': 'str',
+            '*mode': 'NewImageMode' } }
 
 ##
 # @BlockdevAction
qmp-commands.hx +10 -0
index fb4f1df..7c03b62 100644
@@ -705,6 +705,13 @@ A list of dictionaries is accepted, that contains the actions to be performed.
 For snapshots this is the device, the file to use for the new snapshot,
 and the format.  The default format, if not specified, is qcow2.
 
+Each new snapshot defaults to being created by QEMU (wiping any
+contents if the file already exists), but it is also possible to reuse
+an externally-created file.  In the latter case, you should ensure that
+the new image file has the same contents as the current one; QEMU cannot
+perform any meaningful check.  Typically this is achieved by using the
+current image file as the backing file for the new image.
+
 Arguments:
 
 actions array:
@@ -715,6 +722,8 @@ actions array:
       - "device": device name to snapshot (json-string)
       - "snapshot-file": name of new image file (json-string)
       - "format": format of new image (json-string, optional)
+      - "mode": whether and how QEMU should create the snapshot file
+        (NewImageMode, optional, default "absolute-paths")
 
 Example:
 
@@ -725,6 +734,7 @@ Example:
                                          "format": "qcow2" } },
          { 'type': 'blockdev-snapshot-sync', 'data' : { "device": "ide-hd1",
                                          "snapshot-file": "/some/place/my-image2",
+                                         "mode": "existing",
                                          "format": "qcow2" } } ] } }
 <- { "return": {} }
 
Signature · 1 line
1.7.7.6
#6 lore raw

PATCH v4 6/10 qmp: convert blockdev-snapshot-sync to a wrapper around transactions

In reply to Paolo Bonzini

Simplify the blockdev-snapshot-sync code and gain failsafe operation by turning it into a wrapper around the new transaction command. A new option is also added matching "mode".

---
 blockdev.c       |   85 ++++++++++++++---------------------------------------
 hmp-commands.hx  |    9 ++++--
 hmp.c            |    6 +++-
 qapi-schema.json |   15 +++++----
 qmp-commands.hx  |    2 +
 5 files changed, 44 insertions(+), 73 deletions(-)
blockdev.c +23 -62
index cb5bf03..067cc10 100644
@@ -646,72 +646,33 @@ void do_commit(Monitor *mon, const QDict *qdict)
     }
 }
 
+static void blockdev_do_action(int kind, void *data, Error **errp)
+{
+    BlockdevAction action;
+    BlockdevActionList list;
+
+    action.kind = kind;
+    action.data = data;
+    list.value = &action;
+    list.next = NULL;
+    qmp_transaction(&list, errp);
+}
+
 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
                                 bool has_format, const char *format,
+                                bool has_mode, enum NewImageMode mode,
                                 Error **errp)
 {
-    BlockDriverState *bs;
-    BlockDriver *drv, *old_drv, *proto_drv;
-    int ret = 0;
-    int flags;
-    char old_filename[1024];
-
-    bs = bdrv_find(device);
-    if (!bs) {
-        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
-        return;
-    }
-    if (bdrv_in_use(bs)) {
-        error_set(errp, QERR_DEVICE_IN_USE, device);
-        return;
-    }
-
-    pstrcpy(old_filename, sizeof(old_filename), bs->filename);
-
-    old_drv = bs->drv;
-    flags = bs->open_flags;
-
-    if (!has_format) {
-        format = "qcow2";
-    }
-
-    drv = bdrv_find_format(format);
-    if (!drv) {
-        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
-        return;
-    }
-
-    proto_drv = bdrv_find_protocol(snapshot_file);
-    if (!proto_drv) {
-        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
-        return;
-    }
-
-    ret = bdrv_img_create(snapshot_file, format, bs->filename,
-                          bs->drv->format_name, NULL, -1, flags);
-    if (ret) {
-        error_set(errp, QERR_UNDEFINED_ERROR);
-        return;
-    }
-
-    bdrv_drain_all();
-    bdrv_flush(bs);
-
-    bdrv_close(bs);
-    ret = bdrv_open(bs, snapshot_file, flags, drv);
-    /*
-     * If reopening the image file we just created fails, fall back
-     * and try to re-open the original image. If that fails too, we
-     * are in serious trouble.
-     */
-    if (ret != 0) {
-        ret = bdrv_open(bs, old_filename, flags, old_drv);
-        if (ret != 0) {
-            error_set(errp, QERR_OPEN_FILE_FAILED, old_filename);
-        } else {
-            error_set(errp, QERR_OPEN_FILE_FAILED, snapshot_file);
-        }
-    }
+    BlockdevSnapshot snapshot = {
+        .device = (char *) device,
+        .snapshot_file = (char *) snapshot_file,
+        .has_format = has_format,
+        .format = (char *) format,
+        .has_mode = has_mode,
+        .mode = mode,
+    };
+    blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
+                       errp);
 }
 
 
hmp-commands.hx +6 -3
index ed88877..6980214 100644
@@ -882,14 +882,17 @@ ETEXI
 
     {
         .name       = "snapshot_blkdev",
-        .args_type  = "device:B,snapshot-file:s?,format:s?",
-        .params     = "device [new-image-file] [format]",
+        .args_type  = "reuse:-n,device:B,snapshot-file:s?,format:s?",
+        .params     = "[-n] device [new-image-file] [format]",
         .help       = "initiates a live snapshot\n\t\t\t"
                       "of device. If a new image file is specified, the\n\t\t\t"
                       "new image file will become the new root image.\n\t\t\t"
                       "If format is specified, the snapshot file will\n\t\t\t"
                       "be created in that format. Otherwise the\n\t\t\t"
-                      "snapshot will be internal! (currently unsupported)",
+                      "snapshot will be internal! (currently unsupported).\n\t\t\t"
+                      "The default format is qcow2.  The -n flag requests QEMU\n\t\t\t"
+                      "to reuse the image found in new-image-file, instead of\n\t\t\t"
+                      "recreating it from scratch.",
         .mhandler.cmd = hmp_snapshot_blkdev,
     },
 
hmp.c +5 -1
index 3a54455..290c43d 100644
@@ -692,6 +692,8 @@ void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
     const char *device = qdict_get_str(qdict, "device");
     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
     const char *format = qdict_get_try_str(qdict, "format");
+    int reuse = qdict_get_try_bool(qdict, "reuse", 0);
+    enum NewImageMode mode;
     Error *errp = NULL;
 
     if (!filename) {
@@ -702,7 +704,9 @@ void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
         return;
     }
 
-    qmp_blockdev_snapshot_sync(device, filename, !!format, format, &errp);
+    mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
+    qmp_blockdev_snapshot_sync(device, filename, !!format, format,
+                               true, mode, &errp);
     hmp_handle_error(mon, &errp);
 }
 
qapi-schema.json +8 -7
index b062d01..0d24240 100644
@@ -1143,6 +1143,9 @@
 # @snapshot-file: the target of the new image. A new file will be created.
 #
 # @format: #optional the format of the snapshot image, default is 'qcow2'.
+#
+# @mode: #optional whether and how QEMU should create a new image, default is
+# 'absolute-paths'.
 ##
 { 'type': 'BlockdevSnapshot',
   'data': { 'device': 'str', 'snapshot-file': 'str', '*format': 'str',
@@ -1199,21 +1202,19 @@
 #
 # @format: #optional the format of the snapshot image, default is 'qcow2'.
 #
+# @mode: #optional whether and how QEMU should create a new image, default is
+# 'absolute-paths'.
+#
 # Returns: nothing on success
 #          If @device is not a valid block device, DeviceNotFound
 #          If @snapshot-file can't be opened, OpenFileFailed
 #          If @format is invalid, InvalidBlockFormat
 #
-# Notes: One of the last steps taken by this command is to close the current
-#        image being used by @device and open the @snapshot-file one. If that
-#        fails, the command will try to reopen the original image file. If
-#        that also fails OpenFileFailed will be returned and the guest may get
-#        unexpected errors.
-#
 # Since 0.14.0
 ##
 { 'command': 'blockdev-snapshot-sync',
-  'data': { 'device': 'str', 'snapshot-file': 'str', '*format': 'str' } }
+  'data': { 'device': 'str', 'snapshot-file': 'str', '*format': 'str',
+            '*mode': 'NewImageMode'} }
 
 ##
 # @human-monitor-command:
qmp-commands.hx +2 -0
index 7c03b62..dfe8a5b 100644
@@ -760,6 +760,8 @@ Arguments:
 
 - "device": device name to snapshot (json-string)
 - "snapshot-file": name of new image file (json-string)
+- "mode": whether and how QEMU should create the snapshot file
+  (NewImageMode, optional, default "absolute-paths")
 - "format": format of new image (json-string, optional)
 
 Example:
Signature · 1 line
1.7.7.6
#7 lore raw

PATCH v4 7/10 Add blkmirror block driver

In reply to Paolo Bonzini

From: Marcelo Tosatti <[email protected]>

Mirrored writes are used by live block copy.

---
 Makefile.objs     |    2 +-
 block/blkmirror.c |  239 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 240 insertions(+), 1 deletions(-)
 create mode 100644 block/blkmirror.c
Makefile.objs +1 -1
index 808de6a..982f44b 100644
@@ -74,7 +74,7 @@ fsdev-obj-$(CONFIG_VIRTFS) += $(addprefix fsdev/, $(fsdev-nested-y))
 # suppress *all* target specific code in case of system emulation, i.e. a
 # single QEMU executable should support all CPUs and machines.
 
-common-obj-y = $(block-obj-y) blockdev.o
+common-obj-y = $(block-obj-y) blockdev.o block/blkmirror.o
 common-obj-y += $(net-obj-y)
 common-obj-y += $(qobject-obj-y)
 common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))
block/blkmirror.c new file +239 -0

239 lines not on this page · raw message

Signature · 1 line
1.7.7.6
#8 lore raw

PATCH v4 8/10 add mirroring to transaction

In reply to Paolo Bonzini

With it comes a new image creation mode, "no-backing-file", that can be used to stream an image so that the destination does not need the original image's backing file(s).

Both bdrv_append and blkmirror will set the backing_hd on the target, even if the image is created without one, so that both streaming and copy-on-write work properly (at least with qcow2 or qed, not raw).

Streaming mode works with the following gotchas:

- streaming will rewrite every bit of the source image;

- zero writes are not supported by the blkmirror driver, hence both the source and the destination image will grow to full size.

---
 blockdev.c       |   53 +++++++++++++++++++++++++++++++++++++++++++----------
 qapi-schema.json |   22 ++++++++++++++++++++--
 qmp-commands.hx  |   16 +++++++++++++---
 3 files changed, 76 insertions(+), 15 deletions(-)
blockdev.c +43 -10
index 067cc10..9d3a362 100644
@@ -693,6 +693,7 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
     int ret = 0;
     BlockdevActionList *dev_entry = dev_list;
     BlkTransactionStates *states, *next;
+    char *new_source = NULL;
 
     QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
     QSIMPLEQ_INIT(&snap_bdrv_states);
@@ -704,7 +705,8 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
     while (NULL != dev_entry) {
         BlockdevAction *dev_info = NULL;
         BlockDriver *proto_drv;
-        BlockDriver *drv;
+        BlockDriver *target_drv;
+        BlockDriver *drv = NULL;
         int flags;
         enum NewImageMode mode;
         const char *new_image_file;
@@ -728,16 +730,36 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
                 format = dev_info->blockdev_snapshot_sync->format;
             }
             mode = dev_info->blockdev_snapshot_sync->mode;
+            new_source = g_strdup(new_image_file);
             break;
+
+        case BLOCKDEV_ACTION_KIND_DRIVE_MIRROR:
+            device = dev_info->drive_mirror->device;
+            drv = bdrv_find_format("blkmirror");
+            if (!dev_info->drive_mirror->has_mode) {
+                dev_info->drive_mirror->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
+            }
+            new_image_file = dev_info->drive_mirror->target;
+            if (dev_info->drive_mirror->has_format) {
+                format = dev_info->drive_mirror->format;
+            }
+            mode = dev_info->drive_mirror->mode;
+            new_source = g_strdup_printf("blkmirror:%s:%s", format,
+                                         dev_info->drive_mirror->target);
+            break;
+
         default:
             abort();
         }
 
-        drv = bdrv_find_format(format);
-        if (!drv) {
+        target_drv = bdrv_find_format(format);
+        if (!target_drv) {
             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
             goto delete_and_fail;
         }
+        if (!drv) {
+            drv = target_drv;
+        }
 
         states->old_bs = bdrv_find(device);
         if (!states->old_bs) {
@@ -761,7 +783,7 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
 
         flags = states->old_bs->open_flags;
 
-        proto_drv = bdrv_find_protocol(new_image_file);
+        proto_drv = bdrv_find_protocol(new_source);
         if (!proto_drv) {
             error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
             goto delete_and_fail;
@@ -769,10 +791,18 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
 
         /* create new image w/backing file */
         if (mode != NEW_IMAGE_MODE_EXISTING) {
-            ret = bdrv_img_create(new_image_file, format,
-                                  states->old_bs->filename,
-                                  states->old_bs->drv->format_name,
-                                  NULL, -1, flags);
+            if (mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) {
+                ret = bdrv_img_create(new_image_file, format,
+                                      states->old_bs->filename,
+                                      states->old_bs->drv->format_name,
+                                      NULL, -1, flags);
+            } else {
+                uint64_t size;
+                bdrv_get_geometry(states->old_bs, &size);
+                size *= 512;
+                ret = bdrv_img_create(new_image_file, format,
+                                      NULL, NULL, NULL, size, flags);
+            }
             if (ret) {
                 error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
                 goto delete_and_fail;
@@ -781,12 +811,14 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
 
         /* We will manually add the backing_hd field to the bs later */
         states->new_bs = bdrv_new("");
-        ret = bdrv_open(states->new_bs, new_image_file,
+        ret = bdrv_open(states->new_bs, new_source,
                         flags | BDRV_O_NO_BACKING, drv);
         if (ret != 0) {
-            error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
+            error_set(errp, QERR_OPEN_FILE_FAILED, new_source);
             goto delete_and_fail;
         }
+        g_free(new_source);
+        new_source = NULL;
     }
 
 
@@ -814,6 +846,7 @@ exit:
     QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
         g_free(states);
     }
+    g_free(new_source);
     return;
 }
 
qapi-schema.json +21 -1
index 0d24240..75ac978 100644
@@ -1133,10 +1133,12 @@
 # @absolute-paths: QEMU should create a new image with absolute paths
 # for the backing file.
 #
+# @no-backing-file: QEMU should create a new image with no backing file.
+#
 # Since: 1.1
 ##
 { 'enum': 'NewImageMode'
-  'data': [ 'existing', 'absolute-paths' ] }
+  'data': [ 'existing', 'absolute-paths', 'no-backing-file' ] }
 
 ##
 # @BlockdevSnapshot
@@ -1152,6 +1152,23 @@
             '*mode': 'NewImageMode' } }
 
 ##
+# @BlockdevMirror
+#
+# @device:  the name of the device to start mirroring.
+#
+# @target: the image that will start receiving writes for @device. A new
+#          file will be created unless @mode is "existing".
+#
+# @format: #optional the format of the target image, default is 'qcow2'.
+#
+# @mode: #optional whether and how QEMU should create a new image, default is
+# 'absolute-paths'.
+##
+{ 'type': 'BlockdevMirror',
+  'data': { 'device': 'str', 'target': 'str', '*format': 'str',
+            '*mode': 'NewImageMode' } }
+
+##
 # @BlockdevAction
 #
 # A discriminated record of operations that can be performed with
@@ -1159,6 +1176,7 @@
 { 'union': 'BlockdevAction',
   'data': {
        'blockdev-snapshot-sync': 'BlockdevSnapshot',
+       'drive-mirror': 'BlockdevMirror',
    } }
 
 ##
qmp-commands.hx +13 -3
index dfe8a5b..0438632 100644
@@ -696,12 +696,16 @@ SQMP
 transaction
 -----------
 
-Atomically operate on one or more block devices.  The only supported
-operation for now is snapshotting.  If there is any failure performing
+Atomically operate on one or more block devices, snapshotting them
+or enabling mirrored writes.  If there is any failure performing
 any of the operations, all snapshots for the group are abandoned, and
 the original disks pre-snapshot attempt are used.
 
+Mirrored writes keep the previous image file open, and start writing
+data also to the new file specified in the command.
+
 A list of dictionaries is accepted, that contains the actions to be performed.
+
 For snapshots this is the device, the file to use for the new snapshot,
 and the format.  The default format, if not specified, is qcow2.
 
@@ -716,7 +720,7 @@ Arguments:
 
 actions array:
     - "type": the operation to perform.  The only supported
-      value is "blockdev-snapshot-sync". (json-string)
+      values are "blockdev-snapshot-sync" and "mirror". (json-string)
     - "data": a dictionary.  The contents depend on the value
       of "type".  When "type" is "blockdev-snapshot-sync":
       - "device": device name to snapshot (json-string)
@@ -724,6 +728,12 @@ actions array:
       - "format": format of new image (json-string, optional)
       - "mode": whether and how QEMU should create the snapshot file
         (NewImageMode, optional, default "absolute-paths")
+      When "type" is "drive-mirror":
+      - "device": device name to snapshot (json-string)
+      - "target": name of destination image file (json-string)
+      - "format": format of new image (json-string, optional)
+      - "mode": how QEMU should look for an existing image file
+        (NewImageMode, optional, default "absolute-paths")
 
 Example:
 
Signature · 1 line
1.7.7.6
#9 lore raw

PATCH v4 9/10 add drive-mirror command and HMP equivalent

In reply to Paolo Bonzini

Since QMP transactions are supposed to take (pseudo) QMP commands, add a standalone drive-mirror command.

The corresponding HMP command does not provide atomic snapshot+mirror, but it can still be used together with image streaming.

---
 blockdev.c       |   15 +++++++++++++++
 hmp-commands.hx  |   22 ++++++++++++++++++++++
 hmp.c            |   28 ++++++++++++++++++++++++++++
 hmp.h            |    1 +
 qapi-schema.json |   27 +++++++++++++++++++++++++++
 qmp-commands.hx  |   33 +++++++++++++++++++++++++++++++++
 6 files changed, 126 insertions(+), 0 deletions(-)
blockdev.c +15 -0
index 9d3a362..2958419 100644
@@ -675,6 +675,21 @@ void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
                        errp);
 }
 
+void qmp_drive_mirror(const char *device, const char *target,
+                      bool has_format, const char *format,
+                      bool has_mode, enum NewImageMode mode, Error **errp)
+{
+    BlockdevMirror mirror = {
+        .device = (char *) device,
+        .target = (char *) target,
+        .has_format = has_format,
+        .format = (char *) format,
+        .has_mode = has_mode,
+        .mode = mode,
+    };
+    blockdev_do_action(BLOCKDEV_ACTION_KIND_DRIVE_MIRROR, &mirror, errp);
+}
+
 
 /* New and old BlockDriverState structs for group snapshots */
 typedef struct BlkTransactionStates {
hmp-commands.hx +22 -0
index 6980214..9c49c33 100644
@@ -903,6 +903,28 @@ Snapshot device, using snapshot file as target if provided
 ETEXI
 
     {
+        .name       = "drive_mirror",
+        .args_type  = "reuse:-n,no-backing:-s,device:B,target:s,format:s?",
+        .params     = "[-n] [-s] device [new-image-file] [format]",
+        .help       = "initiates live storage\n\t\t\t"
+                      "migration for a device. New writes are mirrored to the\n\t\t\t"
+                      "specified new image file, and the block_stream\n\t\t\t"
+                      "command can then be started.\n\t\t\t"
+                      "The default format is qcow2.  The -n flag requests QEMU\n\t\t\t"
+                      "to reuse the image found in new-image-file, instead of\n\t\t\t"
+                      "recreating it from scratch.  The -s flag requests QEMU\n\t\t\t"
+                      "to create a root image, that does not have the current\n\t\t\t"
+                      "image as the backing file.",
+        .mhandler.cmd = hmp_drive_mirror,
+    },
+STEXI
+@item drive_mirror
+@findex drive_mirror
+Start mirroring a block device's writes to a new destination,
+using the specified target.
+ETEXI
+
+    {
         .name       = "drive_add",
         .args_type  = "pci_addr:s,opts:s",
         .params     = "[[<domain>:]<bus>:]<slot>\n"
hmp.c +28 -0
index 290c43d..e706db9 100644
@@ -687,6 +687,34 @@ void hmp_block_resize(Monitor *mon, const QDict *qdict)
     hmp_handle_error(mon, &errp);
 }
 
+void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
+{
+    const char *device = qdict_get_str(qdict, "device");
+    const char *filename = qdict_get_try_str(qdict, "target");
+    const char *format = qdict_get_try_str(qdict, "format");
+    int reuse = qdict_get_try_bool(qdict, "reuse", 0);
+    int no_backing = qdict_get_try_bool(qdict, "no-backing", 0);
+    enum NewImageMode mode;
+    Error *errp = NULL;
+
+    if (!filename) {
+        error_set(&errp, QERR_MISSING_PARAMETER, "target");
+        hmp_handle_error(mon, &errp);
+        return;
+    }
+
+    if (reuse) {
+        mode = NEW_IMAGE_MODE_EXISTING;
+    } else if (no_backing) {
+        mode = NEW_IMAGE_MODE_NO_BACKING_FILE;
+    } else {
+        mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
+    }
+
+    qmp_drive_mirror(device, filename, !!format, format, true, mode, &errp);
+    hmp_handle_error(mon, &errp);
+}
+
 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
 {
     const char *device = qdict_get_str(qdict, "device");
hmp.h +1 -0
index 5409464..5352f00 100644
@@ -48,6 +48,7 @@ void hmp_block_passwd(Monitor *mon, const QDict *qdict);
 void hmp_balloon(Monitor *mon, const QDict *qdict);
 void hmp_block_resize(Monitor *mon, const QDict *qdict);
 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict);
+void hmp_drive_mirror(Monitor *mon, const QDict *qdict);
 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict);
 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict);
 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict);
qapi-schema.json +27 -0
index 75ac978..4cebf78 100644
@@ -1263,6 +1263,33 @@
   'returns': 'str' } 
 
 ##
+# @drive-mirror
+#
+# Start mirroring a block device's writes to a new destination.
+#
+# @device:  the name of the device whose writes should be mirrored.
+#
+# @target: the target of the new image. If the file exists, or if it
+#          is a device, the existing file/device will be used as the new
+#          destination.  If it does not exist, a new file will be created.
+#
+# @format: #optional the format of the new destination, default is 'qcow2'.
+#
+# @mode: #optional whether and how QEMU should create a new image, default is
+# 'absolute-paths'.
+#
+# Returns: nothing on success
+#          If @device is not a valid block device, DeviceNotFound
+#          If @target can't be opened, OpenFileFailed
+#          If @format is invalid, InvalidBlockFormat
+#
+# Since 1.1
+##
+{ 'command': 'drive-mirror',
+  'data': { 'device': 'str', 'target': 'str', '*format': 'str',
+            '*mode': 'NewImageMode'} }
+
+##
 # @migrate_cancel
 #
 # Cancel the current executing migration process.
qmp-commands.hx +33 -0
index 0438632..122ebe7 100644
@@ -785,6 +785,39 @@ Example:
 EQMP
 
     {
+        .name       = "drive-mirror",
+        .args_type  = "device:B,snapshot-file:s,mode:s?,format:s?",
+        .mhandler.cmd_new = qmp_marshal_input_drive_mirror,
+    },
+
+SQMP
+drive-mirror
+------------
+
+Start mirroring a block device's writes to a new destination. target
+specifies the target of the new image. If the file exists, or if it is
+a device, it will be used as the new destination for writes. If does not
+exist, a new file will be created. format specifies the format of the
+mirror image, default is qcow2.
+
+Arguments:
+
+- "device": device name to operate on (json-string)
+- "target": name of new image file (json-string)
+- "format": format of new image (json-string, optional)
+- "mode": how an image file should be created into the target
+  file/device (NewImageMode, optional, default 'absolute-paths')
+
+Example:
+
+-> { "execute": "drive-mirror", "arguments": { "device": "ide-hd0",
+                                               "target": "/some/place/my-image",
+                                               "format": "qcow2" } }
+<- { "return": {} }
+
+EQMP
+
+    {
         .name       = "balloon",
         .args_type  = "value:M",
         .mhandler.cmd_new = qmp_marshal_input_balloon,
Signature · 1 line
1.7.7.6
#10 lore raw

PATCH v4 10/10 Add the drive-reopen command

In reply to Paolo Bonzini

From: Federico Simoncelli <[email protected]>

---
 blockdev.c       |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hmp-commands.hx  |   16 +++++++++++++
 hmp.c            |   11 +++++++++
 hmp.h            |    1 +
 qapi-schema.json |   22 ++++++++++++++++++
 qmp-commands.hx  |   30 +++++++++++++++++++++++++
 6 files changed, 143 insertions(+), 0 deletions(-)
blockdev.c +63 -0
index 2958419..df8ce14 100644
@@ -646,6 +646,69 @@ void do_commit(Monitor *mon, const QDict *qdict)
     }
 }
 
+static void change_blockdev_image(const char *device, const char *new_image_file,
+                                  const char *format, Error **errp)
+{
+    BlockDriverState *bs;
+    BlockDriver *drv, *old_drv, *proto_drv;
+    int ret = 0;
+    int flags;
+    char old_filename[1024];
+
+    bs = bdrv_find(device);
+    if (!bs) {
+        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
+        return;
+    }
+    if (bdrv_in_use(bs)) {
+        error_set(errp, QERR_DEVICE_IN_USE, device);
+        return;
+    }
+
+    pstrcpy(old_filename, sizeof(old_filename), bs->filename);
+
+    old_drv = bs->drv;
+    flags = bs->open_flags;
+
+    drv = bdrv_find_format(format);
+    if (!drv) {
+        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
+        return;
+    }
+
+    proto_drv = bdrv_find_protocol(new_image_file);
+    if (!proto_drv) {
+        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
+        return;
+    }
+
+    bdrv_drain_all();
+    bdrv_flush(bs);
+
+    bdrv_close(bs);
+    ret = bdrv_open(bs, new_image_file, flags, drv);
+    /*
+     * If reopening the image file we just created fails, fall back
+     * and try to re-open the original image. If that fails too, we
+     * are in serious trouble.
+     */
+    if (ret != 0) {
+        ret = bdrv_open(bs, old_filename, flags, old_drv);
+        if (ret != 0) {
+            error_set(errp, QERR_OPEN_FILE_FAILED, old_filename);
+        } else {
+            error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
+        }
+    }
+}
+
+void qmp_drive_reopen(const char *device, const char *new_image_file,
+                      bool has_format, const char *format, Error **errp)
+{
+    change_blockdev_image(device, new_image_file,
+                          has_format ? format : "qcow2", errp);
+}
+
 static void blockdev_do_action(int kind, void *data, Error **errp)
 {
     BlockdevAction action;
hmp-commands.hx +16 -0
index 9c49c33..4afde71 100644
@@ -925,6 +925,22 @@ using the specified target.
 ETEXI
 
     {
+        .name       = "drive_reopen",
+        .args_type  = "device:B,new-image-file:s,format:s?",
+        .params     = "device new-image-file [format]",
+        .help       = "Assigns a new image file to a device.\n\t\t\t"
+                      "The image will be opened using the format\n\t\t\t"
+                      "specified or 'qcow2' by default.\n\t\t\t",
+        .mhandler.cmd = hmp_drive_reopen,
+    },
+
+STEXI
+@item drive_reopen
+@findex drive_reopen
+Assigns a new image file to a device.
+ETEXI
+
+    {
         .name       = "drive_add",
         .args_type  = "pci_addr:s,opts:s",
         .params     = "[[<domain>:]<bus>:]<slot>\n"
hmp.c +11 -0
index e706db9..ec41d83 100644
@@ -738,6 +738,17 @@ void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
     hmp_handle_error(mon, &errp);
 }
 
+void hmp_drive_reopen(Monitor *mon, const QDict *qdict)
+{
+    const char *device = qdict_get_str(qdict, "device");
+    const char *filename = qdict_get_str(qdict, "new-image-file");
+    const char *format = qdict_get_try_str(qdict, "format");
+    Error *errp = NULL;
+
+    qmp_drive_reopen(device, filename, !!format, format, &errp);
+    hmp_handle_error(mon, &errp);
+}
+
 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
 {
     qmp_migrate_cancel(NULL);
hmp.h +1 -0
index 5352f00..648a84f 100644
@@ -49,6 +49,7 @@ void hmp_balloon(Monitor *mon, const QDict *qdict);
 void hmp_block_resize(Monitor *mon, const QDict *qdict);
 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict);
 void hmp_drive_mirror(Monitor *mon, const QDict *qdict);
+void hmp_drive_reopen(Monitor *mon, const QDict *qdict);
 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict);
 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict);
 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict);
qapi-schema.json +22 -0
index 4cebf78..21eb256 100644
@@ -1235,6 +1235,28 @@
             '*mode': 'NewImageMode'} }
 
 ##
+# @drive-reopen
+#
+# Assigns a new image file to a device.
+#
+# @device: the name of the device for which we are changing the image file.
+#
+# @new-image-file: the target of the new image. If the file doesn't exists the
+#                  command will fail.
+#
+# @format: #optional the format of the new image, default is 'qcow2'.
+#
+# Returns: nothing on success
+#          If @device is not a valid block device, DeviceNotFound
+#          If @new-image-file can't be opened, OpenFileFailed
+#          If @format is invalid, InvalidBlockFormat
+#
+# Since 1.1
+##
+{ 'command': 'drive-reopen',
+  'data': { 'device': 'str', 'new-image-file': 'str', '*format': 'str' } }
+
+##
 # @human-monitor-command:
 #
 # Execute a command on the human monitor and return the output.
qmp-commands.hx +30 -0
index 122ebe7..7fc30c2 100644
@@ -818,6 +818,36 @@ Example:
 EQMP
 
     {
+        .name       = "drive-reopen",
+        .args_type  = "device:B,new-image-file:s,format:s?",
+        .mhandler.cmd_new = qmp_marshal_input_drive_reopen,
+    },
+
+SQMP
+drive-reopen
+------------
+
+Assigns a new image file to a device. Except extremely rare cases where the
+guest is expecting the drive to change its content, the new image should
+contain the same data of the current one.  One use case is to terminate
+a drive-mirror command.
+
+Arguments:
+
+- "device": device name to operate on (json-string)
+- "new-image-file": name of new image file (json-string)
+- "format": format of new image (json-string, optional)
+
+Example:
+
+-> { "execute": "drive-reopen", "arguments": {"device": "ide-hd0",
+                                    "new-image-file": "/some/place/my-image",
+                                    "format": "qcow2" } }
+<- { "return": {} }
+
+EQMP
+
+    {
         .name       = "balloon",
         .args_type  = "value:M",
         .mhandler.cmd_new = qmp_marshal_input_balloon,
Signature · 1 line
1.7.7.6
#11 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Quoted 16 lines · Paolo Bonzini

From: Federico Simoncelli <[email protected]>

##

qapi-schema.json +10 -0
+# @drive-reopen
+#
+# Assigns a new image file to a device.
+#
+# @device: the name of the device for which we are changing the image file.
+#
+# @new-image-file: the target of the new image. If the file doesn't exists the
+#                  command will fail.
+#
+# @format: #optional the format of the new image, default is 'qcow2'.

qapi-schema.json line 1257

Quoted 10 lines
qapi-schema.json +10 -0
+#
+# Returns: nothing on success
+#          If @device is not a valid block device, DeviceNotFound
+#          If @new-image-file can't be opened, OpenFileFailed
+#          If @format is invalid, InvalidBlockFormat
+#
+# Since 1.1
+##
+{ 'command': 'drive-reopen',
+  'data': { 'device': 'str', 'new-image-file': 'str', '*format': 'str' } }

I still think we need a 'drive-reopen' action included in 'transaction', as an 11/10 on this series. For disk migration, it is true that you can migrate one disk at a time, and therefore only need to reopen one disk at a time, to get the guarantee that for a single disk image, the current state of that image will be guaranteed to be consistent using only one storage domain.

But since the API allows the creation of two mirrors in one command, I'm worried that someone will try to start a mirror on two disks at once, but then be stuck doing two separate 'drive-reopen' commands. If the first succeeds but the second fails, you have now stranded the qemu process across two storage domains, which is exactly what we were trying to avoid in the first place by inventing transactions. That is, even if all disks are individually consistent in a single domain, the act of migrating then reopening one disk at a time means you will have a window where disk 1 and disk 2 are opened on different storage domains.

Besides, I'm planning on implementing libvirt support for the 'drive-reopen' command by adding a flag to virDomainSnapshotDelete (basically, the presence of the flag states that for all mirrored disks in a given snapshot, libvirt will then issue a drive-reopen that pivots qemu over to the mirror, and finally delete the snapshot now that mirroring is no longer needed). With separate commands, if drive-reopen on disk 1 succeeds, then drive-reopen on disk 2 fails, I can attempt a rollback by doing another drive-reopen on disk 1; but the rollback will be incomplete since I have lost the ability to reopen the mirroring. I would much rather issue a 'transaction' with multiple reopen commands, and knowing that either all disks were reopened and the mirrors discarded, or that none were reopened and the mirroring remains intact.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#12 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

----- Original Message -----

Quoted 39 lines

From: "Eric Blake" <[email protected]>
To: "Paolo Bonzini" <[email protected]>
Cc: [email protected], [email protected], [email protected], [email protected], [email protected]
Sent: Tuesday, March 13, 2012 9:48:10 PM
Subject: Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Quoted 29 lines · Paolo Bonzini

From: Federico Simoncelli <[email protected]>

##

qapi-schema.json +5 -0
+# @drive-reopen
+#
+# Assigns a new image file to a device.
+#
+# @device: the name of the device for which we are changing the

image file.

(quoted diff) +2 -0
+#
+# @new-image-file: the target of the new image. If the file

doesn't exists the

qapi-schema.json +3 -0
+#                  command will fail.
+#
+# @format: #optional the format of the new image, default is

'qcow2'.

qapi-schema.json +10 -0
+#
+# Returns: nothing on success
+#          If @device is not a valid block device, DeviceNotFound
+#          If @new-image-file can't be opened, OpenFileFailed
+#          If @format is invalid, InvalidBlockFormat
+#
+# Since 1.1
+##
+{ 'command': 'drive-reopen',
+  'data': { 'device': 'str', 'new-image-file': 'str', '*format':
Quoted 1 line

'str' } }

I still think we need a 'drive-reopen' action included in

Quoted 8 lines

'transaction',
as an 11/10 on this series. For disk migration, it is true that you can
migrate one disk at a time, and therefore only need to reopen one disk
at a time, to get the guarantee that for a single disk image, the current state of that image will be guaranteed to be consistent using only one storage domain.

I'm not sure if this was already addressed on this mailing list but the main problem is that as general rule a qcow file cannot be opened in r/w mode twice. I believe there was only one exception to that with the live migration and it generated several issues.

That said, reopen is really hard to be implemented as a transaction without breaking that rule. For example in the blkmirror case you'd need to open the destination image in r/w while the mirroring is in action (already having the same image in r/w mode).

There are several solutions here but they are either really hard to implement or non definitive. For example:

* We could try to implement the reopen command for each special case,
eg: blkmirror, reopening the same image, etc... and in such cases reusing the same bs that we already have. The downside is that this command will be coupled with all this special cases.

* We could use the transaction APIs without actually making it transaction (if we fail in the middle we can't rollback). The only advantage of this is that we'd provide a consistent API to libvirt and we would postpone the problem to the future. Anyway I strongly discourage this as it's completely unsafe and it's going to break the transaction semantic. Moreover it's a solution that relies too much on the hope of finding something appropriate in the future.

* We could leave it as it is, a distinct command that is not part of the transaction and that it's closing the old image before opening the new one.

Quoted 5 lines

But since the API allows the creation of two mirrors in one command, I'm
worried that someone will try to start a mirror on two disks at once, but then be stuck doing two separate 'drive-reopen' commands. If the first succeeds but the second fails, you have now stranded the qemu

Quoted 8 lines

process across two storage domains, which is exactly what we were trying
to avoid in the first place by inventing transactions. That is, even if
all disks are individually consistent in a single domain, the act of migrating then reopening one disk at a time means you will have a window
where disk 1 and disk 2 are opened on different storage domains.

This is not completely correct, the main intent was to not spread one image chain across two storage domains (making it incomplete if one of them was missing). In the next oVirt release a VM can have different disks on different storage domains, so this wouldn't be a special case but just a normal situation.

Signature · 1 line
Federico
#13 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Am 14.03.2012 01:14, schrieb Federico Simoncelli:

Quoted 49 lines

----- Original Message -----

Quoted 39 lines

From: "Eric Blake" <[email protected]>
To: "Paolo Bonzini" <[email protected]>
Cc: [email protected], [email protected], [email protected], [email protected], [email protected]
Sent: Tuesday, March 13, 2012 9:48:10 PM
Subject: Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Quoted 29 lines · Paolo Bonzini

From: Federico Simoncelli <[email protected]>

##

qapi-schema.json +5 -0
+# @drive-reopen
+#
+# Assigns a new image file to a device.
+#
+# @device: the name of the device for which we are changing the

image file.

(quoted diff) +2 -0
+#
+# @new-image-file: the target of the new image. If the file

doesn't exists the

qapi-schema.json +3 -0
+#                  command will fail.
+#
+# @format: #optional the format of the new image, default is

'qcow2'.

qapi-schema.json +10 -0
+#
+# Returns: nothing on success
+#          If @device is not a valid block device, DeviceNotFound
+#          If @new-image-file can't be opened, OpenFileFailed
+#          If @format is invalid, InvalidBlockFormat
+#
+# Since 1.1
+##
+{ 'command': 'drive-reopen',
+  'data': { 'device': 'str', 'new-image-file': 'str', '*format':
Quoted 1 line

'str' } }

I still think we need a 'drive-reopen' action included in

Quoted 8 lines

'transaction',
as an 11/10 on this series. For disk migration, it is true that you can
migrate one disk at a time, and therefore only need to reopen one disk
at a time, to get the guarantee that for a single disk image, the current state of that image will be guaranteed to be consistent using only one storage domain.

Quoted 4 lines

I'm not sure if this was already addressed on this mailing list but the main problem is that as general rule a qcow file cannot be opened in r/w mode twice. I believe there was only one exception to that with the live migration and it generated several issues.

In fact the same is true for any image. There are just some special cases that happen to work anyway, but using them is not more than a hack.

Quoted 5 lines

That said, reopen is really hard to be implemented as a transaction without breaking that rule. For example in the blkmirror case you'd need to open the destination image in r/w while the mirroring is in action (already having the same image in r/w mode).

Quoted 7 lines

There are several solutions here but they are either really hard to implement or non definitive. For example:

* We could try to implement the reopen command for each special case,
eg: blkmirror, reopening the same image, etc... and in such cases reusing the same bs that we already have. The downside is that this command will be coupled with all this special cases.

The problem we're trying to solve is that we have a graph of open BlockDriverStates (connected by bs->file, backing file relations etc.) and we want to transform it into a different graph of open BDSs atomically, reusing zero, one or many of the existing BDSs, and possibly with changed properties (cache mode, read-only, etc.)

What we can have reasonably easily (there are patches floating around, they just need to be completed), is a bdrv_reopen() that changes flags on one given BDS, without changing the file it's backed by. This is already broken up into prepare/commit/abort stages as we need it to reopen VMDK's split images safely.

In theory this should be enough to build the new graph by opening yet unused BDSs, preparing the reopen of reused ones and only if all of that was successful, committing the bdrv_reopen and changing the relations between the nodes. I hope that at the same time it's clear that this isn't exactly trivial to implement.

Quoted 7 lines

* We could use the transaction APIs without actually making it transaction (if we fail in the middle we can't rollback). The only advantage of this is that we'd provide a consistent API to libvirt and we would postpone the problem to the future. Anyway I strongly discourage this as it's completely unsafe and it's going to break the transaction semantic. Moreover it's a solution that relies too much on the hope of finding something appropriate in the future.

This is not an option. Advertising transactional behaviour and not implementing it is just plain wrong.

Quoted 3 lines

* We could leave it as it is, a distinct command that is not part of the transaction and that it's closing the old image before opening the new one.

Yes, this would be the short-term preliminary solution. I would tend to leave it to downstreams to implement it as an extension, though.

Quoted 5 lines

This is not completely correct, the main intent was to not spread one image chain across two storage domains (making it incomplete if one of them was missing). In the next oVirt release a VM can have different disks on different storage domains, so this wouldn't be a special case but just a normal situation.

The problem with this kind of argument is that we're not developing only for oVirt, but need to look for what makes sense for any management tool (or even just direct users of qemu).

Kevin

#14 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Quoted 25 lines · Kevin Wolf
Quoted 5 lines

That said, reopen is really hard to be implemented as a transaction without breaking that rule. For example in the blkmirror case you'd need to open the destination image in r/w while the mirroring is in action (already having the same image in r/w mode).

Quoted 7 lines

There are several solutions here but they are either really hard to implement or non definitive. For example:

* We could try to implement the reopen command for each special case,
eg: blkmirror, reopening the same image, etc... and in such cases reusing the same bs that we already have. The downside is that this command will be coupled with all this special cases.

The problem we're trying to solve is that we have a graph of open BlockDriverStates (connected by bs->file, backing file relations etc.) and we want to transform it into a different graph of open BDSs atomically, reusing zero, one or many of the existing BDSs, and possibly with changed properties (cache mode, read-only, etc.)

What we can have reasonably easily (there are patches floating around, they just need to be completed), is a bdrv_reopen() that changes flags on one given BDS, without changing the file it's backed by. This is already broken up into prepare/commit/abort stages as we need it to reopen VMDK's split images safely.

Quoted 5 lines

In theory this should be enough to build the new graph by opening yet unused BDSs, preparing the reopen of reused ones and only if all of that was successful, committing the bdrv_reopen and changing the relations between the nodes. I hope that at the same time it's clear that this isn't exactly trivial to implement.

Agreed that 1) it looks implementable, and 2) it does not look trivial.

Quoted 11 lines
Quoted 8 lines

* We could use the transaction APIs without actually making it transaction (if we fail in the middle we can't rollback). The only advantage of this is that we'd provide a consistent API to libvirt and we would postpone the problem to the future. Anyway I strongly discourage this as it's completely unsafe and it's going to break the transaction semantic. Moreover it's a solution that relies too much on the hope of finding something appropriate in the future.

This is not an option. Advertising transactional behaviour and not implementing it is just plain wrong.

Absolutely concur. From libvirt's perspective, I will be assuming that:

if 'transaction' exists, then it supports 'blockdev-snapshot-sync' and 'drive-mirror' (assuming that these are the only two commands that are in 'transaction' in qemu 1.1), but nothing else is safe to use without a further probe. Then, if down the road, we go through the pain of making 'drive-reopen' transactionable, then there will be some new introspection command (one idea was an optional argument to query-commands which limits output to just the commands that can also occur in a 'transaction'), and libvirt will have to use that introspection before using the additional features.

Quoted 7 lines
Quoted 4 lines

* We could leave it as it is, a distinct command that is not part of the transaction and that it's closing the old image before opening the new one.

Yes, this would be the short-term preliminary solution. I would tend to leave it to downstreams to implement it as an extension, though.

Correct - I'm fine with libvirt using the direct, non-atomic, 'drive-reopen' for the immediate needs of oVirt while we work on a more complete solution for down the road.

Quoted 10 lines
Quoted 6 lines

This is not completely correct, the main intent was to not spread one image chain across two storage domains (making it incomplete if one of them was missing). In the next oVirt release a VM can have different disks on different storage domains, so this wouldn't be a special case but just a normal situation.

The problem with this kind of argument is that we're not developing only for oVirt, but need to look for what makes sense for any management tool (or even just direct users of qemu).

Indeed - that's why I still think it's dangerous to not have an atomic 'drive-reopen', even if oVirt can be coded to work in spite of an initial implementation being non-atomic. But there's a difference between wish-lists (atomic reopen) and practicality (what can we implement now), and I'm not going to insist on the impossible :)

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#15 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Am 14.03.2012 14:11, schrieb Eric Blake:

Quoted 11 lines
Quoted 10 lines
Quoted 7 lines

* We could use the transaction APIs without actually making it transaction (if we fail in the middle we can't rollback). The only advantage of this is that we'd provide a consistent API to libvirt and we would postpone the problem to the future. Anyway I strongly discourage this as it's completely unsafe and it's going to break the transaction semantic. Moreover it's a solution that relies too much on the hope of finding something appropriate in the future.

This is not an option. Advertising transactional behaviour and not implementing it is just plain wrong.

Quoted 6 lines

Absolutely concur. From libvirt's perspective, I will be assuming that:

if 'transaction' exists, then it supports 'blockdev-snapshot-sync' and 'drive-mirror' (assuming that these are the only two commands that are in 'transaction' in qemu 1.1), but nothing else is safe to use without a further probe.

I don't think it's good to conclude alone from the presence of 'transaction' that subcommands exist. Current master does have 'transaction' (and the 'blockdev-snapshot-sync' action), but not yet 'drive-mirror'.

Quoted 6 lines

Then, if down the road, we go through the pain of making
'drive-reopen' transactionable, then there will be some new introspection command (one idea was an optional argument to query-commands which limits output to just the commands that can also occur in a 'transaction'), and libvirt will have to use that introspection before using the additional features.

We can have the introspection command now. Either a new option like you suggest, or even simpler, just add a 'transactionable': 'bool' to the existing CommandInfo.

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

* We could leave it as it is, a distinct command that is not part of the transaction and that it's closing the old image before opening the new one.

Yes, this would be the short-term preliminary solution. I would tend to leave it to downstreams to implement it as an extension, though.

Correct - I'm fine with libvirt using the direct, non-atomic, 'drive-reopen' for the immediate needs of oVirt while we work on a more complete solution for down the road.

One important difference that probably matters even now is that the proposed drive-reopen can fail in the middle, where the old BDS is closed, but the new one couldn't be opened. In this case the disk will be lost. libvirt must handle this case in some way.

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

This is not completely correct, the main intent was to not spread one image chain across two storage domains (making it incomplete if one of them was missing). In the next oVirt release a VM can have different disks on different storage domains, so this wouldn't be a special case but just a normal situation.

The problem with this kind of argument is that we're not developing only for oVirt, but need to look for what makes sense for any management tool (or even just direct users of qemu).

Quoted 5 lines

Indeed - that's why I still think it's dangerous to not have an atomic 'drive-reopen', even if oVirt can be coded to work in spite of an initial implementation being non-atomic. But there's a difference between wish-lists (atomic reopen) and practicality (what can we implement now), and I'm not going to insist on the impossible :)

Well, at least upstream you always have the option to reject a feature instead of taking something broken. And I'm still not quite sure whether to use this option with part of this series or not.

Kevin

#16 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

In reply to Kevin Wolf

----- Original Message -----

Quoted 23 lines

From: "Kevin Wolf" <[email protected]>
To: "Federico Simoncelli" <[email protected]>
Cc: "Eric Blake" <[email protected]>, [email protected], [email protected], [email protected], "Paolo Bonzini" <[email protected]>, "Markus Armbruster" <[email protected]>
Sent: Wednesday, March 14, 2012 10:34:08 AM
Subject: Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Quoted 5 lines

* We could leave it as it is, a distinct command that is not part of
the transaction and that it's closing the old image before opening
the new one.

Yes, this would be the short-term preliminary solution. I would tend to
leave it to downstreams to implement it as an extension, though.

Quoted 5 lines

This is not completely correct, the main intent was to not spread one image chain across two storage domains (making it incomplete if one of them was missing). In the next oVirt release a VM can have different disks on different storage domains, so this wouldn't be a special case but just a normal situation.

Quoted 3 lines

The problem with this kind of argument is that we're not developing only for oVirt, but need to look for what makes sense for any management tool (or even just direct users of qemu).

That is a general rule, and it's perfectly agreeable, but it has nothing to do with what I was saying. Do you know any management tool or any reason to forbid to a VM to have different disks on different storages? In fact qemu-kvm doesn't even know where the images are coming from (what particular iscsi connection or what particular NFS server).

What I was asking was a tool (mirroring) to avoid the split of an image chain between multiple storages, and not a tool to avoid the ability to have full image chains on different storages.

There is nothing oVirt specific here, it came into play only when I said that for us is not a problem (for once :-).

Signature · 1 line
Federico
#17 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

In reply to Eric Blake

Am 13.03.2012 21:48, schrieb Eric Blake:

Quoted 22 lines · Paolo Bonzini
Quoted 21 lines

From: Federico Simoncelli <[email protected]>

##

qapi-schema.json +15 -0
+# @drive-reopen
+#
+# Assigns a new image file to a device.
+#
+# @device: the name of the device for which we are changing the image file.
+#
+# @new-image-file: the target of the new image. If the file doesn't exists the
+#                  command will fail.
+#
+# @format: #optional the format of the new image, default is 'qcow2'.
+#
+# Returns: nothing on success
+#          If @device is not a valid block device, DeviceNotFound
+#          If @new-image-file can't be opened, OpenFileFailed
+#          If @format is invalid, InvalidBlockFormat
Quoted 8 lines

qapi-schema.json line 1257

Quoted 5 lines
qapi-schema.json +5 -0
+#
+# Since 1.1
+##
+{ 'command': 'drive-reopen',
+  'data': { 'device': 'str', 'new-image-file': 'str', '*format': 'str' } }

I still think we need a 'drive-reopen' action included in 'transaction', as an 11/10 on this series.

If we want to do this, it needs to be the same patch, as we couple the transaction actions with top-level commands as long as there is no other way to discover the possible actions. And it probably makes more sense anyway, because the top-level command would be just a thin wrapper around the transactional one.

Only problem is that just moving the code there doesn't make it suitable for a transaction and doing an all-or-nothing drive-reopen isn't quite trivial.

Kevin

#18 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Il 14/03/2012 10:17, Kevin Wolf ha scritto:

Quoted 9 lines

If we want to do this, it needs to be the same patch, as we couple the transaction actions with top-level commands as long as there is no other way to discover the possible actions. And it probably makes more sense anyway, because the top-level command would be just a thin wrapper around the transactional one.

Only problem is that just moving the code there doesn't make it suitable for a transaction and doing an all-or-nothing drive-reopen isn't quite trivial.

We can also add a "transactionable" item to query-commands. If we do it after 1.1, in absence of it only blockdev-snapshot-sync and drive-mirror are transactionable. Otherwise we can do it now.

Paolo

#19 lore raw

Re: [Qemu-devel] [PATCH v4 10/10] Add the drive-reopen command

Am 14.03.2012 10:19, schrieb Paolo Bonzini:

Quoted 7 lines

Il 14/03/2012 10:17, Kevin Wolf ha scritto:

Quoted 6 lines

If we want to do this, it needs to be the same patch, as we couple the transaction actions with top-level commands as long as there is no other way to discover the possible actions. And it probably makes more sense anyway, because the top-level command would be just a thin wrapper around the transactional one.

Quoted 7 lines
Quoted 3 lines

Only problem is that just moving the code there doesn't make it suitable for a transaction and doing an all-or-nothing drive-reopen isn't quite trivial.

We can also add a "transactionable" item to query-commands. If we do it after 1.1, in absence of it only blockdev-snapshot-sync and drive-mirror are transactionable. Otherwise we can do it now.

Sounds reasonable. If we want to do it, I would prefer to do it right now.

Kevin

Applied
#20 lore raw
In reply to Paolo Bonzini

Am 06.03.2012 18:55, schrieb Paolo Bonzini:

Quoted 97 lines

v4 includes Federico's drive-reopen (patch 10) command, fixes another small bug in Jeff's code (patch 2), and tweaks the union handling for older compilers.

v3 tested with the following scenarios, v4 only d/e:

a) mirror only

1) create base.qcow2 and starat QEMU with it

2) Execute the following QMP command

{ "execute": "qmp_capabilities" }
{ "execute": "transaction", "arguments":
{'actions': [

    { 'type': 'drive-mirror', 'data' :
      { 'device': 'ide0-hd0', 'target': '/home/pbonzini/mirror.qcow2' } } ] } }

{ "execute": "cont" }

3) hibernate the guest (this requires an IDE disk and -cpu kvm64,-kvmclock)

4) restart the guest with mirror.qcow2

b) Same as (a) with drive-mirror command.

c) streaming to new image

1) start QEMU with an existing image test.img

2) execute the following HMP commands

drive_mirror -s ide0-hd0 /home/pbonzini/mirror.qed qed block_stream ide0-hd0

3) shut down the guest (sorry, this took a looong time and I forgot to hibernate here)

4) move away the base image

5) restart the guest with mirror.qed

d) atomic snapshot+mirror (QMP only):

1) start QEMU with an existing image test.img

2) Execute the following QMP command

{ "execute": "qmp_capabilities" }
{ "execute": "transaction", "arguments":
{'actions': [

    { 'type': 'blockdev-snapshot-sync', 'data' :
      { 'device': 'ide0-hd0', 'snapshot-file': '/home/pbonzini/base.qcow2' } },
    { 'type': 'drive-mirror', 'data' :
      { 'device': 'ide0-hd0', 'target': '/home/pbonzini/mirror.qcow2' } } ] } }

{ "execute": "cont" }

3) hibernate the guest (this requires an IDE disk and -cpu kvm64,-kvmclock)

4) check that mirror.qcow2 has test.img as the base

5) restart the guest with base.qcow2

6) restart the guest with mirror.qcow2

e) simple snapshot with snapshot_blkdev and snapshot_blkdev -n.

v3->v4:
avoid literals with anonymous unions in them, add "void *data" to all unions (Mark Wu); add drive-reopen (Federico); new patch to fix use-after-free in group snapshots; patch splitting nits (Eric).

v2->v3:
replace reuse argument with NewImageMode enum and mode argument; renamed all commands and actions; implemented blockdev-snapshot-sync in terms of group snasphots; added new drive-mirror command and HMP equivalent.

Federico Simoncelli (1):
Add the drive-reopen command

Marcelo Tosatti (1):
Add blkmirror block driver

Paolo Bonzini (8):
use QSIMPLEQ_FOREACH_SAFE when freeing list elements
fix format name for backing file
qapi: complete implementation of unions
rename blockdev-group-snapshot-sync
add mode field to blockdev-snapshot-sync transaction item
qmp: convert blockdev-snapshot-sync to a wrapper around transactions add mirroring to transaction
add drive-mirror command and HMP equivalent

Quoted 15 lines
 Makefile.objs             |    2 +-
 block/blkmirror.c         |  239 +++++++++++++++++++++++++++++++++++++++++++++
 blockdev.c                |  202 +++++++++++++++++++++++++++-----------
 hmp-commands.hx           |   47 ++++++++-
 hmp.c                     |   45 ++++++++-
 hmp.h                     |    2 +
 qapi-schema-test.json     |   10 ++
 qapi-schema.json          |  143 +++++++++++++++++++++++----
 qmp-commands.hx           |  135 +++++++++++++++++++++-----
 scripts/qapi-types.py     |    6 +
 scripts/qapi-visit.py     |   31 ++++++-
 test-qmp-input-visitor.c  |   18 ++++
 test-qmp-output-visitor.c |   34 +++++++
 13 files changed, 804 insertions(+), 110 deletions(-)
 create mode 100644 block/blkmirror.c

Thanks, applied patches 1-6 for now. I'll review the mirroring part later.

Kevin