Skip to contentSkip to conversation

Copy snapshots out of QCOW2 disk

PATCH Superseded [email protected] 2 participants 4 messages Sep 10, 2010 – Sep 16, 2010 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Copy snapshots out of QCOW2 disk

From: edison <[email protected]>

In order to backup snapshots, created from QCOW2 iamge, we want to copy snapshots out of QCOW2 disk to a seperate storage. The following patch adds a new option in "qemu-img": qemu-img convert -f qcow2 -O qcow2 -s snapshot_name src_img bck_img. Right now, it only supports to copy the full snapshot, delta snapshot is on the way.

---
 block.c                |   11 +++++++++++
 block.h                |    2 ++
 block/qcow2-snapshot.c |   26 ++++++++++++++++++++++++++
 block/qcow2.c          |    1 +
 block/qcow2.h          |    1 +
 block_int.h            |    2 ++
 qemu-img-cmds.hx       |    4 ++--
 qemu-img.c             |   19 ++++++++++++++++++-
 8 files changed, 63 insertions(+), 3 deletions(-)
block.c +11 -0
index ebbc376..eaf78f6 100644
@@ -1899,6 +1899,17 @@ int bdrv_snapshot_list(BlockDriverState *bs,
     return -ENOTSUP;
 }
 
+int bdrv_snapshot_load(BlockDriverState *bs,
+		       const char *snapshot_name)
+{
+    BlockDriver *drv = bs->drv;
+    if (!drv)
+        return -ENOMEDIUM;
+    if (drv->bdrv_snapshot_load)
+	return drv->bdrv_snapshot_load(bs, snapshot_name);
+    return -ENOTSUP;
+}
+
 #define NB_SUFFIXES 4
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
block.h +2 -0
index 5f64380..9ec6219 100644
@@ -211,6 +211,8 @@ int bdrv_snapshot_goto(BlockDriverState *bs,
 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
 int bdrv_snapshot_list(BlockDriverState *bs,
                        QEMUSnapshotInfo **psn_info);
+int bdrv_snapshot_load(BlockDriverState *bs,
+                       const char *snapshot_name);
 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size);
block/qcow2-snapshot.c +26 -0
index 6228612..e663e8b 100644
@@ -416,3 +416,29 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
     return s->nb_snapshots;
 }
 
