[libvirt] [PATCH v2] virsh: add [--domain DOMAIN] option to domxml-to-native DOMAIN COMMAND

Daniel Liu srwx4096 at gmail.com
Mon May 29 18:08:53 UTC 2017


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
---
 tools/virsh-domain.c | 52 ++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 12 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index ccb514ef9..929f9c896 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -9848,9 +9848,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}
@@ -9859,30 +9863,54 @@ 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;
+    const char *domain = NULL;
+    const char *xml = NULL;
+    char *xmlData = NULL;
+    char *configData = 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)
+        vshCommandOptStringReq(ctl, cmd, "xml", &xml) < 0 ||
+        vshCommandOptStringReq(ctl, cmd, "domain", &domain) < 0)
         return false;
 
-    if (virFileReadAll(xmlFile, VSH_MAX_XML_FILE, &xmlData) < 0)
+    VSH_EXCLUSIVE_OPTIONS_VAR(domain, xml);
+
+    if (domain) {
+        if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+            return false;
+    }
+
+    if (dom) {
+        xmlData = virDomainGetXMLDesc(dom, flags);
+    } else if (xml) {
+        if (virFileReadAll(xml, VSH_MAX_XML_FILE, &xmlData) < 0)
+            goto cleanup;
+    }
+
+    if (!xmlData) {
+        vshError(ctl, "%s",
+                 _("need either domain (ID, UUID, or name) or domain XML configuration file path"));
         return false;
+    }
 
-    configData = virConnectDomainXMLToNative(priv->conn, format, xmlData, flags);
-    if (configData != NULL) {
-        vshPrint(ctl, "%s", configData);
-        VIR_FREE(configData);
+    if (!(configData = virConnectDomainXMLToNative(priv->conn, format, xmlData, flags))) {
+        vshError(ctl, "%s",
+                 _("convert from domain XML to native command failed"));
+        goto cleanup;
     } else {
-        ret = false;
+        vshPrint(ctl, "%s", configData);
+        ret = true;
     }
 
+ cleanup:
+    virshDomainFree(dom);
     VIR_FREE(xmlData);
+    VIR_FREE(configData);
     return ret;
 }
 
-- 
2.13.0




More information about the libvir-list mailing list