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[] = { static const vshCmdOptDef opts_snapshot_list[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")}, {"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} {NULL, 0, 0, NULL}
}; };
@ -12202,6 +12203,9 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
{ {
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
bool ret = false; bool ret = false;
unsigned int flags = 0;
int parent_filter = 0; /* 0 for no parent information needed,
1 for parent column */
int numsnaps; int numsnaps;
char **names = NULL; char **names = NULL;
int actual = 0; int actual = 0;
@ -12211,11 +12215,16 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
char *doc = NULL; char *doc = NULL;
virDomainSnapshotPtr snapshot = NULL; virDomainSnapshotPtr snapshot = NULL;
char *state = NULL; char *state = NULL;
char *parent = NULL;
long long creation_longlong; long long creation_longlong;
time_t creation_time_t; time_t creation_time_t;
char timestr[100]; char timestr[100];
struct tm time_info; struct tm time_info;
if (vshCommandOptBool(cmd, "parent")) {
parent_filter = 1;
}
if (!vshConnectionUsability(ctl, ctl->conn)) if (!vshConnectionUsability(ctl, ctl->conn))
goto cleanup; goto cleanup;
@ -12223,19 +12232,25 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
if (dom == NULL) if (dom == NULL)
goto cleanup; goto cleanup;
numsnaps = virDomainSnapshotNum(dom, 0); numsnaps = virDomainSnapshotNum(dom, flags);
if (numsnaps < 0) if (numsnaps < 0)
goto cleanup; goto cleanup;
vshPrintExtra(ctl, " %-20s %-25s %s\n", _("Name"), _("Creation Time"), _("State")); if (parent_filter > 0)
vshPrintExtra(ctl, "---------------------------------------------------\n"); 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 (numsnaps) {
if (VIR_ALLOC_N(names, numsnaps) < 0) if (VIR_ALLOC_N(names, numsnaps) < 0)
goto cleanup; goto cleanup;
actual = virDomainSnapshotListNames(dom, names, numsnaps, 0); actual = virDomainSnapshotListNames(dom, names, numsnaps, flags);
if (actual < 0) if (actual < 0)
goto cleanup; goto cleanup;
@ -12243,6 +12258,7 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
for (i = 0; i < actual; i++) { for (i = 0; i < actual; i++) {
/* free up memory from previous iterations of the loop */ /* free up memory from previous iterations of the loop */
VIR_FREE(parent);
VIR_FREE(state); VIR_FREE(state);
if (snapshot) if (snapshot)
virDomainSnapshotFree(snapshot); virDomainSnapshotFree(snapshot);
@ -12262,6 +12278,11 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
if (!xml) if (!xml)
continue; continue;
if (parent_filter) {
parent = virXPathString("string(/domainsnapshot/parent/name)",
ctxt);
}
state = virXPathString("string(/domainsnapshot/state)", ctxt); state = virXPathString("string(/domainsnapshot/state)", ctxt);
if (state == NULL) if (state == NULL)
continue; continue;
@ -12274,8 +12295,13 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
continue; continue;
} }
localtime_r(&creation_time_t, &time_info); 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);
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); vshPrint(ctl, " %-20s %-25s %s\n", names[i], timestr, state);
} }
} }
@ -12284,6 +12310,7 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
cleanup: cleanup:
/* this frees up memory from the last iteration of the loop */ /* this frees up memory from the last iteration of the loop */
VIR_FREE(parent);
VIR_FREE(state); VIR_FREE(state);
if (snapshot) if (snapshot)
virDomainSnapshotFree(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 If I<--name> is specified, just list the snapshot name instead of the
full xml. 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. 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> =item B<snapshot-dumpxml> I<domain> I<snapshot>
Output the snapshot XML for the domain's snapshot named I<snapshot>. Output the snapshot XML for the domain's snapshot named I<snapshot>.