+int qcow2_snapshot_load(BlockDriverState *bs, const char *snapshot_name) {
+    int i, snapshot_index, l1_size2;
+    BDRVQcowState *s = bs->opaque;
+    QCowSnapshot *sn;
+    snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name);
+    if (snapshot_index < 0)
+        return -ENOENT;
+    sn = &s->snapshots[snapshot_index];
+    s->l1_size = sn->l1_size;
+    l1_size2 = s->l1_size * sizeof(uint64_t);
+    if (s->l1_table != NULL) {
+	qemu_free(s->l1_table);
+    }
+    s->l1_table_offset = sn->l1_table_offset;
+    s->l1_table = qemu_mallocz(
+            align_offset(l1_size2, 512));
+    /* copy the snapshot l1 table to the current l1 table */
+    if (bdrv_pread(bs->file, sn->l1_table_offset,
+                   s->l1_table, l1_size2) != l1_size2) {
+        return -1;
+    }
+    for(i = 0;i < s->l1_size; i++) {
+        be64_to_cpus(&s->l1_table[i]);
+    }
+    return 0;
+}
block/qcow2.c +1 -0
index a53014d..da98a01 100644
@@ -1352,6 +1352,7 @@ static BlockDriver bdrv_qcow2 = {
     .bdrv_snapshot_goto     = qcow2_snapshot_goto,
     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
     .bdrv_snapshot_list     = qcow2_snapshot_list,
+    .bdrv_snapshot_load     = qcow2_snapshot_load,
     .bdrv_get_info	= qcow_get_info,
 
     .bdrv_save_vmstate    = qcow_save_vmstate,
block/qcow2.h +1 -0
index 3ff162e..cbbba48 100644
@@ -211,6 +211,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
+int qcow2_snapshot_load(BlockDriverState *bs, const char *snapshot_name);
 
 void qcow2_free_snapshots(BlockDriverState *bs);
 int qcow2_read_snapshots(BlockDriverState *bs);
block_int.h +2 -0
index e8e7156..93d5a1b 100644
@@ -93,6 +93,8 @@ struct BlockDriver {
     int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
     int (*bdrv_snapshot_list)(BlockDriverState *bs,
                               QEMUSnapshotInfo **psn_info);
+    int (*bdrv_snapshot_load)(BlockDriverState *bs,
+                              const char *snapshot_name);
     int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
 
     int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
qemu-img-cmds.hx +2 -2
index 6d3e5f8..6c7176f 100644
@@ -28,9 +28,9 @@ STEXI
 ETEXI
 
 DEF("convert", img_convert,
-    "convert [-c] [-f fmt] [-O output_fmt] [-o options] filename [filename2 [...]] output_filename")
+    "convert [-c] [-f fmt] [-O output_fmt] [-o options] [-s snapshot_name] filename [filename2 [...]] output_filename")
 STEXI
-@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 ETEXI
 
 DEF("info", img_info,
qemu-img.c +18 -1
index 4e035e4..7ff015d 100644
@@ -646,13 +646,14 @@ static int img_convert(int argc, char **argv)
     BlockDriverInfo bdi;
     QEMUOptionParameter *param = NULL, *create_options = NULL;
     char *options = NULL;
+    const char *snapshot_name = NULL;
 
     fmt = NULL;
     out_fmt = "raw";
     out_baseimg = NULL;
     flags = 0;
     for(;;) {
-        c = getopt(argc, argv, "f:O:B:hce6o:");
+        c = getopt(argc, argv, "f:O:B:s:hce6o:");
         if (c == -1)
             break;
         switch(c) {
@@ -680,6 +681,9 @@ static int img_convert(int argc, char **argv)
         case 'o':
             options = optarg;
             break;
+        case 's':
+            snapshot_name = optarg;
+            break;
         }
     }
 
@@ -711,6 +715,19 @@ static int img_convert(int argc, char **argv)
         total_sectors += bs_sectors;
     }
 
+    if (snapshot_name != NULL) {
+	if (bs_n > 1) {
+            error("No support for concatenating multiple snapshot\n");
+	     ret = -1;
+            goto out;
+	}
+	if (bdrv_snapshot_load(bs[0], snapshot_name) < 0) {
+            error("Failed to load snapshot\n");
+	     ret = -1;
+            goto out;
+	}
+    }
+
     /* Find driver and parse its options */
     drv = bdrv_find_format(out_fmt);
     if (!drv) {
Signature · 1 line
1.7.2.2
#1 lore raw

Am 10.09.2010 02:08, schrieb [email protected]:

Quoted 23 lines

From: edison <[email protected]>

In order to backup snapshots, created from QCOW2 iamge, we want to copy snapshots out of QCOW2 disk to a seperate storage. The following patch adds a new option in "qemu-img": qemu-img convert -f qcow2 -O qcow2 -s snapshot_name src_img bck_img. Right now, it only supports to copy the full snapshot, delta snapshot is on the way.

---
 block.c                |   11 +++++++++++
 block.h                |    2 ++
 block/qcow2-snapshot.c |   26 ++++++++++++++++++++++++++
 block/qcow2.c          |    1 +
 block/qcow2.h          |    1 +
 block_int.h            |    2 ++
 qemu-img-cmds.hx       |    4 ++--
 qemu-img.c             |   19 ++++++++++++++++++-
 8 files changed, 63 insertions(+), 3 deletions(-)
block.c +0 -0
index ebbc376..eaf78f6 100644

block.c int bdrv_snapshot_list(BlockDriverState *bs, · line 1907

Quoted 9 lines
block.c +6 -0
@@ -1899,6 +1899,17 @@ int bdrv_snapshot_list(BlockDriverState *bs,
     return -ENOTSUP;
 }
 
+int bdrv_snapshot_load(BlockDriverState *bs,
+		       const char *snapshot_name)
+{
+    BlockDriver *drv = bs->drv;
+    if (!drv)
+        return -ENOMEDIUM;

Please add curly braces (see CODING_STYLE for style issues)

block.c int bdrv_snapshot_list(BlockDriverState *bs, · line 1909

Quoted 2 lines
block.c +2 -0
+    if (drv->bdrv_snapshot_load)
+	return drv->bdrv_snapshot_load(bs, snapshot_name);

Indentation should be four spaces, no tabs.

Quoted 24 lines
block.c +3 -0
+    return -ENOTSUP;
+}
+
 #define NB_SUFFIXES 4
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size)
block.h +2 -0
index 5f64380..9ec6219 100644
@@ -211,6 +211,8 @@ int bdrv_snapshot_goto(BlockDriverState *bs,
 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
 int bdrv_snapshot_list(BlockDriverState *bs,
                        QEMUSnapshotInfo **psn_info);
+int bdrv_snapshot_load(BlockDriverState *bs,
+                       const char *snapshot_name);
 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size);
block/qcow2-snapshot.c +0 -0
index 6228612..e663e8b 100644

block/qcow2-snapshot.c int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) · line 419

Quoted 4 lines
block/qcow2-snapshot.c +1 -0
@@ -416,3 +416,29 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
     return s->nb_snapshots;
 }
 
+int qcow2_snapshot_load(BlockDriverState *bs, const char *snapshot_name) {

Brace should be on a line of its own.

Also, this could probably share some code with qcow2_snapshot_goto.

Please add a check that the image is opened read-only. Writing to the L1 table after qcow2_snapshot_load() would probably have a catastrophic effect.

Quoted 6 lines
block/qcow2-snapshot.c +6 -0
+    int i, snapshot_index, l1_size2;
+    BDRVQcowState *s = bs->opaque;
+    QCowSnapshot *sn;
+    snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name);
+    if (snapshot_index < 0)
+        return -ENOENT;

block/qcow2-snapshot.c int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) · line 435

