luks: add qemu-img measure support
Subsystems (2)
v4 cover luks: add qemu-img measure support
v4:
* This revision is what German speakers call "das Tüpfelchen auf dem I". "The icing on the cake" is the English equivalent. Since I like cake and don't want it to be half-baked, and because I like my metaphors shaken, not stirred, I went ahead with the extra revision so I could write this message.
* Use g_autoptr(QCryptoBlock) to make the code more concise [Max]
* Use local_err consistently [Max]
* Folded in Max's Reviewed-by tags
v3:
* Move payload offset calculation function to crypto/block.c [Max]
* Zero/unallocated blocks always require disk space on encrypted files [Max]
* Update qemu-iotests 178 output when changing qemu-img measure command-line options
v2:
* Fix uint64_t <-> size_t type mismatch in block_crypto_measure() so that 32-bit builds pass
This patch series adds qemu-img measure support to the "luks" block driver. We just need to take into account the LUKS header when sizing the image.
Stefan Hajnoczi (4):
luks: extract qcrypto_block_calculate_payload_offset()
luks: implement .bdrv_measure()
qemu-img: allow qemu-img measure --object without a filename
iotests: add 282 luks qemu-img measure test
block/crypto.c | 62 +++++++++++++++++++++ block/qcow2.c | 74 +++++++------------------ crypto/block.c | 36 +++++++++++++ include/crypto/block.h | 22 ++++++++ qemu-img.c | 6 +-- tests/qemu-iotests/178 | 2 +- tests/qemu-iotests/178.out.qcow2 | 8 +-- tests/qemu-iotests/178.out.raw | 8 +-- tests/qemu-iotests/282 | 93 ++++++++++++++++++++++++++++++++ tests/qemu-iotests/282.out | 30 +++++++++++ tests/qemu-iotests/group | 1 + 11 files changed, 274 insertions(+), 68 deletions(-) create mode 100755 tests/qemu-iotests/282 create mode 100644 tests/qemu-iotests/282.out
Signature · 1 line
2.24.1
PATCH v4 1/4 luks: extract qcrypto_block_calculate_payload_offset()
The qcow2 .bdrv_measure() code calculates the crypto payload offset. This logic really belongs in crypto/block.c where it can be reused by other image formats.
The "luks" block driver will need this same logic in order to implement .bdrv_measure(), so extract the qcrypto_block_calculate_payload_offset() function now.
--- block/qcow2.c | 74 +++++++++++------------------------------- crypto/block.c | 36 ++++++++++++++++++++ include/crypto/block.h | 22 +++++++++++++ 3 files changed, 77 insertions(+), 55 deletions(-)
block/qcow2.c +19 -55
@@ -4601,60 +4601,6 @@ static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)return ret; } -static ssize_t qcow2_measure_crypto_hdr_init_func (QCryptoBlock *block, -size_t headerlen,void *opaque,Error **errp) -{ -size_t *headerlenp = opaque; - -/* Stash away the payload size */ - *headerlenp = 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 =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 =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 =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 *in_bs,Error **errp) {
@@ -4705,9 +4651,27 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,g_free (optstr);if (has_luks) { +g_autoptr (QCryptoBlockCreateOptions) create_opts =NULL ; +QDict *opts_qdict; +QDict *cryptoopts;size_t headerlen; -if (!qcow2_measure_luks_headerlen (opts, &headerlen, &local_err)) { + opts_qdict =qemu_opts_to_qdict (opts,NULL ); +qdict_extract_subqdict (opts_qdict, &cryptoopts,"encrypt." ); +qobject_unref (opts_qdict); + +qdict_put_str (cryptoopts,"format" ,"luks" ); + + create_opts =block_crypto_create_opts_init (cryptoopts, errp); +qobject_unref (cryptoopts); +if (!create_opts) { + goto err; + } + +if (!qcrypto_block_calculate_payload_offset (create_opts, +"encrypt." , + &headerlen, + &local_err)) { goto err; }
crypto/block.c +36 -0
@@ -115,6 +115,42 @@ QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOptions *options, } +static ssize_t qcrypto_block_headerlen_hdr_init_func (QCryptoBlock *block, +size_t headerlen,void *opaque,Error **errp) +{ +size_t *headerlenp = opaque; + +/* Stash away the payload size */ + *headerlenp = headerlen; +return 0 ; +} + + +static ssize_t qcrypto_block_headerlen_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; +} + + +bool +qcrypto_block_calculate_payload_offset (QCryptoBlockCreateOptions *create_opts, +const char *optprefix, +size_t *len, +Error **errp) +{ +/* Fake LUKS creation in order to determine the payload size */ +g_autoptr (QCryptoBlock) crypto = +qcrypto_block_create (create_opts, optprefix, + qcrypto_block_headerlen_hdr_init_func, + qcrypto_block_headerlen_hdr_write_func, + len, errp); +return crypto !=NULL ; +} + +QCryptoBlockInfo *qcrypto_block_get_info (QCryptoBlock *block,Error **errp) {
include/crypto/block.h +22 -0
@@ -145,6 +145,26 @@ QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOptions *options, Error **errp); +/** +* qcrypto_block_calculate_payload_offset: +* @create_opts: the encryption options +* @optprefix: name prefix for options +* @len: output for number of header bytes before payload +* @errp: pointer to a NULL-initialized error object +* +* Calculate the number of header bytes before the payload in an encrypted +* storage volume. The header is an area before the payload that is reserved +* for encryption metadata. +* +* Returns: true on success, false on error +*/ +bool +qcrypto_block_calculate_payload_offset (QCryptoBlockCreateOptions *create_opts, +const char *optprefix, +size_t *len, +Error **errp); + +/** * qcrypto_block_get_info: * @block: the block encryption object
@@ -269,5 +289,7 @@ uint64_t qcrypto_block_get_sector_size(QCryptoBlock *block);void qcrypto_block_free (QCryptoBlock *block); G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoBlock, qcrypto_block_free) +G_DEFINE_AUTOPTR_CLEANUP_FUNC (QCryptoBlockCreateOptions, + qapi_free_QCryptoBlockCreateOptions)#endif /* QCRYPTO_BLOCK_H */
Signature · 1 line
2.24.1
PATCH v4 2/4 luks: implement .bdrv_measure()
In reply to Stefan HajnocziAdd qemu-img measure support in the "luks" block driver.
--- block/crypto.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
block/crypto.c +62 -0
@@ -484,6 +484,67 @@ static int64_t block_crypto_getlength(BlockDriverState *bs) } +static BlockMeasureInfo *block_crypto_measure (QemuOpts *opts, +BlockDriverState *in_bs, +Error **errp) +{ +g_autoptr (QCryptoBlockCreateOptions) create_opts =NULL ; +Error *local_err =NULL ; +BlockMeasureInfo *info; +uint64_t size; +size_t luks_payload_size; +QDict *cryptoopts; + +/* +* Preallocation mode doesn't affect size requirements but we must consume +* the option. +*/ +g_free (qemu_opt_get_del (opts,BLOCK_OPT_PREALLOC )); + + size =qemu_opt_get_size_del (opts,BLOCK_OPT_SIZE ,0 ); + +if (in_bs) { +int64_t ssize =bdrv_getlength (in_bs); + +if (ssize <0 ) { +error_setg_errno (&local_err, -ssize, +"Unable to get image virtual_size" ); + goto err; + } + + size = ssize; + } + + cryptoopts =qemu_opts_to_qdict_filtered (opts,NULL , + &block_crypto_create_opts_luks, true); +qdict_put_str (cryptoopts,"format" ,"luks" ); + create_opts =block_crypto_create_opts_init (cryptoopts, &local_err); +qobject_unref (cryptoopts); +if (!create_opts) { + goto err; + } + +if (!qcrypto_block_calculate_payload_offset (create_opts,NULL , + &luks_payload_size, + &local_err)) { + goto err; + } + +/* +* Unallocated blocks are still encrypted so allocation status makes no +* difference to the file size. +*/ + info =g_new (BlockMeasureInfo,1 ); + info->fully_allocated = luks_payload_size + size; + info->required = luks_payload_size + size; +return info; + +err: +error_propagate (errp, local_err); +return NULL ; +} + +static int block_crypto_probe_luks (const uint8_t *buf,int buf_size,const char *filename) {
@@ -670,6 +731,7 @@ static BlockDriver bdrv_crypto_luks = { .bdrv_co_preadv = block_crypto_co_preadv, .bdrv_co_pwritev = block_crypto_co_pwritev, .bdrv_getlength = block_crypto_getlength, + .bdrv_measure = block_crypto_measure, .bdrv_get_info = block_crypto_get_info_luks, .bdrv_get_specific_info = block_crypto_get_specific_info_luks,
Signature · 1 line
2.24.1
PATCH v4 3/4 qemu-img: allow qemu-img measure --object without a filename
In reply to Stefan HajnocziIn most qemu-img sub-commands the --object option only makes sense when there is a filename. qemu-img measure is an exception because objects may be referenced from the image creation options instead of an existing image file. Allow --object without a filename.
--- qemu-img.c | 6 ++---- tests/qemu-iotests/178 | 2 +- tests/qemu-iotests/178.out.qcow2 | 8 ++++---- tests/qemu-iotests/178.out.raw | 8 ++++---- 4 files changed, 11 insertions(+), 13 deletions(-)
qemu-img.c +2 -4
@@ -4912,10 +4912,8 @@ static int img_measure(int argc, char **argv) filename = argv[optind]; } -if (!filename && - (object_opts || image_opts || fmt || snapshot_name || sn_opts)) { -error_report ("--object, --image-opts, -f, and -l " -"require a filename argument." ); +if (!filename && (image_opts || fmt || snapshot_name || sn_opts)) { +error_report ("--image-opts, -f, and -l require a filename argument." ); goto out; }if (filename && img_size !=UINT64_MAX ) {
tests/qemu-iotests/178 +1 -1
@@ -50,7 +50,7 @@ _make_test_img 1G $QEMU_IMG measure # missing arguments $QEMU_IMG measure --size 2G "$TEST_IMG" # only one allowed $QEMU_IMG measure "$TEST_IMG" a # only one filename allowed -$QEMU_IMG measure --object secret,id=sec0,data=MTIzNDU2,format=base64 # missing filename +$QEMU_IMG measure --object secret,id=sec0,data=MTIzNDU2,format=base64 # size or filename needed $QEMU_IMG measure --image-opts # missing filename $QEMU_IMG measure -f qcow2 # missing filename $QEMU_IMG measure -l snap1 # missing filename
tests/qemu-iotests/178.out.qcow2 +4 -4
@@ -5,10 +5,10 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824 qemu-img: Either --size N or one filename must be specified. qemu-img: --size N cannot be used together with a filename. qemu-img: At most one filename argument is allowed. -qemu-img: --object, --image-opts, -f, and -l require a filename argument. -qemu-img: --object, --image-opts, -f, and -l require a filename argument. -qemu-img: --object, --image-opts, -f, and -l require a filename argument. -qemu-img: --object, --image-opts, -f, and -l require a filename argument. +qemu-img: Either --size N or one filename must be specified. +qemu-img: --image-opts, -f, and -l require a filename argument. +qemu-img: --image-opts, -f, and -l require a filename argument. +qemu-img: --image-opts, -f, and -l require a filename argument. qemu-img: Invalid option list: , qemu-img: Invalid parameter 'snapshot.foo' qemu-img: Failed in parsing snapshot param 'snapshot.foo'
tests/qemu-iotests/178.out.raw +4 -4
@@ -5,10 +5,10 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824 qemu-img: Either --size N or one filename must be specified. qemu-img: --size N cannot be used together with a filename. qemu-img: At most one filename argument is allowed. -qemu-img: --object, --image-opts, -f, and -l require a filename argument. -qemu-img: --object, --image-opts, -f, and -l require a filename argument. -qemu-img: --object, --image-opts, -f, and -l require a filename argument. -qemu-img: --object, --image-opts, -f, and -l require a filename argument. +qemu-img: Either --size N or one filename must be specified. +qemu-img: --image-opts, -f, and -l require a filename argument. +qemu-img: --image-opts, -f, and -l require a filename argument. +qemu-img: --image-opts, -f, and -l require a filename argument. qemu-img: Invalid option list: , qemu-img: Invalid parameter 'snapshot.foo' qemu-img: Failed in parsing snapshot param 'snapshot.foo'
Signature · 1 line
2.24.1
PATCH v4 4/4 iotests: add 282 luks qemu-img measure test
In reply to Stefan HajnocziThis test exercises the block/crypto.c "luks" block driver .bdrv_measure() code.
--- tests/qemu-iotests/282 | 93 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/282.out | 30 ++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 124 insertions(+) create mode 100755 tests/qemu-iotests/282 create mode 100644 tests/qemu-iotests/282.out
tests/qemu-iotests/282 new file +93 -0
@@ -0,0 +1,93 @@ +#!/usr/bin/env bash +# +# qemu-img measure tests for LUKS images +# +# Copyright (C) 2020 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +# creator [email protected] + +seq=`basename $0` +echo "QA output created by $seq" + +status=1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img + rm -f "$TEST_IMG.converted" +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter +. ./common.pattern + +_supported_fmt luks +_supported_proto file +_supported_os Linux + +SECRET=secret,id=sec0,data=passphrase + +echo "== measure 1G image file ==" +echo + +$QEMU_IMG measure --object "$SECRET" \ + -O "$IMGFMT" \ + -o key-secret=sec0,iter-time=10 \ + --size 1G + +echo +echo "== create 1G image file (size should be no greater than measured) ==" +echo + +_make_test_img 1G +stat -c "image file size in bytes: %s" "$TEST_IMG_FILE" + +echo +echo "== modified 1G image file (size should be no greater than measured) ==" +echo + +$QEMU_IO --object "$SECRET" --image-opts "$TEST_IMG" -c "write -P 0x51 0x10000 0x400" | _filter_qemu_io | _filter_testdir +stat -c "image file size in bytes: %s" "$TEST_IMG_FILE" + +echo +echo "== measure preallocation=falloc 1G image file ==" +echo + +$QEMU_IMG measure --object "$SECRET" \ + -O "$IMGFMT" \ + -o key-secret=sec0,iter-time=10,preallocation=falloc \ + --size 1G + +echo +echo "== measure with input image file ==" +echo + +IMGFMT=raw IMGKEYSECRET= IMGOPTS= _make_test_img 1G | _filter_imgfmt +QEMU_IO_OPTIONS= IMGOPTSSYNTAX= $QEMU_IO -f raw -c "write -P 0x51 0x10000 0x400" "$TEST_IMG_FILE" | _filter_qemu_io | _filter_testdir +$QEMU_IMG measure --object "$SECRET" \ + -O "$IMGFMT" \ + -o key-secret=sec0,iter-time=10 \ + -f raw \ + "$TEST_IMG_FILE" + +# success, all done +echo "*** done" +rm -f $seq.full +status=0
tests/qemu-iotests/282.out new file +30 -0
@@ -0,0 +1,30 @@ +QA output created by 282 +== measure 1G image file == + +required size: 1075810304 +fully allocated size: 1075810304 + +== create 1G image file (size should be no greater than measured) == + +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824 +image file size in bytes: 1075810304 + +== modified 1G image file (size should be no greater than measured) == + +wrote 1024/1024 bytes at offset 65536 +1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +image file size in bytes: 1075810304 + +== measure preallocation=falloc 1G image file == + +required size: 1075810304 +fully allocated size: 1075810304 + +== measure with input image file == + +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1073741824 +wrote 1024/1024 bytes at offset 65536 +1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +required size: 1075810304 +fully allocated size: 1075810304 +*** done
tests/qemu-iotests/group +1 -0
@@ -289,4 +289,5 @@ 279 rw backing quick 280 rw migration quick 281 rw quick +282 quick 283 auto quick
Signature · 1 line
2.24.1
Max Reitz
AppliedQuoted 5 lines · Stefan Hajnoczi
v4:
* This revision is what German speakers call "das Tüpfelchen auf dem I". "The icing on the cake" is the English equivalent. Since I like cake and don't want it to be half-baked, and because I like my metaphors shaken, not stirred, I went ahead with the extra revision so I could write this message.
:)
Quoted 9 lines
* Use
g_autoptr(QCryptoBlock)to make the code more concise [Max]
* Uselocal_errconsistently [Max]
* Folded in Max's Reviewed-by tags
v3:
* Move payload offset calculation function tocrypto/block.c[Max]
* Zero/unallocated blocks always require disk space on encrypted files [Max]
* Update qemu-iotests 178 output when changing qemu-img measure command-line options
Quoted 6 lines
v2:
* Fixuint64_t<->size_ttype mismatch inblock_crypto_measure()so that 32-bit builds passThis patch series adds qemu-img measure support to the "luks" block driver. We just need to take into account the LUKS header when sizing the image.
Thanks, applied to my block branch:
https://git.xanclic.moe/XanClic/qemu/commits/branch/block
Max