[libvirt] [PATCH v2 2/5] virsh-volume: Introduce virshStorageVolKeyCompleter

Lin Ma lma at suse.com
Wed Jun 16 08:02:51 UTC 2021


Signed-off-by: Lin Ma <lma at suse.com>
---
 tools/virsh-completer-volume.c | 47 ++++++++++++++++++++++++++++++++++
 tools/virsh-completer-volume.h |  5 ++++
 2 files changed, 52 insertions(+)

diff --git a/tools/virsh-completer-volume.c b/tools/virsh-completer-volume.c
index 301ee982a5..a29c5eca41 100644
--- a/tools/virsh-completer-volume.c
+++ b/tools/virsh-completer-volume.c
@@ -69,3 +69,50 @@ virshStorageVolNameCompleter(vshControl *ctl,
     g_free(vols);
     return ret;
 }
+
+char **
+virshStorageVolKeyCompleter(vshControl *ctl,
+                            const vshCmd *cmd G_GNUC_UNUSED,
+                            unsigned int flags)
+{
+    virshControl *priv = ctl->privData;
+    struct virshStoragePoolList *list = NULL;
+    virStorageVolPtr *vols = NULL;
+    int rc;
+    int nvols = 0;
+    size_t i = 0, j = 0;
+    char **ret = NULL;
+    g_auto(GStrv) tmp = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if (!(list = virshStoragePoolListCollect(ctl,
+                    VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE)))
+        goto cleanup;
+
+    for (i = 0; i < list->npools; i++) {
+        if ((rc = virStoragePoolListAllVolumes(list->pools[i], &vols, 0)) < 0)
+            goto cleanup;
+
+        tmp = g_renew(char *, tmp, nvols + rc + 1);
+        memset(&tmp[nvols], 0, sizeof(*tmp) * (rc + 1));
+
+        for (j = 0; j < rc; j++) {
+            const char *key = virStorageVolGetKey(vols[j]);
+            tmp[nvols] = g_strdup(key);
+            nvols++;
+            virStorageVolFree(vols[j]);
+        }
+
+        g_free(vols);
+    }
+
+    ret = g_steal_pointer(&tmp);
+
+ cleanup:
+    virshStoragePoolListFree(list);
+    return ret;
+}
diff --git a/tools/virsh-completer-volume.h b/tools/virsh-completer-volume.h
index 6591e13fdf..b41d8f4f3e 100644
--- a/tools/virsh-completer-volume.h
+++ b/tools/virsh-completer-volume.h
@@ -26,3 +26,8 @@
 char ** virshStorageVolNameCompleter(vshControl *ctl,
                                      const vshCmd *cmd,
                                      unsigned int flags);
+
+
+char ** virshStorageVolKeyCompleter(vshControl *ctl,
+                                    const vshCmd *cmd,
+                                    unsigned int flags);
-- 
2.26.2





More information about the libvir-list mailing list