[PATCH v2 5/8] virsh: Expose virDomainInterfacesAddresses

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

 



---
 tools/virsh-domain.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 104 insertions(+)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index f3da1d5..e6c09f0 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -8491,6 +8491,109 @@ cleanup:
     return ret;
 }
 
+static const vshCmdInfo info_domifaddrs[] = {
+    {"help", N_("Dump domain's IP addresses and other interfaces info")},
+    {"desc", N_("Dump domain's IP addresses and other interfaces info")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_domifaddrs[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"interface", VSH_OT_STRING, 0, N_("Limit selection just to one interface")},
+    {"method", VSH_OT_STRING, 0, N_("Use one method: default, agent, nwfilter")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdDomIfAddrs(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom = NULL;
+    bool ret = false;
+    unsigned int flags = 0;
+    const char *device = NULL;
+    const char *methodStr = NULL;
+    int method = VIR_DOMAIN_INTERFACE_ADDRS_DEFAULT;
+    virDomainInterfacePtr *ifaces = NULL;
+    int i, j, ifacesCount = 0;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        goto cleanup;
+
+    if (vshCommandOptString(cmd, "interface", &device) < 0) {
+        vshError(ctl, _("Unable to parse interface parameter"));
+        goto cleanup;
+    }
+
+    if (vshCommandOptString(cmd, "method", &methodStr) < 0) {
+        vshError(ctl, _("Unable to parse method parameter"));
+        goto cleanup;
+    }
+
+    if (STREQ_NULLABLE(methodStr, "default"))
+        method = VIR_DOMAIN_INTERFACE_ADDRS_DEFAULT;
+    else if (STREQ_NULLABLE(methodStr, "agent"))
+        method = VIR_DOMAIN_INTERFACE_ADDRS_GUEST_AGENT;
+    else if (STREQ_NULLABLE(methodStr, "nwfilter"))
+        method = VIR_DOMAIN_INTERFACE_ADDRS_NWFILTER;
+    else if (methodStr) {
+        vshError(ctl, _("Unknown method: %s"), methodStr);
+        goto cleanup;
+    }
+
+    ifacesCount = virDomainInterfacesAddresses(dom, &ifaces, method, flags);
+    if (ifacesCount < 0)
+        goto cleanup;
+
+    vshPrintExtra(ctl, " %-10s %-17s    %s\n%s\n",
+                  _("Name"), _("HW address"), _("IP address"),
+                  "---------------------------------------------------");
+    for (i = 0; i < ifacesCount; i++) {
+        virDomainInterfacePtr iface = ifaces[i];
+        virBuffer buf = VIR_BUFFER_INITIALIZER;
+        const char *hwaddr = "";
+        const char *ipAddrStr = NULL;
+
+        if (device && STRNEQ(device, iface->name))
+            continue;
+
+        if (iface->hwaddr)
+            hwaddr = iface->hwaddr;
+
+        for (j = 0; j < iface->ip_addrs_count; j++) {
+            const char *type = "";
+            if (iface->ip_addrs[j].type == VIR_IP_ADDR_TYPE_IPV4)
+                type = "inet ";
+            else if (iface->ip_addrs[j].type == VIR_IP_ADDR_TYPE_IPV6)
+                type = "inet6 ";
+            if (j)
+                virBufferAddChar(&buf, ' ');
+            virBufferAsprintf(&buf, "%s%s/%d",
+                              type,
+                              iface->ip_addrs[j].addr,
+                              iface->ip_addrs[j].prefix);
+        }
+
+        ipAddrStr = virBufferContentAndReset(&buf);
+
+        if (!ipAddrStr)
+            ipAddrStr = vshStrdup(ctl,"");
+
+        vshPrintExtra(ctl, " %-10s %-17s    %s\n",
+                      iface->name, hwaddr, ipAddrStr);
+
+        VIR_FREE(ipAddrStr);
+    }
+
+    ret = true;
+cleanup:
+    for (i = 0; i < ifacesCount; i++)
+        virDomainInterfaceFree(ifaces[i]);
+    VIR_FREE(ifaces);
+    if (dom)
+        virDomainFree(dom);
+    return ret;
+}
+
 const vshCmdDef domManagementCmds[] = {
     {"attach-device", cmdAttachDevice, opts_attach_device,
      info_attach_device, 0},
@@ -8527,6 +8630,7 @@ const vshCmdDef domManagementCmds[] = {
     {"domhostname", cmdDomHostname, opts_domhostname, info_domhostname, 0},
     {"domid", cmdDomid, opts_domid, info_domid, 0},
     {"domif-setlink", cmdDomIfSetLink, opts_domif_setlink, info_domif_setlink, 0},
+    {"domifaddrs", cmdDomIfAddrs, opts_domifaddrs, info_domifaddrs, 0},
     {"domiftune", cmdDomIftune, opts_domiftune, info_domiftune, 0},
     {"domjobabort", cmdDomjobabort, opts_domjobabort, info_domjobabort, 0},
     {"domjobinfo", cmdDomjobinfo, opts_domjobinfo, info_domjobinfo, 0},
-- 
1.8.0.2

--
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]