From 41eb92783e831b9362c321bd563c6c4ec592eb3d Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Fri, 2 Jun 2017 11:04:52 -0400 Subject: [PATCH] virsh: add [--domain DOMAIN] option to domxml-to-native DOMAIN COMMAND The option allows someone to run domain-to-native on already existing domain without the need of supplying their XML. It is basically wrapper around 'virsh dumpxml | virsh domxml-to-native /dev/stdin'. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=835476 Signed-off-by: Daniel Liu Reviewed-by: Martin Kletzander Reviewed-by: John Ferlan --- tools/virsh-domain.c | 47 +++++++++++++++++++++++++++++++++----------- tools/virsh.pod | 18 +++++++++++------ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 91bdb587c5..5311a57c23 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -9856,9 +9856,13 @@ static const vshCmdOptDef opts_domxmltonative[] = { .flags = VSH_OFLAG_REQ, .help = N_("target config data type format") }, + {.name = "domain", + .type = VSH_OT_DATA, + .flags = VSH_OFLAG_REQ_OPT, + .help = N_("domain name, id or uuid") + }, {.name = "xml", .type = VSH_OT_DATA, - .flags = VSH_OFLAG_REQ, .help = N_("xml data file to export from") }, {.name = NULL} @@ -9867,30 +9871,51 @@ static const vshCmdOptDef opts_domxmltonative[] = { static bool cmdDomXMLToNative(vshControl *ctl, const vshCmd *cmd) { - bool ret = true; + bool ret = false; const char *format = NULL; const char *xmlFile = NULL; - char *configData; - char *xmlData; + char *configData = NULL; + char *xmlData = NULL; unsigned int flags = 0; virshControlPtr priv = ctl->privData; + virDomainPtr dom = NULL; if (vshCommandOptStringReq(ctl, cmd, "format", &format) < 0 || vshCommandOptStringReq(ctl, cmd, "xml", &xmlFile) < 0) return false; - if (virFileReadAll(xmlFile, VSH_MAX_XML_FILE, &xmlData) < 0) - return false; + VSH_EXCLUSIVE_OPTIONS("domain", "xml"); - configData = virConnectDomainXMLToNative(priv->conn, format, xmlData, flags); - if (configData != NULL) { - vshPrint(ctl, "%s", configData); - VIR_FREE(configData); + if (vshCommandOptBool(cmd, "domain") && + (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))) + return false; + + if (dom) { + xmlData = virDomainGetXMLDesc(dom, flags); + } else if (xmlFile) { + if (virFileReadAll(xmlFile, VSH_MAX_XML_FILE, &xmlData) < 0) + goto cleanup; } else { - ret = false; + vshError(ctl, "%s", _("need either domain or domain XML")); + goto cleanup; } + if (!xmlData) { + vshError(ctl, "%s", _("failed to retrieve XML")); + goto cleanup; + } + + if (!(configData = virConnectDomainXMLToNative(priv->conn, format, xmlData, flags))) { + goto cleanup; + } else { + vshPrint(ctl, "%s", configData); + ret = true; + } + + cleanup: + virshDomainFree(dom); VIR_FREE(xmlData); + VIR_FREE(configData); return ret; } diff --git a/tools/virsh.pod b/tools/virsh.pod index c5bf1684ab..43d6f0cd36 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -1440,13 +1440,19 @@ the I argument must be B. For Xen hypervisor, the I argument may be B, B, or B. For LXC hypervisor, the I argument must be B. -=item B I I +=item B I +{ [I<--xml>] I | I<--domain> I } -Convert the file I in domain XML format to the native guest -configuration format named by I. For QEMU/KVM hypervisor, -the I argument must be B. For Xen hypervisor, the -I argument may be B, B, or B. For -LXC hypervisor, the I argument must be B. +Convert the file I into domain XML format or convert an existing +I<--domain> to the native guest configuration format named by I. +The I and I<--domain> arguments are mutually exclusive. + +For the QEMU/KVM hypervisor, the I argument must be B. + +For the Xen hypervisor, the I argument may be B, B, +or B. + +For the LXC hypervisor, the I argument must be B. =item B I I [I<--bypass-cache>] { [I<--live>] | [I<--crash>] | [I<--reset>] } [I<--verbose>] [I<--memory-only>]