Quoted 10 lines
block/qcow2-snapshot.c +10 -0
+    sn = &s->snapshots[snapshot_index];
+    s->l1_size = sn->l1_size;
+    l1_size2 = s->l1_size * sizeof(uint64_t);
+    if (s->l1_table != NULL) {
+	qemu_free(s->l1_table);
+    }
+    s->l1_table_offset = sn->l1_table_offset;
+    s->l1_table = qemu_mallocz(
+            align_offset(l1_size2, 512));
+    /* copy the snapshot l1 table to the current l1 table */

Actually, you don't copy anything but read it from the file.

block/qcow2-snapshot.c int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) · line 444

Quoted 9 lines
block/qcow2-snapshot.c +9 -0
+    if (bdrv_pread(bs->file, sn->l1_table_offset,
+                   s->l1_table, l1_size2) != l1_size2) {
+        return -1;
+    }
+    for(i = 0;i < s->l1_size; i++) {
+        be64_to_cpus(&s->l1_table[i]);
+    }
+    return 0;
+}

I think the whole function could use some blank lines for readability.

block/qcow2.c static BlockDriver bdrv_qcow2 = { · line 1355

Quoted 9 lines
block/qcow2.c +1 -0
index a53014d..da98a01 100644
@@ -1352,6 +1352,7 @@ static BlockDriver bdrv_qcow2 = {
     .bdrv_snapshot_goto     = qcow2_snapshot_goto,
     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
     .bdrv_snapshot_list     = qcow2_snapshot_list,
+    .bdrv_snapshot_load     = qcow2_snapshot_load,

I'm not sure if bdrv_snapshot_load is a good name. To me it sounds like this was the proper way to load a snapshot to continue normal work on it. It's much less, though.

Quoted 34 lines
block/qcow2.c +0 -0
     .bdrv_get_info	= qcow_get_info,
 
     .bdrv_save_vmstate    = qcow_save_vmstate,
block/qcow2.h +1 -0
index 3ff162e..cbbba48 100644
@@ -211,6 +211,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
+int qcow2_snapshot_load(BlockDriverState *bs, const char *snapshot_name);
 
 void qcow2_free_snapshots(BlockDriverState *bs);
 int qcow2_read_snapshots(BlockDriverState *bs);
block_int.h +2 -0
index e8e7156..93d5a1b 100644
@@ -93,6 +93,8 @@ struct BlockDriver {
     int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
     int (*bdrv_snapshot_list)(BlockDriverState *bs,
                               QEMUSnapshotInfo **psn_info);
+    int (*bdrv_snapshot_load)(BlockDriverState *bs,
+                              const char *snapshot_name);
     int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
 
     int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
qemu-img-cmds.hx +0 -0
index 6d3e5f8..6c7176f 100644
@@ -28,9 +28,9 @@ STEXI
 ETEXI

qemu-img-cmds.hx STEXI · line 36

Quoted 10 lines
qemu-img-cmds.hx +2 -2
 
 DEF("convert", img_convert,
-    "convert [-c] [-f fmt] [-O output_fmt] [-o options] filename [filename2 [...]] output_filename")
+    "convert [-c] [-f fmt] [-O output_fmt] [-o options] [-s snapshot_name] filename [filename2 [...]] output_filename")
 STEXI
-@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 ETEXI
 
 DEF("info", img_info,

Please don't forget to add documentation to qemu-img.texi, too.

Quoted 36 lines
qemu-img.c +7 -1
index 4e035e4..7ff015d 100644
@@ -646,13 +646,14 @@ static int img_convert(int argc, char **argv)
     BlockDriverInfo bdi;
     QEMUOptionParameter *param = NULL, *create_options = NULL;
     char *options = NULL;
+    const char *snapshot_name = NULL;
 
     fmt = NULL;
     out_fmt = "raw";
     out_baseimg = NULL;
     flags = 0;
     for(;;) {
-        c = getopt(argc, argv, "f:O:B:hce6o:");
+        c = getopt(argc, argv, "f:O:B:s:hce6o:");
         if (c == -1)
             break;
         switch(c) {
@@ -680,6 +681,9 @@ static int img_convert(int argc, char **argv)
         case 'o':
             options = optarg;
             break;
+        case 's':
+            snapshot_name = optarg;
+            break;
         }
     }
 
@@ -711,6 +715,19 @@ static int img_convert(int argc, char **argv)
         total_sectors += bs_sectors;
     }
 
+    if (snapshot_name != NULL) {
+	if (bs_n > 1) {

qemu-img.c static int img_convert(int argc, char **argv) · line 729

Quoted 10 lines
qemu-img.c +10 -0
+            error("No support for concatenating multiple snapshot\n");
+	     ret = -1;
+            goto out;
+	}
+	if (bdrv_snapshot_load(bs[0], snapshot_name) < 0) {
+            error("Failed to load snapshot\n");
+	     ret = -1;
+            goto out;
+	}
+    }

Something's wrong with the indentation in this hunk.

Kevin

author
#2 lore raw
Quoted 69 lines · Kevin Wolf

Am 10.09.2010 02:08, schrieb [email protected]:

Quoted 23 lines

From: edison <[email protected]>

In order to backup snapshots, created from QCOW2 iamge, we want to copy snapshots out of QCOW2 disk to a seperate storage. The following patch adds a new option in "qemu-img": qemu-img convert -f qcow2 -O qcow2 -s snapshot_name src_img bck_img. Right now, it only supports to copy the full snapshot, delta snapshot is on the way.

---
 block.c                |   11 +++++++++++ block.h                |    2 ++ block/qcow2-snapshot.c |   26 ++++++++++++++++++++++++++ block/qcow2.c          |    1 +
 block/qcow2.h          |    1 +
 block_int.h            |    2 ++ qemu-img-cmds.hx       |    4 ++--
 qemu-img.c             |   19 ++++++++++++++++++-

 8 files changed, 63 insertions(+), 3 deletions(-)
block.c +0 -0
index ebbc376..eaf78f6 100644

block.c int bdrv_snapshot_list(BlockDriverState *bs, · line 1907

Quoted 9 lines
block.c +6 -0
@@ -1899,6 +1899,17 @@ int bdrv_snapshot_list(BlockDriverState *bs,
      return -ENOTSUP;
  }
 
+int bdrv_snapshot_load(BlockDriverState *bs,
+                    const char *snapshot_name)
+{
+    BlockDriver *drv = bs->drv;
+    if (!drv)
+        return -ENOMEDIUM;

Please add curly braces (see CODING_STYLE for style issues)

block.c int bdrv_snapshot_list(BlockDriverState *bs, · line 1909

Quoted 2 lines
block.c +2 -0
+    if (drv->bdrv_snapshot_load)
+     return drv->bdrv_snapshot_load(bs, snapshot_name);

Indentation should be four spaces, no tabs.

Quoted 27 lines
block.c +3 -0
+    return -ENOTSUP;
+}
+
  #define NB_SUFFIXES 4
 
  char *get_human_readable_size(char *buf, int buf_size, int64_t size)
block.h +2 -0
index 5f64380..9ec6219 100644
@@ -211,6 +211,8 @@ int bdrv_snapshot_goto(BlockDriverState *bs,
  int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
  int bdrv_snapshot_list(BlockDriverState *bs,
                         QEMUSnapshotInfo **psn_info);
+int bdrv_snapshot_load(BlockDriverState *bs,
+                       const char *snapshot_name);
  char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
 
  char *get_human_readable_size(char *buf, int buf_size, int64_t size);
block/qcow2-snapshot.c +0 -0
index 6228612..e663e8b 100644
@@ -416,3 +416,29 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
      return s->nb_snapshots;
  }
 
Quoted 8 lines

block/qcow2-snapshot.c int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) · line 419

Quoted 1 line
block/qcow2-snapshot.c +1 -0
+int qcow2_snapshot_load(BlockDriverState *bs, const char *snapshot_name) {

Brace should be on a line of its own.

Also, this could probably share some code with qcow2_snapshot_goto.

Please add a check that the image is opened read-only. Writing to the L1 table after qcow2_snapshot_load() would probably have a catastrophic effect.

Will do.
BTW, my usage case for this patch is online backup the snapshot. That means the image is opened with r/w by a running QEMU process, at the same time, management tool can backup some snapshots already created before on this image. Here management tool can make sure no one take snapshot while backup snapshots.
If I read the QCOW2 code correctly(not easy to read...), no one snapshots_offset/nb_snapshots in QCOW2 header, if you don't take a new snapshot, so it's OK to copy the snapshot out from the image in my usage case, right?

Quoted 18 lines
Quoted 7 lines
block/qcow2-snapshot.c +6 -0
+    int i, snapshot_index, l1_size2;
+    BDRVQcowState *s = bs->opaque;
+    QCowSnapshot *sn;
+    snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name);
+    if (snapshot_index < 0)
+        return -ENOENT;

block/qcow2-snapshot.c int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) · line 435

Quoted 10 lines
block/qcow2-snapshot.c +10 -0
+    sn = &s->snapshots[snapshot_index];
+    s->l1_size = sn->l1_size;
+    l1_size2 = s->l1_size * sizeof(uint64_t);
+    if (s->l1_table != NULL) {
+     qemu_free(s->l1_table);
+    }
+    s->l1_table_offset = sn->l1_table_offset;
+    s->l1_table = qemu_mallocz(
+            align_offset(l1_size2, 512));
+    /* copy the snapshot l1 table to the current l1 table */
Quoted 1 line

Actually, you don't copy anything but read it from the file.

I see, this is the copy-paste from qcow2_snapshot_goto...:)

Quoted 11 lines

block/qcow2-snapshot.c int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) · line 444

