[v3] qemu-img: add the simplest format recognition

Message ID 20171201234943.14695-1-klim.kireev@virtuozzo.com
State New
Headers show
Series
  • [v3] qemu-img: add the simplest format recognition
Related show

Commit Message

Klim Kireev Dec. 1, 2017, 11:49 p.m.
Now, if you type something like

qemu-img create disk.qcow2 1G
or
qemu-img dd if=/dev/sda of=disk.qcow2

it creates a raw image and if you need you should
manually specify an image format with -f qcow2. It would
be more convenient if it could be assumed from an extension.

This patch adds a simple heuristic to recognize the image format
for qcow, qcow2, vmdk, vhdx, vdi

It warns users about guessed format and informs them about '-f' option.

Signed-off-by: Klim Kireev <klim.kireev@virtuozzo.com>
---
 qemu-img.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

Comments

Kevin Wolf Dec. 4, 2017, 12:06 p.m. | #1
Am 02.12.2017 um 00:49 hat Klim Kireev geschrieben:
> Now, if you type something like
> 
> qemu-img create disk.qcow2 1G
> or
> qemu-img dd if=/dev/sda of=disk.qcow2
> 
> it creates a raw image and if you need you should
> manually specify an image format with -f qcow2. It would
> be more convenient if it could be assumed from an extension.
> 
> This patch adds a simple heuristic to recognize the image format
> for qcow, qcow2, vmdk, vhdx, vdi
> 
> It warns users about guessed format and informs them about '-f' option.
> 
> Signed-off-by: Klim Kireev <klim.kireev@virtuozzo.com>

This is an incompatible change.

If we want to go there, we must introduce a deprecation warning now
without any other change in behaviour. We need to warn users for two
releases that the behaviour will change in the future, and only then we
can switch over (i.e. in qemu 2.14 the earliest if we introduce the
warning in 2.12).

I'm not sure if we even want to automatically guess the format from the
filename, or if a warning/error would be enough even in the long term.

The behaviour I have in mind is like this:

* qemu-img create x.raw 4G            => works, possibly warning
* qemu-img create -f raw x.raw 4G     => works
* qemu-img create x.qcow2 4G          => error, need -f for non-raw
* qemu-img create -f qcow2 x.qcow2 4G => works
* qemu-img create -f raw x.qcow2 4G   => works, possibly warning

Kevin

Patch

diff --git a/qemu-img.c b/qemu-img.c
index 02a6e27beb..4ec04f5c86 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -421,11 +421,21 @@  static int64_t cvtnum(const char *s)
     return value;
 }
 
+static const char *get_format(const char *filename)
+{
+    const char *fmt = strrchr(filename, '.');
+    if (fmt == NULL || bdrv_find_format(++fmt) == NULL) {
+        fmt = "raw";
+    }
+    warn_report("No format specified with -f, assuming %s.", fmt);
+    return fmt;
+}
+
 static int img_create(int argc, char **argv)
 {
     int c;
     uint64_t img_size = -1;
-    const char *fmt = "raw";
+    const char *fmt = NULL;
     const char *base_fmt = NULL;
     const char *filename;
     const char *base_filename = NULL;
@@ -496,6 +506,9 @@  static int img_create(int argc, char **argv)
 
     /* Get the filename */
     filename = (optind < argc) ? argv[optind] : NULL;
+    if (fmt == NULL) {
+        fmt = get_format(filename);
+    }
     if (options && has_help_option(options)) {
         g_free(options);
         return print_block_option_help(filename, fmt);
@@ -4181,7 +4194,7 @@  static int img_dd(int argc, char **argv)
     Error *local_err = NULL;
     bool image_opts = false;
     int c, i;
-    const char *out_fmt = "raw";
+    const char *out_fmt = NULL;
     const char *fmt = NULL;
     int64_t size = 0;
     int64_t block_count = 0, out_pos, in_pos;
@@ -4308,6 +4321,9 @@  static int img_dd(int argc, char **argv)
         goto out;
     }
 
+    if (out_fmt == NULL) {
+        out_fmt = get_format(out.filename);
+    }
     drv = bdrv_find_format(out_fmt);
     if (!drv) {
         error_report("Unknown file format");