summaryrefslogtreecommitdiff
path: root/qemu-img.c
diff options
context:
space:
mode:
authorAlexandre Derumier <aderumier@odiso.com>2013-09-02 19:07:24 +0100
committerStefan Hajnoczi <stefanha@redhat.com>2013-09-06 15:25:07 +0200
commitb2e10493c71160d88bb823cae9a92e806a79b9d6 (patch)
treee3baaffa48931c9106c5474553e95afc4d305f20 /qemu-img.c
parentb3f3a30f387f34308b3e4d910a2824e69c34182f (diff)
downloadqemu-b2e10493c71160d88bb823cae9a92e806a79b9d6.tar.gz
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 <eblake@redhat.com> Signed-off-by: Alexandre Derumier <aderumier@odiso.com> Signed-off-by: Alex Bligh <alex@alex.org.uk> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Diffstat (limited to 'qemu-img.c')
-rw-r--r--qemu-img.c53
1 files changed, 38 insertions, 15 deletions
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) {