Quoted 10 lines
block/qcow2-snapshot.c +9 -0
+    if (bdrv_pread(bs->file, sn->l1_table_offset,
+                   s->l1_table, l1_size2) != l1_size2) {
+        return -1;
+    }
+    for(i = 0;i < s->l1_size; i++) {
+        be64_to_cpus(&s->l1_table[i]);
+    }
+    return 0;
+}
Quoted 1 line

I think the whole function could use some blank lines for readability.

Will do.

Quoted 14 lines

block/qcow2.c static BlockDriver bdrv_qcow2 = { · line 1355

Quoted 10 lines
block/qcow2.c +1 -0
index a53014d..da98a01 100644
@@ -1352,6 +1352,7 @@ static BlockDriver bdrv_qcow2 = {
      .bdrv_snapshot_goto     = qcow2_snapshot_goto,
      .bdrv_snapshot_delete   = qcow2_snapshot_delete,
      .bdrv_snapshot_list     = qcow2_snapshot_list,
+    .bdrv_snapshot_load     = qcow2_snapshot_load,

I'm not sure if bdrv_snapshot_load is a good name. To me it sounds like this was the proper way to load a snapshot to continue normal work on it. It's much less, though.

This is the special operation, maybe only for QCOW2 which takes internal snapshot...
 How about bdrv_snapshot_copy?

Quoted 44 lines
Quoted 44 lines
block/qcow2.c +0 -0
      .bdrv_get_info   = qcow_get_info,
 
      .bdrv_save_vmstate    = qcow_save_vmstate,
block/qcow2.h +1 -0
index 3ff162e..cbbba48 100644
@@ -211,6 +211,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
  int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
  int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
  int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
+int qcow2_snapshot_load(BlockDriverState *bs, const char *snapshot_name);
 
  void qcow2_free_snapshots(BlockDriverState *bs);
  int qcow2_read_snapshots(BlockDriverState *bs);
block_int.h +2 -0
index e8e7156..93d5a1b 100644
@@ -93,6 +93,8 @@ struct BlockDriver {
      int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
      int (*bdrv_snapshot_list)(BlockDriverState *bs,
                                QEMUSnapshotInfo **psn_info);
+    int (*bdrv_snapshot_load)(BlockDriverState *bs,
+                              const char *snapshot_name);
      int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
 
      int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
qemu-img-cmds.hx +0 -0
index 6d3e5f8..6c7176f 100644
@@ -28,9 +28,9 @@ STEXI

 ETEXI

 DEF("convert", img_convert,
-    "convert [-c] [-f fmt] [-O output_fmt] [-o options] filename [filename2 [...]] output_filename")
+    "convert [-c] [-f fmt] [-O output_fmt] [-o options] [-s snapshot_name] filename [filename2 [...]] output_filename")
 STEXI
-@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] @var{filename} [@var{filename2} [...]] @var{output_filename} +@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] @var{filename} [@var{filename2} [...]] @var{output_filename} ETEXI

