cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
902636
From f2add7d5955770318824c3eee774bec2dd850936 Mon Sep 17 00:00:00 2001
902636
From: Eric Blake <eblake@redhat.com>
902636
Date: Tue, 2 Jun 2020 02:34:19 +0100
902636
Subject: [PATCH 14/26] qemu-img: Add convert --bitmaps option
902636
902636
RH-Author: Eric Blake <eblake@redhat.com>
902636
Message-id: <20200602023420.2133649-12-eblake@redhat.com>
902636
Patchwork-id: 97076
902636
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 11/12] qemu-img: Add convert --bitmaps option
902636
Bugzilla: 1779893 1779904
902636
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
902636
RH-Acked-by: Max Reitz <mreitz@redhat.com>
902636
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
902636
902636
Make it easier to copy all the persistent bitmaps of (the top layer
902636
of) a source image along with its guest-visible contents, by adding a
902636
boolean flag for use with qemu-img convert.  This is basically
902636
shorthand, as the same effect could be accomplished with a series of
902636
'qemu-img bitmap --add' and 'qemu-img bitmap --merge -b source'
902636
commands, or by their corresponding QMP commands.
902636
902636
Note that this command will fail in the same scenarios where 'qemu-img
902636
measure' omits a 'bitmaps size:' line, namely, when either the source
902636
or the destination lacks persistent bitmap support altogether.
902636
902636
See also https://bugzilla.redhat.com/show_bug.cgi?id=1779893
902636
902636
While touching this, clean up a couple coding issues spotted in the
902636
same function: an extra blank line, and merging back-to-back 'if
902636
(!skip_create)' blocks.
902636
902636
Signed-off-by: Eric Blake <eblake@redhat.com>
902636
Message-Id: <20200521192137.1120211-5-eblake@redhat.com>
902636
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
902636
(cherry picked from commit 15e39ad95078d528dfb9a75417453cab60332b77)
902636
902636
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
902636
902636
Conflicts:
902636
	docs/tools/qemu-img.rst - qemu-img.texi instead
902636
	qemu-img.c - context: no --target-is-zero
902636
	qemu-img-cmds.hx - context: texi instead of rst
902636
Signed-off-by: Eric Blake <eblake@redhat.com>
902636
902636
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
902636
---
902636
 qemu-img-cmds.hx |  4 ++--
902636
 qemu-img.c       | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
902636
 qemu-img.texi    |  4 +++-
902636
 3 files changed, 72 insertions(+), 6 deletions(-)
902636
902636
diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
902636
index 1a6a8e9..48144aa 100644
902636
--- a/qemu-img-cmds.hx
902636
+++ b/qemu-img-cmds.hx
902636
@@ -50,9 +50,9 @@ STEXI
902636
 ETEXI
902636
 
902636
 DEF("convert", img_convert,
902636
-    "convert [--object objectdef] [--image-opts] [--target-image-opts] [-U] [-C] [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-B backing_file] [-o options] [-l snapshot_param] [-S sparse_size] [-m num_coroutines] [-W] [--salvage] filename [filename2 [...]] output_filename")
902636
+    "convert [--object objectdef] [--image-opts] [--target-image-opts] [--bitmaps] [-U] [-C] [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-B backing_file] [-o options] [-l snapshot_param] [-S sparse_size] [-m num_coroutines] [-W] [--salvage] filename [filename2 [...]] output_filename")
902636
 STEXI
902636
-@item convert [--object @var{objectdef}] [--image-opts] [--target-image-opts] [-U] [-C] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-B @var{backing_file}] [-o @var{options}] [-l @var{snapshot_param}] [-S @var{sparse_size}] [-m @var{num_coroutines}] [-W] [--salvage] @var{filename} [@var{filename2} [...]] @var{output_filename}
902636
+@item convert [--object @var{objectdef}] [--image-opts] [--target-image-opts] [--bitmaps] [-U] [-C] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-B @var{backing_file}] [-o @var{options}] [-l @var{snapshot_param}] [-S @var{sparse_size}] [-m @var{num_coroutines}] [-W] [--salvage] @var{filename} [@var{filename2} [...]] @var{output_filename}
902636
 ETEXI
