Skip to contentSkip to conversation

Mirrored block writes

PATCH Superseded Paolo Bonzini 4 participants 23 messages Mar 5, 2012 – Mar 6, 2012

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Mirrored block writes

v3 comes with a new QMP command drive-mirror, an improved blockdev-snapshot-sync that just reuses transaction functionality, and a new image creation mode enum. I also tested that the command can be used to migrate without shared storage.

Tested with the following scenarios:

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

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.

Marcelo Tosatti (1):
Add blkmirror block driver

Paolo Bonzini (7):
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                |  232 ++++++++++++++++++++++++-------------------
 hmp-commands.hx           |   31 +++++-
 hmp.c                     |   34 ++++++-
 hmp.h                     |    1 +
 qapi-schema-test.json     |   10 ++
 qapi-schema.json          |  119 ++++++++++++++++++----
 qmp-commands.hx           |  105 +++++++++++++++-----
 scripts/qapi-types.py     |    5 +
 scripts/qapi-visit.py     |   31 ++++++-
 test-qmp-input-visitor.c  |   18 ++++
 test-qmp-output-visitor.c |   34 +++++++
 13 files changed, 707 insertions(+), 154 deletions(-)
 create mode 100644 block/blkmirror.c
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH v3 1/8 fix format name for backing file

---
 blockdev.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
blockdev.c +2 -1
index d78aa51..96a893b 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
#2 lore raw

PATCH v3 2/8 qapi: complete implementation of unions

In reply to Paolo Bonzini
---
 qapi-schema-test.json     |   10 ++++++++++
 scripts/qapi-types.py     |    5 +++++
 scripts/qapi-visit.py     |   31 ++++++++++++++++++++++++++++++-
 test-qmp-input-visitor.c  |   18 ++++++++++++++++++
 test-qmp-output-visitor.c |   34 ++++++++++++++++++++++++++++++++++
 5 files changed, 97 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 +5 -0
index b56225b..6968e7f 100644
@@ -269,6 +269,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 +284,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
#3 lore raw

Re: [Qemu-devel] [PATCH v3 2/8] qapi: complete implementation of unions

Quoted 186 lines · Paolo Bonzini
---
  qapi-schema-test.json     |   10 ++++++++++
  scripts/qapi-types.py     |    5 +++++
  scripts/qapi-visit.py     |   31 ++++++++++++++++++++++++++++++-
  test-qmp-input-visitor.c  |   18 ++++++++++++++++++
  test-qmp-output-visitor.c |   34 ++++++++++++++++++++++++++++++++++
  5 files changed, 97 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 +5 -0
index b56225b..6968e7f 100644
@@ -269,6 +269,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 +284,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 +32 -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))

test-qmp-output-visitor.c int main(int argc, char **argv) · line 455

Quoted 8 lines
test-qmp-output-visitor.c +2 -0
@@ -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();
 

I got the following error when I tried to compile it:
blockdev.c: In function ‘qmp_blockdev_snapshot_sync’:
blockdev.c:664: error: unknown field ‘blockdev_snapshot_sync’ specified in initializer
cc1: warnings being treated as errors
blockdev.c:664: error: missing braces around initializer
blockdev.c:664: error: (near initialization for ‘action.<anonymous>’) blockdev.c: In function ‘qmp_drive_mirror’:
blockdev.c:688: error: unknown field ‘drive_mirror’ specified in initializer blockdev.c:688: error: missing braces around initializer
blockdev.c:688: error: (near initialization for ‘action.<anonymous>’) blockdev.c:688: error: initialization from incompatible pointer type
make: *** [blockdev.o] Error 1
make: *** Waiting for unfinished jobs....

It seems we need a name for the union to reference its member. So I modified the scripts as the following patch. I also updated blockdev.c accordingly. After that I can compile it without error. Actually, I don't know why we need introduce a union for BlockdevAction. Can we just use a void pointer like "void *action_param" to replace the union? Or can we change the field ."snapshot_file to "target" too? Then they can share the same action parameter structure. It could make the code in qmp_transaction more compact because most of the code for cases mirror and snapshot are the same.

Mark

