qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] qemu-img: add the simplest format recognition
@ 2017-12-01 22:49 Klim Kireev
  2017-12-01 23:28 ` Eric Blake
  0 siblings, 1 reply; 2+ messages in thread
From: Klim Kireev @ 2017-12-01 22:49 UTC (permalink / raw)
  To: kwolf, mreitz, qemu-block, qemu-devel; +Cc: den, vsementsov, klim.kireev

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

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

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

diff --git a/qemu-img.c b/qemu-img.c
index 02a6e27beb..ac1adf1582 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -421,11 +421,22 @@ 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";
+    }
+    printf("!!! %s format was detected.\n"
+           "!!! If you meant another format, specify it with -f.\n", 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 +507,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 +4195,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 +4322,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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH v2] qemu-img: add the simplest format recognition
  2017-12-01 22:49 [Qemu-devel] [PATCH v2] qemu-img: add the simplest format recognition Klim Kireev
@ 2017-12-01 23:28 ` Eric Blake
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Blake @ 2017-12-01 23:28 UTC (permalink / raw)
  To: Klim Kireev, kwolf, mreitz, qemu-block, qemu-devel; +Cc: den, vsementsov

[-- Attachment #1: Type: text/plain, Size: 1877 bytes --]

On 12/01/2017 04:49 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
> 
> It warns users about guessed format and informs them about '-f' option.
> 
> Signed-off-by: Klim Kireev <klim.kireev@virtuozzo.com>
> ---
>  qemu-img.c | 21 +++++++++++++++++++--
>  1 file changed, 19 insertions(+), 2 deletions(-)
> 
> diff --git a/qemu-img.c b/qemu-img.c
> index 02a6e27beb..ac1adf1582 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -421,11 +421,22 @@ 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";
> +    }
> +    printf("!!! %s format was detected.\n"

Assumed, not detected.  (We didn't read the file, because we're creating
it; we're assuming a format based on the file name, not its contents,
and detection tends to imply a contents-based check).

> +           "!!! If you meant another format, specify it with -f.\n", fmt);

Not our usual error reporting style; the !!! is yelling at the user, and
we tend to avoid trailing '.'.  printf() goes to stdout, but the message
should go to stderr(); you really want to use warn_report().

I suggest:

warn_report("No format specified with -f, assuming %s.", fmt);

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


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-12-01 23:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-01 22:49 [Qemu-devel] [PATCH v2] qemu-img: add the simplest format recognition Klim Kireev
2017-12-01 23:28 ` Eric Blake

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).