902636
 
902636
 DEF("create", img_create,
902636
diff --git a/qemu-img.c b/qemu-img.c
902636
index 39e1586..6dc881b 100644
902636
--- a/qemu-img.c
902636
+++ b/qemu-img.c
902636
@@ -77,6 +77,7 @@ enum {
902636
     OPTION_ENABLE = 272,
902636
     OPTION_DISABLE = 273,
902636
     OPTION_MERGE = 274,
902636
+    OPTION_BITMAPS = 275,
902636
 };
902636
 
902636
 typedef enum OutputFormat {
902636
@@ -190,6 +191,7 @@ static void QEMU_NORETURN help(void)
902636
            "       hiding corruption that has already occurred.\n"
902636
            "\n"
902636
            "Parameters to convert subcommand:\n"
902636
+           "  '--bitmaps' copies all top-level persistent bitmaps to destination\n"
902636
            "  '-m' specifies how many coroutines work in parallel during the convert\n"
902636
            "       process (defaults to 8)\n"
902636
            "  '-W' allow to write to the target out of order rather than sequential\n"
902636
@@ -2084,6 +2086,39 @@ static int convert_do_copy(ImgConvertState *s)
902636
     return s->ret;
902636
 }
902636
 
902636
+static int convert_copy_bitmaps(BlockDriverState *src, BlockDriverState *dst)
902636
+{
902636
+    BdrvDirtyBitmap *bm;
902636
+    Error *err = NULL;
902636
+
902636
+    FOR_EACH_DIRTY_BITMAP(src, bm) {
902636
+        const char *name;
902636
+
902636
+        if (!bdrv_dirty_bitmap_get_persistence(bm)) {
902636
+            continue;
902636
+        }
902636
+        name = bdrv_dirty_bitmap_name(bm);
902636
+        qmp_block_dirty_bitmap_add(dst->node_name, name,
902636
+                                   true, bdrv_dirty_bitmap_granularity(bm),
902636
+                                   true, true,
902636
+                                   true, !bdrv_dirty_bitmap_enabled(bm),
902636
+                                   &err;;
902636
+        if (err) {
902636
+            error_reportf_err(err, "Failed to create bitmap %s: ", name);
902636
+            return -1;
902636
+        }
902636
+
902636
+        do_dirty_bitmap_merge(dst->node_name, name, src->node_name, name,
902636
+                              &err;;
902636
+        if (err) {
902636
+            error_reportf_err(err, "Failed to populate bitmap %s: ", name);
902636
+            return -1;
902636
+        }
902636
+    }
902636
+
902636
+    return 0;
902636
+}
902636
+
902636
 #define MAX_BUF_SECTORS 32768
902636
 
902636
 static int img_convert(int argc, char **argv)
902636
@@ -2105,6 +2140,7 @@ static int img_convert(int argc, char **argv)
902636
     int64_t ret = -EINVAL;
902636
     bool force_share = false;
902636
     bool explict_min_sparse = false;
902636
+    bool bitmaps = false;
902636
 
902636
     ImgConvertState s = (ImgConvertState) {
902636
         /* Need at least 4k of zeros for sparse detection */
902636
@@ -2123,6 +2159,7 @@ static int img_convert(int argc, char **argv)
902636
             {"force-share", no_argument, 0, 'U'},
902636
             {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS},
902636
             {"salvage", no_argument, 0, OPTION_SALVAGE},
902636
+            {"bitmaps", no_argument, 0, OPTION_BITMAPS},
902636
             {0, 0, 0, 0}
902636
         };
902636
         c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU",
902636
@@ -2248,6 +2285,9 @@ static int img_convert(int argc, char **argv)
902636
         case OPTION_TARGET_IMAGE_OPTS:
902636
             tgt_image_opts = true;
902636
             break;
902636
+        case OPTION_BITMAPS:
902636
+            bitmaps = true;
902636
+            break;
902636
         }
902636
     }
902636
 
902636
@@ -2304,7 +2344,6 @@ static int img_convert(int argc, char **argv)
902636
         goto fail_getopt;
902636
     }