scripts/qapi-types.py +0 -0
index 6968e7f..de43790 100644
@@ -128,7 +128,7 @@ struct %(name)s

c_name=c_var(key))

ret += mcgen('''
- };
+ } u;
};
''')

scripts/qapi-visit.py +0 -0
index 54117d4..4f8ac4d 100644
@@ -125,7 +125,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **

for key in members:
ret += mcgen('''
case %(abbrev)s_KIND_%(enum)s:
- visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", errp);
+ visit_type_%(c_type)s(m, &(*obj)->u.%(c_name)s, "data", errp); break;
''',
abbrev = de_camel_case(name).upper(),

#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/8] qapi: complete implementation of unions

Il 06/03/2012 08:16, Mark Wu ha scritto:

Quoted 1 line

It seems we need a name for the union to reference its member.

What version is your compiler?

Quoted 7 lines

So I
modified the scripts as the following patch. I also updated blockdev.c accordingly. After that I can compile it without error. Actually, I don't know why we need introduce a union for BlockdevAction. Can we just use a void pointer like "void *action_param" to replace the union? Or can we change the field ."snapshot_file to "target" too? Then they can share the same action parameter structure.

No, the struct must match the existing blockdev-snapshot-sync command.

Paolo

#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/8] qapi: complete implementation of unions

Quoted 11 lines · Paolo Bonzini

Il 06/03/2012 08:16, Mark Wu ha scritto:

Quoted 1 line

It seems we need a name for the union to reference its member.

What version is your compiler?

Quoted 7 lines

So I
modified the scripts as the following patch. I also updated blockdev.c accordingly. After that I can compile it without error. Actually, I don't know why we need introduce a union for BlockdevAction. Can we just use a void pointer like "void *action_param" to replace the union? Or can we change the field ."snapshot_file to "target" too? Then they can share the same action parameter structure.

Quoted 4 lines

No, the struct must match the existing blockdev-snapshot-sync command.

Paolo

gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix
gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)

#6 lore raw

Re: [Qemu-devel] [PATCH v3 2/8] qapi: complete implementation of unions

Il 06/03/2012 09:19, Mark Wu ha scritto:

Quoted 10 lines

gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada

Quoted 8 lines

--enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)

And you do not have any problems compiling, for example, usb-redir.c or hw/usb-ehci.c?

Paolo

#7 lore raw

Re: [Qemu-devel] [PATCH v3 2/8] qapi: complete implementation of unions

Quoted 19 lines · Paolo Bonzini

Il 06/03/2012 09:19, Mark Wu ha scritto:

Quoted 10 lines

gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada

Quoted 8 lines

--enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)

Quoted 2 lines

And you do not have any problems compiling, for example, usb-redir.c or hw/usb-ehci.c?

I can compile hw/usb-ehci.c, but for usb-redir.c, I am not sure. The version of usbredir required by qemu is at least 0.3.4. I can't get the rpm package for RHEL. I tried to build it from source code, but I got another dependency issue:
Requested 'libusb-1.0 >= 1.0.9' but version of libusb-1.0 is 1.0.3. Then I give up. Any other files I can try to compile to narrow down the problem?

Thanks!!

Quoted 3 lines

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH v3 2/8] qapi: complete implementation of unions

In reply to Mark Wu
Quoted 16 lines

I got the following error when I tried to compile it:
blockdev.c: In function ‘qmp_blockdev_snapshot_sync’:
blockdev.c:664: error: unknown field ‘blockdev_snapshot_sync’ specified in initializer
cc1: warnings being treated as errors
blockdev.c:664: error: missing braces around initializer blockdev.c:664: error: (near initialization for ‘action.<anonymous>’) blockdev.c: In function ‘qmp_drive_mirror’:
blockdev.c:688: error: unknown field ‘drive_mirror’ specified in initializer
blockdev.c:688: error: missing braces around initializer blockdev.c:688: error: (near initialization for ‘action.<anonymous>’) blockdev.c:688: error: initialization from incompatible pointer type
make: *** [blockdev.o] Error 1
make: *** Waiting for unfinished jobs....

Quoted 1 line

It seems we need a name for the union to reference its member.

