902636
From 53baacb72e8561391841363b2acbd85a783cbc66 Mon Sep 17 00:00:00 2001
902636
From: Eric Blake <eblake@redhat.com>
902636
Date: Tue, 2 Jun 2020 02:34:15 +0100
902636
Subject: [PATCH 10/26] qemu-img: Add bitmap sub-command
902636
902636
RH-Author: Eric Blake <eblake@redhat.com>
902636
Message-id: <20200602023420.2133649-8-eblake@redhat.com>
902636
Patchwork-id: 97074
902636
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 07/12] qemu-img: Add bitmap sub-command
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
Include actions for --add, --remove, --clear, --enable, --disable, and
902636
--merge (note that --clear is a bit of fluff, because the same can be
902636
accomplished by removing a bitmap and then adding a new one in its
902636
place, but it matches what QMP commands exist).  Listing is omitted,
902636
because it does not require a bitmap name and because it was already
902636
possible with 'qemu-img info'.  A single command line can play one or
902636
more bitmap commands in sequence on the same bitmap name (although all
902636
added bitmaps share the same granularity, and and all merged bitmaps
902636
come from the same source file).  Merge defaults to other bitmaps in
902636
the primary image, but can also be told to merge bitmaps from a
902636
distinct image.
902636
902636
While this supports --image-opts for the file being modified, I did
902636
not think it worth the extra complexity to support that for the source
902636
file in a cross-file merges.  Likewise, I chose to have --merge only
902636
take a single source rather than following the QMP support for
902636
multiple merges in one go (although you can still use more than one
902636
--merge in the command line); in part because qemu-img is offline and
902636
therefore atomicity is not an issue.
902636
902636
Upcoming patches will add iotest coverage of these commands while
902636
also testing other features.
902636
902636
Signed-off-by: Eric Blake <eblake@redhat.com>
902636
Reviewed-by: Max Reitz <mreitz@redhat.com>
902636
Message-Id: <20200513011648.166876-7-eblake@redhat.com>
902636
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
902636
(cherry picked from commit 3b51ab4bf0f49a01cc2db7b954e0669e081719b5)
902636
902636
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
902636
902636
Conflicts:
902636
	docs/tools/qemu-img.rst - lives in qemu-img.texi instead; plus
902636
	 fix a typo in the text for --merge rather than waiting for
902636
	 a one-line upstream followup patch
902636
	qemu-img-cmds.hx - context, use texi instead of rst
902636
	qemu-img.c - context
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 |   6 ++
902636
 qemu-img.c       | 248 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
902636
 qemu-img.texi    |  27 ++++++
902636
 3 files changed, 281 insertions(+)
902636
902636
diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
902636
index 1c93e6d..1a6a8e9 100644
902636
--- a/qemu-img-cmds.hx
902636
+++ b/qemu-img-cmds.hx
902636
@@ -25,6 +25,12 @@ STEXI
902636
 @item bench [-c @var{count}] [-d @var{depth}] [-f @var{fmt}] [--flush-interval=@var{flush_interval}] [-n] [--no-drain] [-o @var{offset}] [--pattern=@var{pattern}] [-q] [-s @var{buffer_size}] [-S @var{step_size}] [-t @var{cache}] [-w] [-U] @var{filename}
902636
 ETEXI
902636
 
902636
+DEF("bitmap", img_bitmap,
902636
+    "bitmap (--merge SOURCE | --add | --remove | --clear | --enable | --disable)... [-b source_file [-F source_fmt]] [-g granularity] [--object objectdef] [--image-opts | -f fmt] filename bitmap")
902636
+STEXI
902636
+.. option:: bitmap (--merge @var{source} | --add | --remove | --clear | --enable | --disable)... [-b @var{source_file} [-F @var{source_fmt}]] [-g @var{granularity}] [--object @var{objectdef}] [--image-opts | -f @var{fmt}] @var{filename} @var{bitmap}
902636
+ETEXI
902636
+
902636
 DEF("check", img_check,
902636
     "check [--object objectdef] [--image-opts] [-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] [-U] filename")
902636
 STEXI
902636
diff --git a/qemu-img.c b/qemu-img.c
902636
index e69529b..11a4537 100644
902636
--- a/qemu-img.c
902636
+++ b/qemu-img.c
902636
@@ -28,6 +28,7 @@
902636
 #include "qemu-common.h"
902636
 #include "qemu-version.h"
902636
 #include "qapi/error.h"
902636
+#include "qapi/qapi-commands-block-core.h"
902636
 #include "qapi/qapi-visit-block-core.h"
