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