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