9ae3a8
From 5fcb6627be0a9a6744546adab5e638d4a265d2b7 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Tue, 7 Jan 2014 21:57:09 +0100
9ae3a8
Subject: [PATCH 04/14] block: Image file option amendment
9ae3a8
9ae3a8
RH-Author: Max Reitz <mreitz@redhat.com>
9ae3a8
Message-id: <1389131839-12920-5-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 56540
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 04/14] block: Image file option amendment
9ae3a8
Bugzilla: 1033490
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
BZ: 1033490
9ae3a8
9ae3a8
This patch adds the "amend" option to qemu-img which allows changing
9ae3a8
image options on existing image files. It also adds the generic bdrv
9ae3a8
implementation which is basically just a wrapper for the image format
9ae3a8
specific function.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 6f176b48f9f98820ed192a1355cc1c7c08ddf46b)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	block.c
9ae3a8
	qemu-img.c
9ae3a8
9ae3a8
Conflicts because d616b224745b2c522f965cf8de7da17b553b959a has not yet
9ae3a8
been backported. This is a single additional function which is missing
9ae3a8
from the context in this case, therefore later backporting of that
9ae3a8
commit would be trivial and obvious to resolve.
9ae3a8
9ae3a8
Conflicts in qemu-img.c, because it is bdrv_delete() downstream for now,
9ae3a8
rather than bdrv_unref().
9ae3a8
---
9ae3a8
 block.c                   |  8 +++++
9ae3a8
 include/block/block.h     |  2 ++
9ae3a8
 include/block/block_int.h |  3 ++
9ae3a8
 qemu-img-cmds.hx          |  6 ++++
9ae3a8
 qemu-img.c                | 84 +++++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 qemu-img.texi             |  5 +++
9ae3a8
 6 files changed, 108 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block.c                   |    8 ++++
9ae3a8
 include/block/block.h     |    2 +
9ae3a8
 include/block/block_int.h |    3 ++
9ae3a8
 qemu-img-cmds.hx          |    6 +++
9ae3a8
 qemu-img.c                |   84 +++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 qemu-img.texi             |    5 +++
9ae3a8
 6 files changed, 108 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/block.c b/block.c
9ae3a8
index 1ac1ab3..cc7afd4 100644
9ae3a8
--- a/block.c
9ae3a8
+++ b/block.c
9ae3a8
@@ -4980,3 +4980,11 @@ AioContext *bdrv_get_aio_context(BlockDriverState *bs)
9ae3a8
     /* Currently BlockDriverState always uses the main loop AioContext */
9ae3a8
     return qemu_get_aio_context();
9ae3a8
 }
9ae3a8
+
9ae3a8
+int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options)
9ae3a8
+{
9ae3a8
+    if (bs->drv->bdrv_amend_options == NULL) {
9ae3a8
+        return -ENOTSUP;
9ae3a8
+    }
9ae3a8
+    return bs->drv->bdrv_amend_options(bs, options);
9ae3a8
+}
9ae3a8
diff --git a/include/block/block.h b/include/block/block.h
9ae3a8
index bcf71e2..e7f718c 100644
9ae3a8
--- a/include/block/block.h
9ae3a8
+++ b/include/block/block.h
9ae3a8
@@ -280,6 +280,8 @@ typedef enum {
9ae3a8
 
9ae3a8
 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
9ae3a8
 
9ae3a8
+int bdrv_amend_options(BlockDriverState *bs_new, QEMUOptionParameter *options);
9ae3a8
+
9ae3a8
 /* async block I/O */
9ae3a8
 typedef void BlockDriverDirtyHandler(BlockDriverState *bs, int64_t sector,
9ae3a8
                                      int sector_num);
9ae3a8
diff --git a/include/block/block_int.h b/include/block/block_int.h
9ae3a8
index 71c8b53..d49a317 100644
9ae3a8
--- a/include/block/block_int.h
9ae3a8
+++ b/include/block/block_int.h
9ae3a8
@@ -202,6 +202,9 @@ struct BlockDriver {
9ae3a8
     int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result,
9ae3a8
         BdrvCheckMode fix);
9ae3a8
 
9ae3a8
+    int (*bdrv_amend_options)(BlockDriverState *bs,
9ae3a8
+        QEMUOptionParameter *options);
9ae3a8
+
9ae3a8
     void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
9ae3a8
 
9ae3a8
     /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */
9ae3a8
diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
9ae3a8
index 0c36e59..da1d965 100644
9ae3a8
--- a/qemu-img-cmds.hx
9ae3a8
+++ b/qemu-img-cmds.hx
9ae3a8
@@ -67,5 +67,11 @@ DEF("resize", img_resize,
9ae3a8
     "resize [-q] filename [+ | -]size")
9ae3a8
 STEXI
9ae3a8
 @item resize [-q] @var{filename} [+ | -]@var{size}
9ae3a8
+ETEXI
9ae3a8
+
9ae3a8
+DEF("amend", img_amend,
9ae3a8
+    "amend [-q] [-f fmt] -o options filename")
9ae3a8
+STEXI
9ae3a8
+@item amend [-q] [-f @var{fmt}] -o @var{options} @var{filename}
9ae3a8
 @end table
9ae3a8
 ETEXI
9ae3a8
diff --git a/qemu-img.c b/qemu-img.c
9ae3a8
index 1fe175b..f0f70e4 100644
9ae3a8
--- a/qemu-img.c
9ae3a8
+++ b/qemu-img.c
9ae3a8
@@ -2566,6 +2566,90 @@ out:
9ae3a8
     return 0;
9ae3a8
 }
