diff mbox

[V7,4/6] qemu-img: add -l for snapshot in convert

Message ID 1386148259-10962-5-git-send-email-xiawenc@linux.vnet.ibm.com
State New
Headers show

Commit Message

Wayne Xia Dec. 4, 2013, 9:10 a.m. UTC
Now qemu-img convert have similar options as qemu-nbd for internal
snapshot.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 qemu-img-cmds.hx |    4 ++--
 qemu-img.c       |   44 +++++++++++++++++++++++++++++++++++---------
 qemu-img.texi    |   12 ++++++++----
 3 files changed, 45 insertions(+), 15 deletions(-)

Comments

Eric Blake Dec. 4, 2013, 8:42 p.m. UTC | #1
On 12/04/2013 02:10 AM, Wenchao Xia wrote:
> Now qemu-img convert have similar options as qemu-nbd for internal
> snapshot.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---

> @@ -1183,6 +1189,18 @@ static int img_convert(int argc, char **argv)
>          case 's':
>              snapshot_name = optarg;
>              break;
> +        case 'l':
> +            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
> +                sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
> +                if (!sn_opts) {
> +                    error_report("Failed in parsing snapshot param '%s'",
> +                                 optarg);
> +                    return 1;
> +                }
> +            } else {
> +                snapshot_name = optarg;
> +            }
> +            break;

Do we want a followup patch that makes it an error to use -l and -s
together?  Without such a patch, we have the odd behavior that:

convert -l name1 -s name2

loads name2, but:

convert -l snapshot.name=name1 -s name2

loads name1.  Confusing that the choice of HOW the argument to -l is
specified determines whether the -s has any impact.

For that matter, why can't '-s' and '-l' be made synonyms of each other?
 In other words, why not support:

convert -s snapshot.name=name1
Wayne Xia Dec. 5, 2013, 6:06 a.m. UTC | #2
于 2013/12/5 4:42, Eric Blake 写道:
> On 12/04/2013 02:10 AM, Wenchao Xia wrote:
>> Now qemu-img convert have similar options as qemu-nbd for internal
>> snapshot.
>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>> ---
>
>> @@ -1183,6 +1189,18 @@ static int img_convert(int argc, char **argv)
>>           case 's':
>>               snapshot_name = optarg;
>>               break;
>> +        case 'l':
>> +            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
>> +                sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
>> +                if (!sn_opts) {
>> +                    error_report("Failed in parsing snapshot param '%s'",
>> +                                 optarg);
>> +                    return 1;
>> +                }
>> +            } else {
>> +                snapshot_name = optarg;
>> +            }
>> +            break;
>
> Do we want a followup patch that makes it an error to use -l and -s
> together?  Without such a patch, we have the odd behavior that:
>
> convert -l name1 -s name2
>
> loads name2, but:
>
> convert -l snapshot.name=name1 -s name2
>
> loads name1.  Confusing that the choice of HOW the argument to -l is
> specified determines whether the -s has any impact.
>
> For that matter, why can't '-s' and '-l' be made synonyms of each other?
>   In other words, why not support:
>
> convert -s snapshot.name=name1
>
   Previous I planned to use -l for internal snapshot in all possible
program, since -s is taken as external snapshot in qemu, qemu-nbd.
let -s stands for internal in qemu-img convert only, may bring
confuse to user, so I deprecated it instead of enhance it(I want
to remove it but may bring compatiablity issue).
   Yes, it should report error when both specified, will send a patch
if you agree '-l' should still be used.
Wayne Xia Dec. 9, 2013, 3:43 a.m. UTC | #3
>>
>> convert -s snapshot.name=name1
>>
>    Previous I planned to use -l for internal snapshot in all possible
> program, since -s is taken as external snapshot in qemu, qemu-nbd.
> let -s stands for internal in qemu-img convert only, may bring
> confuse to user, so I deprecated it instead of enhance it(I want
> to remove it but may bring compatiablity issue).
>    Yes, it should report error when both specified, will send a patch
> if you agree '-l' should still be used.
>
>
   Eric, I hope to get your idea before patching, any comments?
