Re: [PATCHv4 4/5] domifaddr: Add virsh support

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

 



On 25/08/13 07:15, Nehal J Wani wrote:
Use virDomainInterfacesAddresses in virsh

tools/virsh-domain-monitor.c
    * Introduce new command : domifaddr
    virsh # domifaddr f18
    Name       MAC address          IPv4 address        IPv6 address
    -------------------------------------------------------------------------------
    lo         00:00:00:00:00:00    127.0.0.1/8                      ::1/128
    eth0       52:54:00:89:4e:97    192.168.101.130/24  fe80::5054:ff:fe89:4e97/64
    eth0:1     52:54:00:89:4e:97    192.168.101.133/24
    eth0:2     52:54:00:89:4e:97    192.168.101.132/24
    eth1       52:54:00:89:ad:35    192.168.102.142/24  fe80::5054:ff:fe89:ad35/64
    eth1:1     52:54:00:89:ad:35    192.168.102.143/24
    eth2       52:54:00:d3:39:ee    192.168.103.183/24  fe80::5054:ff:fed3:39ee/64
    eth2:0     52:54:00:d3:39:ee    192.168.103.184/24
    eth2:1     52:54:00:d3:39:ee    192.168.103.185/24
    eth3       52:54:00:fe:4c:4f    192.168.101.197/24  fe80::5054:ff:fefe:4c4f/64
    eth3:1     52:54:00:fe:4c:4f    192.168.101.198/24



tools/virsh.pod
    * Document new command

---
  tools/virsh-domain-monitor.c | 101 +++++++++++++++++++++++++++++++++++++++++++
  tools/virsh.pod              |  10 +++++
  2 files changed, 111 insertions(+)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index b29b82a..040b0e4 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1871,6 +1871,101 @@ cleanup:
  }
  #undef FILTER
+/* "domifaddr" command
+ */
+static const vshCmdInfo info_domifaddr[] = {
+    {"help", N_("Get network interfaces' addresses for a running domain")},
+    {"desc", N_("Get network interfaces' addresses for a running domain")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_domifaddr[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"interface", VSH_OT_DATA, VSH_OFLAG_NONE, N_("network interface name")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom = NULL;
+    const char *interface = NULL;
+    virDomainInterfacePtr *ifaces = NULL;
+    size_t i, j;
+    int ifaces_count = 0;
+    unsigned int flags = 0;
+    bool ret = false;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    if (vshCommandOptString(cmd, "interface", &interface) < 0) {
+        goto cleanup;
+    }
+
+    if ((ifaces_count = virDomainInterfacesAddresses(dom, &ifaces, flags)) < 0) {
+        vshError(ctl, _("Failed to query for interfaces addresses"));
+        goto cleanup;
+    }
+
+    vshPrintExtra(ctl, " %-10s %-20s %-15s     %s\n%s%s\n", _("Name"),
+                  _("MAC address"), _("IPv4 address"), _("IPv6 address"),
+                  _("-------------------------------------------------"),
+                  _("------------------------------"));
+
+    for (i = 0; i < ifaces_count; i++) {
+        virDomainInterfacePtr iface = ifaces[i];
+        virBuffer buf = VIR_BUFFER_INITIALIZER;
+        const char *hwaddr = "";
+        const char *ip_addr_str = NULL;
+
+        if (interface && STRNEQ(interface, iface->name)) {
+            virBufferFreeAndReset(&buf);
+            continue;
+        }
+
+        if (iface->hwaddr)
+            hwaddr = iface->hwaddr;
+
+        for (j = 0; j < iface->naddrs; j++) {
+            if (j)
+                virBufferAsprintf(&buf, "%25s/%d",
+                                  iface->addrs[j].addr,
+                                  iface->addrs[j].prefix);
+            else
+                virBufferAsprintf(&buf, "%s/%d",
+                                  iface->addrs[j].addr,
+                                  iface->addrs[j].prefix);
+        }
+
+        if (virBufferError(&buf)) {
+            virBufferFreeAndReset(&buf);
+            virReportOOMError();
+            return ret;
+        }
+
+        ip_addr_str = virBufferContentAndReset(&buf);
+
+        if (!ip_addr_str)
+            ip_addr_str = "";
+
+        vshPrintExtra(ctl, " %-10s %-17s    %s\n",
+                     iface->name, hwaddr, ip_addr_str);

ACK with above indention problem fixed.

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