Quoted 3 lines
Quoted 1 line

 DEF("info", img_info,

Please don't forget to add documentation to qemu-img.texi, too.

Will do.

Quoted 35 lines
Quoted 35 lines
qemu-img.c +2 -1
index 4e035e4..7ff015d 100644
@@ -646,13 +646,14 @@ static int img_convert(int argc, char **argv)
      BlockDriverInfo bdi;
      QEMUOptionParameter *param = NULL, *create_options = NULL;
      char *options = NULL;
+    const char *snapshot_name = NULL;
 
      fmt = NULL;
      out_fmt = "raw";
      out_baseimg = NULL;
      flags = 0;
      for(;;) {
-        c = getopt(argc, argv, "f:O:B:hce6o:");
+        c = getopt(argc, argv, "f:O:B:s:hce6o:");
          if (c == -1)
              break;
          switch(c) {
@@ -680,6 +681,9 @@ static int img_convert(int argc, char **argv)

         case 'o':
             options = optarg;
             break;
+        case 's':
+            snapshot_name = optarg;
+            break;
         }
     }

qemu-img.c +0 -0
@@ -711,6 +715,19 @@ static int img_convert(int argc, char **argv)
          total_sectors += bs_sectors;
      }
 
Quoted 14 lines

qemu-img.c static int img_convert(int argc, char **argv) · line 729

