[libvirt] [PATCH 09/12] virsh-volume: Introduce virshStorageVolKeyCompleter

Lin Ma lma at suse.com
Tue Jun 15 00:38:29 UTC 2021


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

diff --git a/tools/virsh-completer-volume.c b/tools/virsh-completer-volume.c
index 301ee982a5..14e9069c5a 100644
--- a/tools/virsh-completer-volume.c
+++ b/tools/virsh-completer-volume.c
@@ -69,3 +69,56 @@ 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, idx = 0;
+    char **ret = NULL;
+    g_auto(GStrv) tmp = NULL;
+
+    virCheckFlags(0, NULL);
+
+    flags = VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE;
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if (!(list = virshStoragePoolListCollect(ctl, flags)))
+        goto cleanup;
+
+    for (i = 0; i < list->npools; i++) {
+        if ((rc = virStoragePoolNumOfVolumes(list->pools[i])) < 0)
+            goto cleanup;
+        nvols += rc;
+    }
+
+    tmp = g_new0(char *, nvols + 1);
+
+    for (i = 0; i < list->npools; i++) {
+        if ((rc = virStoragePoolListAllVolumes(list->pools[i], &vols, 0)) < 0)
+            goto cleanup;
+        for (j = 0; j < rc; j++) {
+            const char *key = virStorageVolGetKey(vols[j]);
+            tmp[idx++] = g_strdup(key);
+        }
+    }
+
+    ret = g_steal_pointer(&tmp);
+
+ cleanup:
+    for (i = 0; i < list->npools; i++)
+        if ((rc = virStoragePoolListAllVolumes(list->pools[i], &vols, 0)) > 0)
+            for (j = 0; j < rc; j++)
+                virStorageVolFree(vols[j]);
+    g_free(vols);
+    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