提交 25e2d897 编写于 作者: P Peter Krempa

qemu: command: Refactor creation of RNG device commandline

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.
上级 b9f2d781
...@@ -6160,15 +6160,38 @@ qemuBuildSclpDevStr(virDomainChrDefPtr dev) ...@@ -6160,15 +6160,38 @@ qemuBuildSclpDevStr(virDomainChrDefPtr dev)
static int static int
qemuBuildRNGBackendArgs(virCommandPtr cmd, qemuBuildRNGBackendChrdevStr(virDomainRNGDefPtr rng,
virDomainRNGDefPtr dev, virQEMUCapsPtr qemuCaps,
virQEMUCapsPtr qemuCaps) char **chr)
{ {
virBuffer buf = VIR_BUFFER_INITIALIZER; *chr = NULL;
char *backend = NULL;
switch ((virDomainRNGBackend) rng->backend) {
case VIR_DOMAIN_RNG_BACKEND_RANDOM:
case VIR_DOMAIN_RNG_BACKEND_LAST:
/* no chardev backend is needed */
return 0;
case VIR_DOMAIN_RNG_BACKEND_EGD:
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; int ret = -1;
switch ((virDomainRNGBackend) dev->backend) { switch ((virDomainRNGBackend) rng->backend) {
case VIR_DOMAIN_RNG_BACKEND_RANDOM: case VIR_DOMAIN_RNG_BACKEND_RANDOM:
if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_RANDOM)) { if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_RANDOM)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
...@@ -6177,15 +6200,14 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd, ...@@ -6177,15 +6200,14 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd,
goto cleanup; goto cleanup;
} }
virBufferAsprintf(&buf, "rng-random,id=obj%s,filename=%s", *type = "rng-random";
dev->info.alias, dev->source.file);
virCommandAddArg(cmd, "-object"); if (virJSONValueObjectCreate(props, "s:filename", rng->source.file,
virCommandAddArgBuffer(cmd, &buf); NULL) < 0)
goto cleanup;
break; break;
case VIR_DOMAIN_RNG_BACKEND_EGD: case VIR_DOMAIN_RNG_BACKEND_EGD:
if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_EGD)) { if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_RNG_EGD)) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("this qemu doesn't support the rng-egd " _("this qemu doesn't support the rng-egd "
...@@ -6193,15 +6215,15 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd, ...@@ -6193,15 +6215,15 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd,
goto cleanup; goto cleanup;
} }
if (!(backend = qemuBuildChrChardevStr(dev->source.chardev, *type = "rng-egd";
dev->info.alias, qemuCaps)))
if (virAsprintf(&charBackendAlias, "char%s", rng->info.alias) < 0)
goto cleanup; 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; break;
case VIR_DOMAIN_RNG_BACKEND_LAST: case VIR_DOMAIN_RNG_BACKEND_LAST:
...@@ -6211,8 +6233,31 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd, ...@@ -6211,8 +6233,31 @@ qemuBuildRNGBackendArgs(virCommandPtr cmd,
ret = 0; ret = 0;
cleanup: cleanup:
virBufferFreeAndReset(&buf); VIR_FREE(charBackendAlias);
VIR_FREE(backend); 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; return ret;
} }
...@@ -10161,16 +10206,36 @@ qemuBuildCommandLine(virConnectPtr conn, ...@@ -10161,16 +10206,36 @@ qemuBuildCommandLine(virConnectPtr conn,
} }
for (i = 0; i < def->nrngs; i++) { 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 */ /* add the RNG source backend */
if (qemuBuildRNGBackendArgs(cmd, def->rngs[i], qemuCaps) < 0) if (!(tmp = qemuBuildRNGBackendStr(rng, qemuCaps)))
goto error; goto error;
virCommandAddArgList(cmd, "-object", tmp, NULL);
VIR_FREE(tmp);
/* add the device */ /* add the device */
if (!(devstr = qemuBuildRNGDevStr(def, def->rngs[i], qemuCaps))) if (!(tmp = qemuBuildRNGDevStr(def, rng, qemuCaps)))
goto error; goto error;
virCommandAddArgList(cmd, "-device", devstr, NULL); virCommandAddArgList(cmd, "-device", tmp, NULL);
VIR_FREE(devstr); VIR_FREE(tmp);
} }
if (def->nvram) { if (def->nvram) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册