902636
 #include "qapi/qobject-output-visitor.h"
902636
 #include "qapi/qmp/qjson.h"
902636
@@ -70,6 +71,12 @@ enum {
902636
     OPTION_PREALLOCATION = 265,
902636
     OPTION_SHRINK = 266,
902636
     OPTION_SALVAGE = 267,
902636
+    OPTION_ADD = 269,
902636
+    OPTION_REMOVE = 270,
902636
+    OPTION_CLEAR = 271,
902636
+    OPTION_ENABLE = 272,
902636
+    OPTION_DISABLE = 273,
902636
+    OPTION_MERGE = 274,
902636
 };
902636
 
902636
 typedef enum OutputFormat {
902636
@@ -168,6 +175,14 @@ static void QEMU_NORETURN help(void)
902636
            "  '-n' skips the target volume creation (useful if the volume is created\n"
902636
            "       prior to running qemu-img)\n"
902636
            "\n"
902636
+           "Parameters to bitmap subcommand:\n"
902636
+           "  'bitmap' is the name of the bitmap to manipulate, through one or more\n"
902636
+           "       actions from '--add', '--remove', '--clear', '--enable', '--disable',\n"
902636
+           "       or '--merge source'\n"
902636
+           "  '-g granularity' sets the granularity for '--add' actions\n"
902636
+           "  '-b source' and '-F src_fmt' tell '--merge' actions to find the source\n"
902636
+           "       bitmaps from an alternative file\n"
902636
+           "\n"
902636
            "Parameters to check subcommand:\n"
902636
            "  '-r' tries to repair any inconsistencies that are found during the check.\n"
902636
            "       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
902636
@@ -4402,6 +4417,239 @@ out:
902636
     return 0;
902636
 }
902636
 
