[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v6 11/22] block: Add bdrv_open_image()
From: |
Max Reitz |
Subject: |
[Qemu-devel] [PATCH v6 11/22] block: Add bdrv_open_image() |
Date: |
Thu, 19 Dec 2013 20:47:12 +0100 |
Add a common function for opening images to be used for block drivers
specified through BlockdevRefs in an option QDict. The difference from
bdrv_file_open() is that this function may invoke bdrv_open() instead,
allowing auto-detection of the driver to be used; and second, it
automatically extracts the BlockdevRef from the option QDict.
Signed-off-by: Max Reitz <address@hidden>
---
block.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
include/block/block.h | 3 +++
2 files changed, 76 insertions(+)
diff --git a/block.c b/block.c
index 7464fb2..76b6c25 100644
--- a/block.c
+++ b/block.c
@@ -1041,6 +1041,79 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict
*options, Error **errp)
}
/*
+ * Opens a disk image whose options are given as BlockdevRef in another block
+ * device's options.
+ *
+ * If force_raw is true, bdrv_file_open() will be used, thereby preventing any
+ * image format auto-detection. If it is false and a filename is given,
+ * bdrv_open() will be used for auto-detection.
+ *
+ * If allow_none is true, no image will be opened if filename is false and no
+ * BlockdevRef is given. *pbs will remain unchanged and 0 will be returned.
+ *
+ * bdrev_key specifies the key for the image's BlockdevRef in the options
QDict.
+ * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict
+ * itself, all options starting with "${bdref_key}." are considered part of the
+ * BlockdevRef.
+ *
+ * The BlockdevRef will be removed from the options QDict.
+ */
+int bdrv_open_image(BlockDriverState **pbs, const char *filename,
+ QDict *options, const char *bdref_key, int flags,
+ bool force_raw, bool allow_none, Error **errp)
+{
+ QDict *image_options;
+ int ret;
+ char *bdref_key_dot;
+ const char *reference;
+
+ bdref_key_dot = g_strdup_printf("%s.", bdref_key);
+ qdict_extract_subqdict(options, &image_options, bdref_key_dot);
+ g_free(bdref_key_dot);
+
+ reference = qdict_get_try_str(options, bdref_key);
+ if (!filename && !reference && !qdict_size(image_options)) {
+ if (allow_none) {
+ ret = 0;
+ } else {
+ error_setg(errp, "A block device must be specified for \"%s\"",
+ bdref_key);
+ ret = -EINVAL;
+ }
+ goto done;
+ }
+
+ if (filename && !force_raw) {
+ /* If a filename is given and the block driver should be detected
+ automatically (instead of using none), use bdrv_open() in order to
do
+ that auto-detection. */
+ BlockDriverState *bs;
+
+ if (reference) {
+ error_setg(errp, "Cannot reference an existing block device while "
+ "giving a filename");
+ ret = -EINVAL;
+ goto done;
+ }
+
+ bs = bdrv_new("");
+ ret = bdrv_open(bs, filename, image_options, flags, NULL, errp);
+ if (ret < 0) {
+ bdrv_unref(bs);
+ } else {
+ *pbs = bs;
+ }
+ } else {
+ ret = bdrv_file_open(pbs, filename, reference, image_options, flags,
+ errp);
+ }
+
+done:
+ qdict_del(options, bdref_key);
+ return ret;
+}
+
+/*
* Opens a disk image (raw, qcow2, vmdk, ...)
*
* options is a QDict of options to pass to the block drivers, or NULL for an
diff --git a/include/block/block.h b/include/block/block.h
index e2b2a15..a47f3d4 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -186,6 +186,9 @@ int bdrv_parse_discard_flags(const char *mode, int *flags);
int bdrv_file_open(BlockDriverState **pbs, const char *filename,
const char *reference, QDict *options, int flags,
Error **errp);
+int bdrv_open_image(BlockDriverState **pbs, const char *filename,
+ QDict *options, const char *bdref_key, int flags,
+ bool force_raw, bool allow_none, Error **errp);
int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
int flags, BlockDriver *drv, Error **errp);
--
1.8.5.1
- Re: [Qemu-devel] [PATCH v6 01/22] blkdebug: Use errp for read_config(), (continued)
- [Qemu-devel] [PATCH v6 02/22] blkdebug: Don't require sophisticated filename, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 03/22] qdict: Add qdict_array_split(), Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 04/22] qapi: extend qdict_flatten() for QLists, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 05/22] qemu-option: Add qemu_config_parse_qdict(), Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 06/22] blkdebug: Always call read_config(), Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 07/22] blkdebug: Use command-line in read_config(), Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 08/22] block: Allow reference for bdrv_file_open(), Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 09/22] block: Pass reference to bdrv_file_open(), Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 10/22] block: Allow block devices without files, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 11/22] block: Add bdrv_open_image(),
Max Reitz <=
- [Qemu-devel] [PATCH v6 12/22] block: Use bdrv_open_image() in bdrv_open(), Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 13/22] block: Allow recursive "file"s, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 14/22] blockdev: Move "file" to legacy_opts, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 15/22] blkdebug: Allow command-line file configuration, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 16/22] blkverify: Allow command-line configuration, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 18/22] qapi: Add "errno" to the list of polluted words, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 17/22] blkverify: Don't require protocol filename, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 19/22] qapi: QMP interface for blkdebug and blkverify, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 20/22] qemu-io: Make filename optional, Max Reitz, 2013/12/19
- [Qemu-devel] [PATCH v6 21/22] iotests: Test new blkdebug/blkverify interface, Max Reitz, 2013/12/19