From nobody Fri May 3 13:07:26 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) 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 1512172283092275.2831454487467; Fri, 1 Dec 2017 15:51:23 -0800 (PST) Received: from localhost ([::1]:33563 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eKv5Q-0004i5-PH for importer@patchew.org; Fri, 01 Dec 2017 18:51:16 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55946) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eKv4N-0004IV-F7 for qemu-devel@nongnu.org; Fri, 01 Dec 2017 18:50:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eKv4M-0008NX-MV for qemu-devel@nongnu.org; Fri, 01 Dec 2017 18:50:11 -0500 Received: from [195.214.232.23] (port=21454 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 1eKv4H-0008DE-4h; Fri, 01 Dec 2017 18:50:05 -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 15:49:48 -0800 From: Klim Kireev To: , , , Date: Sat, 2 Dec 2017 02:49:43 +0300 Message-ID: <20171201234943.14695-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 v3] 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_6 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 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 --- qemu-img.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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; } =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"; + } + 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 =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 +506,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 +4194,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 +4321,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