[PATCH 17/18] virsh: Introduce virshDomainInterfaceCompleter

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



For given domain fetch list of defined interfaces. This can be
used for commands like domif-getlink and others. If available,
the interface name is returned (e.g. "vnet0", usually available
only for running domains), if not the MAC address is returned.
Moreover, the detach-interface command requires only MAC address
and therefore we have new flag that forces the completer to
return just the MAC address.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 tools/virsh-completer.c      | 60 ++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh-completer.h      |  8 ++++++
 tools/virsh-domain-monitor.c |  3 +++
 tools/virsh-domain.c         |  4 +++
 4 files changed, 75 insertions(+)

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index 4e32b882b..25c3aaa0d 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -29,6 +29,7 @@
 #include "internal.h"
 #include "viralloc.h"
 #include "virstring.h"
+#include "virxml.h"
 
 
 char **
@@ -88,3 +89,62 @@ virshDomainNameCompleter(vshControl *ctl,
     VIR_FREE(ret);
     return NULL;
 }
+
+
+char **
+virshDomainInterfaceCompleter(vshControl *ctl,
+                              const vshCmd *cmd,
+                              unsigned int flags)
+{
+    virshControlPtr priv = ctl->privData;
+    xmlDocPtr xmldoc = NULL;
+    xmlXPathContextPtr ctxt = NULL;
+    int ninterfaces;
+    xmlNodePtr *interfaces = NULL;
+    size_t i;
+    unsigned int domainXMLFlags = 0;
+    char **ret = NULL;
+
+    virCheckFlags(VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC, NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if (vshCommandOptBool(cmd, "config"))
+        domainXMLFlags = VIR_DOMAIN_XML_INACTIVE;
+
+    if (virshDomainGetXML(ctl, cmd, domainXMLFlags, &xmldoc, &ctxt) < 0)
+        goto error;
+
+    ninterfaces = virXPathNodeSet("./devices/interface", ctxt, &interfaces);
+    if (ninterfaces < 0)
+        goto error;
+
+    if (VIR_ALLOC_N(ret, ninterfaces + 1) < 0)
+        goto error;
+
+    for (i = 0; i < ninterfaces; i++) {
+        ctxt->node = interfaces[i];
+
+        if (!(flags & VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC) &&
+            (ret[i] = virXPathString("string(./target/@dev)", ctxt)))
+            continue;
+
+        /* In case we are dealing with inactive domain XML there's no
+         * <target dev=''/>. Offer MAC addresses then. */
+        if (!(ret[i] = virXPathString("string(./mac/@address)", ctxt)))
+            goto error;
+    }
+
+    VIR_FREE(interfaces);
+    xmlFreeDoc(xmldoc);
+    xmlXPathFreeContext(ctxt);
+    return ret;
+
+ error:
+    VIR_FREE(interfaces);
+    xmlFreeDoc(xmldoc);
+    xmlXPathFreeContext(ctxt);
+    virStringListFree(ret);
+    return NULL;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index 288e17909..680cd12ff 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -30,4 +30,12 @@ char ** virshDomainNameCompleter(vshControl *ctl,
                                  const vshCmd *cmd,
                                  unsigned int flags);
 
+enum {
+    VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC = 1 << 1, /* Return just MACs */
+};
+
+char ** virshDomainInterfaceCompleter(vshControl *ctl,
+                                      const vshCmd *cmd,
+                                      unsigned int flags);
+
 #endif
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index a09eb010c..32a42707e 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -659,6 +659,7 @@ static const vshCmdOptDef opts_domif_getlink[] = {
     {.name = "interface",
      .type = VSH_OT_DATA,
      .flags = VSH_OFLAG_REQ,
+     .completer = virshDomainInterfaceCompleter,
      .help = N_("interface device (MAC Address)")
     },
     {.name = "persistent",
@@ -995,6 +996,7 @@ static const vshCmdOptDef opts_domifstat[] = {
     {.name = "interface",
      .type = VSH_OT_DATA,
      .flags = VSH_OFLAG_REQ,
+     .completer = virshDomainInterfaceCompleter,
      .help = N_("interface device specified by name or MAC Address")
     },
     {.name = NULL}
@@ -2149,6 +2151,7 @@ static const vshCmdOptDef opts_domifaddr[] = {
     {.name = "interface",
      .type = VSH_OT_STRING,
      .flags = VSH_OFLAG_NONE,
+     .completer = virshDomainInterfaceCompleter,
      .help = N_("network interface name")},
     {.name = "full",
      .type = VSH_OT_BOOL,
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 13f8db3dd..0f329d6d7 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -2977,6 +2977,7 @@ static const vshCmdOptDef opts_domif_setlink[] = {
     {.name = "interface",
      .type = VSH_OT_DATA,
      .flags = VSH_OFLAG_REQ,
+     .completer = virshDomainInterfaceCompleter,
      .help = N_("interface device (MAC Address)")
     },
     {.name = "state",
@@ -3147,6 +3148,7 @@ static const vshCmdOptDef opts_domiftune[] = {
     {.name = "interface",
      .type = VSH_OT_DATA,
      .flags = VSH_OFLAG_REQ,
+     .completer = virshDomainInterfaceCompleter,
      .help = N_("interface device (MAC Address)")
     },
     {.name = "inbound",
@@ -11983,6 +11985,8 @@ static const vshCmdOptDef opts_detach_interface[] = {
     },
     {.name = "mac",
      .type = VSH_OT_STRING,
+     .completer = virshDomainInterfaceCompleter,
+     .completer_flags = VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC,
      .help = N_("MAC address")
     },
     VIRSH_COMMON_OPT_DOMAIN_PERSISTENT,
-- 
2.13.6

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux