From patchwork Fri Dec 1 22:49:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Klim Kireev X-Patchwork-Id: 843775 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3ypTxR5Wt2z9sNV for ; Sat, 2 Dec 2017 09:50:03 +1100 (AEDT) Received: from localhost ([::1]:33381 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eKu89-0003lH-Ud for incoming@patchwork.ozlabs.org; Fri, 01 Dec 2017 17:50:01 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59193) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eKu7j-0003jd-0a for qemu-devel@nongnu.org; Fri, 01 Dec 2017 17:49:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eKu7i-0004nq-5h for qemu-devel@nongnu.org; Fri, 01 Dec 2017 17:49:35 -0500 Received: from [195.214.232.23] (port=15589 helo=US-EXCH2.sw.swsoft.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eKu7b-0004jQ-Sj; Fri, 01 Dec 2017 17:49:28 -0500 Received: from localhost.localdomain (93.175.28.16) by US-EXCH2.sw.swsoft.com (172.16.10.60) with Microsoft SMTP Server (TLS) id 15.0.1236.3; Fri, 1 Dec 2017 14:49:14 -0800 From: Klim Kireev To: , , , Date: Sat, 2 Dec 2017 01:49:09 +0300 Message-ID: <20171201224909.9373-1-klim.kireev@virtuozzo.com> X-Mailer: git-send-email 2.13.6 MIME-Version: 1.0 X-ClientProxiedBy: US-EXCH2.sw.swsoft.com (172.16.10.60) To US-EXCH2.sw.swsoft.com (172.16.10.60) X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 195.214.232.23 Subject: [Qemu-devel] [PATCH v2] qemu-img: add the simplest format recognition X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: den@openvz.org, vsementsov@virtuozzo.com, klim.kireev@virtuozzo.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 --- 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");