]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: add snapshot-parent
authorEric Blake <eblake@redhat.com>
Wed, 10 Aug 2011 23:18:55 +0000 (17:18 -0600)
committerEric Blake <eblake@redhat.com>
Thu, 11 Aug 2011 19:18:24 +0000 (13:18 -0600)
Down the road, I want to add virDomainSnapshotGetParent, and use
the new API rather than xml scraping; but this virsh command can
be implemented even without the new API.

* tools/virsh.c (cmdSnapshotParent): New command.
* tools/virsh.pod (snapshot-parent): Document it.

tools/virsh.c
tools/virsh.pod

index 1d660d0ba150a6ac410756fbae7c5241469e040c..ee08c012059e8043e7ec6cd022ecfc45e57707e8 100644 (file)
@@ -12284,6 +12284,82 @@ cleanup:
     return ret;
 }
 
+/*
+ * "snapshot-parent" command
+ */
+static const vshCmdInfo info_snapshot_parent[] = {
+    {"help", N_("Get the name of the parent of the current snapshot")},
+    {"desc", N_("Extract the snapshot's parent, if any")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_snapshot_parent[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"snapshotname", VSH_OT_DATA, VSH_OFLAG_REQ, N_("snapshot name")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdSnapshotParent(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom = NULL;
+    bool ret = false;
+    const char *name = NULL;
+    virDomainSnapshotPtr snapshot = NULL;
+    char *xml = NULL;
+    char *parent = NULL;
+    xmlDocPtr xmldoc = NULL;
+    xmlXPathContextPtr ctxt = NULL;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        goto cleanup;
+
+    dom = vshCommandOptDomain(ctl, cmd, NULL);
+    if (dom == NULL)
+        goto cleanup;
+
+    if (vshCommandOptString(cmd, "snapshotname", &name) <= 0)
+        goto cleanup;
+
+    snapshot = virDomainSnapshotLookupByName(dom, name, 0);
+    if (snapshot == NULL)
+        goto cleanup;
+
+    xml = virDomainSnapshotGetXMLDesc(snapshot, 0);
+    if (!xml)
+        goto cleanup;
+
+    xmldoc = xmlReadDoc((const xmlChar *) xml, "domainsnapshot.xml", NULL,
+                        XML_PARSE_NOENT | XML_PARSE_NONET |
+                        XML_PARSE_NOWARNING);
+    if (!xmldoc)
+        goto cleanup;
+    ctxt = xmlXPathNewContext(xmldoc);
+    if (!ctxt)
+        goto cleanup;
+
+    parent = virXPathString("string(/domainsnapshot/parent/name)", ctxt);
+    if (!parent)
+        goto cleanup;
+
+    vshPrint(ctl, "%s", parent);
+
+    ret = true;
+
+cleanup:
+    VIR_FREE(parent);
+    xmlXPathFreeContext(ctxt);
+    if (xmldoc)
+        xmlFreeDoc(xmldoc);
+    VIR_FREE(xml);
+    if (snapshot)
+        virDomainSnapshotFree(snapshot);
+    if (dom)
+        virDomainFree(dom);
+
+    return ret;
+}
+
 /*
  * "snapshot-revert" command
  */
@@ -12754,6 +12830,8 @@ static const vshCmdDef snapshotCmds[] = {
      info_snapshot_dumpxml, 0},
     {"snapshot-list", cmdSnapshotList, opts_snapshot_list,
      info_snapshot_list, 0},
+    {"snapshot-parent", cmdSnapshotParent, opts_snapshot_parent,
+     info_snapshot_parent, 0},
     {"snapshot-revert", cmdDomainSnapshotRevert, opts_snapshot_revert,
      info_snapshot_revert, 0},
     {NULL, NULL, NULL, NULL, 0}
index a6af1e6b2d3df433187a02925ed0c3fd851d02e4..1893c23619227d02dfb3f78d0910f0c4179e7bb1 100644 (file)
@@ -1583,6 +1583,10 @@ List all of the available snapshots for the given domain.
 
 Output the snapshot XML for the domain's snapshot named I<snapshot>.
 
+=item B<snapshot-parent> I<domain> I<snapshot>
+
+Output the name of the parent snapshot for the given I<snapshot>, if any.
+
 =item B<snapshot-revert> I<domain> I<snapshot>
 
 Revert the given domain to the snapshot specified by I<snapshot>.  Be aware