mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-09 06:25:19 +00:00
snapshot: refactor virsh snapshot creation
The next patch will make snapshot creation more complex, so it's better to avoid repetition of the complexity. * tools/virsh.c (vshSnapshotCreate): New helper function. (cmdSnapshotCreate, cmdSnapshotCreateAs): Use it.
This commit is contained in:
parent
af65695af0
commit
90ec08ed73
119
tools/virsh.c
119
tools/virsh.c
@ -11915,6 +11915,54 @@ cmdQuit(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Helper for snapshot-create and snapshot-create-as */
|
||||||
|
static bool
|
||||||
|
vshSnapshotCreate(vshControl *ctl, virDomainPtr dom, const char *buffer,
|
||||||
|
unsigned int flags, const char *from)
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
virDomainSnapshotPtr snapshot;
|
||||||
|
char *doc = NULL;
|
||||||
|
xmlDocPtr xml = NULL;
|
||||||
|
xmlXPathContextPtr ctxt = NULL;
|
||||||
|
char *name = NULL;
|
||||||
|
|
||||||
|
snapshot = virDomainSnapshotCreateXML(dom, buffer, flags);
|
||||||
|
if (snapshot == NULL)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
doc = virDomainSnapshotGetXMLDesc(snapshot, 0);
|
||||||
|
if (!doc)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
xml = virXMLParseStringCtxt(doc, "domainsnapshot.xml", &ctxt);
|
||||||
|
if (!xml)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
name = virXPathString("string(/domainsnapshot/name)", ctxt);
|
||||||
|
if (!name) {
|
||||||
|
vshError(ctl, "%s",
|
||||||
|
_("Could not find 'name' element in domain snapshot XML"));
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (from)
|
||||||
|
vshPrint(ctl, _("Domain snapshot %s created from '%s'"), name, from);
|
||||||
|
else
|
||||||
|
vshPrint(ctl, _("Domain snapshot %s created"), name);
|
||||||
|
|
||||||
|
ret = true;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(name);
|
||||||
|
xmlXPathFreeContext(ctxt);
|
||||||
|
xmlFreeDoc(xml);
|
||||||
|
if (snapshot)
|
||||||
|
virDomainSnapshotFree(snapshot);
|
||||||
|
VIR_FREE(doc);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* "snapshot-create" command
|
* "snapshot-create" command
|
||||||
*/
|
*/
|
||||||
@ -11937,11 +11985,6 @@ cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd)
|
|||||||
bool ret = false;
|
bool ret = false;
|
||||||
const char *from = NULL;
|
const char *from = NULL;
|
||||||
char *buffer = NULL;
|
char *buffer = NULL;
|
||||||
virDomainSnapshotPtr snapshot = NULL;
|
|
||||||
xmlDocPtr xml = NULL;
|
|
||||||
xmlXPathContextPtr ctxt = NULL;
|
|
||||||
char *doc = NULL;
|
|
||||||
char *name = NULL;
|
|
||||||
|
|
||||||
if (!vshConnectionUsability(ctl, ctl->conn))
|
if (!vshConnectionUsability(ctl, ctl->conn))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -11967,39 +12010,9 @@ cmdSnapshotCreate(vshControl *ctl, const vshCmd *cmd)
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
snapshot = virDomainSnapshotCreateXML(dom, buffer, 0);
|
ret = vshSnapshotCreate(ctl, dom, buffer, 0, from);
|
||||||
if (snapshot == NULL)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
doc = virDomainSnapshotGetXMLDesc(snapshot, 0);
|
|
||||||
if (!doc)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
xml = virXMLParseStringCtxt(doc, "domainsnapshot.xml", &ctxt);
|
|
||||||
if (!xml)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
name = virXPathString("string(/domainsnapshot/name)", ctxt);
|
|
||||||
if (!name) {
|
|
||||||
vshError(ctl, "%s",
|
|
||||||
_("Could not find 'name' element in domain snapshot XML"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
vshPrint(ctl, _("Domain snapshot %s created"), name);
|
|
||||||
if (from)
|
|
||||||
vshPrint(ctl, _(" from '%s'"), from);
|
|
||||||
vshPrint(ctl, "\n");
|
|
||||||
|
|
||||||
ret = true;
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FREE(name);
|
|
||||||
xmlXPathFreeContext(ctxt);
|
|
||||||
xmlFreeDoc(xml);
|
|
||||||
if (snapshot)
|
|
||||||
virDomainSnapshotFree(snapshot);
|
|
||||||
VIR_FREE(doc);
|
|
||||||
VIR_FREE(buffer);
|
VIR_FREE(buffer);
|
||||||
if (dom)
|
if (dom)
|
||||||
virDomainFree(dom);
|
virDomainFree(dom);
|
||||||
@ -12030,13 +12043,8 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd)
|
|||||||
virDomainPtr dom = NULL;
|
virDomainPtr dom = NULL;
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
char *buffer = NULL;
|
char *buffer = NULL;
|
||||||
virDomainSnapshotPtr snapshot = NULL;
|
|
||||||
xmlDocPtr xml = NULL;
|
|
||||||
xmlXPathContextPtr ctxt = NULL;
|
|
||||||
char *doc = NULL;
|
|
||||||
const char *name = NULL;
|
const char *name = NULL;
|
||||||
const char *desc = NULL;
|
const char *desc = NULL;
|
||||||
char *parsed_name = NULL;
|
|
||||||
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
|
|
||||||
if (!vshConnectionUsability(ctl, ctl->conn))
|
if (!vshConnectionUsability(ctl, ctl->conn))
|
||||||
@ -12071,36 +12079,9 @@ cmdSnapshotCreateAs(vshControl *ctl, const vshCmd *cmd)
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
snapshot = virDomainSnapshotCreateXML(dom, buffer, 0);
|
ret = vshSnapshotCreate(ctl, dom, buffer, 0, NULL);
|
||||||
if (snapshot == NULL)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
doc = virDomainSnapshotGetXMLDesc(snapshot, 0);
|
|
||||||
if (!doc)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
xml = virXMLParseStringCtxt(doc, "domainsnapshot.xml", &ctxt);
|
|
||||||
if (!xml)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
parsed_name = virXPathString("string(/domainsnapshot/name)", ctxt);
|
|
||||||
if (!parsed_name) {
|
|
||||||
vshError(ctl, "%s",
|
|
||||||
_("Could not find 'name' element in domain snapshot XML"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
vshPrint(ctl, _("Domain snapshot %s created\n"), name ? name : parsed_name);
|
|
||||||
|
|
||||||
ret = true;
|
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FREE(parsed_name);
|
|
||||||
xmlXPathFreeContext(ctxt);
|
|
||||||
xmlFreeDoc(xml);
|
|
||||||
if (snapshot)
|
|
||||||
virDomainSnapshotFree(snapshot);
|
|
||||||
VIR_FREE(doc);
|
|
||||||
VIR_FREE(buffer);
|
VIR_FREE(buffer);
|
||||||
if (dom)
|
if (dom)
|
||||||
virDomainFree(dom);
|
virDomainFree(dom);
|
||||||
|
Loading…
Reference in New Issue
Block a user