From nobody Sat May 4 08:14:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1512168647162750.0812209372884; Fri, 1 Dec 2017 14:50:47 -0800 (PST) Received: from localhost ([::1]:33387 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eKu8n-0004MH-Fp for importer@patchew.org; Fri, 01 Dec 2017 17:50:41 -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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now, if you type something like qemu-img create disk.qcow2 1G or qemu-img dd if=3D/dev/sda of=3Ddisk.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; } =20 +static const char *get_format(const char *filename) +{ + const char *fmt =3D strrchr(filename, '.'); + if (fmt =3D=3D NULL || bdrv_find_format(++fmt) =3D=3D NULL) { + fmt =3D "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 =3D -1; - const char *fmt =3D "raw"; + const char *fmt =3D NULL; const char *base_fmt =3D NULL; const char *filename; const char *base_filename =3D NULL; @@ -496,6 +507,9 @@ static int img_create(int argc, char **argv) =20 /* Get the filename */ filename =3D (optind < argc) ? argv[optind] : NULL; + if (fmt =3D=3D NULL) { + fmt =3D 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 =3D NULL; bool image_opts =3D false; int c, i; - const char *out_fmt =3D "raw"; + const char *out_fmt =3D NULL; const char *fmt =3D NULL; int64_t size =3D 0; int64_t block_count =3D 0, out_pos, in_pos; @@ -4308,6 +4322,9 @@ static int img_dd(int argc, char **argv) goto out; } =20 + if (out_fmt =3D=3D NULL) { + out_fmt =3D get_format(out.filename); + } drv =3D bdrv_find_format(out_fmt); if (!drv) { error_report("Unknown file format"); --=20 2.13.6