[PATCH] Add virshAllocpagesPagesizeCompleter

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

 



Returns list of host page sizes from capabilities xml

Signed-off-by: Roland Schulz <schullzroll@xxxxxxxxx>
---
 tools/virsh-completer.c | 71 +++++++++++++++++++++++++++++++++++++++++
 tools/virsh-completer.h |  4 +++
 tools/virsh-host.c      |  1 +
 3 files changed, 76 insertions(+)

diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index eb091070c..5f8a35b5f 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -27,6 +27,7 @@
 #include "virsh-pool.h"
 #include "virsh-util.h"
 #include "internal.h"
+#include "virutil.h"
 #include "viralloc.h"
 #include "virstring.h"
 #include "virxml.h"
@@ -570,3 +571,73 @@ virshSnapshotNameCompleter(vshControl *ctl,
     virshDomainFree(dom);
     return NULL;
 }
+
+char **
+virshAllocpagesPagesizeCompleter(vshControl *ctl,
+                        const vshCmd *cmd ATTRIBUTE_UNUSED,
+                        unsigned int flags)
+{
+    unsigned long long byteval = 0;
+    xmlXPathContextPtr ctxt = NULL;
+    virshControlPtr priv = ctl->privData;
+    unsigned int npages = 0;
+    xmlNodePtr *pages = NULL;
+    double size = 0;
+    size_t i = 0;
+    const char *suffix = NULL;
+    char *pagesize = NULL;
+    char *cap_xml = NULL;
+    char **ret = NULL;
+    char *unit = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        goto error;
+
+    if (!(cap_xml = virConnectGetCapabilities(priv->conn)))
+        goto error;
+
+    if (!(virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt)))
+        goto error;
+
+    npages = virXPathNodeSet("/capabilities/host/cpu/pages", ctxt, &pages);
+    if (npages <= 0)
+        goto error;
+
+    if (VIR_ALLOC_N(ret, npages + 1) < 0)
+        goto error;
+
+    for (i = 0; i < npages; i++) {
+        VIR_FREE(pagesize);
+        VIR_FREE(unit);
+        pagesize = virXMLPropString(pages[i], "size");
+        unit = virXMLPropString(pages[i], "unit");
+        if (virStrToLong_ull(pagesize, NULL, 10, &byteval) < 0)
+            goto error;
+        if (virScaleInteger(&byteval, unit, 1024, UINT_MAX) < 0)
+            goto error;
+        size = vshPrettyCapacity(byteval, &suffix);
+        if (virAsprintf(&ret[i], "%.0f%s", size, suffix) < 0)
+            goto error;
+    }
+
+ cleanup:
+    xmlXPathFreeContext(ctxt);
+    for (i = 0; i < npages; i++)
+        VIR_FREE(pages[i]);
+    VIR_FREE(pages);
+    VIR_FREE(cap_xml);
+    VIR_FREE(pagesize);
+    VIR_FREE(unit);
+
+    return ret;
+
+ error:
+    if (ret) {
+        for (i = 0; i < npages; i++)
+            VIR_FREE(ret[i]);
+    }
+    VIR_FREE(ret);
+    goto cleanup;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index ae9626fea..c7b181879 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -74,4 +74,8 @@ char ** virshSnapshotNameCompleter(vshControl *ctl,
                                    const vshCmd *cmd,
                                    unsigned int flags);
 
+char ** virshAllocpagesPagesizeCompleter(vshControl *ctl,
+                                    const vshCmd *cmd,
+                                    unsigned int flags);
+
 #endif
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index ecaf830e3..293f06e9e 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -472,6 +472,7 @@ static const vshCmdOptDef opts_allocpages[] = {
     {.name = "pagesize",
      .type = VSH_OT_INT,
      .flags = VSH_OFLAG_REQ,
+     .completer = virshAllocpagesPagesizeCompleter,
      .help = N_("page size (in kibibytes)")
     },
     {.name = "pagecount",
-- 
2.17.0

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