[libvirt] [PATCH 2/8] snapshot: expose new flags in virsh

Eric Blake eblake at redhat.com
Sun Jun 10 03:37:47 UTC 2012


Previously, to get the name of all snapshots with children, it was
necessary to get the name of all snapshots and then remove the
name of leaf snapshots.  This is racy, and somewhat inefficient
compared to planned API additions.  We can emulate --no-metadata on
0.9.5-0.9.12, but for now, there is no emulation of --no-leaves.

* tools/virsh.c (cmdSnapshotList): Add new options --no-leaves and
--no-metadata.
(vshSnapshotList): Emulate where possible.
* tools/virsh.pod (snapshot-list): Document them.
---
 tools/virsh.c   |   53 +++++++++++++++++++++++++++++++++++++++++++++++------
 tools/virsh.pod |   14 ++++++++++----
 2 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 1228508..936b9fe 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -16762,6 +16762,30 @@ vshSnapshotListCollect(vshControl *ctl, virDomainPtr dom,

     /* 0.9.13 will be adding a new listing API.  */

+    /* Assume that if we got this far, then the --no-leaves and
+     * --no-metadata flags were not supported.  Disable groups that
+     * have no impact.  */
+    /* XXX should we emulate --no-leaves?  */
+    if (flags & VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES &&
+        flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES)
+        flags &= ~(VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES |
+                   VIR_DOMAIN_SNAPSHOT_LIST_LEAVES);
+    if (flags & VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA &&
+        flags & VIR_DOMAIN_SNAPSHOT_LIST_METADATA)
+        flags &= ~(VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA |
+                   VIR_DOMAIN_SNAPSHOT_LIST_METADATA);
+    if (flags & VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA) {
+        /* We can emulate --no-metadata if --metadata was supported,
+         * since it was an all-or-none attribute on old servers.  */
+        count = virDomainSnapshotNum(dom,
+                                     VIR_DOMAIN_SNAPSHOT_LIST_METADATA);
+        if (count < 0)
+            goto cleanup;
+        if (count > 0)
+            return snaplist;
+        flags &= ~VIR_DOMAIN_SNAPSHOT_LIST_METADATA;
+    }
+
     /* This is the interface available in 0.9.12 and earlier,
      * including fallbacks to 0.9.4 and earlier.  */
     if (from) {
@@ -16986,8 +17010,12 @@ static const vshCmdOptDef opts_snapshot_list[] = {
     {"parent", VSH_OT_BOOL, 0, N_("add a column showing parent snapshot")},
     {"roots", VSH_OT_BOOL, 0, N_("list only snapshots without parents")},
     {"leaves", VSH_OT_BOOL, 0, N_("list only snapshots without children")},
+    {"no-leaves", VSH_OT_BOOL, 0,
+     N_("list only snapshots that are not leaves (with children)")},
     {"metadata", VSH_OT_BOOL, 0,
      N_("list only snapshots that have metadata that would prevent undefine")},
+    {"no-metadata", VSH_OT_BOOL, 0,
+     N_("list only snapshots that have no metadata managed by libvirt")},
     {"tree", VSH_OT_BOOL, 0, N_("list snapshots in a tree")},
     {"from", VSH_OT_DATA, 0, N_("limit list to children of given snapshot")},
     {"current", VSH_OT_BOOL, 0,
@@ -17016,9 +17044,9 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
     struct tm time_info;
     bool tree = vshCommandOptBool(cmd, "tree");
     bool leaves = vshCommandOptBool(cmd, "leaves");
+    bool no_leaves = vshCommandOptBool(cmd, "no-leaves");
     const char *from = NULL;
     virDomainSnapshotPtr start = NULL;
-    bool descendants = false;
     vshSnapshotListPtr snaplist = NULL;

     if (!vshConnectionUsability(ctl, ctl->conn))
@@ -17066,15 +17094,28 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
         }
         flags |= VIR_DOMAIN_SNAPSHOT_LIST_LEAVES;
     }
+    if (no_leaves) {
+        if (tree) {
+            vshError(ctl, "%s",
+                     _("--no-leaves and --tree are mutually exclusive"));
+            goto cleanup;
+        }
+        flags |= VIR_DOMAIN_SNAPSHOT_LIST_NO_LEAVES;
+    }

     if (vshCommandOptBool(cmd, "metadata")) {
         flags |= VIR_DOMAIN_SNAPSHOT_LIST_METADATA;
     }
-
-    if (from) {
-        descendants = vshCommandOptBool(cmd, "descendants");
-        if (descendants)
-            flags |= VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS;
+    if (vshCommandOptBool(cmd, "no-metadata")) {
+        flags |= VIR_DOMAIN_SNAPSHOT_LIST_NO_METADATA;
+    }
+    if (vshCommandOptBool(cmd, "descendants")) {
+        if (!from) {
+            vshError(ctl, "%s",
+                     _("--descendants requires either --from or --current"));
+            goto cleanup;
+        }
+        flags |= VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS;
     }

     if ((snaplist = vshSnapshotListCollect(ctl, dom, start, flags,
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 6553825..efdf41f 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2511,7 +2511,7 @@ with I<--current>.

 =item B<snapshot-list> I<domain> [{I<--parent> | I<--roots> | I<--tree>}]
 [{[I<--from>] B<snapshot> | I<--current>} [I<--descendants>]]
-[I<--metadata>] [I<--leaves>]
+[I<--metadata>] [I<--no-metadata>] [I<--leaves>] [I<--no-leaves>]

 List all of the available snapshots for the given domain, defaulting
 to show columns for the snapshot name, creation time, and domain state.
@@ -2531,13 +2531,19 @@ use of I<--descendants> is implied.  This option is not compatible
 with I<--roots>.

 If I<--leaves> is specified, the list will be filtered to just
-snapshots that have no children.  This option is not compatible
-with I<--tree>.
+snapshots that have no children.  Likewise, if I<--no-leaves> is
+specified, the list will be filtered to just snapshots with
+children.  (Note that omitting both options does no filtering,
+while providing both options will either produce the same list
+or error out depending on whether the server recognizes the flags).
+These options are not compatible with I<--tree>.

 If I<--metadata> is specified, the list will be filtered to just
 snapshots that involve libvirt metadata, and thus would prevent
 B<undefine> of a persistent domain, or be lost on B<destroy> of
-a transient domain.
+a transient domain.  Likewise, if I<--no-metadata> is specified,
+the list will be filtered to just snapshots that exist without
+the need for libvirt metadata.

 =item B<snapshot-dumpxml> I<domain> I<snapshot> [I<--security-info>]

-- 
1.7.10.2




More information about the libvir-list mailing list