902636
 
902636
-
902636
     /* ret is still -EINVAL until here */
902636
     ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough);
902636
     if (ret < 0) {
902636
@@ -2458,6 +2497,20 @@ static int img_convert(int argc, char **argv)
902636
         }
902636
     }
902636
 
902636
+    /* Determine if bitmaps need copying */
902636
+    if (bitmaps) {
902636
+        if (s.src_num > 1) {
902636
+            error_report("Copying bitmaps only possible with single source");
902636
+            ret = -1;
902636
+            goto out;
902636
+        }
902636
+        if (!bdrv_supports_persistent_dirty_bitmap(blk_bs(s.src[0]))) {
902636
+            error_report("Source lacks bitmap support");
902636
+            ret = -1;
902636
+            goto out;
902636
+        }
902636
+    }
902636
+
902636
     /*
902636
      * The later open call will need any decryption secrets, and
902636
      * bdrv_create() will purge "opts", so extract them now before
902636
@@ -2466,9 +2519,7 @@ static int img_convert(int argc, char **argv)
902636
     if (!skip_create) {
902636
         open_opts = qdict_new();
902636
         qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort);
902636
-    }
902636
 
902636
-    if (!skip_create) {
902636
         /* Create the new image */
902636
         ret = bdrv_create(drv, out_filename, opts, &local_err);
902636
         if (ret < 0) {
902636
@@ -2506,6 +2557,13 @@ static int img_convert(int argc, char **argv)
902636
     }
902636
     out_bs = blk_bs(s.target);
902636
 
902636
+    if (bitmaps && !bdrv_supports_persistent_dirty_bitmap(out_bs)) {
902636
+        error_report("Format driver '%s' does not support bitmaps",
902636
+                     out_bs->drv->format_name);
902636
+        ret = -1;
902636
+        goto out;
902636
+    }
902636
+
902636
     if (s.compressed && !block_driver_can_compress(out_bs->drv)) {
902636
         error_report("Compression not supported for this file format");
902636
         ret = -1;
902636
@@ -2565,6 +2623,12 @@ static int img_convert(int argc, char **argv)
902636
     }
902636
 
902636
     ret = convert_do_copy(&s);
902636
+
902636
+    /* Now copy the bitmaps */
902636
+    if (bitmaps && ret == 0) {
902636
+        ret = convert_copy_bitmaps(blk_bs(s.src[0]), out_bs);
902636
+    }
902636
+
902636
 out:
902636
     if (!ret) {
902636
         qemu_progress_print(100, 0);
902636
diff --git a/qemu-img.texi b/qemu-img.texi
902636
index 3670b96..b95d019 100644
902636
--- a/qemu-img.texi
902636
+++ b/qemu-img.texi
902636
@@ -161,6 +161,8 @@ Parameters to convert subcommand:
902636
 
902636
 @table @option
902636
 
902636
+@item --bitmaps
902636
+Additionally copy all persistent bitmaps from the top layer of the source
902636
 @item -n
902636
 Skip the creation of the target volume
902636
 @item -m
902636
@@ -357,7 +359,7 @@ Error on reading data
902636
 
902636
 @end table
902636
 
902636
-@item convert [--object @var{objectdef}] [--image-opts] [--target-image-opts] [-U] [-C] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-B @var{backing_file}] [-o @var{options}] [-l @var{snapshot_param}] [-S @var{sparse_size}] [-m @var{num_coroutines}] [-W] @var{filename} [@var{filename2} [...]] @var{output_filename}
902636
+@item convert [--object @var{objectdef}] [--image-opts] [--target-image-opts] [--bitmaps] [-U] [-C] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-B @var{backing_file}] [-o @var{options}] [-l @var{snapshot_param}] [-S @var{sparse_size}] [-m @var{num_coroutines}] [-W] @var{filename} [@var{filename2} [...]] @var{output_filename}
902636
 
902636
 Convert the disk image @var{filename} or a snapshot @var{snapshot_param}
902636
 to disk image @var{output_filename} using format @var{output_fmt}. It can be optionally compressed (@code{-c}
902636
-- 
902636
1.8.3.1
902636