Ah, this is in the initializer. Please try the attached patch.

Paolo

blockdev.c +15 -20
index 9480dbb..df8ce14 100644
@@ -709,6 +709,18 @@ void qmp_drive_reopen(const char *device, const char *new_image_file,
                           has_format ? format : "qcow2", errp);
 }
 
+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,
@@ -722,16 +734,8 @@ void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
         .has_mode = has_mode,
         .mode = mode,
     };
-    BlockdevAction action = {
-        .kind = BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
-        .blockdev_snapshot_sync = &snapshot,
-    };
-    BlockdevActionList list = {
-        .value = &action,
-        .next = NULL
-    };
-
-    qmp_transaction(&list, errp);
+    blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot,
+                       errp);
 }
 
 void qmp_drive_mirror(const char *device, const char *target,
@@ -746,16 +750,7 @@ void qmp_drive_mirror(const char *device, const char *target,
         .has_mode = has_mode,
         .mode = mode,
     };
-    BlockdevAction action = {
-        .kind = BLOCKDEV_ACTION_KIND_DRIVE_MIRROR,
-        .drive_mirror = &mirror,
-    };
-    BlockdevActionList list = {
-        .value = &action,
-        .next = NULL
-    };
-
-    qmp_transaction(&list, errp);
+    blockdev_do_action(BLOCKDEV_ACTION_KIND_DRIVE_MIRROR, &mirror, errp);
 }
 
 
