9ae3a8
From f6e47d854362ee1f5d405852ac75f2015bf94ad7 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Thu, 7 Nov 2013 12:27:37 +0100
9ae3a8
Subject: [PATCH 15/87] add qemu-img convert -n option (skip target volume creation)
9ae3a8
9ae3a8
RH-Author: Max Reitz <mreitz@redhat.com>
9ae3a8
Message-id: <1383604354-12743-18-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 55317
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 17/43] add qemu-img convert -n option (skip target volume creation)
9ae3a8
Bugzilla: 1026524
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
From: Alexandre Derumier <aderumier@odiso.com>
9ae3a8
9ae3a8
BZ: 1026524
9ae3a8
9ae3a8
Add a -n option to skip volume creation on qemu-img convert.
9ae3a8
This is useful for targets such as rbd / ceph, where the
9ae3a8
target volume may already exist; we cannot always rely on
9ae3a8
qemu-img convert to create the image, as dependent on the
9ae3a8
output format, there may be parameters which are not possible
9ae3a8
to specify through the qemu-img convert command line.
9ae3a8
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
9ae3a8
Signed-off-by: Alex Bligh <alex@alex.org.uk>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
(cherry picked from commit b2e10493c71160d88bb823cae9a92e806a79b9d6)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	tests/qemu-iotests/group
9ae3a8
9ae3a8
Conflicts because upstream contains more tests in the group file.
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 qemu-img-cmds.hx           |    4 +-
9ae3a8
 qemu-img.c                 |   53 +++++++++++++++++-------
9ae3a8
 qemu-img.texi              |   15 ++++++-
9ae3a8
 tests/qemu-iotests/063     |   97 ++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 tests/qemu-iotests/063.out |   10 +++++
9ae3a8
 tests/qemu-iotests/group   |    1 +
9ae3a8
 6 files changed, 162 insertions(+), 18 deletions(-)
9ae3a8
 create mode 100644 tests/qemu-iotests/063
9ae3a8
 create mode 100644 tests/qemu-iotests/063.out
9ae3a8
9ae3a8
diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
9ae3a8
index c97a1f4..0c36e59 100644
9ae3a8
--- a/qemu-img-cmds.hx
9ae3a8
+++ b/qemu-img-cmds.hx
9ae3a8
@@ -34,9 +34,9 @@ STEXI
9ae3a8
 ETEXI
9ae3a8
 
9ae3a8
 DEF("convert", img_convert,
9ae3a8
-    "convert [-c] [-p] [-q] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
9ae3a8
+    "convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename")
9ae3a8
 STEXI
9ae3a8
-@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}
9ae3a8
+@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}
9ae3a8
 ETEXI
9ae3a8
 