Quoted 12 lines
qemu-img.c +12 -0
+    if (snapshot_name != NULL) {
+     if (bs_n > 1) {
+            error("No support for concatenating multiple snapshot\n");
+          ret = -1;
+            goto out;
+     }
+     if (bdrv_snapshot_load(bs[0], snapshot_name) < 0) {
+            error("Failed to load snapshot\n");
+          ret = -1;
+            goto out;
+     }
+    }

Something's wrong with the indentation in this hunk.

I see, will change it to 4 space.

Quoted 2 lines

Kevin

#3 lore raw

Am 15.09.2010 19:56, schrieb edison:

Quoted 33 lines · Kevin Wolf
Quoted 23 lines

Am 10.09.2010 02:08, schrieb [email protected]:

Quoted 21 lines
block.h +2 -0
index 5f64380..9ec6219 100644
@@ -211,6 +211,8 @@ int bdrv_snapshot_goto(BlockDriverState *bs,
 int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
 int bdrv_snapshot_list(BlockDriverState *bs,
                        QEMUSnapshotInfo **psn_info);
+int bdrv_snapshot_load(BlockDriverState *bs,
+                       const char *snapshot_name);
 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size);
block/qcow2-snapshot.c +0 -0
index 6228612..e663e8b 100644
@@ -416,3 +416,29 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
     return s->nb_snapshots;
 }
 
