From b2e10493c71160d88bb823cae9a92e806a79b9d6 Mon Sep 17 00:00:00 2001 From: Alexandre Derumier Date: Mon, 2 Sep 2013 19:07:24 +0100 Subject: add qemu-img convert -n option (skip target volume creation) Add a -n 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. Reviewed-by: Eric Blake Signed-off-by: Alexandre Derumier Signed-off-by: Alex Bligh Signed-off-by: Stefan Hajnoczi --- qemu-img.c | 53 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 15 deletions(-) (limited to 'qemu-img.c') diff --git a/qemu-img.c b/qemu-img.c index b9a848db74..e790d6ae99 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -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" + " '-n' 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, skip_create; 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; + skip_create = 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:qn"); if (c == -1) { break; } @@ -1197,6 +1201,9 @@ static int img_convert(int argc, char **argv) case 'q': quiet = true; break; + case 'n': + skip_create = 1; + break; } } @@ -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 (!skip_create) { + /* 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; @@ -1363,6 +1372,20 @@ static int img_convert(int argc, char **argv) bdrv_get_geometry(bs[0], &bs_sectors); buf = qemu_blockalign(out_bs, IO_BUF_SIZE); + if (skip_create) { + int64_t output_length = bdrv_getlength(out_bs); + if (output_length < 0) { + error_report("unable to get output image length: %s\n", + strerror(-output_length)); + ret = -1; + goto out; + } else if (output_length < total_sectors << BDRV_SECTOR_BITS) { + error_report("output file is smaller than input file"); + ret = -1; + goto out; + } + } + if (compress) { ret = bdrv_get_info(out_bs, &bdi); if (ret < 0) { -- cgit v1.2.1