[libvirt] [PATCH] virsh: add --domain option for domain-to-native

Daniel Liu srwx4096 at gmail.com
Mon May 15 08:29:48 UTC 2017


Fix bug 835476[1].
virsh: add [--domain DOMAIN] option to  domxml-to-native DOMAIN COMMAND
Add support for the following syntax:
domxml-to-native <format> { [--domain DOMAIN] | [XML] }, i.e., it supports
either designating domain (domain id, uuid, or name), or path to XML domain
configuration file.

E.g.:
virsh domxml-to-native qemu-argv --domain RHEL7.3   # domain name
virsh domxml-to-native qemu-argv --domain 10        # domain id
virsh domxml-to-native qemu-argv dumped_dom.xml     # dumped xml

[1] https://bugzilla.redhat.com/show_bug.cgi?id=835476
---
 tools/virsh-domain.c | 54 ++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 44 insertions(+), 10 deletions(-)

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




More information about the libvir-list mailing list