[libvirt] [PATCHv4 40/51] snapshot: expose halt-after-creation in virsh

Eric Blake eblake at redhat.com
Fri Sep 2 04:25:17 UTC 2011


Easy enough to emulate even with older servers.

* tools/virsh.c (cmdSnapshotCreate, cmdSnapshotCreateAs): Add
--halt flag.
(vshSnapshotCreate): Emulate halt when flag is unsupported.
* tools/virsh.pod (snapshot-create, snapshot-create-as): Document
it.
---
 tools/virsh.c   |   41 +++++++++++++++++++++++++++++++++++++++++
 tools/virsh.pod |   11 ++++++++---
 2 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 754c619..15e30e9 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -11986,15 +11986,45 @@ vshSnapshotCreate(vshControl *ctl, virDomainPtr dom, const char *buffer,
 {
     bool ret = false;
     virDomainSnapshotPtr snapshot;
+    bool halt = false;
     char *doc = NULL;
     xmlDocPtr xml = NULL;
     xmlXPathContextPtr ctxt = NULL;
     char *name = NULL;

     snapshot = virDomainSnapshotCreateXML(dom, buffer, flags);
+
+    /* Emulate --halt on older servers.  */
+    if (!snapshot && last_error->code == VIR_ERR_INVALID_ARG &&
+        (flags & VIR_DOMAIN_SNAPSHOT_CREATE_HALT)) {
+        int persistent;
+
+        virFreeError(last_error);
+        last_error = NULL;
+        persistent = virDomainIsPersistent(dom);
+        if (persistent < 0) {
+            virshReportError(ctl);
+            goto cleanup;
+        }
+        if (!persistent) {
+            vshError(ctl, "%s",
+                     _("cannot halt after snapshot of transient domain"));
+            goto cleanup;
+        }
+        if (virDomainIsActive(dom) == 1)
+            halt = true;
+        flags &= ~VIR_DOMAIN_SNAPSHOT_CREATE_HALT;
+        snapshot = virDomainSnapshotCreateXML(dom, buffer, flags);
+    }
+
     if (snapshot == NULL)
         goto cleanup;

+    if (halt && virDomainDestroy(dom) < 0) {
+        virshReportError(ctl);
+        goto cleanup;
+    }
+
     if (flags & VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA)
         doc = vshStrdup(ctl, buffer);
     else
@@ -12045,6 +12075,7 @@ static const vshCmdOptDef opts_snapshot_create[] = {
     {"redefine", VSH_OT_BOOL, 0, N_("redefine metadata for existing snapshot")},
     {"current", VSH_OT_BOOL, 0, N_("with redefine, set current snapshot")},
     {"no-metadata", VSH_OT_BOOL, 0, N_("take snapshot but create no metadata")},
+    {"halt", VSH_OT_BOOL, 0, N_("halt domain after snapshot is created")},
     {NULL, 0, 0, NULL}
 };

@@ -12063,6 +12094,8 @@ cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd)
         flags |= VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT;
     if (vshCommandOptBool(cmd, "no-metadata"))
         flags |= VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA;
+    if (vshCommandOptBool(cmd, "halt"))
+        flags |= VIR_DOMAIN_SNAPSHOT_CREATE_HALT;

     if (!vshConnectionUsability(ctl, ctl->conn))
         goto cleanup;
@@ -12113,6 +12146,7 @@ static const vshCmdOptDef opts_snapshot_create_as[] = {
     {"description", VSH_OT_DATA, 0, N_("description of snapshot")},
     {"print-xml", VSH_OT_BOOL, 0, N_("print XML document rather than create")},
     {"no-metadata", VSH_OT_BOOL, 0, N_("take snapshot but create no metadata")},
+    {"halt", VSH_OT_BOOL, 0, N_("halt domain after snapshot is created")},
     {NULL, 0, 0, NULL}
 };

@@ -12129,6 +12163,8 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd)

     if (vshCommandOptBool(cmd, "no-metadata"))
         flags |= VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA;
+    if (vshCommandOptBool(cmd, "halt"))
+        flags |= VIR_DOMAIN_SNAPSHOT_CREATE_HALT;

     if (!vshConnectionUsability(ctl, ctl->conn))
         goto cleanup;
@@ -12157,6 +12193,11 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd)
     }

     if (vshCommandOptBool(cmd, "print-xml")) {
+        if (vshCommandOptBool(cmd, "halt")) {
+            vshError(ctl, "%s",
+                     _("--print-xml and --halt are mutually exclusive"));
+            goto cleanup;
+        }
         vshPrint(ctl, "%s\n",  buffer);
         ret = true;
         goto cleanup;
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 28e1d20..30548b4 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -1692,7 +1692,7 @@ used to represent properties of snapshots.
 =over 4

 =item B<snapshot-create> I<domain> [I<xmlfile>] {[I<--redefine> [I<--current>]]
-| [I<--no-metadata>]}
+| [I<--no-metadata>] [I<--halt>]}

 Create a snapshot for domain I<domain> with the properties specified in
 I<xmlfile>.  Normally, the only properties settable for a domain snapshot
@@ -1701,6 +1701,9 @@ ignored, and automatically filled in by libvirt.  If I<xmlfile> is
 completely omitted, then libvirt will choose a value for all fields.
 The new snapshot will become current, as listed by B<snapshot-current>.

+If I<--halt> is specified, the domain will be left in an inactive state
+after the snapshot is created.
+
 If I<--redefine> is specified, then all XML elements produced by
 B<snapshot-dumpxml> are valid; this can be used to migrate snapshot
 hierarchy from one machine to another, to recreate hierarchy for the
@@ -1722,13 +1725,15 @@ a persistent domain.  However, for transient domains, snapshot
 metadata is silently lost when the domain quits running (whether
 by command such as B<destroy> or by internal guest action).

-=item B<snapshot-create-as> I<domain> {[I<--print-xml>] | [I<--no-metadata>]}
-[I<name>] [I<description>]
+=item B<snapshot-create-as> I<domain> {[I<--print-xml>]
+| [I<--no-metadata>] [I<--halt>]} [I<name>] [I<description>]

 Create a snapshot for domain I<domain> with the given <name> and
 <description>; if either value is omitted, libvirt will choose a
 value.  If I<--print-xml> is specified, then XML appropriate for
 I<snapshot-create> is output, rather than actually creating a snapshot.
+Otherwise, if I<--halt> is specified, the domain will be left in an
+inactive state after the snapshot is created.

 If I<--no-metadata> is specified, then the snapshot data is created,
 but any metadata is immediately discarded (that is, libvirt does not
-- 
1.7.4.4




More information about the libvir-list mailing list