Quoted 8 lines

block/qcow2-snapshot.c int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) · line 419

Quoted 1 line
block/qcow2-snapshot.c +1 -0
+int qcow2_snapshot_load(BlockDriverState *bs, const char *snapshot_name) {

Brace should be on a line of its own.

Also, this could probably share some code with qcow2_snapshot_goto.

Please add a check that the image is opened read-only. Writing to the L1 table after qcow2_snapshot_load() would probably have a catastrophic effect.

Quoted 10 lines

Will do.
BTW, my usage case for this patch is online backup the snapshot. That means the image is opened with r/w by a running QEMU process, at the same time, management tool can backup some snapshots already created before on this image. Here management tool can make sure no one take snapshot while backup snapshots.
If I read the QCOW2 code correctly(not easy to read...), no one snapshots_offset/nb_snapshots in QCOW2 header, if you don't take a new snapshot, so it's OK to copy the snapshot out from the image in my usage case, right?

Right, I _think_ this might happen to work, at least with today's code. Still I discourage having an image opened twice. It's not something that code changes will take into consideration.

I think the proper way to do this would be a monitor command. However, I do understand that implementing this without opening the image a second time is way harder than doing something that is targeted at working offline and just happens to work online, too.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
block/qcow2.c +0 -0
index a53014d..da98a01 100644
@@ -1352,6 +1352,7 @@ static BlockDriver bdrv_qcow2 = {
Quoted 8 lines

block/qcow2.c static BlockDriver bdrv_qcow2 = { · line 1355

Quoted 4 lines
block/qcow2.c +1 -0
     .bdrv_snapshot_goto     = qcow2_snapshot_goto,
     .bdrv_snapshot_delete   = qcow2_snapshot_delete,
     .bdrv_snapshot_list     = qcow2_snapshot_list,
+    .bdrv_snapshot_load     = qcow2_snapshot_load,

I'm not sure if bdrv_snapshot_load is a good name. To me it sounds like this was the proper way to load a snapshot to continue normal work on it. It's much less, though.

Quoted 3 lines

This is the special operation, maybe only for QCOW2 which takes internal snapshot...
How about bdrv_snapshot_copy?

Maybe just something like bdrv_snapshot_load_tmp?

Kevin