9ae3a8
 
9ae3a8
+static int img_amend(int argc, char **argv)
9ae3a8
+{
9ae3a8
+    int c, ret = 0;
9ae3a8
+    char *options = NULL;
9ae3a8
+    QEMUOptionParameter *create_options = NULL, *options_param = NULL;
9ae3a8
+    const char *fmt = NULL, *filename;
9ae3a8
+    bool quiet = false;
9ae3a8
+    BlockDriverState *bs = NULL;
9ae3a8
+
9ae3a8
+    for (;;) {
9ae3a8
+        c = getopt(argc, argv, "hqf:o:");
9ae3a8
+        if (c == -1) {
9ae3a8
+            break;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        switch (c) {
9ae3a8
+            case 'h':
9ae3a8
+            case '?':
9ae3a8
+                help();
9ae3a8
+                break;
9ae3a8
+            case 'o':
9ae3a8
+                options = optarg;
9ae3a8
+                break;
9ae3a8
+            case 'f':
9ae3a8
+                fmt = optarg;
9ae3a8
+                break;
9ae3a8
+            case 'q':
9ae3a8
+                quiet = true;
9ae3a8
+                break;
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (optind != argc - 1) {
9ae3a8
+        help();
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (!options) {
9ae3a8
+        help();
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    filename = argv[argc - 1];
9ae3a8
+
9ae3a8
+    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
9ae3a8
+    if (!bs) {
9ae3a8
+        error_report("Could not open image '%s'", filename);
9ae3a8
+        ret = -1;
9ae3a8
+        goto out;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    fmt = bs->drv->format_name;
9ae3a8
+
9ae3a8
+    if (is_help_option(options)) {
9ae3a8
+        ret = print_block_option_help(filename, fmt);
9ae3a8
+        goto out;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    create_options = append_option_parameters(create_options,
9ae3a8
+            bs->drv->create_options);
9ae3a8
+    options_param = parse_option_parameters(options, create_options,
9ae3a8
+            options_param);
9ae3a8
+    if (options_param == NULL) {
9ae3a8
+        error_report("Invalid options for file format '%s'", fmt);
9ae3a8
+        ret = -1;
9ae3a8
+        goto out;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    ret = bdrv_amend_options(bs, options_param);
9ae3a8
+    if (ret < 0) {
9ae3a8
+        error_report("Error while amending options: %s", strerror(-ret));
9ae3a8
+        goto out;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+out:
9ae3a8
+    if (bs) {
9ae3a8
+        bdrv_delete(bs);
9ae3a8
+    }
9ae3a8
+    free_option_parameters(create_options);
9ae3a8
+    free_option_parameters(options_param);
9ae3a8
+    if (ret) {
9ae3a8
+        return 1;
9ae3a8
+    }
9ae3a8
+    return 0;
9ae3a8
+}
9ae3a8
+
9ae3a8
 static const img_cmd_t img_cmds[] = {
9ae3a8
 #define DEF(option, callback, arg_string)        \
9ae3a8
     { option, callback },
9ae3a8
diff --git a/qemu-img.texi b/qemu-img.texi
9ae3a8
index dc578bb..da36975 100644
9ae3a8
--- a/qemu-img.texi
9ae3a8
+++ b/qemu-img.texi
9ae3a8
@@ -356,6 +356,11 @@ sizes accordingly.  Failure to do so will result in data loss!
9ae3a8
 After using this command to grow a disk image, you must use file system and
9ae3a8
 partitioning tools inside the VM to actually begin using the new space on the
9ae3a8
 device.
9ae3a8
+
9ae3a8
+@item amend [-f @var{fmt}] -o @var{options} @var{filename}
9ae3a8
+
9ae3a8
+Amends the image format specific @var{options} for the image file
9ae3a8
+@var{filename}. Not all file formats support this operation.
9ae3a8
 @end table
9ae3a8
 @c man end
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8