snapshot: add snapshot-list --parent to virsh

Even though I recently added 'virsh snapshot-parent', doing it one
snapshot at a time is painful, so make it possible to expand the
snapshot-list table at once.

* tools/virsh.c (cmdSnapshotList): Add --parent.
* tools/virsh.pod (snapshot-list): Document it.
This commit is contained in:
Eric Blake 2011-08-16 13:34:22 -06:00
parent 7dc44eb059
commit d4a965c6a8
2 changed files with 37 additions and 7 deletions

View File

@ -12194,6 +12194,7 @@ static const vshCmdInfo info_snapshot_list[] = {
static const vshCmdOptDef opts_snapshot_list[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
{"parent", VSH_OT_BOOL, 0, N_("add a column showing parent snapshot")},
{NULL, 0, 0, NULL}
};
@ -12202,6 +12203,9 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
{
virDomainPtr dom = NULL;
bool ret = false;
unsigned int flags = 0;
int parent_filter = 0; /* 0 for no parent information needed,
1 for parent column */
int numsnaps;
char **names = NULL;
int actual = 0;
@ -12211,11 +12215,16 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
char *doc = NULL;
virDomainSnapshotPtr snapshot = NULL;
char *state = NULL;
char *parent = NULL;
long long creation_longlong;
time_t creation_time_t;
char timestr[100];
struct tm time_info;
if (vshCommandOptBool(cmd, "parent")) {
parent_filter = 1;
}
if (!vshConnectionUsability(ctl, ctl->conn))
goto cleanup;
@ -12223,19 +12232,25 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
if (dom == NULL)
goto cleanup;
numsnaps = virDomainSnapshotNum(dom, 0);
numsnaps = virDomainSnapshotNum(dom, flags);
if (numsnaps < 0)
goto cleanup;
vshPrintExtra(ctl, " %-20s %-25s %s\n", _("Name"), _("Creation Time"), _("State"));
vshPrintExtra(ctl, "---------------------------------------------------\n");
if (parent_filter > 0)
vshPrintExtra(ctl, " %-20s %-25s %-15s %s",
_("Name"), _("Creation Time"), _("State"), _("Parent"));
else
vshPrintExtra(ctl, " %-20s %-25s %s",
_("Name"), _("Creation Time"), _("State"));
vshPrintExtra(ctl, "\n\
------------------------------------------------------------\n");
if (numsnaps) {
if (VIR_ALLOC_N(names, numsnaps) < 0)
goto cleanup;
actual = virDomainSnapshotListNames(dom, names, numsnaps, 0);
actual = virDomainSnapshotListNames(dom, names, numsnaps, flags);
if (actual < 0)
goto cleanup;
@ -12243,6 +12258,7 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
for (i = 0; i < actual; i++) {
/* free up memory from previous iterations of the loop */
VIR_FREE(parent);
VIR_FREE(state);
if (snapshot)
virDomainSnapshotFree(snapshot);
@ -12262,6 +12278,11 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
if (!xml)
continue;
if (parent_filter) {
parent = virXPathString("string(/domainsnapshot/parent/name)",
ctxt);
}
state = virXPathString("string(/domainsnapshot/state)", ctxt);
if (state == NULL)
continue;
@ -12274,9 +12295,14 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
continue;
}
localtime_r(&creation_time_t, &time_info);
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S %z", &time_info);
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S %z",
&time_info);
vshPrint(ctl, " %-20s %-25s %s\n", names[i], timestr, state);
if (parent)
vshPrint(ctl, " %-20s %-25s %-15s %s\n",
names[i], timestr, state, parent);
else
vshPrint(ctl, " %-20s %-25s %s\n", names[i], timestr, state);
}
}
@ -12284,6 +12310,7 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
cleanup:
/* this frees up memory from the last iteration of the loop */
VIR_FREE(parent);
VIR_FREE(state);
if (snapshot)
virDomainSnapshotFree(snapshot);

View File

@ -1707,10 +1707,13 @@ Output the snapshot XML for the domain's current snapshot (if any).
If I<--name> is specified, just list the snapshot name instead of the
full xml.
=item B<snapshot-list> I<domain>
=item B<snapshot-list> I<domain> [I<--parent>]
List all of the available snapshots for the given domain.
If I<--parent> is specified, add a column to the output table giving
the name of the parent of each snapshot.
=item B<snapshot-dumpxml> I<domain> I<snapshot>
Output the snapshot XML for the domain's snapshot named I<snapshot>.