As the RNG device is using an -object as backend refactor the code to
use the JSON to commandline generator so that we can reuse the code
later in hotplug.
---
src/qemu/qemu_command.c | 108 +++++++++++++++++++++++++++++++++++++-----------
1 file changed, 84 insertions(+), 24 deletions(-)
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 6380621..9179c1f 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -6154,15 +6154,33 @@ qemuBuildSclpDevStr(virDomainChrDefPtr dev)
static int
-qemuBuildRNGBackendArgs(virCommandPtr cmd,
- virDomainRNGDefPtr dev,
- virQEMUCapsPtr qemuCaps)
+qemuBuildRNGBackendChrdevStr(virDomainRNGDefPtr rng,
+ virQEMUCapsPtr qemuCaps,
+ char **chr)
{
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- char *backend = NULL;
+ *chr = NULL;
+
+ if (rng->backend != VIR_DOMAIN_RNG_BACKEND_EGD)
+ return 0;
+
+ if (!(*chr = qemuBuildChrChardevStr(rng->source.chardev,
+ rng->info.alias, qemuCaps)))
+ return -1;
+
+ return 0;
+}
+
+
+static int
+qemuBuildRNGBackendProps(virDomainRNGDefPtr rng,
+ virQEMUCapsPtr qemuCaps,
+ const char **type,
+ virJSONValuePtr *props)
+{
+ char *charBackendAlias = NULL;
int ret = -1;
- switch ((virDomainRNGBackend) dev->backend) {
+ switch ((virDomainRNGBackend) rng->backend) {
case VIR_DOMAIN_RNG_BACKEND_RANDOM:
if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_RANDOM)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
@@ -6171,15 +6189,14 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd,
goto cleanup;
}
- virBufferAsprintf(&buf, "rng-random,id=obj%s,filename=%s",
- dev->info.alias, dev->source.file);
+ *type = "rng-random";
- virCommandAddArg(cmd, "-object");
- virCommandAddArgBuffer(cmd, &buf);
+ if (virJSONValueObjectCreate(props, "s:filename", rng->source.file,
+ NULL) < 0)
+ goto cleanup;
break;
case VIR_DOMAIN_RNG_BACKEND_EGD:
-
if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_EGD)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("this qemu doesn't support the rng-egd "
@@ -6187,15 +6204,15 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd,
goto cleanup;
}
- if (!(backend = qemuBuildChrChardevStr(dev->source.chardev,
- dev->info.alias, qemuCaps)))
+ *type = "rng-egd";
+
+ if (virAsprintf(&charBackendAlias, "char%s", rng->info.alias)
< 0)
goto cleanup;
- virCommandAddArgList(cmd, "-chardev", backend, NULL);
+ if (virJSONValueObjectCreate(props, "s:chardev", charBackendAlias,
+ NULL) < 0)
+ goto cleanup;
- virCommandAddArg(cmd, "-object");
- virCommandAddArgFormat(cmd, "rng-egd,id=obj%s,chardev=char%s",
- dev->info.alias, dev->info.alias);
break;
case VIR_DOMAIN_RNG_BACKEND_LAST:
@@ -6205,8 +6222,31 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd,
ret = 0;
cleanup:
- virBufferFreeAndReset(&buf);
- VIR_FREE(backend);
+ VIR_FREE(charBackendAlias);
+ return ret;
+}
+
+
+static char *
+qemuBuildRNGBackendStr(virDomainRNGDefPtr rng,
+ virQEMUCapsPtr qemuCaps)
+{
+ const char *type = NULL;
+ char *alias = NULL;
+ virJSONValuePtr props = NULL;
+ char *ret = NULL;
+
+ if (virAsprintf(&alias, "obj%s", rng->info.alias) < 0)
+ goto cleanup;
+
+ if (qemuBuildRNGBackendProps(rng, qemuCaps, &type, &props) < 0)
+ goto cleanup;
+
+ ret = qemuBuildObjectCommandlineFromJSON(type, alias, props);
+
+ cleanup:
+ VIR_FREE(alias);
+ virJSONValueFree(props);
return ret;
}
@@ -10146,16 +10186,36 @@ qemuBuildCommandLine(virConnectPtr conn,
}
for (i = 0; i < def->nrngs; i++) {
- char *devstr;
+ virDomainRNGDefPtr rng = def->rngs[i];
+ char *tmp;
+
+ if (!rng->info.alias) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("RNG device is missing alias"));
+ goto error;
+ }
+
+ /* possibly add character device for backend */
+ if (qemuBuildRNGBackendChrdevStr(rng, qemuCaps, &tmp) < 0)
+ goto error;
+
+ if (tmp) {
+ virCommandAddArgList(cmd, "-chardev", tmp, NULL);
+ VIR_FREE(tmp);
+ }
+
/* add the RNG source backend */
- if (qemuBuildRNGBackendArgs(cmd, def->rngs[i], qemuCaps) < 0)
+ if (!(tmp = qemuBuildRNGBackendStr(rng, qemuCaps)))
goto error;
+ virCommandAddArgList(cmd, "-object", tmp, NULL);
+ VIR_FREE(tmp);
+
/* add the device */
- if (!(devstr = qemuBuildRNGDevStr(def, def->rngs[i], qemuCaps)))
+ if (!(tmp = qemuBuildRNGDevStr(def, rng, qemuCaps)))
goto error;
- virCommandAddArgList(cmd, "-device", devstr, NULL);
- VIR_FREE(devstr);
+ virCommandAddArgList(cmd, "-device", tmp, NULL);
+ VIR_FREE(tmp);
}
if (def->nvram) {
--
2.2.2