Eric Blake Dec. 13, 2013, 1:44 p.m. UTC | #4
On 12/08/2013 08:43 PM, Wenchao Xia wrote:
>>>
>>> convert -s snapshot.name=name1
>>>
>>    Previous I planned to use -l for internal snapshot in all possible
>> program, since -s is taken as external snapshot in qemu, qemu-nbd.

Consistency in command line options between different tools is nice, but
is less important than adding functionality.  I'm perfectly fine if we
use -l in one tool and -s in another, as long as the documentation is
clear on how to spell the option for the tool I want to use.

>> let -s stands for internal in qemu-img convert only, may bring
>> confuse to user, so I deprecated it instead of enhance it(I want
>> to remove it but may bring compatiablity issue).
>>    Yes, it should report error when both specified, will send a patch
>> if you agree '-l' should still be used.
>>
>>
>   Eric, I hope to get your idea before patching, any comments?
> 

My biggest concern was that by adding -l as a superset of -s, but not
taking care of the relation between the two, you created odd command
line usage patterns.  For qemu-img, it may be simpler to just make -s do
everything, instead of trying to deprecate it (that is, adding -l for
consistency with other tools while breaking -s isn't nice).
Wayne Xia Dec. 16, 2013, 2:47 a.m. UTC | #5
于 2013/12/13 21:44, Eric Blake 写道:
> On 12/08/2013 08:43 PM, Wenchao Xia wrote:
>>>>
>>>> convert -s snapshot.name=name1
>>>>
>>>     Previous I planned to use -l for internal snapshot in all possible
>>> program, since -s is taken as external snapshot in qemu, qemu-nbd.
>
> Consistency in command line options between different tools is nice, but
> is less important than adding functionality.  I'm perfectly fine if we
> use -l in one tool and -s in another, as long as the documentation is
> clear on how to spell the option for the tool I want to use.
>
>>> let -s stands for internal in qemu-img convert only, may bring
>>> confuse to user, so I deprecated it instead of enhance it(I want
>>> to remove it but may bring compatiablity issue).
>>>     Yes, it should report error when both specified, will send a patch
>>> if you agree '-l' should still be used.
>>>
>>>
>>    Eric, I hope to get your idea before patching, any comments?
>>
>
> My biggest concern was that by adding -l as a superset of -s, but not
> taking care of the relation between the two, you created odd command
> line usage patterns.  For qemu-img, it may be simpler to just make -s do
> everything, instead of trying to deprecate it (that is, adding -l for
> consistency with other tools while breaking -s isn't nice).
>
   OK, there is still one cornor case to consider:
-s snapshot.name=name1
   It may change the semantics if a caller used qemu-img convert as
above before, although it seems insane.:)
diff mbox

Patch

diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
index da1d965..d029609 100644
--- a/qemu-img-cmds.hx
+++ b/qemu-img-cmds.hx
@@ -34,9 +34,9 @@  STEXI
 ETEXI
 
 DEF("convert", img_convert,
-    "convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
+    "convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_id_or_name] [-l snapshot_param] [-S sparse_size] filename [filename2 [...]] output_filename")
 STEXI
-@item convert [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 ETEXI
 
 DEF("info", img_info,
diff --git a/qemu-img.c b/qemu-img.c
index a73beb5..ee940c2 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -93,6 +93,11 @@  static void help(void)
            "  'options' is a comma separated list of format specific options in a\n"
            "    name=value format. Use -o ? for an overview of the options supported by the\n"
            "    used format\n"
+           "  'snapshot_param' is param used for internal snapshot, format\n"
+           "    is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
+           "    '[ID_OR_NAME]'\n"
+           "  'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
+           "    instead\n"
            "  '-c' indicates that target image must be compressed (qcow format only)\n"
            "  '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
            "       match exactly. The image doesn't need a working backing file before\n"
@@ -1140,6 +1145,7 @@  static int img_convert(int argc, char **argv)
     int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
     bool quiet = false;
     Error *local_err = NULL;
+    QemuOpts *sn_opts = NULL;
 
     fmt = NULL;
     out_fmt = "raw";
@@ -1148,7 +1154,7 @@  static int img_convert(int argc, char **argv)
     compress = 0;
     skip_create = 0;
     for(;;) {
-        c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qn");
+        c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qnl:");
         if (c == -1) {
             break;
         }
@@ -1183,6 +1189,18 @@  static int img_convert(int argc, char **argv)
         case 's':
             snapshot_name = optarg;
             break;
+        case 'l':
+            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
+                sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
+                if (!sn_opts) {
+                    error_report("Failed in parsing snapshot param '%s'",
+                                 optarg);
+                    return 1;
+                }
+            } else {
+                snapshot_name = optarg;
+            }
+            break;
         case 'S':
         {
             int64_t sval;
@@ -1254,7 +1272,12 @@  static int img_convert(int argc, char **argv)
         total_sectors += bs_sectors;
     }
 
-    if (snapshot_name != NULL) {
+    if (sn_opts) {
+        ret = bdrv_snapshot_load_tmp(bs[0],
+                                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
+                                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
+                                     &local_err);
+    } else if (snapshot_name != NULL) {
         if (bs_n > 1) {
             error_report("No support for concatenating multiple snapshot");
             ret = -1;
@@ -1262,13 +1285,13 @@  static int img_convert(int argc, char **argv)
         }
 
         bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
-        if (error_is_set(&local_err)) {
-            error_report("Failed to load snapshot: %s",
-                         error_get_pretty(local_err));
-            error_free(local_err);
-            ret = -1;
-            goto out;
-        }
+    }
+    if (error_is_set(&local_err)) {
+        error_report("Failed to load snapshot: %s",
+                     error_get_pretty(local_err));
+        error_free(local_err);
+        ret = -1;
+        goto out;
     }
 
     /* Find driver and parse its options */
@@ -1559,6 +1582,9 @@  out:
     free_option_parameters(create_options);
     free_option_parameters(param);
     qemu_vfree(buf);
+    if (sn_opts) {
+        qemu_opts_del(sn_opts);
+    }
     if (out_bs) {
         bdrv_unref(out_bs);
     }
diff --git a/qemu-img.texi b/qemu-img.texi
index 768054e..fbdbfa7 100644
--- a/qemu-img.texi
+++ b/qemu-img.texi
@@ -46,7 +46,11 @@  is the destination disk image filename
 is a comma separated list of format specific options in a
 name=value format. Use @code{-o ?} for an overview of the options supported
 by the used format or see the format descriptions below for details.
-
+@item snapshot_param
+is param used for internal snapshot, format is
+'snapshot.id=[ID],snapshot.name=[NAME]' or '[ID_OR_NAME]'
+@item snapshot_id_or_name
+is deprecated, use snapshot_param instead
 
 @item -c
 indicates that target image must be compressed (qcow format only)
@@ -179,10 +183,10 @@  Error on reading data
 
 @end table
 
-@item convert [-c] [-p] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_name}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-p] [-n] [-f @var{fmt}] [-t @var{cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 
-Convert the disk image @var{filename} or a snapshot @var{snapshot_name} to disk image @var{output_filename}
-using format @var{output_fmt}. It can be optionally compressed (@code{-c}
+Convert the disk image @var{filename} or a snapshot @var{snapshot_param}(@var{snapshot_id_or_name} is deprecated)
+to disk image @var{output_filename} using format @var{output_fmt}. It can be optionally compressed (@code{-c}
 option) or use any format specific options like encryption (@code{-o} option).
 
 Only the formats @code{qcow} and @code{qcow2} support compression. The