From nobody Tue May 7 10:41:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.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 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550486868159593.0084386764152; Mon, 18 Feb 2019 02:47:48 -0800 (PST) Received: from localhost ([127.0.0.1]:56251 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gvgSd-0001l0-4w for importer@patchew.org; Mon, 18 Feb 2019 05:47:43 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46704) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gvgQq-0000nX-Hx for qemu-devel@nongnu.org; Mon, 18 Feb 2019 05:45:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gvgQp-0006Qu-Lh for qemu-devel@nongnu.org; Mon, 18 Feb 2019 05:45:52 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44664) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gvgQn-0006Ok-AP; Mon, 18 Feb 2019 05:45:49 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7EFE4C04FFFC; Mon, 18 Feb 2019 10:45:48 +0000 (UTC) Received: from localhost (unknown [10.36.118.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id 257AE60F8A; Mon, 18 Feb 2019 10:45:34 +0000 (UTC) From: Stefan Hajnoczi To: Date: Mon, 18 Feb 2019 10:45:24 +0000 Message-Id: <20190218104525.23674-2-stefanha@redhat.com> In-Reply-To: <20190218104525.23674-1-stefanha@redhat.com> References: <20190218104525.23674-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 18 Feb 2019 10:45:48 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/2] qcow2: include LUKS payload overhead in qemu-img measure 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: Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" LUKS encryption reserves clusters for its own payload data. The size of this area must be included in the qemu-img measure calculation so that we arrive at the correct minimum required image size. (Ab)use the qcrypto_block_create() API to determine the payload overhead. We discard the payload data that qcrypto thinks will be written to the image. Signed-off-by: Stefan Hajnoczi Reviewed-by: Max Reitz --- block/qcow2.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/block/qcow2.c b/block/qcow2.c index 65a54c9ac6..81722177d6 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -4232,6 +4232,60 @@ static coroutine_fn int qcow2_co_flush_to_os(BlockDr= iverState *bs) return ret; } =20 +static ssize_t qcow2_measure_crypto_hdr_init_func(QCryptoBlock *block, + size_t headerlen, void *opaque, Error **errp) +{ + size_t *headerlenp =3D opaque; + + /* Stash away the payload size */ + *headerlenp =3D headerlen; + return 0; +} + +static ssize_t qcow2_measure_crypto_hdr_write_func(QCryptoBlock *block, + size_t offset, const uint8_t *buf, size_t buflen, + void *opaque, Error **errp) +{ + /* Discard the bytes, we're not actually writing to an image */ + return buflen; +} + +/* Determine the number of bytes for the LUKS payload */ +static bool qcow2_measure_luks_headerlen(QemuOpts *opts, size_t *len, + Error **errp) +{ + QDict *opts_qdict; + QDict *cryptoopts_qdict; + QCryptoBlockCreateOptions *cryptoopts; + QCryptoBlock *crypto; + + /* Extract "encrypt." options into a qdict */ + opts_qdict =3D qemu_opts_to_qdict(opts, NULL); + qdict_extract_subqdict(opts_qdict, &cryptoopts_qdict, "encrypt."); + qobject_unref(opts_qdict); + + /* Build QCryptoBlockCreateOptions object from qdict */ + qdict_put_str(cryptoopts_qdict, "format", "luks"); + cryptoopts =3D block_crypto_create_opts_init(cryptoopts_qdict, errp); + qobject_unref(cryptoopts_qdict); + if (!cryptoopts) { + return false; + } + + /* Fake LUKS creation in order to determine the payload size */ + crypto =3D qcrypto_block_create(cryptoopts, "encrypt.", + qcow2_measure_crypto_hdr_init_func, + qcow2_measure_crypto_hdr_write_func, + len, errp); + qapi_free_QCryptoBlockCreateOptions(cryptoopts); + if (!crypto) { + return false; + } + + qcrypto_block_free(crypto); + return true; +} + static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *i= n_bs, Error **errp) { @@ -4241,11 +4295,13 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *op= ts, BlockDriverState *in_bs, uint64_t virtual_size; /* disk size as seen by guest */ uint64_t refcount_bits; uint64_t l2_tables; + uint64_t luks_payload_size =3D 0; size_t cluster_size; int version; char *optstr; PreallocMode prealloc; bool has_backing_file; + bool has_luks; =20 /* Parse image creation options */ cluster_size =3D qcow2_opt_get_cluster_size_del(opts, &local_err); @@ -4275,6 +4331,20 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opt= s, BlockDriverState *in_bs, has_backing_file =3D !!optstr; g_free(optstr); =20 + optstr =3D qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT); + has_luks =3D optstr && strcmp(optstr, "luks") =3D=3D 0; + g_free(optstr); + + if (has_luks) { + size_t headerlen; + + if (!qcow2_measure_luks_headerlen(opts, &headerlen, &local_err)) { + goto err; + } + + luks_payload_size =3D ROUND_UP(headerlen, cluster_size); + } + virtual_size =3D qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); virtual_size =3D ROUND_UP(virtual_size, cluster_size); =20 @@ -4345,7 +4415,7 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts= , BlockDriverState *in_bs, info =3D g_new(BlockMeasureInfo, 1); info->fully_allocated =3D qcow2_calc_prealloc_size(virtual_size, cluster_size, - ctz32(refcount_bits)); + ctz32(refcount_bits)) + luks_payload_size; =20 /* Remove data clusters that are not required. This overestimates the * required size because metadata needed for the fully allocated file = is --=20 2.20.1 From nobody Tue May 7 10:41:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.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 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550486885245545.4561566562886; Mon, 18 Feb 2019 02:48:05 -0800 (PST) Received: from localhost ([127.0.0.1]:56253 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gvgSv-0001xm-6T for importer@patchew.org; Mon, 18 Feb 2019 05:48:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46750) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gvgRB-000133-P1 for qemu-devel@nongnu.org; Mon, 18 Feb 2019 05:46:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gvgR6-0006YE-07 for qemu-devel@nongnu.org; Mon, 18 Feb 2019 05:46:13 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41738) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gvgQz-0006VO-U7; Mon, 18 Feb 2019 05:46:02 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 370C940D94; Mon, 18 Feb 2019 10:46:01 +0000 (UTC) Received: from localhost (unknown [10.36.118.66]) by smtp.corp.redhat.com (Postfix) with ESMTP id DBAA85D96F; Mon, 18 Feb 2019 10:45:49 +0000 (UTC) From: Stefan Hajnoczi To: Date: Mon, 18 Feb 2019 10:45:25 +0000 Message-Id: <20190218104525.23674-3-stefanha@redhat.com> In-Reply-To: <20190218104525.23674-1-stefanha@redhat.com> References: <20190218104525.23674-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 18 Feb 2019 10:46:01 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 2/2] iotests: add LUKS payload overhead to 178 qemu-img measure test 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: Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The previous patch includes the LUKS payload overhead into the qemu-img measure calculation for qcow2. Update qemu-iotests 178 to exercise this new code path. Reviewed-by: Max Reitz Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Stefan Hajnoczi --- tests/qemu-iotests/178 | 8 ++++++++ tests/qemu-iotests/178.out.qcow2 | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/tests/qemu-iotests/178 b/tests/qemu-iotests/178 index 3f4b4a4564..927bf06e4d 100755 --- a/tests/qemu-iotests/178 +++ b/tests/qemu-iotests/178 @@ -142,6 +142,14 @@ for ofmt in human json; do # The backing file doesn't need to exist :) $QEMU_IMG measure --output=3D$ofmt -o backing_file=3Dx \ -f "$fmt" -O "$IMGFMT" "$TEST_IMG" + + echo + echo "=3D=3D $fmt input image and LUKS encryption =3D=3D" + echo + $QEMU_IMG measure --output=3D$ofmt \ + --object secret,id=3Dsec0,data=3Dbase \ + -o encrypt.format=3Dluks,encrypt.key-secret= =3Dsec0,encrypt.iter-time=3D10 \ + -f "$fmt" -O "$IMGFMT" "$TEST_IMG" fi =20 echo diff --git a/tests/qemu-iotests/178.out.qcow2 b/tests/qemu-iotests/178.out.= qcow2 index d42d4a4597..55a8dc926f 100644 --- a/tests/qemu-iotests/178.out.qcow2 +++ b/tests/qemu-iotests/178.out.qcow2 @@ -68,6 +68,11 @@ converted image file size in bytes: 458752 required size: 1074135040 fully allocated size: 1074135040 =20 +=3D=3D qcow2 input image and LUKS encryption =3D=3D + +required size: 2686976 +fully allocated size: 1076232192 + =3D=3D qcow2 input image and preallocation (human) =3D=3D =20 required size: 1074135040 @@ -114,6 +119,11 @@ converted image file size in bytes: 524288 required size: 1074135040 fully allocated size: 1074135040 =20 +=3D=3D raw input image and LUKS encryption =3D=3D + +required size: 2686976 +fully allocated size: 1076232192 + =3D=3D raw input image and preallocation (human) =3D=3D =20 required size: 1074135040 @@ -205,6 +215,13 @@ converted image file size in bytes: 458752 "fully-allocated": 1074135040 } =20 +=3D=3D qcow2 input image and LUKS encryption =3D=3D + +{ + "required": 2686976, + "fully-allocated": 1076232192 +} + =3D=3D qcow2 input image and preallocation (json) =3D=3D =20 { @@ -263,6 +280,13 @@ converted image file size in bytes: 524288 "fully-allocated": 1074135040 } =20 +=3D=3D raw input image and LUKS encryption =3D=3D + +{ + "required": 2686976, + "fully-allocated": 1076232192 +} + =3D=3D raw input image and preallocation (json) =3D=3D =20 { --=20 2.20.1