9ae3a8
 DEF("info", img_info,
9ae3a8
diff --git a/qemu-img.c b/qemu-img.c
9ae3a8
index 3a59bf0..8fcea44 100644
9ae3a8
--- a/qemu-img.c
9ae3a8
+++ b/qemu-img.c
9ae3a8
@@ -101,6 +101,8 @@ static void help(void)
9ae3a8
            "  '-S' indicates the consecutive number of bytes that must contain only zeros\n"
9ae3a8
            "       for qemu-img to create a sparse image during conversion\n"
9ae3a8
            "  '--output' takes the format in which the output must be done (human or json)\n"
9ae3a8
+           "  '-n' skips the target volume creation (useful if the volume is created\n"
9ae3a8
+           "       prior to running qemu-img)\n"
9ae3a8
            "\n"
9ae3a8
            "Parameters to check subcommand:\n"
9ae3a8
            "  '-r' tries to repair any inconsistencies that are found during the check.\n"
9ae3a8
@@ -1113,7 +1115,8 @@ out3:
9ae3a8
 
9ae3a8
 static int img_convert(int argc, char **argv)
9ae3a8
 {
9ae3a8
-    int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
9ae3a8
+    int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size,
9ae3a8
+        cluster_sectors, skip_create;
9ae3a8
     int progress = 0, flags;
9ae3a8
     const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
9ae3a8
     BlockDriver *drv, *proto_drv;
9ae3a8
@@ -1136,8 +1139,9 @@ static int img_convert(int argc, char **argv)
9ae3a8
     cache = "unsafe";
9ae3a8
     out_baseimg = NULL;
9ae3a8
     compress = 0;
9ae3a8
+    skip_create = 0;
9ae3a8
     for(;;) {
9ae3a8
-        c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:q");
9ae3a8
+        c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qn");
9ae3a8
         if (c == -1) {
9ae3a8
             break;
9ae3a8
         }
9ae3a8
@@ -1194,6 +1198,9 @@ static int img_convert(int argc, char **argv)
9ae3a8
         case 'q':
9ae3a8
             quiet = true;
9ae3a8
             break;
9ae3a8
+        case 'n':
9ae3a8
+            skip_create = 1;
9ae3a8
+            break;
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
@@ -1326,20 +1333,22 @@ static int img_convert(int argc, char **argv)
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
-    /* Create the new image */
9ae3a8
-    ret = bdrv_create(drv, out_filename, param);
9ae3a8
-    if (ret < 0) {
9ae3a8
-        if (ret == -ENOTSUP) {
9ae3a8
-            error_report("Formatting not supported for file format '%s'",
9ae3a8
-                         out_fmt);
9ae3a8
-        } else if (ret == -EFBIG) {
9ae3a8
-            error_report("The image size is too large for file format '%s'",
9ae3a8
-                         out_fmt);
9ae3a8
-        } else {
9ae3a8
-            error_report("%s: error while converting %s: %s",
9ae3a8
-                         out_filename, out_fmt, strerror(-ret));
9ae3a8
+    if (!skip_create) {
9ae3a8
+        /* Create the new image */
9ae3a8
+        ret = bdrv_create(drv, out_filename, param);
9ae3a8
+        if (ret < 0) {
9ae3a8
+            if (ret == -ENOTSUP) {
9ae3a8
+                error_report("Formatting not supported for file format '%s'",
9ae3a8
+                             out_fmt);
9ae3a8
+            } else if (ret == -EFBIG) {
9ae3a8
+                error_report("The image size is too large for file format '%s'",
9ae3a8
+                             out_fmt);
9ae3a8
+            } else {
9ae3a8
+                error_report("%s: error while converting %s: %s",
9ae3a8
+                             out_filename, out_fmt, strerror(-ret));
9ae3a8
+            }
9ae3a8
+            goto out;
9ae3a8
         }
9ae3a8
-        goto out;
9ae3a8
     }
9ae3a8
 
9ae3a8
     flags = BDRV_O_RDWR;
9ae3a8
@@ -1360,6 +1369,20 @@ static int img_convert(int argc, char **argv)
9ae3a8
     bdrv_get_geometry(bs[0], &bs_sectors);
9ae3a8
     buf = qemu_blockalign(out_bs, IO_BUF_SIZE);
9ae3a8
 
9ae3a8
+    if (skip_create) {
9ae3a8
+        int64_t output_length = bdrv_getlength(out_bs);
9ae3a8
+        if (output_length < 0) {
9ae3a8
+            error_report("unable to get output image length: %s\n",
9ae3a8
+                         strerror(-output_length));
9ae3a8
+            ret = -1;
9ae3a8
+            goto out;
9ae3a8
+        } else if (output_length < total_sectors << BDRV_SECTOR_BITS) {
9ae3a8
+            error_report("output file is smaller than input file");
9ae3a8
+            ret = -1;
9ae3a8
+            goto out;
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
+
9ae3a8
     if (compress) {
9ae3a8
         ret = bdrv_get_info(out_bs, &bdi;;
9ae3a8
         if (ret < 0) {
9ae3a8
diff --git a/qemu-img.texi b/qemu-img.texi
9ae3a8
index 8364fa1..43ee4eb 100644
9ae3a8
--- a/qemu-img.texi
9ae3a8
+++ b/qemu-img.texi
9ae3a8
@@ -96,6 +96,14 @@ Second image format
9ae3a8
 Strict mode - fail on on different image size or sector allocation
9ae3a8
 @end table
9ae3a8
 
9ae3a8
+Parameters to convert subcommand:
9ae3a8
+
9ae3a8
+@table @option
9ae3a8
+
9ae3a8
+@item -n
9ae3a8
+Skip the creation of the target volume
9ae3a8
+@end table
9ae3a8
+
9ae3a8
 Command description:
9ae3a8
 
9ae3a8
 @table @option
9ae3a8
@@ -171,7 +179,7 @@ Error on reading data
9ae3a8
 
9ae3a8
 @end table
9ae3a8
 
9ae3a8
-@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}
9ae3a8
+@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}
9ae3a8
 
9ae3a8
 Convert the disk image @var{filename} or a snapshot @var{snapshot_name} to disk image @var{output_filename}
9ae3a8
 using format @var{output_fmt}. It can be optionally compressed (@code{-c}
9ae3a8
@@ -190,6 +198,11 @@ created as a copy on write image of the specified base image; the
9ae3a8
 @var{backing_file} should have the same content as the input's base image,
9ae3a8
 however the path, image format, etc may differ.
9ae3a8
 
9ae3a8
+If the @code{-n} option is specified, the target volume creation will be
9ae3a8
+skipped. This is useful for formats such as @code{rbd} if the target
9ae3a8
+volume has already been created with site specific options that cannot
9ae3a8
+be supplied through qemu-img.
9ae3a8
+
9ae3a8
 @item info [-f @var{fmt}] [--output=@var{ofmt}] [--backing-chain] @var{filename}
9ae3a8
 
9ae3a8
 Give information about the disk image @var{filename}. Use it in
9ae3a8
diff --git a/tests/qemu-iotests/063 b/tests/qemu-iotests/063
9ae3a8
new file mode 100644
9ae3a8
index 0000000..de0cbbd
9ae3a8
--- /dev/null
9ae3a8
+++ b/tests/qemu-iotests/063
9ae3a8
@@ -0,0 +1,97 @@
9ae3a8
+#!/bin/bash
9ae3a8
+#
9ae3a8
+# test of qemu-img convert -n - convert without creation
9ae3a8
+#
9ae3a8
+# Copyright (C) 2009 Red Hat, Inc.
9ae3a8
+# Copyright (C) 2013 Alex Bligh (alex@alex.org.uk)
9ae3a8
+#
9ae3a8
+# This program is free software; you can redistribute it and/or modify
9ae3a8
+# it under the terms of the GNU General Public License as published by
9ae3a8
+# the Free Software Foundation; either version 2 of the License, or
9ae3a8
+# (at your option) any later version.
9ae3a8
+#
9ae3a8
+# This program is distributed in the hope that it will be useful,
9ae3a8
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
9ae3a8
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9ae3a8
+# GNU General Public License for more details.
9ae3a8
+#
9ae3a8
+# You should have received a copy of the GNU General Public License
9ae3a8
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
9ae3a8
+#
9ae3a8
+
9ae3a8
+# creator
9ae3a8
+owner=alex@alex.org.uk
9ae3a8
+
9ae3a8
+seq=`basename $0`
9ae3a8
+echo "QA output created by $seq"
9ae3a8
+
9ae3a8
+here=`pwd`
9ae3a8
+tmp=/tmp/$$
9ae3a8
+status=1	# failure is the default!
9ae3a8
+
9ae3a8
+_cleanup()
9ae3a8
+{
9ae3a8
+	_cleanup_test_img
9ae3a8
+	rm -f $TEST_IMG.orig $TEST_IMG.raw $TEST_IMG.raw2
9ae3a8
+}
9ae3a8
+trap "_cleanup; exit \$status" 0 1 2 3 15
9ae3a8
+
9ae3a8
+# get standard environment, filters and checks
9ae3a8
+. ./common.rc
9ae3a8
+. ./common.filter
9ae3a8
+. ./common.pattern
9ae3a8
+
9ae3a8
+_supported_fmt qcow qcow2 vmdk qed raw
9ae3a8
+_supported_proto generic
9ae3a8
+_supported_os Linux
9ae3a8
+
9ae3a8
+_make_test_img 4M
9ae3a8
+
9ae3a8
+echo "== Testing conversion with -n fails with no target file =="
9ae3a8
+# check .orig file does not exist
9ae3a8
+rm -f $TEST_IMG.orig
9ae3a8
+if $QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n $TEST_IMG $TEST_IMG.orig >/dev/null 2>&1; then
9ae3a8
+    exit 1
9ae3a8
+fi
9ae3a8
+
9ae3a8
+echo "== Testing conversion with -n succeeds with a target file =="
9ae3a8
+rm -f $TEST_IMG.orig
9ae3a8
+cp $TEST_IMG $TEST_IMG.orig
9ae3a8
+if ! $QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n $TEST_IMG $TEST_IMG.orig ; then
9ae3a8
+    exit 1
9ae3a8
+fi
9ae3a8
+
9ae3a8
+echo "== Testing conversion to raw is the same after conversion with -n =="
9ae3a8
+# compare the raw files
9ae3a8
+if ! $QEMU_IMG convert -f $IMGFMT -O raw $TEST_IMG $TEST_IMG.raw1 ; then
9ae3a8
+    exit 1
9ae3a8
+fi
9ae3a8
+
9ae3a8
+if ! $QEMU_IMG convert -f $IMGFMT -O raw $TEST_IMG.orig $TEST_IMG.raw2 ; then
9ae3a8
+    exit 1
9ae3a8
+fi
9ae3a8
+
9ae3a8
+if ! cmp $TEST_IMG.raw1 $TEST_IMG.raw2 ; then
9ae3a8
+    exit 1
9ae3a8
+fi
9ae3a8
+
9ae3a8
+echo "== Testing conversion back to original format =="
9ae3a8
+if ! $QEMU_IMG convert -f raw -O $IMGFMT -n $TEST_IMG.raw2 $TEST_IMG ; then
9ae3a8
+    exit 1
9ae3a8
+fi
9ae3a8
+_check_test_img
9ae3a8
+
9ae3a8
+echo "== Testing conversion to a smaller file fails =="
9ae3a8
+rm -f $TEST_IMG.orig
9ae3a8
+mv $TEST_IMG $TEST_IMG.orig
9ae3a8
+_make_test_img 2M
9ae3a8
+if $QEMU_IMG convert -f $IMGFMT -O $IMGFMT -n $TEST_IMG.orig $TEST_IMG >/dev/null 2>&1; then
9ae3a8
+    exit 1
9ae3a8
+fi
9ae3a8
+
9ae3a8
+rm -f $TEST_IMG.orig $TEST_IMG.raw $TEST_IMG.raw2
9ae3a8
+
9ae3a8
+echo "*** done"
9ae3a8
+rm -f $seq.full
9ae3a8
+status=0
9ae3a8
+exit 0
9ae3a8
diff --git a/tests/qemu-iotests/063.out b/tests/qemu-iotests/063.out
9ae3a8
new file mode 100644
9ae3a8
index 0000000..de1c99a
9ae3a8
--- /dev/null
9ae3a8
+++ b/tests/qemu-iotests/063.out
9ae3a8
@@ -0,0 +1,10 @@
9ae3a8
+QA output created by 063
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=4194304
9ae3a8
+== Testing conversion with -n fails with no target file ==
9ae3a8
+== Testing conversion with -n succeeds with a target file ==
9ae3a8
+== Testing conversion to raw is the same after conversion with -n ==
9ae3a8
+== Testing conversion back to original format ==
9ae3a8
+No errors were found on the image.
9ae3a8
+== Testing conversion to a smaller file fails ==
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=2097152
9ae3a8
+*** done
9ae3a8
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
9ae3a8
index 9c8826d..ff3c505 100644
9ae3a8
--- a/tests/qemu-iotests/group
9ae3a8
+++ b/tests/qemu-iotests/group
9ae3a8
@@ -62,4 +62,5 @@
9ae3a8
 053 rw auto
9ae3a8
 059 rw auto
9ae3a8
 060 rw auto
9ae3a8
+063 rw auto
9ae3a8
 068 rw auto
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8