902636
+enum ImgBitmapAct {
902636
+    BITMAP_ADD,
902636
+    BITMAP_REMOVE,
902636
+    BITMAP_CLEAR,
902636
+    BITMAP_ENABLE,
902636
+    BITMAP_DISABLE,
902636
+    BITMAP_MERGE,
902636
+};
902636
+typedef struct ImgBitmapAction {
902636
+    enum ImgBitmapAct act;
902636
+    const char *src; /* only used for merge */
902636
+    QSIMPLEQ_ENTRY(ImgBitmapAction) next;
902636
+} ImgBitmapAction;
902636
+
902636
+static int img_bitmap(int argc, char **argv)
902636
+{
902636
+    Error *err = NULL;
902636
+    int c, ret = 1;
902636
+    QemuOpts *opts = NULL;
902636
+    const char *fmt = NULL, *src_fmt = NULL, *src_filename = NULL;
902636
+    const char *filename, *bitmap;
902636
+    BlockBackend *blk = NULL, *src = NULL;
902636
+    BlockDriverState *bs = NULL, *src_bs = NULL;
902636
+    bool image_opts = false;
902636
+    int64_t granularity = 0;
902636
+    bool add = false, merge = false;
902636
+    QSIMPLEQ_HEAD(, ImgBitmapAction) actions;
902636
+    ImgBitmapAction *act, *act_next;
902636
+    const char *op;
902636
+
902636
+    QSIMPLEQ_INIT(&actions);
902636
+
902636
+    for (;;) {
902636
+        static const struct option long_options[] = {
902636
+            {"help", no_argument, 0, 'h'},
902636
+            {"object", required_argument, 0, OPTION_OBJECT},
902636
+            {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
902636
+            {"add", no_argument, 0, OPTION_ADD},
902636
+            {"remove", no_argument, 0, OPTION_REMOVE},
902636
+            {"clear", no_argument, 0, OPTION_CLEAR},
902636
+            {"enable", no_argument, 0, OPTION_ENABLE},
902636
+            {"disable", no_argument, 0, OPTION_DISABLE},
902636
+            {"merge", required_argument, 0, OPTION_MERGE},
902636
+            {"granularity", required_argument, 0, 'g'},
902636
+            {"source-file", required_argument, 0, 'b'},
902636
+            {"source-format", required_argument, 0, 'F'},
902636
+            {0, 0, 0, 0}
902636
+        };
902636
+        c = getopt_long(argc, argv, ":b:f:F:g:h", long_options, NULL);
902636
+        if (c == -1) {
902636
+            break;
902636
+        }
902636
+
902636
+        switch (c) {
902636
+        case ':':
902636
+            missing_argument(argv[optind - 1]);
902636
+            break;
902636
+        case '?':
902636
+            unrecognized_option(argv[optind - 1]);
902636
+            break;
902636
+        case 'h':
902636
+            help();
902636
+            break;
902636
+        case 'b':
902636
+            src_filename = optarg;
902636
+            break;
902636
+        case 'f':
902636
+            fmt = optarg;
902636
+            break;
902636
+        case 'F':
902636
+            src_fmt = optarg;
902636
+            break;
902636
+        case 'g':
902636
+            granularity = cvtnum("granularity", optarg);
902636
+            if (granularity < 0) {
902636
+                return 1;
902636
+            }
902636
+            break;
902636
+        case OPTION_ADD:
902636
+            act = g_new0(ImgBitmapAction, 1);
902636
+            act->act = BITMAP_ADD;
902636
+            QSIMPLEQ_INSERT_TAIL(&actions, act, next);
902636
+            add = true;
902636
+            break;
902636
+        case OPTION_REMOVE:
902636
+            act = g_new0(ImgBitmapAction, 1);
902636
+            act->act = BITMAP_REMOVE;
902636
+            QSIMPLEQ_INSERT_TAIL(&actions, act, next);
902636
+            break;
902636
+        case OPTION_CLEAR:
902636
+            act = g_new0(ImgBitmapAction, 1);
902636
+            act->act = BITMAP_CLEAR;
902636
+            QSIMPLEQ_INSERT_TAIL(&actions, act, next);
902636
+            break;
902636
+        case OPTION_ENABLE:
902636
+            act = g_new0(ImgBitmapAction, 1);
902636
+            act->act = BITMAP_ENABLE;
902636
+            QSIMPLEQ_INSERT_TAIL(&actions, act, next);
902636
+            break;
902636
+        case OPTION_DISABLE:
902636
+            act = g_new0(ImgBitmapAction, 1);
902636
+            act->act = BITMAP_DISABLE;
902636
+            QSIMPLEQ_INSERT_TAIL(&actions, act, next);
902636
+            break;
902636
+        case OPTION_MERGE:
902636
+            act = g_new0(ImgBitmapAction, 1);
902636
+            act->act = BITMAP_MERGE;
902636
+            act->src = optarg;
902636
+            QSIMPLEQ_INSERT_TAIL(&actions, act, next);
902636
+            merge = true;
902636
+            break;
902636
+        case OPTION_OBJECT:
902636
+            opts = qemu_opts_parse_noisily(&qemu_object_opts, optarg, true);
902636
+            if (!opts) {
902636
+                goto out;
902636
+            }
902636
+            break;
902636
+        case OPTION_IMAGE_OPTS:
902636
+            image_opts = true;
902636
+            break;
902636
+        }
902636
+    }
902636
+
902636
+    if (qemu_opts_foreach(&qemu_object_opts,
902636
+                          user_creatable_add_opts_foreach,
902636
+                          qemu_img_object_print_help, &error_fatal)) {
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (QSIMPLEQ_EMPTY(&actions)) {
902636
+        error_report("Need at least one of --add, --remove, --clear, "
902636
+                     "--enable, --disable, or --merge");
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (granularity && !add) {
902636
+        error_report("granularity only supported with --add");
902636
+        goto out;
902636
+    }
902636
+    if (src_fmt && !src_filename) {
902636
+        error_report("-F only supported with -b");
902636
+        goto out;
902636
+    }
902636
+    if (src_filename && !merge) {
902636
+        error_report("Merge bitmap source file only supported with "
902636
+                     "--merge");
902636
+        goto out;
902636
+    }
902636
+
902636
+    if (optind != argc - 2) {
902636
+        error_report("Expecting filename and bitmap name");
902636
+        goto out;
902636
+    }
902636
+
902636
+    filename = argv[optind];
902636
+    bitmap = argv[optind + 1];
902636
+
902636
+    blk = img_open(image_opts, filename, fmt, BDRV_O_RDWR, false, false,
902636
+                   false);
902636
+    if (!blk) {
902636
+        goto out;
902636
+    }
902636
+    bs = blk_bs(blk);
902636
+    if (src_filename) {
902636
+        src = img_open(false, src_filename, src_fmt, 0, false, false, false);
902636
+        if (!src) {
902636
+            goto out;
902636
+        }
902636
+        src_bs = blk_bs(src);
902636
+    } else {
902636
+        src_bs = bs;
902636
+    }
902636
+
902636
+    QSIMPLEQ_FOREACH_SAFE(act, &actions, next, act_next) {
902636
+        switch (act->act) {
902636
+        case BITMAP_ADD:
902636
+            qmp_block_dirty_bitmap_add(bs->node_name, bitmap,
902636
+                                       !!granularity, granularity, true, true,
902636
+                                       false, false, &err;;
902636
+            op = "add";
902636
+            break;
902636
+        case BITMAP_REMOVE:
902636
+            qmp_block_dirty_bitmap_remove(bs->node_name, bitmap, &err;;
902636
+            op = "remove";
902636
+            break;
902636
+        case BITMAP_CLEAR:
902636
+            qmp_block_dirty_bitmap_clear(bs->node_name, bitmap, &err;;
902636
+            op = "clear";
902636
+            break;
902636
+        case BITMAP_ENABLE:
902636
+            qmp_block_dirty_bitmap_enable(bs->node_name, bitmap, &err;;
902636
+            op = "enable";
902636
+            break;
902636
+        case BITMAP_DISABLE:
902636
+            qmp_block_dirty_bitmap_disable(bs->node_name, bitmap, &err;;
902636
+            op = "disable";
902636
+            break;
902636
+        case BITMAP_MERGE: {
902636
+            BlockDirtyBitmapMergeSource *merge_src;
902636
+            BlockDirtyBitmapMergeSourceList *list;
902636
+
902636
+            merge_src = g_new0(BlockDirtyBitmapMergeSource, 1);
902636
+            merge_src->type = QTYPE_QDICT;
902636
+            merge_src->u.external.node = g_strdup(src_bs->node_name);
902636
+            merge_src->u.external.name = g_strdup(act->src);
902636
+            list = g_new0(BlockDirtyBitmapMergeSourceList, 1);
902636
+            list->value = merge_src;
902636
+            qmp_block_dirty_bitmap_merge(bs->node_name, bitmap, list, &err;;
902636
+            qapi_free_BlockDirtyBitmapMergeSourceList(list);
902636
+            op = "merge";
902636
+            break;
902636
+        }
902636
+        default:
902636
+            g_assert_not_reached();
902636
+        }
902636
+
902636
+        if (err) {
902636
+            error_reportf_err(err, "Operation %s on bitmap %s failed: ",
902636
+                              op, bitmap);
902636
+            goto out;
902636
+        }
902636
+        g_free(act);
902636
+    }
902636
+
902636
+    ret = 0;
902636
+
902636
+ out:
902636
+    blk_unref(src);
902636
+    blk_unref(blk);
902636
+    qemu_opts_del(opts);
902636
+    return ret;
902636
+}
902636
+
902636
 #define C_BS      01
902636
 #define C_COUNT   02
902636
 #define C_IF      04
902636
diff --git a/qemu-img.texi b/qemu-img.texi
902636
index b5156d6..abf2771 100644
902636
--- a/qemu-img.texi
902636
+++ b/qemu-img.texi
902636
@@ -230,6 +230,33 @@ specified as well.
902636
 For write tests, by default a buffer filled with zeros is written. This can be
902636
 overridden with a pattern byte specified by @var{pattern}.
902636
 
902636
+@item bitmap (--merge @var{source} | --add | --remove | --clear | --enable | --disable)... [-b @var{source_file} [-F @var{source_fmt}]] [-g @var{granularity}] [--object @var{objectdef}] [--image-opts | -f @var{fmt}] @var{filename} @var{bitmap}
902636
+
902636
+Perform one or more modifications of the persistent bitmap @var{bitmap}
902636
+in the disk image @var{filename}.  The various modifications are:
902636
+
902636
+@table @option
902636
+@item add
902636
+create @var{bitmap}, enabled to record future edits.
902636
+@item remove
902636
+remove @var{bitmap}.
902636
+@item clear
902636
+clear @var{bitmap}.
902636
+@item enable
902636
+change @var{bitmap} to start recording future edits.
902636
+@item disable
902636
+change @var{bitmap} to stop recording future edits.
902636
+@item merge @var{source}
902636
+merge the contents of the @var{source} bitmap into @var{bitmap}.
902636
+@end table
902636
+
902636
+Additional options include @option{-g} which sets a non-default
902636
+@var{granularity} for @option{--add}, and @option{-b} and @option{-F}
902636
+which select an alternative source file for all @var{source} bitmaps used by
902636
+@option{--merge}.
902636
+
902636
+To see what bitmaps are present in an image, use @code{qemu-img info}.
902636
+
902636
 @item check [--object @var{objectdef}] [--image-opts] [-q] [-f @var{fmt}] [--output=@var{ofmt}] [-r [leaks | all]] [-T @var{src_cache}] [-U] @var{filename}
902636
 
902636
 Perform a consistency check on the disk image @var{filename}. The command can
902636
-- 
902636
1.8.3.1
902636