Skip to contentSkip to conversation

add qemu-img convert -C option (skip target volume creation)

PATCH Superseded Alex Bligh 4 participants 11 messages Aug 12, 2013 – Aug 26, 2013

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Alex Bligh

author
#0 lore raw

PATCH v4 1/1 add qemu-img convert -C option (skip target volume creation)

From: Alexandre Derumier <[email protected]>

Add a -C option to skip volume creation on qemu-img convert. This is useful for targets such as rbd / ceph, where the target volume may already exist; we cannot always rely on qemu-img convert to create the image, as dependent on the output format, there may be parameters which are not possible to specify through the qemu-img convert command line.

---
 qemu-img-cmds.hx |    4 ++--
 qemu-img.c       |   39 ++++++++++++++++++++++++---------------
 qemu-img.texi    |   15 ++++++++++++++-
 3 files changed, 40 insertions(+), 18 deletions(-)
qemu-img-cmds.hx +2 -2
index 4ca7e95..74ced81 100644
@@ -34,9 +34,9 @@ STEXI
 ETEXI
 
 DEF("convert", img_convert,
-    "convert [-c] [-p] [-q] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
+    "convert [-c] [-p] [-q] [-C] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
 STEXI
-@item convert [-c] [-p] [-q] [-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] [-C] [-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}
 ETEXI
 
 DEF("info", img_info,
qemu-img.c +24 -15
index b9a848d..146909b 100644
@@ -103,6 +103,8 @@ static void help(void)
            "  '-S' indicates the consecutive number of bytes that must contain only zeros\n"
            "       for qemu-img to create a sparse image during conversion\n"
            "  '--output' takes the format in which the output must be done (human or json)\n"
+           "  '-C' skips the target volume creation (useful if the volume is created\n"
+           "       prior to running qemu-img)\n"
            "\n"
            "Parameters to check subcommand:\n"
            "  '-r' tries to repair any inconsistencies that are found during the check.\n"
@@ -1116,7 +1118,8 @@ out3:
 
 static int img_convert(int argc, char **argv)
 {
-    int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
+    int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size,
+        cluster_sectors, skipcreate;
     int progress = 0, flags;
     const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
     BlockDriver *drv, *proto_drv;
@@ -1139,8 +1142,9 @@ static int img_convert(int argc, char **argv)
     cache = "unsafe";
     out_baseimg = NULL;
     compress = 0;
+    skipcreate = 0;
     for(;;) {
-        c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:q");
+        c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qC");
         if (c == -1) {
             break;
         }
@@ -1161,6 +1165,9 @@ static int img_convert(int argc, char **argv)
         case 'c':
             compress = 1;
             break;
+        case 'C':
+            skipcreate = 1;
+            break;
         case 'e':
             error_report("option -e is deprecated, please use \'-o "
                   "encryption\' instead!");
@@ -1329,20 +1336,22 @@ static int img_convert(int argc, char **argv)
         }
     }
 
-    /* Create the new image */
-    ret = bdrv_create(drv, out_filename, param);
-    if (ret < 0) {
-        if (ret == -ENOTSUP) {
-            error_report("Formatting not supported for file format '%s'",
-                         out_fmt);
-        } else if (ret == -EFBIG) {
-            error_report("The image size is too large for file format '%s'",
-                         out_fmt);
-        } else {
-            error_report("%s: error while converting %s: %s",
-                         out_filename, out_fmt, strerror(-ret));
+    if (!skipcreate) {
+        /* Create the new image */
+        ret = bdrv_create(drv, out_filename, param);
+        if (ret < 0) {
+            if (ret == -ENOTSUP) {
+                error_report("Formatting not supported for file format '%s'",
+                             out_fmt);
+            } else if (ret == -EFBIG) {
+                error_report("The image size is too large for file format '%s'",
+                             out_fmt);
+            } else {
+                error_report("%s: error while converting %s: %s",
+                             out_filename, out_fmt, strerror(-ret));
+            }
+            goto out;
         }
-        goto out;
     }
 
     flags = BDRV_O_RDWR;
qemu-img.texi +14 -1
index 69f1bda..9e5ba36 100644
@@ -96,6 +96,14 @@ Second image format
 Strict mode - fail on on different image size or sector allocation
 @end table
 
+Parameters to convert subcommand:
+
+@table @option
+
+@item -C
+Skip the creation of the target volume
+@end table
+
 Command description:
 
 @table @option
@@ -171,7 +179,7 @@ Error on reading data
 
 @end table
 
-@item convert [-c] [-p] [-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] [-C] [-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}
 
 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}
@@ -190,6 +198,11 @@ created as a copy on write image of the specified base image; the
 @var{backing_file} should have the same content as the input's base image,
 however the path, image format, etc may differ.
 
+If the @code{-C} option is specified, the target volume creation will be
+skipped. This is useful for formats such as @code{rbd} if the target
+volume has already been created with site specific options that cannot
+be supplied through qemu-img.
+
 @item info [-f @var{fmt}] [--output=@var{ofmt}] [--backing-chain] @var{filename}
 
 Give information about the disk image @var{filename}. Use it in
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 9 lines · Alex Bligh

From: Alexandre Derumier <[email protected]>

Add a -C option to skip volume creation on qemu-img convert. This is useful for targets such as rbd / ceph, where the target volume may already exist; we cannot always rely on qemu-img convert to create the image, as dependent on the output format, there may be parameters which are not possible to specify through the qemu-img convert command line.

Quoted 7 lines
---
 qemu-img-cmds.hx |    4 ++--
 qemu-img.c       |   39 ++++++++++++++++++++++++---------------
 qemu-img.texi    |   15 ++++++++++++++-
 3 files changed, 40 insertions(+), 18 deletions(-)

Looks good but please include a new qemu-iotest test case that checks:

1. Error if the target volume does not exist.

2. Success if a correctly sized target volume exists.

3. ?? if an incorrectly sized target volume exists.

...and anything else you feel is worth testing.

I recommend keeping the test volume size small so the test case can execute quickly. 1 MB should be fine for raw or qcow2 images.

Stefan

#2 lore raw

Il 22/08/2013 13:45, Stefan Hajnoczi ha scritto:

Quoted 21 lines · Alex Bligh
Quoted 9 lines
Quoted 9 lines

From: Alexandre Derumier <[email protected]>

Add a -C option to skip volume creation on qemu-img convert. This is useful for targets such as rbd / ceph, where the target volume may already exist; we cannot always rely on qemu-img convert to create the image, as dependent on the output format, there may be parameters which are not possible to specify through the qemu-img convert command line.

Quoted 7 lines
Quoted 7 lines
---
 qemu-img-cmds.hx |    4 ++--
 qemu-img.c       |   39 ++++++++++++++++++++++++---------------
 qemu-img.texi    |   15 ++++++++++++++-
 3 files changed, 40 insertions(+), 18 deletions(-)

Looks good but please include a new qemu-iotest test case that checks:

1. Error if the target volume does not exist.

Quoted 8 lines

2. Success if a correctly sized target volume exists.

3. ?? if an incorrectly sized target volume exists.

...and anything else you feel is worth testing.

I recommend keeping the test volume size small so the test case can execute quickly. 1 MB should be fine for raw or qcow2 images.

Also, this is the same as some HMP commands' "-n" option (live snapshots, mirroring, backup) so I suggest to use that name.

Paolo

author
#3 lore raw

Paolo,

Quoted 2 lines · Paolo Bonzini

Also, this is the same as some HMP commands' "-n" option (live snapshots, mirroring, backup) so I suggest to use that name.

You mean -n instead of -C? Sure I can do that, but is that something you feel strongly about? I am aware there are a number of people who have been using the patch with -C for some time.

Signature · 1 line
Alex Bligh
#4 lore raw

Il 22/08/2013 20:03, Alex Bligh ha scritto:

Quoted 10 lines

Paolo,

Quoted 2 lines · Paolo Bonzini

Also, this is the same as some HMP commands' "-n" option (live snapshots, mirroring, backup) so I suggest to use that name.

You mean -n instead of -C? Sure I can do that, but is that something you feel strongly about? I am aware there are a number of people who have been using the patch with -C for some time.

I'll be fine if Stefan and Kevin override me, but yes, I think consistency is important. qemu-img has offline functionality equivalent to block jobs, and it is useful to keep qemu-img subcommands as consistent as possible between them and with the corresponding HMP commands. convert is the equivalent of the online mirroring block job.

Paolo

author
#5 lore raw
Quoted 7 lines · --On 22 August 2013 21:30:41 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 6 lines
Quoted 2 lines

Also, this is the same as some HMP commands' "-n" option (live snapshots, mirroring, backup) so I suggest to use that name.

You mean -n instead of -C? Sure I can do that, but is that something you feel strongly about? I am aware there are a number of people who have been using the patch with -C for some time.

Quoted 5 lines

I'll be fine if Stefan and Kevin override me, but yes, I think consistency is important. qemu-img has offline functionality equivalent to block jobs, and it is useful to keep qemu-img subcommands as consistent as possible between them and with the corresponding HMP commands. convert is the equivalent of the online mirroring block job.

I note -f in hmp means 'whole disk' whereas in qemu-img it means 'format'. A quick scan suggests there are no other flags that do similar things. So this would be the first flag that is consistent I think!

Signature · 1 line
Alex Bligh
#6 lore raw
In reply to Alex Bligh
Quoted 10 lines · Alex Bligh

Paolo,

Quoted 2 lines · Paolo Bonzini

Also, this is the same as some HMP commands' "-n" option (live snapshots, mirroring, backup) so I suggest to use that name.

You mean -n instead of -C? Sure I can do that, but is that something you feel strongly about? I am aware there are a number of people who have been using the patch with -C for some time.

Anyone using a non-upstream patch should already be prepared for upstream to take a different course. Such a user can add -C as a synonym for -n (at least, until upstream DOES add a -C with different semantics), or rewrite their scripts.

I too would like to reuse -n for consistency.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw
In reply to Alex Bligh
Quoted 8 lines · Alex Bligh
Quoted 2 lines · Paolo Bonzini

Also, this is the same as some HMP commands' "-n" option (live snapshots, mirroring, backup) so I suggest to use that name.

You mean -n instead of -C? Sure I can do that, but is that something you feel strongly about? I am aware there are a number of people who have been using the patch with -C for some time.

Sharing the -n flag with other commands is better than introducing a new flag.

Stefan

author
#8 lore raw
In reply to Stefan Hajnoczi
Quoted 3 lines · Stefan Hajnoczi

Looks good but please include a new qemu-iotest test case that checks:

1. Error if the target volume does not exist.

Can do

Quoted 3 lines

2. Success if a correctly sized target volume exists.

3. ?? if an incorrectly sized target volume exists.

Good catch re size.

The behaviour of the current code appears to be as follows (Alexandre's code not mine):

a) if the target volume size > the converted volume size, convert leaving the remaining data on the target volume as is. This is I believe useful, as on (e.g.) rbd, the target volume size may be larger than required due to rounding requirements.

b) if the target volume size < the converted volume size, convert truncates it but does not error. I'm torn between whether this continue to do exactly as it asked, attempt to expand the volume, or error. These all seem reasonably easy (the expand option presumably being a call to bdrv_truncate).

Before we get to testing, I'm guessing we should establish whether (a) is correct (I think yes), and what the correct behaviour is for (b).

Signature · 1 line
Alex Bligh
#9 lore raw
Quoted 9 lines · Alex Bligh

On 22 Aug 2013, at 12:45, Stefan Hajnoczi wrote:
The behaviour of the current code appears to be as follows (Alexandre's code not mine):

a) if the target volume size > the converted volume size, convert leaving the remaining data on the target volume as is. This is I believe useful, as on (e.g.) rbd, the target volume size may be larger than required due to rounding requirements.

Seems ok.

Quoted 5 lines

b) if the target volume size < the converted volume size, convert truncates it but does not error. I'm torn between whether this continue to do exactly as it asked, attempt to expand the volume, or error. These all seem reasonably easy (the expand option presumably being a call to bdrv_truncate).

Silently truncating can be a problem, e.g. the user deletes the original file after conversion completes and later discovers not all data was copied.

I think we should fail here.

Stefan

author
#10 lore raw
Quoted 5 lines · Stefan Hajnoczi

Silently truncating can be a problem, e.g. the user deletes the original file after conversion completes and later discovers not all data was copied.

I think we should fail here.

Fixed in v5 (just sent), together with using -n not -C (obviously I'm fighting a losing battle there).

Signature · 1 line
Alex Bligh