If the "id" field is missing from the options given to blockdev-add, just omit the BlockBackend and create the BlockDriverState tree alone.
Signed-off-by: Max Reitz <mre...@redhat.com> --- blockdev.c | 38 +++++++++++++++++++++++++------------- tests/qemu-iotests/087 | 20 -------------------- tests/qemu-iotests/087.out | 12 ------------ 3 files changed, 25 insertions(+), 45 deletions(-) diff --git a/blockdev.c b/blockdev.c index 7573746..81fa7bd 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2844,17 +2844,12 @@ out: void qmp_blockdev_add(BlockdevOptions *options, Error **errp) { QmpOutputVisitor *ov = qmp_output_visitor_new(); - BlockBackend *blk; + BlockDriverState *bs; + BlockBackend *blk = NULL; QObject *obj; QDict *qdict; Error *local_err = NULL; - /* Require an ID in the top level */ - if (!options->has_id) { - error_setg(errp, "Block device needs an ID"); - goto fail; - } - /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with * cache.direct=false instead of silently switching to aio=threads, except * when called from drive_new(). @@ -2882,14 +2877,31 @@ void qmp_blockdev_add(BlockdevOptions *options, Error **errp) qdict_flatten(qdict); - blk = blockdev_init(NULL, qdict, &local_err); - if (local_err) { - error_propagate(errp, local_err); - goto fail; + if (options->has_id) { + blk = blockdev_init(NULL, qdict, &local_err); + if (local_err) { + error_propagate(errp, local_err); + goto fail; + } + + bs = blk_bs(blk); + } else { + int ret; + + bs = NULL; + ret = bdrv_open(&bs, NULL, NULL, qdict, BDRV_O_RDWR | BDRV_O_CACHE_WB, + NULL, errp); + if (ret < 0) { + goto fail; + } } - if (bdrv_key_required(blk_bs(blk))) { - blk_unref(blk); + if (bs && bdrv_key_required(bs)) { + if (blk) { + blk_unref(blk); + } else { + bdrv_unref(bs); + } error_setg(errp, "blockdev-add doesn't support encrypted devices"); goto fail; } diff --git a/tests/qemu-iotests/087 b/tests/qemu-iotests/087 index 8694749..4b2eddc 100755 --- a/tests/qemu-iotests/087 +++ b/tests/qemu-iotests/087 @@ -54,26 +54,6 @@ size=128M _make_test_img $size echo -echo === Missing ID === -echo - -run_qemu <<EOF -{ "execute": "qmp_capabilities" } -{ "execute": "blockdev-add", - "arguments": { - "options": { - "driver": "$IMGFMT", - "file": { - "driver": "file", - "filename": "$TEST_IMG" - } - } - } - } -{ "execute": "quit" } -EOF - -echo echo === Duplicate ID === echo diff --git a/tests/qemu-iotests/087.out b/tests/qemu-iotests/087.out index 0ba2e43..681ef93 100644 --- a/tests/qemu-iotests/087.out +++ b/tests/qemu-iotests/087.out @@ -1,18 +1,6 @@ QA output created by 087 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 -=== Missing ID === - -Testing: -QMP_VERSION -{"return": {}} -{"error": {"class": "GenericError", "desc": "Block device needs an ID"}} -{"return": {}} -{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "SHUTDOWN"} -{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "DEVICE_TRAY_MOVED", "data": {"device": "ide1-cd0", "tray-open": true}} -{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "DEVICE_TRAY_MOVED", "data": {"device": "floppy0", "tray-open": true}} - - === Duplicate ID === Testing: -- 2.1.0