thebeanogamer / rpms / qemu-kvm

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