scripts/qapi-types.py +1 -0
index 6968e7f..727fb77 100644
@@ -117,6 +117,7 @@ struct %(name)s
 {
     %(name)sKind kind;
     union {
+        void *data;
 ''',
                 name=name)
 
#9 lore raw

PATCH v3 3/8 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 96a893b..b434cd4 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;
-    BlockDriver *proto_drv;
-    BlockDriver *drv;
-    int flags;
-    const char *format;
-    const char *snapshot_file;
+    BlockdevActionList *dev_entry = dev_list;
+    BlkTransactionStates *states;
 
-    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
#10 lore raw

PATCH v3 4/8 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 b434cd4..c9bd25b 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 +20 -1
index 1d1ffa6..b062d01 100644
@@ -1118,6 +1118,24 @@
 { '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.
+#
+# @no-backing-file: QEMU should create a new image with no 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
#11 lore raw

Re: [Qemu-devel] [PATCH v3 4/8] add mode field to blockdev-snapshot-sync transaction item

Quoted 18 lines · 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(-)

##

qapi-schema.json +1 -0
+# @NewImageMode

qapi-schema.json line 1131

Quoted 10 lines
qapi-schema.json +10 -0
+#
+# 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.
+#
+# @no-backing-file: QEMU should create a new image with no backing file.

Not present in this patch. Does it need to be rebased into the correct part of the series?

Quoted 25 lines
qapi-schema.json +9 -1
+#
+# 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 +0 -0
index fb4f1df..7c03b62 100644

qmp-commands.hx A list of dictionaries is accepted, that contains the actions to be performed. · line 709

Quoted 5 lines
qmp-commands.hx +2 -0
@@ -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),

so this is mode of 'absolute-paths', which is the default if mode is not present,

qmp-commands.hx A list of dictionaries is accepted, that contains the actions to be performed. · line 713

Quoted 5 lines

but it is also possible to reuse

qmp-commands.hx +4 -0
+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.

and this is mode 'existing'. Correct? If so, let's actually call that out in the wording.

qmp-commands.hx actions array: · line 726

Quoted 10 lines
qmp-commands.hx +3 -0
+
 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")

Well, this solves one of my two comments about the above wording, by calling out the default.

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

PATCH v3 5/8 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       |   81 ++++++++++++-----------------------------------------
 hmp-commands.hx  |    9 ++++--
 hmp.c            |    6 +++-
 qapi-schema.json |   15 +++++----
 qmp-commands.hx  |    2 +
 5 files changed, 40 insertions(+), 73 deletions(-)
blockdev.c +19 -62
index c9bd25b..06c3017 100644
@@ -648,70 +648,27 @@ void do_commit(Monitor *mon, const QDict *qdict)
 
 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,
+    };
+    BlockdevAction action = {
+        .kind = BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC,
+        .blockdev_snapshot_sync = &snapshot,
+    };
+    BlockdevActionList list = {
+        .value = &action,
+        .next = NULL
+    };
+
+    qmp_transaction(&list, 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
#13 lore raw

Re: [Qemu-devel] [PATCH v3 5/8] qmp: convert blockdev-snapshot-sync to a wrapper around transactions

Quoted 26 lines · 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       |   81 ++++++++++++-----------------------------------------
 hmp-commands.hx  |    9 ++++--
 hmp.c            |    6 +++-
 qapi-schema.json |   15 +++++----
 qmp-commands.hx  |    2 +
 5 files changed, 40 insertions(+), 73 deletions(-)

+++ b/qapi-schema.json

qapi-schema.json +3 -0
@@ -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',

qapi-schema.json line 1206

Quoted 5 lines
qapi-schema.json +2 -0
@@ -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'.

Right now, libvirt has an API virDomainSnapshotCreateXML with a flag VIR_DOMAIN_SNAPSHOT_CREATE_REUSE_EXT, which should map to this new mode operand. Am I guaranteed that if I pass a 'mode' argument of 'existing' to an older qemu that lacked mode, I will get a sane error? If so, then this rewrite looks fine from libvirt's point of view.

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

Re: [Qemu-devel] [PATCH v3 5/8] qmp: convert blockdev-snapshot-sync to a wrapper around transactions

Il 05/03/2012 19:55, Eric Blake ha scritto:

Quoted 5 lines

Right now, libvirt has an API virDomainSnapshotCreateXML with a flag VIR_DOMAIN_SNAPSHOT_CREATE_REUSE_EXT, which should map to this new mode operand. Am I guaranteed that if I pass a 'mode' argument of 'existing' to an older qemu that lacked mode, I will get a sane error? If so, then this rewrite looks fine from libvirt's point of view.

Not sure... Luiz?

Paolo

#15 lore raw

Re: [Qemu-devel] [PATCH v3 5/8] qmp: convert blockdev-snapshot-sync to a wrapper around transactions

Il 05/03/2012 20:44, Paolo Bonzini ha scritto:

Quoted 8 lines

Il 05/03/2012 19:55, Eric Blake ha scritto:

Quoted 5 lines

Right now, libvirt has an API virDomainSnapshotCreateXML with a flag VIR_DOMAIN_SNAPSHOT_CREATE_REUSE_EXT, which should map to this new mode operand. Am I guaranteed that if I pass a 'mode' argument of 'existing' to an older qemu that lacked mode, I will get a sane error? If so, then this rewrite looks fine from libvirt's point of view.

Not sure... Luiz?

Well, you could always use the transaction command if the flag is there.
That command will have it always.

Paolo

#16 lore raw

Re: [Qemu-devel] [PATCH v3 5/8] qmp: convert blockdev-snapshot-sync to a wrapper around transactions

In reply to Paolo Bonzini
Quoted 8 lines · On Mon, 05 Mar 2012 20:44:39 +0100 Paolo Bonzini <[email protected]> wrote:

Il 05/03/2012 19:55, Eric Blake ha scritto:

Quoted 5 lines

Right now, libvirt has an API virDomainSnapshotCreateXML with a flag VIR_DOMAIN_SNAPSHOT_CREATE_REUSE_EXT, which should map to this new mode operand. Am I guaranteed that if I pass a 'mode' argument of 'existing' to an older qemu that lacked mode, I will get a sane error? If so, then this rewrite looks fine from libvirt's point of view.

Not sure... Luiz?

The question is: will I get an error if I pass an unknown argument to a command? Yes, you will.

#17 lore raw

PATCH v3 6/8 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
#18 lore raw

PATCH v3 7/8 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 |   21 +++++++++++++++++++--
 qmp-commands.hx  |   12 +++++++++++-
 3 files changed, 73 insertions(+), 13 deletions(-)
blockdev.c +43 -10
index 06c3017..df086d1 100644
@@ -689,6 +689,7 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
     int ret = 0;
     BlockdevActionList *dev_entry = dev_list;
     BlkTransactionStates *states;
+    char *new_source = NULL;
 
     QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
     QSIMPLEQ_INIT(&snap_bdrv_states);
@@ -700,7 +701,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;
@@ -724,16 +726,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) {
@@ -757,7 +779,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;
@@ -765,10 +787,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;
@@ -777,12 +807,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;
     }
 
 
@@ -810,6 +842,7 @@ exit:
     QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
         g_free(states);
     }
+    g_free(new_source);
     return;
 }
 
qapi-schema.json +20 -2
index 0d24240..a61e90c 100644
@@ -1133,7 +1133,7 @@
 # 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,7 +1175,8 @@
 ##
 { 'union': 'BlockdevAction',
   'data': {
-       'blockdev-snapshot-sync': 'BlockdevSnapshot'
+       'blockdev-snapshot-sync': 'BlockdevSnapshot',
+       'drive-mirror': 'BlockdevMirror',
    } }
 
 ##
qmp-commands.hx +13 -3
index dfe8a5b..9dcafd4 100644
@@ -701,12 +701,16 @@ operation for now is snapshotting.  If there is any failure performing
 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
#19 lore raw

Re: [Qemu-devel] [PATCH v3 7/8] add mirroring to transaction

Quoted 16 lines · 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.

qapi-schema.json line 1178

Quoted 5 lines
qapi-schema.json +1 -1
@@ -1159,7 +1175,8 @@
 ##
 { 'union': 'BlockdevAction',
   'data': {
-       'blockdev-snapshot-sync': 'BlockdevSnapshot'
+       'blockdev-snapshot-sync': 'BlockdevSnapshot',

Minor nit - should we rebase this trailing comma earlier in the series, to make it obvious that BlockdevAction will grow and for less churn in this patch?

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

PATCH v3 8/8 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       |   24 ++++++++++++++++++++++++
 hmp-commands.hx  |   22 ++++++++++++++++++++++
 hmp.c            |   28 ++++++++++++++++++++++++++++
 hmp.h            |    1 +
 qapi-schema.json |   26 ++++++++++++++++++++++++++
 qmp-commands.hx  |   33 +++++++++++++++++++++++++++++++++
 6 files changed, 134 insertions(+), 0 deletions(-)
blockdev.c +24 -0
index df086d1..60fdc7b 100644
@@ -671,6 +671,30 @@ void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
     qmp_transaction(&list, 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,
+    };
+    BlockdevAction action = {
+        .kind = BLOCKDEV_ACTION_KIND_DRIVE_MIRROR,
+        .drive_mirror = &mirror,
+    };
+    BlockdevActionList list = {
+        .value = &action,
+        .next = NULL
+    };
+
+    qmp_transaction(&list, 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 a61e90c..16e8786 100644
@@ -1262,6 +1262,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 9dcafd4..b8a93d1 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
#21 lore raw

[Qemu-devel] [PATCH v3 9/8] 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 ++++++++++++++++++
 5 files changed, 113 insertions(+), 0 deletions(-)
blockdev.c +63 -0
index 60fdc7b..1fb2a17 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);
+}
+
 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file,
                                 bool has_format, const char *format,
                                 bool has_mode, enum NewImageMode mode,
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.
Signature · 1 line
1.7.7.6
#22 lore raw

[Qemu-devel] [PATCH v3 10/8] use QSIMPLEQ_FOREACH_SAFE when freeing list elements

In reply to Paolo Bonzini

QSIMPLEQ_FOREACH will use the states pointer after the loop has freed it.

---
Found while (re)reviewing Jeff's patches.

 blockdev.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
blockdev.c +2 -2
index 1fb2a17..9480dbb 100644
@@ -775,7 +775,7 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
 {
     int ret = 0;
     BlockdevActionList *dev_entry = dev_list;
-    BlkTransactionStates *states;
+    BlkTransactionStates *states, *next;
     char *new_source = NULL;
 
     QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states;
@@ -926,7 +926,7 @@ delete_and_fail:
         }
     }
 exit:
-    QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) {
+    QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) {
         g_free(states);
     }
     g_free(new_source);
Signature · 1 line
1.7.7.6