From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40958) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VinN0-0006XM-LA for qemu-devel@nongnu.org; Tue, 19 Nov 2013 10:37:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VinMu-000678-LZ for qemu-devel@nongnu.org; Tue, 19 Nov 2013 10:37:42 -0500 Received: from mx1.redhat.com ([209.132.183.28]:30029) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VinMu-00066Z-Dq for qemu-devel@nongnu.org; Tue, 19 Nov 2013 10:37:36 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rAJFbZPC031530 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 19 Nov 2013 10:37:35 -0500 From: Kevin Wolf Date: Tue, 19 Nov 2013 16:37:27 +0100 Message-Id: <1384875448-13115-2-git-send-email-kwolf@redhat.com> In-Reply-To: <1384875448-13115-1-git-send-email-kwolf@redhat.com> References: <1384875448-13115-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH 1/2] block: Enable BDRV_O_SNAPSHOT with driver-specific options List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@redhat.com In the case of snapshot=on, don't rely on the backing file path in the temporary image any more, but override the backing file with the given set of options. This way, block drivers that don't use a file name can be accessed with snapshot=on, for example: -drive file.driver=nbd,file.host=localhost,snapshot=on Which becomes internally something like: file.filename=/tmp/vl.AWQZCu,backing.file.driver=nbd,backing.file.host=localhost Signed-off-by: Kevin Wolf --- block.c | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/block.c b/block.c index 382ea71..51fcb59 100644 --- a/block.c +++ b/block.c @@ -1052,21 +1052,15 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, int64_t total_size; BlockDriver *bdrv_qcow2; QEMUOptionParameter *create_options; - char backing_filename[PATH_MAX]; - - if (qdict_size(options) != 0) { - error_setg(errp, "Can't use snapshot=on with driver-specific options"); - ret = -EINVAL; - goto fail; - } - assert(filename != NULL); + QDict *snapshot_options; /* if snapshot, we create a temporary backing file and open it instead of opening 'filename' directly */ - /* if there is a backing file, use it */ + /* Get the required size from the image */ bs1 = bdrv_new(""); - ret = bdrv_open(bs1, filename, NULL, 0, drv, &local_err); + QINCREF(options); + ret = bdrv_open(bs1, filename, options, 0, drv, &local_err); if (ret < 0) { bdrv_unref(bs1); goto fail; @@ -1075,33 +1069,18 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, bdrv_unref(bs1); + /* Create the temporary image */ ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); if (ret < 0) { error_setg_errno(errp, -ret, "Could not get temporary filename"); goto fail; } - /* Real path is meaningless for protocols */ - if (path_has_protocol(filename)) { - snprintf(backing_filename, sizeof(backing_filename), - "%s", filename); - } else if (!realpath(filename, backing_filename)) { - ret = -errno; - error_setg_errno(errp, errno, "Could not resolve path '%s'", filename); - goto fail; - } - bdrv_qcow2 = bdrv_find_format("qcow2"); create_options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); - set_option_parameter(create_options, BLOCK_OPT_BACKING_FILE, - backing_filename); - if (drv) { - set_option_parameter(create_options, BLOCK_OPT_BACKING_FMT, - drv->format_name); - } ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err); free_option_parameters(create_options); @@ -1114,6 +1093,24 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, goto fail; } + /* Prepare a new options QDict for the temporary file, where user + * options refer to the backing file */ + if (!options) { + options = qdict_new(); + } + if (filename) { + qdict_put(options, "file.filename", qstring_from_str(filename)); + } + if (drv) { + qdict_put(options, "driver", qstring_from_str(drv->format_name)); + } + + snapshot_options = qdict_new(); + qdict_put(snapshot_options, "backing", options); + qdict_flatten(snapshot_options); + + options = snapshot_options; + filename = tmp_filename; drv = bdrv_qcow2; bs->is_temporary = 1; -- 1.8.1.4