[Qemu-devel] [PATCH] qemu-img: add the simplest format recognition

Klim Kireev posted 1 patch 6 years, 4 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20171201200515.9938-1-klim.kireev@virtuozzo.com
Test checkpatch passed
Test docker passed
There is a newer version of this series
qemu-img.c | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
[Qemu-devel] [PATCH] qemu-img: add the simplest format recognition
Posted by Klim Kireev 6 years, 4 months ago
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 detected from an extension.

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

Signed-off-by: Klim Kireev <klim.kireev@virtuozzo.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 qemu-img.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index 02a6e27beb..0a659d2257 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -421,11 +421,25 @@ 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) {
+        return "raw";
+    }
+    fmt++;
+    if (bdrv_find_format(fmt) != NULL) {
+        return fmt;
+    } else {
+        return "raw";
+    }
+}
+
 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 +510,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 +4198,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 +4325,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");
-- 
2.13.6


Re: [Qemu-devel] [PATCH] qemu-img: add the simplest format recognition
Posted by Eric Blake 6 years, 4 months ago
On 12/01/2017 02:05 PM, Klim Kireev wrote:
> 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 detected from an extension.
> 
> This patch adds a simple heuristic to recognize the image format
> for qcow, qcow2, vmdk, vhdx, vdi
> 
> Signed-off-by: Klim Kireev <klim.kireev@virtuozzo.com>
> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---
>   qemu-img.c | 24 ++++++++++++++++++++++--
>   1 file changed, 22 insertions(+), 2 deletions(-)

Is it also worth warning the user that we guessed, and that they should 
specify -f if our guess was wrong?

> @@ -496,6 +510,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);

Particularly if fmt == "raw", because the user typed something like 
'foo.img' instead of 'foo.qcow2'. I suspect another common mis-guess 
would be users that type .qcow but want qcow2.

The idea makes sense to me, though.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org