[libvirt] [PATCH v10 00/19] Checkpoint APIs (incremental backup saga)

This is a subset of v10 of incremental backup, fixing Peter's review comments from v9 (and even some from v8 that I had missed earlier). There's still a lot more rebasing churn to resolve in the backup portion of the series before I can post a full backup-v10 label. There's still a decision to make: do we want this series in 5.6 (possibly with the addition of just patch 2/10 at [1] to introduce the backup API to make it possible for downstream to backport features without bumping .so)? This series has checkpoint support for test and qemu drivers, and I think is probably clean enough to finally satisfy everything Peter has been pointing out; but while the backup API itself seems reasonable, the qemu implementation will likely miss 5.6 (as that half of the v9 posting was further behind, and still has a lot of rebase churn to resolve). Or do we delay the checkpoint API to 5.7, to only go in with backup API? There's also still an outstanding question of whether the backup API needs a tweak to use 'const char *' instead of 'int' for the job identifier, given that Peter has a proposal for overhauling the representation of libvirt jobs. [1] https://www.redhat.com/archives/libvir-list/2019-July/msg00330.html Here's a comparison of these patches to v9: - rebase on top of master - use more VIR_AUTOFREE and friends (but probably still missed some spots where it would be worthwhile) - split several patches - add in a new patch to ensure the node names are learned at domain startup (but I didn't play enough with hotplug to see if that also has problems); necessary in order to... - get rid of detecting node names in checkpoint code - completely got rid of the notion of a current checkpoint in the public API (the internal code still has one, but solely to track which checkpoint becomes the parent of a new one to maintain a linear chain; when loading checkpoints on libvirtd restart, there should be at most one leaf node which becomes the internal current node) - improved code for handling partial checkpoints (the code now correctly handles: c1: disks A, B; c2: disks B, C; c3: disks A, B) 001/19:[down] 'snapshot: Don't leak moment obj list metaroot to callers' 002/19:[down] 'snapshot: Saner error message for duplicate create' 003/19:[down] 'snapshot: Documentation and comment improvements' 004/19:[down] 'backup: qemu: Detect node names at domain startup' 005/19:[0101] [FC] 'backup: Document new XML for checkpoints' 006/19:[0031] [FC] 'backup: Introduce virDomainCheckpoint APIs' 007/19:[0051] [FC] 'backup: Document nuances between different state capture APIs' 008/19:[0189] [FC] 'backup: Parse and output checkpoint XML' 009/19:[0066] [FC] 'backup: Allow for lists of checkpoint objects' 010/19:[----] [--] 'backup: Add new domain:checkpoint access control' 011/19:[----] [--] 'backup: Implement checkpoint APIs for remote driver' 012/19:[0161] [FC] 'backup: Implement virsh support for checkpoints' 013/19:[0047] [FC] 'backup: test: Implement metadata tracking for checkpoint APIs' 014/19:[0044] [FC] 'backup: Add virsh-checkpoints test' 015/19:[down] 'backup: qemu: Add directory for tracking checkpoints' 016/19:[0193] [FC] 'backup: qemu: Implement metadata tracking for checkpoint APIs' 017/19:[down] 'backup: qemu: Add helper API for looking up node name' 018/19:[0119] [FC] 'backup: Wire up qemu checkpoint commands over QMP' 019/19:[down] 'backup: Prevent snapshots and checkpoints at same time' Eric Blake (19): snapshot: Don't leak moment obj list metaroot to callers snapshot: Saner error message for duplicate create snapshot: Documentation and comment improvements backup: qemu: Detect node names at domain startup backup: Document new XML for checkpoints backup: Introduce virDomainCheckpoint APIs backup: Document nuances between different state capture APIs backup: Parse and output checkpoint XML backup: Allow for lists of checkpoint objects backup: Add new domain:checkpoint access control backup: Implement checkpoint APIs for remote driver backup: Implement virsh support for checkpoints backup: test: Implement metadata tracking for checkpoint APIs backup: Add virsh-checkpoints test backup: qemu: Add directory for tracking checkpoints backup: qemu: Implement metadata tracking for checkpoint APIs backup: qemu: Add helper API for looking up node name backup: Wire up qemu checkpoint commands over QMP backup: Prevent snapshots and checkpoints at same time include/libvirt/libvirt-domain-checkpoint.h | 137 ++ include/libvirt/libvirt-domain.h | 6 + include/libvirt/libvirt.h | 5 +- src/access/viraccessperm.h | 6 + src/conf/checkpoint_conf.h | 91 ++ src/conf/domain_conf.h | 2 + src/conf/virconftypes.h | 9 + src/conf/virdomaincheckpointobjlist.h | 72 + src/conf/virdomainmomentobjlist.h | 10 +- src/conf/virdomainobjlist.h | 7 +- src/conf/virdomainsnapshotobjlist.h | 2 + src/driver-hypervisor.h | 38 + src/qemu/qemu_conf.h | 2 + src/qemu/qemu_domain.h | 18 + tools/virsh-checkpoint.h | 26 + tools/virsh-completer.h | 4 + tools/virsh-util.h | 3 + tools/virsh.h | 1 + docs/Makefile.am | 3 + docs/apibuild.py | 2 + docs/docs.html.in | 4 +- docs/format.html.in | 1 + docs/formatcheckpoint.html.in | 196 +++ docs/formatsnapshot.html.in | 14 +- docs/index.html.in | 3 +- docs/kbase.html.in | 5 + docs/kbase/domainstatecapture.html.in | 303 +++++ docs/schemas/domaincheckpoint.rng | 94 ++ libvirt.spec.in | 2 + mingw-libvirt.spec.in | 4 + po/POTFILES | 3 + src/Makefile.am | 2 + src/access/viraccessperm.c | 3 +- src/conf/Makefile.inc.am | 4 + src/conf/checkpoint_conf.c | 597 +++++++++ src/conf/domain_conf.c | 6 + src/conf/virdomaincheckpointobjlist.c | 243 ++++ src/conf/virdomainmomentobjlist.c | 55 +- src/conf/virdomainobjlist.c | 11 + src/conf/virdomainsnapshotobjlist.c | 9 + src/libvirt-domain-checkpoint.c | 567 ++++++++ src/libvirt-domain.c | 19 +- src/libvirt_private.syms | 29 +- src/libvirt_public.syms | 16 + src/qemu/qemu_conf.c | 5 + src/qemu/qemu_domain.c | 190 +++ src/qemu/qemu_driver.c | 731 ++++++++++- src/qemu/qemu_process.c | 5 + src/remote/remote_daemon_dispatch.c | 20 + src/remote/remote_driver.c | 24 +- src/remote/remote_protocol.x | 123 +- src/remote_protocol-structs | 69 + src/rpc/gendispatch.pl | 32 +- src/test/test_driver.c | 386 +++++- tests/Makefile.am | 12 +- .../disk-default.xml | 7 + .../disk-invalid.xml | 8 + tests/qemudomaincheckpointxml2xmlin/empty.xml | 1 + .../name-invalid.xml | 4 + .../qemudomaincheckpointxml2xmlin/sample.xml | 7 + tests/qemudomaincheckpointxml2xmlin/size.xml | 4 + .../disk-default.xml | 11 + .../qemudomaincheckpointxml2xmlout/empty.xml | 7 + .../internal-active-invalid.xml | 53 + .../internal-inactive-invalid.xml | 53 + .../redefine.xml | 63 + .../qemudomaincheckpointxml2xmlout/sample.xml | 12 + tests/qemudomaincheckpointxml2xmlout/size.xml | 11 + tests/qemudomaincheckpointxml2xmltest.c | 213 +++ tests/virschematest.c | 2 + tests/virsh-checkpoint | 174 +++ tests/virsh-snapshot | 15 +- tools/Makefile.am | 1 + tools/virsh-checkpoint.c | 1153 +++++++++++++++++ tools/virsh-completer.c | 51 + tools/virsh-domain-monitor.c | 23 + tools/virsh-domain.c | 7 + tools/virsh-util.c | 11 + tools/virsh.c | 2 + tools/virsh.pod | 201 ++- 80 files changed, 6262 insertions(+), 63 deletions(-) create mode 100644 include/libvirt/libvirt-domain-checkpoint.h create mode 100644 src/conf/checkpoint_conf.h create mode 100644 src/conf/virdomaincheckpointobjlist.h create mode 100644 tools/virsh-checkpoint.h create mode 100644 docs/formatcheckpoint.html.in create mode 100644 docs/kbase/domainstatecapture.html.in create mode 100644 docs/schemas/domaincheckpoint.rng create mode 100644 src/conf/checkpoint_conf.c create mode 100644 src/conf/virdomaincheckpointobjlist.c create mode 100644 src/libvirt-domain-checkpoint.c create mode 100644 tests/qemudomaincheckpointxml2xmlin/disk-default.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/disk-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/empty.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/name-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/sample.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/size.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/disk-default.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/empty.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/redefine.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/sample.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/size.xml create mode 100644 tests/qemudomaincheckpointxml2xmltest.c create mode 100755 tests/virsh-checkpoint create mode 100644 tools/virsh-checkpoint.c -- 2.20.1

virDomainSnapshotFindByName(list, NULL) should return NULL, rather than the internal-use-only metaroot. Most existing callers pass in a non-NULL name; the few that don't are immediately calling virDomainMomentSetParent (which indeed needs the metaroot if the parent name is NULL); but as the leak is ugly, it is worth instead making virDomainMomentSetParent static and adding a new function for resolving the link of a brand new moment within its list (and where we are either requesting NULL for a new root moment, or requesting the name of the current moment, and therefore the new function does not need to return failure, but merely warn if future code breaks our assumptions). Missed when commit 02c4e24d refactored things to attempt to remove direct metaroot manipulations out of the qemu and test drivers into internal-only details, and made more obvious when commit dc8d3dc6 factored it out into a separate file. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/virdomainmomentobjlist.h | 4 ++-- src/conf/virdomainsnapshotobjlist.h | 2 ++ src/conf/virdomainmomentobjlist.c | 34 +++++++++++++++++++++++++---- src/conf/virdomainsnapshotobjlist.c | 9 ++++++++ src/libvirt_private.syms | 2 +- src/qemu/qemu_driver.c | 5 +---- src/test/test_driver.c | 5 +---- 7 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/conf/virdomainmomentobjlist.h b/src/conf/virdomainmomentobjlist.h index 4067e928f4..897d8d54d1 100644 --- a/src/conf/virdomainmomentobjlist.h +++ b/src/conf/virdomainmomentobjlist.h @@ -60,8 +60,8 @@ void virDomainMomentDropParent(virDomainMomentObjPtr moment); void virDomainMomentDropChildren(virDomainMomentObjPtr moment); void virDomainMomentMoveChildren(virDomainMomentObjPtr from, virDomainMomentObjPtr to); -void virDomainMomentSetParent(virDomainMomentObjPtr moment, - virDomainMomentObjPtr parent); +void virDomainMomentLinkParent(virDomainMomentObjListPtr moments, + virDomainMomentObjPtr moment); virDomainMomentObjListPtr virDomainMomentObjListNew(void); void virDomainMomentObjListFree(virDomainMomentObjListPtr moments); diff --git a/src/conf/virdomainsnapshotobjlist.h b/src/conf/virdomainsnapshotobjlist.h index fed8d22bc8..b8c80b39ed 100644 --- a/src/conf/virdomainsnapshotobjlist.h +++ b/src/conf/virdomainsnapshotobjlist.h @@ -51,6 +51,8 @@ void virDomainSnapshotObjListRemoveAll(virDomainSnapshotObjListPtr snapshots); int virDomainSnapshotForEach(virDomainSnapshotObjListPtr snapshots, virHashIterator iter, void *data); +void virDomainSnapshotLinkParent(virDomainSnapshotObjListPtr snapshots, + virDomainMomentObjPtr moment); int virDomainSnapshotUpdateRelations(virDomainSnapshotObjListPtr snapshots); int virDomainSnapshotCheckCycles(virDomainSnapshotObjListPtr snapshots, virDomainSnapshotDefPtr def, diff --git a/src/conf/virdomainmomentobjlist.c b/src/conf/virdomainmomentobjlist.c index 7cb1745525..5c147bdbbf 100644 --- a/src/conf/virdomainmomentobjlist.c +++ b/src/conf/virdomainmomentobjlist.c @@ -151,7 +151,7 @@ virDomainMomentDropChildren(virDomainMomentObjPtr moment) /* Add @moment to @parent's list of children. */ -void +static void virDomainMomentSetParent(virDomainMomentObjPtr moment, virDomainMomentObjPtr parent) { @@ -162,6 +162,27 @@ virDomainMomentSetParent(virDomainMomentObjPtr moment, } +/* Add @moment to the appropriate parent's list of children. The + * caller must ensure that moment->def->parent_name is either NULL + * (for a new root) or set to an existing moment already in the + * list. */ +void +virDomainMomentLinkParent(virDomainMomentObjListPtr moments, + virDomainMomentObjPtr moment) +{ + virDomainMomentObjPtr parent; + + parent = virDomainMomentFindByName(moments, moment->def->parent_name); + if (!parent) { + parent = &moments->metaroot; + if (moment->def->parent_name) + VIR_WARN("moment %s lacks parent %s", moment->def->name, + moment->def->parent_name); + } + virDomainMomentSetParent(moment, parent); +} + + /* Take all children of @from and convert them into children of @to. */ void virDomainMomentMoveChildren(virDomainMomentObjPtr from, @@ -390,7 +411,9 @@ virDomainMomentObjPtr virDomainMomentFindByName(virDomainMomentObjListPtr moments, const char *name) { - return name ? virHashLookup(moments->objs, name) : &moments->metaroot; + if (name) + return virHashLookup(moments->objs, name); + return NULL; } @@ -484,9 +507,12 @@ virDomainMomentSetRelations(void *payload, parent = virDomainMomentFindByName(curr->moments, obj->def->parent_name); if (!parent) { - curr->err = -1; parent = &curr->moments->metaroot; - VIR_WARN("moment %s lacks parent", obj->def->name); + if (obj->def->parent_name) { + curr->err = -1; + VIR_WARN("moment %s lacks parent %s", obj->def->name, + obj->def->parent_name); + } } else { tmp = parent; while (tmp && tmp->def) { diff --git a/src/conf/virdomainsnapshotobjlist.c b/src/conf/virdomainsnapshotobjlist.c index 99bc4bb0c5..95622f0ba7 100644 --- a/src/conf/virdomainsnapshotobjlist.c +++ b/src/conf/virdomainsnapshotobjlist.c @@ -223,6 +223,15 @@ virDomainSnapshotForEach(virDomainSnapshotObjListPtr snapshots, } +/* Populate parent link of a given snapshot. */ +void +virDomainSnapshotLinkParent(virDomainSnapshotObjListPtr snapshots, + virDomainMomentObjPtr snap) +{ + return virDomainMomentLinkParent(snapshots->base, snap); +} + + /* Populate parent link and child count of all snapshots, with all * assigned defs having relations starting as 0/NULL. Return 0 on * success, -1 if a parent is missing or if a circular relationship diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index dff97bd82a..ff5a77b0e2 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -978,7 +978,6 @@ virDomainMomentDropParent; virDomainMomentForEachChild; virDomainMomentForEachDescendant; virDomainMomentMoveChildren; -virDomainMomentSetParent; # conf/virdomainobjlist.h @@ -1007,6 +1006,7 @@ virDomainSnapshotFindByName; virDomainSnapshotForEach; virDomainSnapshotGetCurrent; virDomainSnapshotGetCurrentName; +virDomainSnapshotLinkParent; virDomainSnapshotObjListFree; virDomainSnapshotObjListGetNames; virDomainSnapshotObjListNew; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 482f915b67..065e0a1bd8 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -15540,7 +15540,6 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain, bool update_current = true; bool redefine = flags & VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE; unsigned int parse_flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS; - virDomainMomentObjPtr other = NULL; int align_location = VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL; bool align_match = true; virQEMUDriverConfigPtr cfg = NULL; @@ -15807,9 +15806,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain, snap->def->name); virDomainSnapshotObjListRemove(vm->snapshots, snap); } else { - other = virDomainSnapshotFindByName(vm->snapshots, - snap->def->parent_name); - virDomainMomentSetParent(snap, other); + virDomainSnapshotLinkParent(vm->snapshots, snap); } } else if (snap) { virDomainSnapshotObjListRemove(vm->snapshots, snap); diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 2e33a9dd55..d96dd638e7 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -7361,12 +7361,9 @@ testDomainSnapshotCreateXML(virDomainPtr domain, cleanup: if (vm) { if (snapshot) { - virDomainMomentObjPtr other; if (update_current) virDomainSnapshotSetCurrent(vm->snapshots, snap); - other = virDomainSnapshotFindByName(vm->snapshots, - snap->def->parent_name); - virDomainMomentSetParent(snap, other); + virDomainSnapshotLinkParent(vm->snapshots, snap); } virDomainObjEndAPI(&vm); } -- 2.20.1

On Wed, Jul 24, 2019 at 12:55:51AM -0500, Eric Blake wrote:
virDomainSnapshotFindByName(list, NULL) should return NULL, rather than the internal-use-only metaroot. Most existing callers pass in a non-NULL name; the few that don't are immediately calling virDomainMomentSetParent (which indeed needs the metaroot if the parent name is NULL); but as the leak is ugly, it is worth instead making virDomainMomentSetParent static and adding a new function for resolving the link of a brand new moment within its list (and where we are either requesting NULL for a new root moment, or requesting the name of the current moment, and therefore the new function does not need to return failure, but merely warn if future code breaks our assumptions).
Missed when commit 02c4e24d refactored things to attempt to remove direct metaroot manipulations out of the qemu and test drivers into internal-only details, and made more obvious when commit dc8d3dc6 factored it out into a separate file.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/virdomainmomentobjlist.h | 4 ++-- src/conf/virdomainsnapshotobjlist.h | 2 ++ src/conf/virdomainmomentobjlist.c | 34 +++++++++++++++++++++++++---- src/conf/virdomainsnapshotobjlist.c | 9 ++++++++ src/libvirt_private.syms | 2 +- src/qemu/qemu_driver.c | 5 +---- src/test/test_driver.c | 5 +---- 7 files changed, 46 insertions(+), 15 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Any message that is easy to trigger (as evidenced by the testsuite update) should not use 'internal error' as its category. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/virdomainmomentobjlist.c | 4 ++-- tests/virsh-snapshot | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/conf/virdomainmomentobjlist.c b/src/conf/virdomainmomentobjlist.c index 5c147bdbbf..c8dd8f8e01 100644 --- a/src/conf/virdomainmomentobjlist.c +++ b/src/conf/virdomainmomentobjlist.c @@ -242,8 +242,8 @@ virDomainMomentAssignDef(virDomainMomentObjListPtr moments, virDomainMomentObjPtr moment; if (virHashLookup(moments->objs, def->name) != NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("unexpected domain moment %s already exists"), + virReportError(VIR_ERR_OPERATION_FAILED, + _("domain moment %s already exists"), def->name); return NULL; } diff --git a/tests/virsh-snapshot b/tests/virsh-snapshot index 510904f470..c31da52ef9 100755 --- a/tests/virsh-snapshot +++ b/tests/virsh-snapshot @@ -34,6 +34,7 @@ $abs_top_builddir/tools/virsh --connect test:///default >out 2>err ' # Create a series of snapshots, with names that intentionally sort # differently by topology than by name. Use revert to create fanout. snapshot-create-as test s1 + snapshot-create-as test s1 snapshot-create-as test s3 snapshot-create-as test s2 snapshot-revert test s3 @@ -90,6 +91,7 @@ sed -n '/MarkerB/,/MarkerC/p' < out > s2.xml || fail=1 cat <<\EOF > exp || fail=1 Domain snapshot s1 created + Domain snapshot s3 created Domain snapshot s2 created @@ -171,6 +173,7 @@ EOF compare exp out.cooked || fail=1 cat <<EOF > exp || fail=1 +error: operation failed: domain moment s1 already exists error: marker error: domain 'test' has no current snapshot error: marker -- 2.20.1

On Wed, Jul 24, 2019 at 12:55:52AM -0500, Eric Blake wrote:
Any message that is easy to trigger (as evidenced by the testsuite update) should not use 'internal error' as its category.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/virdomainmomentobjlist.c | 4 ++-- tests/virsh-snapshot | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Changes noticed while copying to similar aspects of checkpoints. Signed-off-by: Eric Blake <eblake@redhat.com> --- docs/formatsnapshot.html.in | 8 +++----- tests/virsh-snapshot | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/formatsnapshot.html.in b/docs/formatsnapshot.html.in index 92cc566467..005800bc00 100644 --- a/docs/formatsnapshot.html.in +++ b/docs/formatsnapshot.html.in @@ -223,11 +223,9 @@ guest. </dd> <dt><code>parent</code></dt> - <dd>An optional readonly representation of the parent of this - snapshot. If present, this element contains exactly one child - element, <code>name</code>. This specifies the name of the - parent snapshot of this snapshot, and is used to represent - trees of snapshots. + <dd>Readonly, present only if this snapshot has a parent. The + parent name is given by the sub-element <code>name</code>. The + parent relationship allows tracking a tree of related snapshots. </dd> <dt><code>domain</code></dt> <dd>A readonly representation of the domain that this snapshot diff --git a/tests/virsh-snapshot b/tests/virsh-snapshot index c31da52ef9..b23d949f62 100755 --- a/tests/virsh-snapshot +++ b/tests/virsh-snapshot @@ -85,7 +85,7 @@ $abs_top_builddir/tools/virsh --connect test:///default >out 2>err ' # and snapshot-list produces timestamps sed 's/ *$//; s/[0-9-]\{10\} [0-9:.]* .[0-9]\{4\}/TIMESTAMP/; /MarkerA/,/MarkerC/d' < out > out.cooked || fail=1 -# Second part holds domain XMLs +# Second part holds domain snapshot XMLs sed -n '/MarkerA/,/MarkerB/p' < out > s3.xml || fail=1 sed -n '/MarkerB/,/MarkerC/p' < out > s2.xml || fail=1 -- 2.20.1

On Wed, Jul 24, 2019 at 12:55:53AM -0500, Eric Blake wrote:
Changes noticed while copying to similar aspects of checkpoints.
Signed-off-by: Eric Blake <eblake@redhat.com> --- docs/formatsnapshot.html.in | 8 +++----- tests/virsh-snapshot | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

If we are using -blockdev, then node names are always available (because we set them). But when not using it, we were scraping node names after reconnecting a new libvirtd to an existing guest (see qemuProcessReconnect), and after any block job that may have changed the set of node names we care about, but forgot to scrape the names when first starting a guest. Do so now in order to allow the checkpoint code to always have access to a node name without having to repeat a node name scrape itself. Signed-off-by: Eric Blake <eblake@redhat.com> --- Grepping for use of qemuBlockNodeNamesDetect, I see that qemuDomainSetBlockThreshold is still scraping names, and that hotplug and cdrom disk changes do not appear to scrape names. Either of these situations could interfere with checkpoints; perhaps they should also be addressed here... --- src/qemu/qemu_process.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 75205bc121..53b8ff36f6 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -7060,6 +7060,8 @@ qemuProcessRefreshState(virQEMUDriverPtr driver, virDomainObjPtr vm, qemuDomainAsyncJob asyncJob) { + qemuDomainObjPrivatePtr priv = vm->privateData; + VIR_DEBUG("Fetching list of active devices"); if (qemuDomainUpdateDeviceList(driver, vm, asyncJob) < 0) return -1; @@ -7075,6 +7077,9 @@ qemuProcessRefreshState(virQEMUDriverPtr driver, VIR_DEBUG("Updating disk data"); if (qemuProcessRefreshDisks(driver, vm, asyncJob) < 0) return -1; + if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV) && + qemuBlockNodeNamesDetect(driver, vm, asyncJob) < 0) + return -1; return 0; } -- 2.20.1

On Wed, Jul 24, 2019 at 12:55:54AM -0500, Eric Blake wrote:
If we are using -blockdev, then node names are always available (because we set them). But when not using it, we were scraping node names after reconnecting a new libvirtd to an existing guest (see qemuProcessReconnect), and after any block job that may have changed the set of node names we care about, but forgot to scrape the names when first starting a guest. Do so now in order to allow the checkpoint code to always have access to a node name without having to repeat a node name scrape itself.
Signed-off-by: Eric Blake <eblake@redhat.com>
---
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Prepare for new checkpoint APIs by describing the XML that will represent a checkpoint. The checkpoint XML is modeled heavily after virDomainSnapshotPtr. See the docs for more details. Add testsuite coverage for some minimal uses of the XML (bare minimum, the sample from html, and a full dumpxml, and some counter-examples that should fail schema validation). Although use of the REDEFINE flag will require the <domain> subelement to be present, it is easier for most of the tests to provide counterpart output produced with the NO_DOMAIN flag (particularly since synthesizing a valid <domain> during testing is not trivial). Signed-off-by: Eric Blake <eblake@redhat.com> --- docs/docs.html.in | 3 +- docs/format.html.in | 1 + docs/formatcheckpoint.html.in | 194 ++++++++++++++++++ docs/index.html.in | 3 +- docs/schemas/domaincheckpoint.rng | 94 +++++++++ libvirt.spec.in | 1 + mingw-libvirt.spec.in | 2 + tests/Makefile.am | 2 + .../disk-default.xml | 7 + .../disk-invalid.xml | 8 + tests/qemudomaincheckpointxml2xmlin/empty.xml | 1 + .../name-invalid.xml | 4 + .../qemudomaincheckpointxml2xmlin/sample.xml | 7 + tests/qemudomaincheckpointxml2xmlin/size.xml | 4 + .../disk-default.xml | 11 + .../qemudomaincheckpointxml2xmlout/empty.xml | 7 + .../redefine.xml | 63 ++++++ .../qemudomaincheckpointxml2xmlout/sample.xml | 12 ++ tests/qemudomaincheckpointxml2xmlout/size.xml | 11 + tests/virschematest.c | 2 + 20 files changed, 435 insertions(+), 2 deletions(-) create mode 100644 docs/formatcheckpoint.html.in create mode 100644 docs/schemas/domaincheckpoint.rng create mode 100644 tests/qemudomaincheckpointxml2xmlin/disk-default.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/disk-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/empty.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/name-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/sample.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/size.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/disk-default.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/empty.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/redefine.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/sample.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/size.xml diff --git a/docs/docs.html.in b/docs/docs.html.in index 9e1ef6be41..74af38002e 100644 --- a/docs/docs.html.in +++ b/docs/docs.html.in @@ -81,7 +81,8 @@ <a href="formatstoragecaps.html">storage pool capabilities</a>, <a href="formatnode.html">node devices</a>, <a href="formatsecret.html">secrets</a>, - <a href="formatsnapshot.html">snapshots</a></dd> + <a href="formatsnapshot.html">snapshots</a>, + <a href="formatcheckpoint.html">checkpoints</a></dd> <dt><a href="uri.html">URI format</a></dt> <dd>The URI formats used for connecting to libvirt</dd> diff --git a/docs/format.html.in b/docs/format.html.in index b488f7b38f..3be2237663 100644 --- a/docs/format.html.in +++ b/docs/format.html.in @@ -26,6 +26,7 @@ <li><a href="formatnode.html">Node devices</a></li> <li><a href="formatsecret.html">Secrets</a></li> <li><a href="formatsnapshot.html">Snapshots</a></li> + <li><a href="formatcheckpoint.html">Checkpoints</a></li> </ul> <h2>Command line validation</h2> diff --git a/docs/formatcheckpoint.html.in b/docs/formatcheckpoint.html.in new file mode 100644 index 0000000000..50dd4d284c --- /dev/null +++ b/docs/formatcheckpoint.html.in @@ -0,0 +1,194 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml"> + <body> + <h1>Checkpoint XML format</h1> + + <ul id="toc"></ul> + + <h2><a id="CheckpointAttributes">Checkpoint XML</a></h2> + + <p> + One method of capturing domain disk backups is via the use of + incremental backups. Right now, incremental backups are only + supported for the QEMU hypervisor when using qcow2 disks at the + active layer; if other disk formats are in use, capturing disk + backups requires different libvirt APIs. + </p> + <p> + Libvirt is able to facilitate incremental backups by tracking + disk checkpoints, which are points in time against which it is + easy to compute which portion of the disk has changed. Given a + full backup (a backup created from the creation of the disk to a + given point in time), coupled with the creation of a disk + checkpoint at that time, and an incremental backup (a backup + created from just the dirty portion of the disk between the + first checkpoint and the second backup operation), it is + possible to do an offline reconstruction of the state of the + disk at the time of the second backup without having to copy as + much data as a second full backup would require. Future API + additions will make it possible to create checkpoints in + conjunction with a backup + via <code>virDomainBackupBegin()</code> or with an external + snapshot via <code>virDomainSnapshotCreateXML2</code>; but for + now, libvirt exposes enough support to create disk checkpoints + independently from a backup operation + via <code>virDomainCheckpointCreateXML()</code> <span class="since">since + 5.6.0</span>. + </p> + <p> + Attributes of libvirt checkpoints are stored as child elements + of the <code>domaincheckpoint</code> element. At checkpoint + creation time, normally only + the <code>name</code>, <code>description</code>, + and <code>disks</code> elements are settable. The rest of the + fields are ignored on creation and will be filled in by libvirt + in for informational purposes + by <code>virDomainCheckpointGetXMLDesc()</code>. However, when + redefining a checkpoint, with + the <code>VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE</code> flag + of <code>virDomainCheckpointCreateXML()</code>, all of the XML + fields described here are relevant on input, even the fields + that are normally described as readonly for output. + </p> + <p> + The top-level <code>domaincheckpoint</code> element may contain + the following elements: + </p> + <dl> + <dt><code>name</code></dt> + <dd>The optional name for this checkpoint. If the name is + omitted, libvirt will create a name based on the time of the + creation. + </dd> + <dt><code>description</code></dt> + <dd>An optional human-readable description of the checkpoint. + If the description is omitted when initially creating the + checkpoint, then this field will be empty. + </dd> + <dt><code>disks</code></dt> + <dd>On input, this is an optional listing of specific + instructions for disk checkpoints; it is needed when making a + checkpoint on only a subset of the disks associated with a + domain. In particular, since QEMU checkpoints require qcow2 + disks, this element may be needed on input for excluding guest + disks that are not in qcow2 format. If the entire element was + omitted on input, then all disks participate in the + checkpoint, otherwise, only the disks explicitly listed which + do not also use <code>checkpoint='no'</code> will + participate. On output, this is the checkpoint state of each + of the domain's disks. + <dl> + <dt><code>disk</code></dt> + <dd>This sub-element describes the checkpoint properties of + a specific disk with the following attributes: + <dl> + <dt><code>name</code></dt> + <dd>A mandatory attribute which must match either + the <code><target dev='name'/></code> or an + unambiguous <code><source file='name'/></code> + of one of + the <a href="formatdomain.html#elementsDisks">disk + devices</a> specified for the domain at the time of + the checkpoint.</dd> + <dt><code>checkpoint</code></dt> + <dd>An optional attribute; possible values + are <code>no</code> when the disk does not participate + in this checkpoint; or <code>bitmap</code> if the disk + will track all changes since the creation of this + checkpoint via a bitmap.</dd> + <dt><code>bitmap</code></dt> + <dd>The attribute <code>bitmap</code> is only valid + if <code>checkpoint='bitmap'</code>; it describes the + name of the tracking bitmap (defaulting to the + checkpoint name).</dd> + <dt><code>size</code></dt> + <dd>The attribute <code>size</code> is ignored on input; + on output, it is only present if + the <code>VIR_DOMAIN_CHECKPOINT_XML_SIZE</code> flag + was used to perform a dynamic query of the estimated + size in bytes of the changes made since the checkpoint + was created.</dd> + </dl> + </dd> + </dl> + </dd> + <dt><code>creationTime</code></dt> + <dd>A readonly representation of the time this checkpoint was + created. The time is specified in seconds since the Epoch, + UTC (i.e. Unix time). + </dd> + <dt><code>parent</code></dt> + <dd>Readonly, present if this checkpoint has a parent. The + parent name is given by the sub-element <code>name</code>. The + parent relationship allows tracking a list of related checkpoints. + </dd> + <dt><code>domain</code></dt> + <dd>A readonly representation of the + inactive <a href="formatdomain.html">domain configuration</a> + at the time the checkpoint was created. This element may be + omitted for output brevity by supplying + the <code>VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN</code> flag, but + the resulting XML is no longer viable for use with + the <code>VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE</code> flag + of <code>virDomainCheckpointCreateXML()</code>. The domain + will have security-sensitive information omitted unless the + flag <code>VIR_DOMAIN_CHECKPOINT_XML_SECURE</code> is provided + on a read-write connection. + </dd> + </dl> + + <h2><a id="example">Examples</a></h2> + + <p>Using this XML to create a checkpoint of just vda on a qemu + domain with two disks and a prior checkpoint:</p> + <pre> +<domaincheckpoint> + <description>Completion of updates after OS install</description> + <disks> + <disk name='vda' checkpoint='bitmap'/> + <disk name='vdb' checkpoint='no'/> + </disks> +</domaincheckpoint></pre> + + <p>will result in XML similar to this from + <code>virDomainCheckpointGetXMLDesc()</code>:</p> + <pre> +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <parent> + <name>1525111885</name> + </parent> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + <disk name='vdb' checkpoint='no'/> + </disks> + <domain type='qemu'> + <name>fedora</name> + <uuid>93a5c045-6457-2c09-e56c-927cdf34e178</uuid> + <memory>1048576</memory> + ... + <devices> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/path/to/file1'/> + <target dev='vda' bus='virtio'/> + </disk> + <disk type='file' device='disk' snapshot='external'> + <driver name='qemu' type='raw'/> + <source file='/path/to/file2'/> + <target dev='vdb' bus='virtio'/> + </disk> + ... + </devices> + </domain> +</domaincheckpoint></pre> + + <p>With that checkpoint created, the qcow2 image is now tracking + all changes that occur in the image since the checkpoint via + the persistent bitmap named <code>1525889631</code>. + </p> + </body> +</html> diff --git a/docs/index.html.in b/docs/index.html.in index 2bd647f8dd..7d0ab650e3 100644 --- a/docs/index.html.in +++ b/docs/index.html.in @@ -58,7 +58,8 @@ <a href="formatstoragecaps.html">storage pool capabilities</a>, <a href="formatnode.html">node devices</a>, <a href="formatsecret.html">secrets</a>, - <a href="formatsnapshot.html">snapshots</a></dd> + <a href="formatsnapshot.html">snapshots</a>, + <a href="formatcheckpoint.html">checkpoints</a></dd> <dt><a href="http://wiki.libvirt.org">Wiki</a></dt> <dd>Read further community contributed content</dd> </dl> diff --git a/docs/schemas/domaincheckpoint.rng b/docs/schemas/domaincheckpoint.rng new file mode 100644 index 0000000000..4399eca7fc --- /dev/null +++ b/docs/schemas/domaincheckpoint.rng @@ -0,0 +1,94 @@ +<?xml version='1.0'?> +<!-- A Relax NG schema for the libvirt domain checkpoint properties XML format --> +<grammar xmlns='http://relaxng.org/ns/structure/1.0' datatypeLibrary='http://www.w3.org/2001/XMLSchema-datatypes'> + <start> + <ref name='domaincheckpoint'/> + </start> + + <include href='domaincommon.rng'/> + + <define name='domaincheckpoint'> + <element name='domaincheckpoint'> + <interleave> + <optional> + <element name='name'> + <ref name='checkpointName'/> + </element> + </optional> + <optional> + <element name='description'> + <text/> + </element> + </optional> + <optional> + <element name='creationTime'> + <ref name='unsignedLong'/> + </element> + </optional> + <optional> + <element name='disks'> + <oneOrMore> + <ref name='diskcheckpoint'/> + </oneOrMore> + </element> + </optional> + <optional> + <!-- Nested grammar ensures that any of our overrides of + storagecommon/domaincommon defines do not conflict + with any domain.rng overrides. --> + <grammar> + <include href='domain.rng'/> + </grammar> + </optional> + <optional> + <element name='parent'> + <element name='name'> + <ref name='checkpointName'/> + </element> + </element> + </optional> + </interleave> + </element> + </define> + + <define name='diskcheckpoint'> + <element name='disk'> + <attribute name='name'> + <choice> + <ref name='diskTarget'/> + <ref name='absFilePath'/> + </choice> + </attribute> + <choice> + <attribute name='checkpoint'> + <value>no</value> + </attribute> + <group> + <optional> + <attribute name='checkpoint'> + <value>bitmap</value> + </attribute> + </optional> + <optional> + <attribute name='bitmap'> + <text/> + </attribute> + </optional> + <optional> + <attribute name='size'> + <ref name='unsignedLong'/> + </attribute> + </optional> + </group> + </choice> + </element> + </define> + + <define name='checkpointName'> + <data type='string'> + <!-- Notably: no leading '.' and no embedded '/' or newline --> + <param name='pattern'>[a-zA-Z0-9_\-][a-zA-Z0-9_\-.]*</param> + </data> + </define> + +</grammar> diff --git a/libvirt.spec.in b/libvirt.spec.in index b13b863928..4dee2d8ac2 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1802,6 +1802,7 @@ exit 0 %{_datadir}/libvirt/schemas/cputypes.rng %{_datadir}/libvirt/schemas/domain.rng %{_datadir}/libvirt/schemas/domaincaps.rng +%{_datadir}/libvirt/schemas/domaincheckpoint.rng %{_datadir}/libvirt/schemas/domaincommon.rng %{_datadir}/libvirt/schemas/domainsnapshot.rng %{_datadir}/libvirt/schemas/interface.rng diff --git a/mingw-libvirt.spec.in b/mingw-libvirt.spec.in index ad6d6e952e..df89d9f8b3 100644 --- a/mingw-libvirt.spec.in +++ b/mingw-libvirt.spec.in @@ -232,6 +232,7 @@ rm -rf $RPM_BUILD_ROOT%{mingw64_libexecdir}/libvirt-guests.sh %{mingw32_datadir}/libvirt/schemas/cputypes.rng %{mingw32_datadir}/libvirt/schemas/domain.rng %{mingw32_datadir}/libvirt/schemas/domaincaps.rng +%{mingw32_datadir}/libvirt/schemas/domaincheckpoint.rng %{mingw32_datadir}/libvirt/schemas/domaincommon.rng %{mingw32_datadir}/libvirt/schemas/domainsnapshot.rng %{mingw32_datadir}/libvirt/schemas/interface.rng @@ -321,6 +322,7 @@ rm -rf $RPM_BUILD_ROOT%{mingw64_libexecdir}/libvirt-guests.sh %{mingw64_datadir}/libvirt/schemas/cputypes.rng %{mingw64_datadir}/libvirt/schemas/domain.rng %{mingw64_datadir}/libvirt/schemas/domaincaps.rng +%{mingw64_datadir}/libvirt/schemas/domaincheckpoint.rng %{mingw64_datadir}/libvirt/schemas/domaincommon.rng %{mingw64_datadir}/libvirt/schemas/domainsnapshot.rng %{mingw64_datadir}/libvirt/schemas/interface.rng diff --git a/tests/Makefile.am b/tests/Makefile.am index f480e68e7d..9e2e57459e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -110,6 +110,8 @@ EXTRA_DIST = \ qemublocktestdata \ qemucapabilitiesdata \ qemucaps2xmloutdata \ + qemudomaincheckpointxml2xmlin \ + qemudomaincheckpointxml2xmlout \ qemudomainsnapshotxml2xmlin \ qemudomainsnapshotxml2xmlout \ qemuhotplugtestcpus \ diff --git a/tests/qemudomaincheckpointxml2xmlin/disk-default.xml b/tests/qemudomaincheckpointxml2xmlin/disk-default.xml new file mode 100644 index 0000000000..706a9a8fd5 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlin/disk-default.xml @@ -0,0 +1,7 @@ +<domaincheckpoint> + <description>Completion of updates after OS install</description> + <disks> + <!-- The default checkpoint='...' is bitmap --> + <disk name='vda'/> + </disks> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlin/disk-invalid.xml b/tests/qemudomaincheckpointxml2xmlin/disk-invalid.xml new file mode 100644 index 0000000000..c57c8f6490 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlin/disk-invalid.xml @@ -0,0 +1,8 @@ +<domaincheckpoint> + <name>bogus</name> + <disks> + <!-- A disk must have a name='...'; ensure that we catch this + thanks to our forced RNG schema validation --> + <disk/> + </disks> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlin/empty.xml b/tests/qemudomaincheckpointxml2xmlin/empty.xml new file mode 100644 index 0000000000..dc36449142 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlin/empty.xml @@ -0,0 +1 @@ +<domaincheckpoint/> diff --git a/tests/qemudomaincheckpointxml2xmlin/name-invalid.xml b/tests/qemudomaincheckpointxml2xmlin/name-invalid.xml new file mode 100644 index 0000000000..b13d7b09e9 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlin/name-invalid.xml @@ -0,0 +1,4 @@ +<domaincheckpoint> + <!-- Our schema prevents names that can't be used as sane file names --> + <name>../escape</name> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlin/sample.xml b/tests/qemudomaincheckpointxml2xmlin/sample.xml new file mode 100644 index 0000000000..70ed964e1e --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlin/sample.xml @@ -0,0 +1,7 @@ +<domaincheckpoint> + <description>Completion of updates after OS install</description> + <disks> + <disk name='vda' checkpoint='bitmap'/> + <disk name='vdb' checkpoint='no'/> + </disks> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlin/size.xml b/tests/qemudomaincheckpointxml2xmlin/size.xml new file mode 100644 index 0000000000..99ce84c7a0 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlin/size.xml @@ -0,0 +1,4 @@ +<domaincheckpoint> + <name>c2</name> + <description>something fun</description> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlout/disk-default.xml b/tests/qemudomaincheckpointxml2xmlout/disk-default.xml new file mode 100644 index 0000000000..ea18252c17 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/disk-default.xml @@ -0,0 +1,11 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <parent> + <name>1525111885</name> + </parent> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + </disks> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlout/empty.xml b/tests/qemudomaincheckpointxml2xmlout/empty.xml new file mode 100644 index 0000000000..6fe6124ce4 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/empty.xml @@ -0,0 +1,7 @@ +<domaincheckpoint> + <name>1525889631</name> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + </disks> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlout/redefine.xml b/tests/qemudomaincheckpointxml2xmlout/redefine.xml new file mode 100644 index 0000000000..823ae1ee2d --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/redefine.xml @@ -0,0 +1,63 @@ +<domaincheckpoint> + <name>c1</name> + <creationTime>1553647812</creationTime> + <disks> + <disk name='hda' checkpoint='no'/> + <disk name='hdc' checkpoint='no'/> + <disk name='vda' checkpoint='bitmap' bitmap='c1'/> + <disk name='vdb' checkpoint='bitmap' bitmap='c1'/> + </disks> + <domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu-system-i686</emulator> + <disk type='block' device='disk'> + <driver name='qemu' type='raw'/> + <source dev='/dev/HostVG/QEMUGuest1'/> + <target dev='hda' bus='ide'/> + <address type='drive' controller='0' bus='0' target='0' unit='0'/> + </disk> + <disk type='block' device='cdrom'> + <driver name='qemu' type='raw'/> + <source dev='/dev/HostVG/QEMUGuest2'/> + <target dev='hdc' bus='ide'/> + <readonly/> + <address type='drive' controller='0' bus='1' target='0' unit='0'/> + </disk> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/data.img'/> + <target dev='vda' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> + </disk> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/logs.img'/> + <target dev='vdb' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> + </disk> + <controller type='usb' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> + </controller> + <controller type='ide' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> + </controller> + <controller type='pci' index='0' model='pci-root'/> + <input type='mouse' bus='ps2'/> + <input type='keyboard' bus='ps2'/> + <memballoon model='none'/> + </devices> + </domain> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlout/sample.xml b/tests/qemudomaincheckpointxml2xmlout/sample.xml new file mode 100644 index 0000000000..91cfac27df --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/sample.xml @@ -0,0 +1,12 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <parent> + <name>1525111885</name> + </parent> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + <disk name='vdb' checkpoint='no'/> + </disks> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlout/size.xml b/tests/qemudomaincheckpointxml2xmlout/size.xml new file mode 100644 index 0000000000..4f11cfa680 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/size.xml @@ -0,0 +1,11 @@ +<domaincheckpoint> + <name>c2</name> + <description>something fun</description> + <parent> + <name>1525111885</name> + </parent> + <creationTime>1553648510</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='c2' size='1048576'/> + </disks> +</domaincheckpoint> diff --git a/tests/virschematest.c b/tests/virschematest.c index 56ee6ec77e..d5a56d3f96 100644 --- a/tests/virschematest.c +++ b/tests/virschematest.c @@ -222,6 +222,8 @@ mymain(void) "genericxml2xmloutdata", "xlconfigdata", "libxlxml2domconfigdata", "qemuhotplugtestdomains"); DO_TEST_DIR("domaincaps.rng", "domaincapsschemadata"); + DO_TEST_DIR("domaincheckpoint.rng", "qemudomaincheckpointxml2xmlin", + "qemudomaincheckpointxml2xmlout"); DO_TEST_DIR("domainsnapshot.rng", "qemudomainsnapshotxml2xmlin", "qemudomainsnapshotxml2xmlout"); DO_TEST_DIR("interface.rng", "interfaceschemadata"); -- 2.20.1

On Wed, Jul 24, 2019 at 12:55:55AM -0500, Eric Blake wrote:
Prepare for new checkpoint APIs by describing the XML that will represent a checkpoint. The checkpoint XML is modeled heavily after virDomainSnapshotPtr. See the docs for more details.
Add testsuite coverage for some minimal uses of the XML (bare minimum, the sample from html, and a full dumpxml, and some counter-examples that should fail schema validation). Although use of the REDEFINE flag will require the <domain> subelement to be present, it is easier for most of the tests to provide counterpart output produced with the NO_DOMAIN flag (particularly since synthesizing a valid <domain> during testing is not trivial).
Signed-off-by: Eric Blake <eblake@redhat.com> --- docs/docs.html.in | 3 +- docs/format.html.in | 1 + docs/formatcheckpoint.html.in | 194 ++++++++++++++++++ docs/index.html.in | 3 +- docs/schemas/domaincheckpoint.rng | 94 +++++++++ libvirt.spec.in | 1 + mingw-libvirt.spec.in | 2 + tests/Makefile.am | 2 + .../disk-default.xml | 7 + .../disk-invalid.xml | 8 + tests/qemudomaincheckpointxml2xmlin/empty.xml | 1 + .../name-invalid.xml | 4 + .../qemudomaincheckpointxml2xmlin/sample.xml | 7 + tests/qemudomaincheckpointxml2xmlin/size.xml | 4 + .../disk-default.xml | 11 + .../qemudomaincheckpointxml2xmlout/empty.xml | 7 + .../redefine.xml | 63 ++++++ .../qemudomaincheckpointxml2xmlout/sample.xml | 12 ++ tests/qemudomaincheckpointxml2xmlout/size.xml | 11 + tests/virschematest.c | 2 + 20 files changed, 435 insertions(+), 2 deletions(-) create mode 100644 docs/formatcheckpoint.html.in create mode 100644 docs/schemas/domaincheckpoint.rng create mode 100644 tests/qemudomaincheckpointxml2xmlin/disk-default.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/disk-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/empty.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/name-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/sample.xml create mode 100644 tests/qemudomaincheckpointxml2xmlin/size.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/disk-default.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/empty.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/redefine.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/sample.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/size.xml
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Introduce a bunch of new public APIs related to backup checkpoints. Checkpoints are modeled heavily after virDomainSnapshotPtr (both represent a point in time of the guest), although a snapshot exists with the intent of rolling back to that state, while a checkpoint exists to make it possible to create an incremental backup at a later time. We may have a future hypervisor that can completely manage checkpoints without libvirt metadata, but the first two planned hypervisors (qemu and test) both always use libvirt for tracking metadata relations between checkpoints, so for now, I've deferred the counterpart of virDomainSnapshotHasMetadata for a separate API addition at a later date if there is ever a need for it. Note that until we allow snapshots and checkpoints to exist simultaneously on the same domain (although the actual prevention of this will be in a separate patch for the sake of an easier revert down the road), that it is not possible to branch out to create more than one checkpoint child to a given parent, although it may become possible later when we revert to a snapshot that coincides with a checkpoint. This also means that for now, the decision of which checkpoint becomes the parent of a newly created one is the only checkpoint with no child (so while there are APIs for dealing with a current snapshot, we do not need those for checkpoints). We may end up exposing a notion of a current checkpoint later, but it's easier to add stuff when proven needed than to blindly support it now and wish we hadn't exposed it. The following map shows the API relations to snapshots, with new APIs on the right: Operate on a domain object to create/redefine a child: virDomainSnapshotCreateXML virDomainCheckpointCreateXML Operate on a child object for lifetime management: virDomainSnapshotDelete virDomainCheckpointDelete virDomainSnapshotFree virDomainCheckpointFree virDomainSnapshotRef virDomainCheckpointRef Operate on a child object to learn more about it: virDomainSnapshotGetXMLDesc virDomainCheckpointGetXMLDesc virDomainSnapshotGetConnect virDomainCheckpointGetConnect virDomainSnapshotGetDomain virDomainCheckpointGetDomain virDomainSnapshotGetName virDomainCheckpiontGetName virDomainSnapshotGetParent virDomainCheckpiontGetParent virDomainSnapshotHasMetadata (deferred for later) virDomainSnapshotIsCurrent (no counterpart, see note above) Operate on a domain object to list all children: virDomainSnapshotNum (no counterparts, these are the old virDomainSnapshotListNames racy interfaces) virDomainSnapshotListAllSnapshots virDomainListAllCheckpoints Operate on a child object to list descendents: virDomainSnapshotNumChildren (no counterparts, these are the old virDomainSnapshotListChildrenNames racy interfaces) virDomainSnapshotListAllChildren virDomainCheckpointListAllChildren Operate on a domain to locate a particular child: virDomainSnapshotLookupByName virDomainCheckpointLookupByName virDomainSnapshotCurrent (no counterpart, see note above) virDomainHasCurrentSnapshot (no counterpart, old racy interface) Operate on a snapshot to roll back to earlier state: virDomainSnapshotRevert (no counterpart, instead checkpoints are used in incremental backups via XML to virDomainBackupBegin) Signed-off-by: Eric Blake <eblake@redhat.com> --- include/libvirt/libvirt-domain-checkpoint.h | 137 +++++ include/libvirt/libvirt-domain.h | 6 + include/libvirt/libvirt.h | 5 +- src/conf/virdomainmomentobjlist.h | 5 +- src/driver-hypervisor.h | 38 ++ docs/Makefile.am | 3 + docs/apibuild.py | 2 + docs/docs.html.in | 1 + libvirt.spec.in | 1 + mingw-libvirt.spec.in | 2 + po/POTFILES | 1 + src/Makefile.am | 2 + src/libvirt-domain-checkpoint.c | 567 ++++++++++++++++++++ src/libvirt-domain.c | 19 +- src/libvirt_public.syms | 16 + 15 files changed, 796 insertions(+), 9 deletions(-) create mode 100644 include/libvirt/libvirt-domain-checkpoint.h create mode 100644 src/libvirt-domain-checkpoint.c diff --git a/include/libvirt/libvirt-domain-checkpoint.h b/include/libvirt/libvirt-domain-checkpoint.h new file mode 100644 index 0000000000..f4bd92e81c --- /dev/null +++ b/include/libvirt/libvirt-domain-checkpoint.h @@ -0,0 +1,137 @@ +/* + * libvirt-domain-checkpoint.h + * Summary: APIs for management of domain checkpoints + * Description: Provides APIs for the management of domain checkpoints + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#ifndef LIBVIRT_DOMAIN_CHECKPOINT_H +# define LIBVIRT_DOMAIN_CHECKPOINT_H + +# ifndef __VIR_LIBVIRT_H_INCLUDES__ +# error "Don't include this file directly, only use libvirt/libvirt.h" +# endif + +/** + * virDomainCheckpoint: + * + * A virDomainCheckpoint is a private structure representing a checkpoint of + * a domain. A checkpoint is useful for tracking which portions of the + * domain disks have been altered since a point in time, but by itself does + * not allow reverting back to that point in time. + */ +typedef struct _virDomainCheckpoint virDomainCheckpoint; + +/** + * virDomainCheckpointPtr: + * + * A virDomainCheckpointPtr is pointer to a virDomainCheckpoint + * private structure, and is the type used to reference a domain + * checkpoint in the API. + */ +typedef virDomainCheckpoint *virDomainCheckpointPtr; + +const char *virDomainCheckpointGetName(virDomainCheckpointPtr checkpoint); +virDomainPtr virDomainCheckpointGetDomain(virDomainCheckpointPtr checkpoint); +virConnectPtr virDomainCheckpointGetConnect(virDomainCheckpointPtr checkpoint); + +typedef enum { + VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE = (1 << 0), /* Restore or alter + metadata */ + VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE = (1 << 1), /* use guest agent to + quiesce all mounted + file systems within + the domain */ +} virDomainCheckpointCreateFlags; + +/* Create a checkpoint using the current VM state. */ +virDomainCheckpointPtr virDomainCheckpointCreateXML(virDomainPtr domain, + const char *xmlDesc, + unsigned int flags); + +typedef enum { + VIR_DOMAIN_CHECKPOINT_XML_SECURE = (1 << 0), /* Include sensitive data */ + VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN = (1 << 1), /* Suppress <domain> + subelement */ + VIR_DOMAIN_CHECKPOINT_XML_SIZE = (1 << 2), /* Include dynamic + per-<disk> size */ +} virDomainCheckpointXMLFlags; + +/* Dump the XML of a checkpoint */ +char *virDomainCheckpointGetXMLDesc(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +/** + * virDomainCheckpointListFlags: + * + * Flags valid for virDomainListAllCheckpoints() and + * virDomainCheckpointListAllChildren(). Note that the interpretation of + * flag (1<<0) depends on which function it is passed to; but serves + * to toggle the per-call default of whether the listing is shallow or + * recursive. Remaining bits come in groups; if all bits from a group + * are 0, then that group is not used to filter results. */ +typedef enum { + VIR_DOMAIN_CHECKPOINT_LIST_ROOTS = (1 << 0), /* Filter by checkpoints + with no parents, when + listing a domain */ + VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS = (1 << 0), /* List all descendants, + not just children, when + listing a checkpoint */ + VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL = (1 << 1), /* Ensure parents occur + before children in + the resulting list */ + + VIR_DOMAIN_CHECKPOINT_LIST_LEAVES = (1 << 2), /* Filter by checkpoints + with no children */ + VIR_DOMAIN_CHECKPOINT_LIST_NO_LEAVES = (1 << 3), /* Filter by checkpoints + that have children */ +} virDomainCheckpointListFlags; + +/* Get all checkpoint objects for this domain */ +int virDomainListAllCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags); + +/* Get all checkpoint object children for this checkpoint */ +int virDomainCheckpointListAllChildren(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **children, + unsigned int flags); + +/* Get a handle to a named checkpoint */ +virDomainCheckpointPtr virDomainCheckpointLookupByName(virDomainPtr domain, + const char *name, + unsigned int flags); + +/* Get a handle to the parent checkpoint, if one exists */ +virDomainCheckpointPtr virDomainCheckpointGetParent(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +/* Delete a checkpoint */ +typedef enum { + VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN = (1 << 0), /* Also delete children */ + VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY = (1 << 1), /* Delete just metadata */ + VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY = (1 << 2), /* Delete just children */ +} virDomainCheckpointDeleteFlags; + +int virDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +int virDomainCheckpointRef(virDomainCheckpointPtr checkpoint); +int virDomainCheckpointFree(virDomainCheckpointPtr checkpoint); + +#endif /* LIBVIRT_DOMAIN_CHECKPOINT_H */ diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h index 2dbd74d4f3..f160ee88b5 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -1800,6 +1800,9 @@ typedef enum { VIR_DOMAIN_UNDEFINE_NVRAM = (1 << 2), /* Also remove any nvram file */ VIR_DOMAIN_UNDEFINE_KEEP_NVRAM = (1 << 3), /* Keep nvram file */ + VIR_DOMAIN_UNDEFINE_CHECKPOINTS_METADATA = (1 << 4), /* If last use of domain, + then also remove any + checkpoint metadata */ /* Future undefine control flags should come here. */ } virDomainUndefineFlagsValues; @@ -1838,6 +1841,9 @@ typedef enum { VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT = 1 << 12, VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT = 1 << 13, + + VIR_CONNECT_LIST_DOMAINS_HAS_CHECKPOINT = 1 << 14, + VIR_CONNECT_LIST_DOMAINS_NO_CHECKPOINT = 1 << 15, } virConnectListAllDomainsFlags; int virConnectListAllDomains (virConnectPtr conn, diff --git a/include/libvirt/libvirt.h b/include/libvirt/libvirt.h index 13de151cb6..f93e3bfa85 100644 --- a/include/libvirt/libvirt.h +++ b/include/libvirt/libvirt.h @@ -34,10 +34,7 @@ extern "C" { # include <libvirt/libvirt-common.h> # include <libvirt/libvirt-host.h> # include <libvirt/libvirt-domain.h> -/* FIXME: Temporary hack until later patch creates new - * libvirt-domain-checkpoint.h file */ -typedef struct _virDomainCheckpoint virDomainCheckpoint; -typedef virDomainCheckpoint *virDomainCheckpointPtr; +# include <libvirt/libvirt-domain-checkpoint.h> # include <libvirt/libvirt-domain-snapshot.h> # include <libvirt/libvirt-event.h> # include <libvirt/libvirt-interface.h> diff --git a/src/conf/virdomainmomentobjlist.h b/src/conf/virdomainmomentobjlist.h index 897d8d54d1..5b73175e8d 100644 --- a/src/conf/virdomainmomentobjlist.h +++ b/src/conf/virdomainmomentobjlist.h @@ -70,8 +70,9 @@ virDomainMomentObjPtr virDomainMomentAssignDef(virDomainMomentObjListPtr moments virDomainMomentDefPtr def); /* Various enum bits that map to public API filters. Note that the - * values of the internal bits are not necessarily the same as the - * public ones. */ + * values of the internal bits are not the same as the public ones for + * snapshot, however, this list should be kept in sync with the public + * ones for checkpoint. */ typedef enum { VIR_DOMAIN_MOMENT_LIST_ROOTS = (1 << 0), VIR_DOMAIN_MOMENT_LIST_DESCENDANTS = (1 << 0), diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h index b15aaa36bc..c1632ae4c6 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -1327,6 +1327,37 @@ typedef int int *nparams, unsigned int flags); +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointCreateXML)(virDomainPtr domain, + const char *xmlDesc, + unsigned int flags); + +typedef char * +(*virDrvDomainCheckpointGetXMLDesc)(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +typedef int +(*virDrvDomainListAllCheckpoints)(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointListAllChildren)(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **children, + unsigned int flags); + +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointLookupByName)(virDomainPtr domain, + const char *name, + unsigned int flags); + +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointGetParent)(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointDelete)(virDomainCheckpointPtr checkpoint, + unsigned int flags); typedef struct _virHypervisorDriver virHypervisorDriver; typedef virHypervisorDriver *virHypervisorDriverPtr; @@ -1579,4 +1610,11 @@ struct _virHypervisorDriver { virDrvConnectBaselineHypervisorCPU connectBaselineHypervisorCPU; virDrvNodeGetSEVInfo nodeGetSEVInfo; virDrvDomainGetLaunchSecurityInfo domainGetLaunchSecurityInfo; + virDrvDomainCheckpointCreateXML domainCheckpointCreateXML; + virDrvDomainCheckpointGetXMLDesc domainCheckpointGetXMLDesc; + virDrvDomainListAllCheckpoints domainListAllCheckpoints; + virDrvDomainCheckpointListAllChildren domainCheckpointListAllChildren; + virDrvDomainCheckpointLookupByName domainCheckpointLookupByName; + virDrvDomainCheckpointGetParent domainCheckpointGetParent; + virDrvDomainCheckpointDelete domainCheckpointDelete; }; diff --git a/docs/Makefile.am b/docs/Makefile.am index ae2673ca9b..1cdb584b0b 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -22,6 +22,7 @@ DEVHELP_DIR=$(datadir)/gtk-doc/html/libvirt modules = \ libvirt-common \ libvirt-domain \ + libvirt-domain-checkpoint \ libvirt-domain-snapshot \ libvirt-event \ libvirt-host \ @@ -313,6 +314,7 @@ $(python_generated_files): $(APIBUILD_STAMP) $(APIBUILD_STAMP): $(srcdir)/apibuild.py \ $(top_srcdir)/include/libvirt/libvirt.h \ $(top_srcdir)/include/libvirt/libvirt-common.h.in \ + $(top_srcdir)/include/libvirt/libvirt-domain-checkpoint.h \ $(top_srcdir)/include/libvirt/libvirt-domain-snapshot.h \ $(top_srcdir)/include/libvirt/libvirt-domain.h \ $(top_srcdir)/include/libvirt/libvirt-event.h \ @@ -329,6 +331,7 @@ $(APIBUILD_STAMP): $(srcdir)/apibuild.py \ $(top_srcdir)/include/libvirt/libvirt-admin.h \ $(top_srcdir)/include/libvirt/virterror.h \ $(top_srcdir)/src/libvirt.c \ + $(top_srcdir)/src/libvirt-domain-checkpoint.c \ $(top_srcdir)/src/libvirt-domain-snapshot.c \ $(top_srcdir)/src/libvirt-domain.c \ $(top_srcdir)/src/libvirt-host.c \ diff --git a/docs/apibuild.py b/docs/apibuild.py index 9e04871220..dbdc1c95af 100755 --- a/docs/apibuild.py +++ b/docs/apibuild.py @@ -26,6 +26,7 @@ debugsym = None included_files = { "libvirt-common.h": "header with general libvirt API definitions", "libvirt-domain.h": "header with general libvirt API definitions", + "libvirt-domain-checkpoint.h": "header with general libvirt API definitions", "libvirt-domain-snapshot.h": "header with general libvirt API definitions", "libvirt-event.h": "header with general libvirt API definitions", "libvirt-host.h": "header with general libvirt API definitions", @@ -39,6 +40,7 @@ included_files = { "virterror.h": "header with error specific API definitions", "libvirt.c": "Main interfaces for the libvirt library", "libvirt-domain.c": "Domain interfaces for the libvirt library", + "libvirt-domain-checkpoint.c": "Domain checkpoint interfaces for the libvirt library", "libvirt-domain-snapshot.c": "Domain snapshot interfaces for the libvirt library", "libvirt-host.c": "Host interfaces for the libvirt library", "libvirt-interface.c": "Interface interfaces for the libvirt library", diff --git a/docs/docs.html.in b/docs/docs.html.in index 74af38002e..ba9514279a 100644 --- a/docs/docs.html.in +++ b/docs/docs.html.in @@ -94,6 +94,7 @@ <dd>Reference manual for the C public API, split in <a href="html/libvirt-libvirt-common.html">common</a>, <a href="html/libvirt-libvirt-domain.html">domain</a>, + <a href="html/libvirt-libvirt-domain-checkpoint.html">domain checkpoint</a>, <a href="html/libvirt-libvirt-domain-snapshot.html">domain snapshot</a>, <a href="html/libvirt-virterror.html">error</a>, <a href="html/libvirt-libvirt-event.html">event</a>, diff --git a/libvirt.spec.in b/libvirt.spec.in index 4dee2d8ac2..045c0fed1a 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1862,6 +1862,7 @@ exit 0 %{_includedir}/libvirt/libvirt-admin.h %{_includedir}/libvirt/libvirt-common.h %{_includedir}/libvirt/libvirt-domain.h +%{_includedir}/libvirt/libvirt-domain-checkpoint.h %{_includedir}/libvirt/libvirt-domain-snapshot.h %{_includedir}/libvirt/libvirt-event.h %{_includedir}/libvirt/libvirt-host.h diff --git a/mingw-libvirt.spec.in b/mingw-libvirt.spec.in index df89d9f8b3..a20c4b7d74 100644 --- a/mingw-libvirt.spec.in +++ b/mingw-libvirt.spec.in @@ -264,6 +264,7 @@ rm -rf $RPM_BUILD_ROOT%{mingw64_libexecdir}/libvirt-guests.sh %{mingw32_includedir}/libvirt/libvirt.h %{mingw32_includedir}/libvirt/libvirt-common.h %{mingw32_includedir}/libvirt/libvirt-domain.h +%{mingw32_includedir}/libvirt/libvirt-domain-checkpoint.h %{mingw32_includedir}/libvirt/libvirt-domain-snapshot.h %{mingw32_includedir}/libvirt/libvirt-event.h %{mingw32_includedir}/libvirt/libvirt-host.h @@ -354,6 +355,7 @@ rm -rf $RPM_BUILD_ROOT%{mingw64_libexecdir}/libvirt-guests.sh %{mingw64_includedir}/libvirt/libvirt.h %{mingw64_includedir}/libvirt/libvirt-common.h %{mingw64_includedir}/libvirt/libvirt-domain.h +%{mingw64_includedir}/libvirt/libvirt-domain-checkpoint.h %{mingw64_includedir}/libvirt/libvirt-domain-snapshot.h %{mingw64_includedir}/libvirt/libvirt-event.h %{mingw64_includedir}/libvirt/libvirt-host.h diff --git a/po/POTFILES b/po/POTFILES index ab582ffbe9..5d0bf43f02 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -69,6 +69,7 @@ src/interface/interface_backend_netcf.c src/interface/interface_backend_udev.c src/internal.h src/libvirt-admin.c +src/libvirt-domain-checkpoint.c src/libvirt-domain-snapshot.c src/libvirt-domain.c src/libvirt-host.c diff --git a/src/Makefile.am b/src/Makefile.am index 0b562dc250..4a8cae11dc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -167,6 +167,7 @@ DRIVER_SOURCES += \ $(DATATYPES_SOURCES) \ libvirt.c libvirt_internal.h \ libvirt-domain.c \ + libvirt-domain-checkpoint.c \ libvirt-domain-snapshot.c \ libvirt-host.c \ libvirt-interface.c \ @@ -719,6 +720,7 @@ libvirt_setuid_rpc_client_la_SOURCES = \ datatypes.c \ libvirt.c \ libvirt-domain.c \ + libvirt-domain-checkpoint.c \ libvirt-domain-snapshot.c \ libvirt-host.c \ libvirt-interface.c \ diff --git a/src/libvirt-domain-checkpoint.c b/src/libvirt-domain-checkpoint.c new file mode 100644 index 0000000000..fa391f8a06 --- /dev/null +++ b/src/libvirt-domain-checkpoint.c @@ -0,0 +1,567 @@ +/* + * libvirt-domain-checkpoint.c: entry points for virDomainCheckpointPtr APIs + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "datatypes.h" +#include "virlog.h" + +VIR_LOG_INIT("libvirt.domain-checkpoint"); + +#define VIR_FROM_THIS VIR_FROM_DOMAIN_CHECKPOINT + +/** + * virDomainCheckpointGetName: + * @checkpoint: a checkpoint object + * + * Get the public name for that checkpoint + * + * Returns a pointer to the name or NULL, the string need not be deallocated + * as its lifetime will be the same as the checkpoint object. + */ +const char * +virDomainCheckpointGetName(virDomainCheckpointPtr checkpoint) +{ + VIR_DEBUG("checkpoint=%p", checkpoint); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, NULL); + + return checkpoint->name; +} + + +/** + * virDomainCheckpointGetDomain: + * @checkpoint: a checkpoint object + * + * Provides the domain pointer associated with a checkpoint. The + * reference counter on the domain is not increased by this + * call. + * + * Returns the domain or NULL. + */ +virDomainPtr +virDomainCheckpointGetDomain(virDomainCheckpointPtr checkpoint) +{ + VIR_DEBUG("checkpoint=%p", checkpoint); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, NULL); + + return checkpoint->domain; +} + + +/** + * virDomainCheckpointGetConnect: + * @checkpoint: a checkpoint object + * + * Provides the connection pointer associated with a checkpoint. The + * reference counter on the connection is not increased by this + * call. + * + * Returns the connection or NULL. + */ +virConnectPtr +virDomainCheckpointGetConnect(virDomainCheckpointPtr checkpoint) +{ + VIR_DEBUG("checkpoint=%p", checkpoint); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, NULL); + + return checkpoint->domain->conn; +} + + +/** + * virDomainCheckpointCreateXML: + * @domain: a domain object + * @xmlDesc: description of the checkpoint to create + * @flags: bitwise-OR of supported virDomainCheckpointCreateFlags + * + * Create a new checkpoint using @xmlDesc, with a top-level + * <domaincheckpoint> element, on a running @domain. Note that @xmlDesc + * must validate against the <domaincheckpoint> XML schema. + * + * See <a href="formatcheckpoint.html#CheckpointAttributes">Checkpoint XML</a> + * for more details on @xmlDesc. In particular, some hypervisors may require + * particular disk formats, such as qcow2, in order to support this + * command; where @xmlDesc can be used to limit the checkpoint to a working + * subset of the domain's disks. + * + * If @flags includes VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, then this + * is a request to reinstate checkpoint metadata that was previously + * captured from virDomainCheckpointGetXMLDesc() before removing that + * metadata, rather than creating a new checkpoint. Note that while + * original creation can omit a number of elements from @xmlDesc (and + * libvirt will supply sane defaults based on the domain state at that + * point in time), a redefinition must supply more elements (as the + * domain may have changed in the meantime, so that libvirt no longer + * has a way to resupply correct defaults). Not all hypervisors support + * this flag. + * + * If @flags includes VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE, then the + * libvirt will attempt to use guest agent to freeze and thaw all file + * systems in use within domain OS. However, if the guest agent is not + * present, an error is thrown. This flag is incompatible with + * VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE. + * + * Returns an (opaque) new virDomainCheckpointPtr on success or NULL + * on failure. + */ +virDomainCheckpointPtr +virDomainCheckpointCreateXML(virDomainPtr domain, + const char *xmlDesc, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "xmlDesc=%s, flags=0x%x", xmlDesc, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, NULL); + conn = domain->conn; + + virCheckNonNullArgGoto(xmlDesc, error); + virCheckReadOnlyGoto(conn->flags, error); + + VIR_EXCLUSIVE_FLAGS_GOTO(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, + VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE, + error); + + if (conn->driver->domainCheckpointCreateXML) { + virDomainCheckpointPtr ret; + ret = conn->driver->domainCheckpointCreateXML(domain, xmlDesc, flags); + if (!ret) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainCheckpointGetXMLDesc: + * @checkpoint: a domain checkpoint object + * @flags: bitwise-OR of supported virDomainCheckpointXMLFlags + * + * Provide an XML description of the domain checkpoint. + * + * No security-sensitive data will be included unless @flags contains + * VIR_DOMAIN_CHECKPOINT_XML_SECURE; this flag is rejected on read-only + * connections. + * + * Normally, the XML description includes an element giving a full + * description of the domain at the time the checkpoint was created; to + * reduce parsing time, it will be suppressed when @flags contains + * VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN. + * + * By default, the XML description contains only static information that + * does not change over time. However, when @flags contains + * VIR_DOMAIN_CHECKPOINT_XML_SIZE, each <disk> listing adds an additional + * attribute that shows an estimate of the current size in bytes that + * have been dirtied between the time the checkpoint was created and the + * current point in time. + * + * Returns a 0 terminated UTF-8 encoded XML instance or NULL in case + * of error. The caller must free() the returned value. + */ +char * +virDomainCheckpointGetXMLDesc(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virConnectPtr conn; + VIR_DEBUG("checkpoint=%p, flags=0x%x", checkpoint, flags); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, NULL); + conn = checkpoint->domain->conn; + + if ((conn->flags & VIR_CONNECT_RO) && + (flags & VIR_DOMAIN_CHECKPOINT_XML_SECURE)) { + virReportError(VIR_ERR_OPERATION_DENIED, "%s", + _("virDomainCheckpointGetXMLDesc with secure flag")); + goto error; + } + + if (conn->driver->domainCheckpointGetXMLDesc) { + char *ret; + ret = conn->driver->domainCheckpointGetXMLDesc(checkpoint, flags); + if (!ret) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainListAllCheckpoints: + * @domain: a domain object + * @checkpoints: pointer to variable to store the array containing checkpoint + * object, or NULL if the list is not required (just returns + * number of checkpoints) + * @flags: bitwise-OR of supported virDomainCheckpoinListFlags + * + * Collect the list of domain checkpoints for the given domain and allocate + * an array to store those objects. + * + * If @flags contains VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL, + * @checkpoints is non-NULL, and no other connection is modifying + * checkpoints, then it is guaranteed that for any checkpoint in the + * resulting list, no checkpoints later in the list can be reached by + * a sequence of virDomainCheckpointGetParent() starting from that + * earlier checkpoint; otherwise, the order of checkpoints in the + * resulting list is unspecified. + * + * By default, this command covers all checkpoints. It is also + * possible to limit things to just checkpoints with no parents, when + * @flags includes VIR_DOMAIN_CHECKPOINT_LIST_ROOTS. Additional + * filters are provided in groups listed below. Within a group, bits + * are mutually exclusive, where all possible checkpoints are + * described by exactly one bit from the group. Some hypervisors might + * reject particular flags where it cannot make a distinction for + * filtering. If the set of filter flags selected forms an impossible + * combination, the hypervisor may return either 0 or an error. + * + * The first group of @flags is VIR_DOMAIN_CHECKPOINT_LIST_LEAVES and + * VIR_DOMAIN_CHECKPOINT_LIST_NO_LEAVES, to filter based on checkpoints that + * have no further children (a leaf checkpoint). + * + * Returns the number of domain checkpoints found or -1 and sets @checkpoints + * to NULL in case of error. On success, the array stored into @checkpoints + * is guaranteed to have an extra allocated element set to NULL but not + * included in the return count, to make iteration easier. The caller is + * responsible for calling virDomainCheckpointFree() on each array element, + * then calling free() on @checkpoints. + */ +int +virDomainListAllCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "checkpoints=%p, flags=0x%x", checkpoints, flags); + + virResetLastError(); + + if (checkpoints) + *checkpoints = NULL; + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + if (conn->driver->domainListAllCheckpoints) { + int ret = conn->driver->domainListAllCheckpoints(domain, checkpoints, + flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointListAllChildren: + * @checkpoint: a domain checkpoint object + * @children: pointer to variable to store the array containing checkpoint + * objects or NULL if the list is not required (just returns + * number of checkpoints) + * @flags: bitwise-OR of supported virDomainCheckpointListFlags + * + * Collect the list of domain checkpoints that are children of the given + * checkpoint, and allocate an array to store those objects. + * + * If @flags contains VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL, + * @checkpoints is non-NULL, and no other connection is modifying + * checkpoints, then it is guaranteed that for any checkpoint in the + * resulting list, no checkpoints later in the list can be reached by + * a sequence of virDomainCheckpointGetParent() starting from that + * earlier checkpoint; otherwise, the order of checkpoints in the + * resulting list is unspecified. + * + * By default, this command covers only direct children. It is also + * possible to expand things to cover all descendants, when @flags + * includes VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS. Additional + * are provided via the remaining @flags values as documented in + * virDomainListAllCheckpoints(), with the exception that + * VIR_DOMAIN_CHECKPOINT_LIST_ROOTS is not supported (in fact, + * VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS has the same bit value but + * opposite semantics of widening rather than narrowing the listing). + * + * Returns the number of domain checkpoints found or -1 and sets @children to + * NULL in case of error. On success, the array stored into @children is + * guaranteed to have an extra allocated element set to NULL but not included + * in the return count, to make iteration easier. The caller is responsible + * for calling virDomainCheckpointFree() on each array element, then calling + * free() on @children. + */ +int +virDomainCheckpointListAllChildren(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **children, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("checkpoint=%p, children=%p, flags=0x%x", + checkpoint, children, flags); + + virResetLastError(); + + if (children) + *children = NULL; + + virCheckDomainCheckpointReturn(checkpoint, -1); + conn = checkpoint->domain->conn; + + if (conn->driver->domainCheckpointListAllChildren) { + int ret = conn->driver->domainCheckpointListAllChildren(checkpoint, + children, + flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointLookupByName: + * @domain: a domain object + * @name: name for the domain checkpoint + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Try to lookup a domain checkpoint based on its name. + * + * Returns a domain checkpoint object or NULL in case of failure. If the + * domain checkpoint cannot be found, then the VIR_ERR_NO_DOMAIN_CHECKPOINT + * error is raised. + */ +virDomainCheckpointPtr +virDomainCheckpointLookupByName(virDomainPtr domain, + const char *name, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "name=%s, flags=0x%x", name, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, NULL); + conn = domain->conn; + + virCheckNonNullArgGoto(name, error); + + if (conn->driver->domainCheckpointLookupByName) { + virDomainCheckpointPtr checkpoint; + checkpoint = conn->driver->domainCheckpointLookupByName(domain, name, + flags); + if (!checkpoint) + goto error; + return checkpoint; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainCheckpointGetParent: + * @checkpoint: a checkpoint object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Get the parent checkpoint for @checkpoint, if any. + * + * virDomainCheckpointFree should be used to free the resources after the + * checkpoint object is no longer needed. + * + * Returns a domain checkpoint object or NULL in case of failure. If the + * given checkpoint is a root (no parent), then the VIR_ERR_NO_DOMAIN_CHECKPOINT + * error is raised. + */ +virDomainCheckpointPtr +virDomainCheckpointGetParent(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("checkpoint=%p, flags=0x%x", checkpoint, flags); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, NULL); + conn = checkpoint->domain->conn; + + if (conn->driver->domainCheckpointGetParent) { + virDomainCheckpointPtr parent; + parent = conn->driver->domainCheckpointGetParent(checkpoint, flags); + if (!parent) + goto error; + return parent; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainCheckpointDelete: + * @checkpoint: the checkpoint to remove + * @flags: bitwise-OR of supported virDomainCheckpointDeleteFlags + * + * Removes a checkpoint from the domain. + * + * When removing a checkpoint, the record of which portions of the + * disk were dirtied after the checkpoint will be merged into the + * record tracked by the parent checkpoint, if any. + * + * If @flags includes VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN, then any + * descendant checkpoints are also deleted. If @flags includes + * VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY, then any descendant + * checkepoints are deleted, but this checkpoint remains. These two + * flags are mutually exclusive. + * + * If @flags includes VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY, then + * any checkpoint metadata tracked by libvirt is removed while keeping + * the checkpoint contents intact; if a hypervisor does not require + * any libvirt metadata to track checkpoints, then this flag is + * silently ignored. + * + * Returns 0 on success, -1 on error. + */ +int +virDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("checkpoint=%p, flags=0x%x", checkpoint, flags); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, -1); + conn = checkpoint->domain->conn; + + virCheckReadOnlyGoto(conn->flags, error); + + VIR_EXCLUSIVE_FLAGS_GOTO(VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN, + VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY, + error); + + if (conn->driver->domainCheckpointDelete) { + int ret = conn->driver->domainCheckpointDelete(checkpoint, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointRef: + * @checkpoint: the checkpoint to hold a reference on + * + * Increment the reference count on the checkpoint. For each + * additional call to this method, there shall be a corresponding + * call to virDomainCheckpointFree to release the reference count, once + * the caller no longer needs the reference to this object. + * + * This method is typically useful for applications where multiple + * threads are using a connection, and it is required that the + * connection and domain remain open until all threads have finished + * using the checkpoint. ie, each new thread using a checkpoint would + * increment the reference count. + * + * Returns 0 in case of success and -1 in case of failure. + */ +int +virDomainCheckpointRef(virDomainCheckpointPtr checkpoint) +{ + VIR_DEBUG("checkpoint=%p, refs=%d", checkpoint, + checkpoint ? checkpoint->parent.u.s.refs : 0); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, -1); + + virObjectRef(checkpoint); + return 0; +} + + +/** + * virDomainCheckpointFree: + * @checkpoint: a domain checkpoint object + * + * Free the domain checkpoint object. The checkpoint itself is not modified. + * The data structure is freed and should not be used thereafter. + * + * Returns 0 in case of success and -1 in case of failure. + */ +int +virDomainCheckpointFree(virDomainCheckpointPtr checkpoint) +{ + VIR_DEBUG("checkpoint=%p", checkpoint); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, -1); + + virObjectUnref(checkpoint); + return 0; +} diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index 3d12e7c125..2fe9bb8e91 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -6218,8 +6218,9 @@ virDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) * * If the domain has a managed save image (see * virDomainHasManagedSaveImage()), or if it is inactive and has any - * snapshot metadata (see virDomainSnapshotNum()), then the undefine will - * fail. See virDomainUndefineFlags() for more control. + * snapshot metadata (see virDomainSnapshotNum()) or checkpoint + * metadata (see virDomainListAllCheckpoints()), then the undefine + * will fail. See virDomainUndefineFlags() for more control. * * Returns 0 in case of success, -1 in case of error */ @@ -6276,6 +6277,16 @@ virDomainUndefine(virDomainPtr domain) * support snapshots, but where snapshots do not use libvirt metadata, * this flag has no effect. * + * If the domain is inactive and has any checkpoint metadata (see + * virDomainListAllCheckpoints()), then including + * VIR_DOMAIN_UNDEFINE_CHECKPOINTS_METADATA in @flags will also remove + * that metadata. Omitting the flag will cause the undefine of an + * inactive domain with checkpoints to fail. Active domains will + * retain checkpoint metadata until the (now-transient) domain halts, + * regardless of whether this flag is present. On hypervisors that + * support checkpoints, but where checkpoints do not use libvirt + * metadata, this flag has no effect. + * * If the domain has any nvram specified, the undefine process will fail * unless VIR_DOMAIN_UNDEFINE_KEEP_NVRAM is specified, or if * VIR_DOMAIN_UNDEFINE_NVRAM is specified to remove the nvram file. @@ -6436,7 +6447,9 @@ virConnectListDefinedDomains(virConnectPtr conn, char **const names, * VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART, for filtering based on autostart; * VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT and * VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT, for filtering based on whether - * a domain has snapshots. + * a domain has snapshots; VIR_CONNECT_LIST_DOMAINS_HAS_CHECKPOINT and + * VIR_CONNECT_LIST_DOMAINS_NO_CHECKPOINT, for filtering based on whether + * a domain has checkpoints. * * Example of usage: * diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 18500ec8b2..54256b6317 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -836,4 +836,20 @@ LIBVIRT_5.5.0 { virNetworkPortSetParameters; } LIBVIRT_5.2.0; +LIBVIRT_5.6.0 { + global: + virDomainCheckpointCreateXML; + virDomainCheckpointDelete; + virDomainCheckpointFree; + virDomainCheckpointGetConnect; + virDomainCheckpointGetDomain; + virDomainCheckpointGetName; + virDomainCheckpointGetParent; + virDomainCheckpointGetXMLDesc; + virDomainCheckpointListAllChildren; + virDomainCheckpointLookupByName; + virDomainCheckpointRef; + virDomainListAllCheckpoints; +} LIBVIRT_5.5.0; + # .... define new API here using predicted next version number .... -- 2.20.1

On Wed, Jul 24, 2019 at 12:55:56AM -0500, Eric Blake wrote:
Introduce a bunch of new public APIs related to backup checkpoints. Checkpoints are modeled heavily after virDomainSnapshotPtr (both represent a point in time of the guest), although a snapshot exists with the intent of rolling back to that state, while a checkpoint exists to make it possible to create an incremental backup at a later time. We may have a future hypervisor that can completely manage checkpoints without libvirt metadata, but the first two planned hypervisors (qemu and test) both always use libvirt for tracking metadata relations between checkpoints, so for now, I've deferred the counterpart of virDomainSnapshotHasMetadata for a separate API addition at a later date if there is ever a need for it.
Note that until we allow snapshots and checkpoints to exist simultaneously on the same domain (although the actual prevention of this will be in a separate patch for the sake of an easier revert down the road), that it is not possible to branch out to create more than one checkpoint child to a given parent, although it may become possible later when we revert to a snapshot that coincides with a checkpoint. This also means that for now, the decision of which checkpoint becomes the parent of a newly created one is the only checkpoint with no child (so while there are APIs for dealing with a current snapshot, we do not need those for checkpoints). We may end up exposing a notion of a current checkpoint later, but it's easier to add stuff when proven needed than to blindly support it now and wish we hadn't exposed it.
The following map shows the API relations to snapshots, with new APIs on the right:
Operate on a domain object to create/redefine a child: virDomainSnapshotCreateXML virDomainCheckpointCreateXML
Operate on a child object for lifetime management: virDomainSnapshotDelete virDomainCheckpointDelete virDomainSnapshotFree virDomainCheckpointFree virDomainSnapshotRef virDomainCheckpointRef
Operate on a child object to learn more about it: virDomainSnapshotGetXMLDesc virDomainCheckpointGetXMLDesc virDomainSnapshotGetConnect virDomainCheckpointGetConnect virDomainSnapshotGetDomain virDomainCheckpointGetDomain virDomainSnapshotGetName virDomainCheckpiontGetName virDomainSnapshotGetParent virDomainCheckpiontGetParent virDomainSnapshotHasMetadata (deferred for later) virDomainSnapshotIsCurrent (no counterpart, see note above)
Operate on a domain object to list all children: virDomainSnapshotNum (no counterparts, these are the old virDomainSnapshotListNames racy interfaces) virDomainSnapshotListAllSnapshots virDomainListAllCheckpoints
Operate on a child object to list descendents: virDomainSnapshotNumChildren (no counterparts, these are the old virDomainSnapshotListChildrenNames racy interfaces) virDomainSnapshotListAllChildren virDomainCheckpointListAllChildren
Operate on a domain to locate a particular child: virDomainSnapshotLookupByName virDomainCheckpointLookupByName virDomainSnapshotCurrent (no counterpart, see note above) virDomainHasCurrentSnapshot (no counterpart, old racy interface)
Operate on a snapshot to roll back to earlier state: virDomainSnapshotRevert (no counterpart, instead checkpoints are used in incremental backups via XML to virDomainBackupBegin)
Signed-off-by: Eric Blake <eblake@redhat.com> --- include/libvirt/libvirt-domain-checkpoint.h | 137 +++++ include/libvirt/libvirt-domain.h | 6 + include/libvirt/libvirt.h | 5 +- src/conf/virdomainmomentobjlist.h | 5 +- src/driver-hypervisor.h | 38 ++ docs/Makefile.am | 3 + docs/apibuild.py | 2 + docs/docs.html.in | 1 + libvirt.spec.in | 1 + mingw-libvirt.spec.in | 2 + po/POTFILES | 1 + src/Makefile.am | 2 + src/libvirt-domain-checkpoint.c | 567 ++++++++++++++++++++ src/libvirt-domain.c | 19 +- src/libvirt_public.syms | 16 + 15 files changed, 796 insertions(+), 9 deletions(-) create mode 100644 include/libvirt/libvirt-domain-checkpoint.h create mode 100644 src/libvirt-domain-checkpoint.c
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Now that various new API have been added or are coming soon, it is worth a landing page that gives an overview of capturing various pieces of guest state, and which APIs are best suited to which tasks. Signed-off-by: Eric Blake <eblake@redhat.com> Reviewed-by: John Ferlan <jferlan@redhat.com> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> --- docs/formatcheckpoint.html.in | 4 +- docs/formatsnapshot.html.in | 2 + docs/kbase.html.in | 5 + docs/kbase/domainstatecapture.html.in | 303 ++++++++++++++++++++++++++ 4 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 docs/kbase/domainstatecapture.html.in diff --git a/docs/formatcheckpoint.html.in b/docs/formatcheckpoint.html.in index 50dd4d284c..aae0e27463 100644 --- a/docs/formatcheckpoint.html.in +++ b/docs/formatcheckpoint.html.in @@ -13,7 +13,9 @@ incremental backups. Right now, incremental backups are only supported for the QEMU hypervisor when using qcow2 disks at the active layer; if other disk formats are in use, capturing disk - backups requires different libvirt APIs. + backups requires different libvirt APIs + (see <a href="domainstatecapture.html">domain state capture</a> + for a comparison between APIs). </p> <p> Libvirt is able to facilitate incremental backups by tracking diff --git a/docs/formatsnapshot.html.in b/docs/formatsnapshot.html.in index 005800bc00..ced8bbd6db 100644 --- a/docs/formatsnapshot.html.in +++ b/docs/formatsnapshot.html.in @@ -9,6 +9,8 @@ <h2><a id="SnapshotAttributes">Snapshot XML</a></h2> <p> + Snapshots are one form + of <a href="domainstatecapture.html">domain state capture</a>. There are several types of snapshots: </p> <dl> diff --git a/docs/kbase.html.in b/docs/kbase.html.in index 78f2a4e0c5..97d3f4c384 100644 --- a/docs/kbase.html.in +++ b/docs/kbase.html.in @@ -16,6 +16,11 @@ <dt><a href="kbase/launch_security_sev.html">Launch security</a></dt> <dd>Securely launching VMs with AMD SEV</dd> + + <dt><a href="kbase/domainstatecapture.html">Domain state + capture</a></dt> + <dd>Comparison between different methods of capturing domain + state</dd> </dl> </div> diff --git a/docs/kbase/domainstatecapture.html.in b/docs/kbase/domainstatecapture.html.in new file mode 100644 index 0000000000..f8c7394785 --- /dev/null +++ b/docs/kbase/domainstatecapture.html.in @@ -0,0 +1,303 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml"> + <body> + + <h1>Domain state capture using Libvirt</h1> + + <ul id="toc"></ul> + + <p> + In order to aid application developers to choose which + operations best suit their needs, this page compares the + different means for capturing state related to a domain managed + by libvirt. + </p> + + <p> + The information here is primarily geared towards capturing the + state of an active domain. Capturing the state of an inactive + domain essentially amounts to copying the contents of guest + disks, followed by a fresh boot of the same domain configuration + with disks restored back to that saved state. + </p> + + <h2><a id="definitions">State capture trade-offs</a></h2> + + <p>One of the features made possible with virtual machines is live + migration -- transferring all state related to the guest from + one host to another with minimal interruption to the guest's + activity. In this case, state includes domain memory (including + register and device contents), and domain storage (whether the + guest's view of the disks are backed by local storage on the + host, or by the hypervisor accessing shared storage over a + network). A clever observer will then note that if all state is + available for live migration, then there is nothing stopping a + user from saving some or all of that state at a given point of + time in order to be able to later rewind guest execution back to + the state it previously had. The astute reader will also realize + that state capture at any level requires that the data must be + stored and managed by some mechanism. This processing might fit + in a single file, or more likely require a chain of related + files, and may require synchronization with third-party tools + built around managing the amount of data resulting from + capturing the state of multiple guests that each use multiple + disks. + </p> + + <p> + There are several libvirt APIs associated with capturing the + state of a guest, which can later be used to rewind that guest + to the conditions it was in earlier. The following is a list of + trade-offs and differences between the various facets that + affect capturing domain state for active domains: + </p> + + <dl> + <dt>Duration</dt> + <dd>Capturing state can be a lengthy process, so while the + captured state ideally represents an atomic point in time + corresponding to something the guest was actually executing, + capturing state tends to focus on minimizing guest downtime + while performing the rest of the state capture in parallel + with guest execution. Some interfaces require up-front + preparation (the state captured is not complete until the API + ends, which may be some time after the command was first + started), while other interfaces track the state when the + command was first issued, regardless of the time spent in + capturing the rest of the state. Also, time spent in state + capture may be longer than the time required for live + migration, when state must be duplicated rather than shared. + </dd> + + <dt>Amount of state</dt> + <dd>For an online guest, there is a choice between capturing the + guest's memory (all that is needed during live migration when + the storage is already shared between source and destination), + the guest's disk state (all that is needed if there are no + pending guest I/O transactions that would be lost without the + corresponding memory state), or both together. Reverting to + partial state may still be viable, but typically, booting from + captured disk state without corresponding memory is comparable + to rebooting a machine that had power cut before I/O could be + flushed. Guests may need to use proper journaling methods to + avoid problems when booting from partial state. + </dd> + + <dt>Quiescing of data</dt> + <dd>Even if a guest has no pending I/O, capturing disk state may + catch the guest at a time when the contents of the disk are + inconsistent. Cooperating with the guest to perform data + quiescing is an optional step to ensure that captured disk + state is fully consistent without requiring additional memory + state, rather than just crash-consistent. But guest + cooperation may also have time constraints, where the guest + can rightfully panic if there is too much downtime while I/O + is frozen. + </dd> + + <dt>Quantity of files</dt> + <dd>When capturing state, some approaches store all state within + the same file (internal), while others expand a chain of + related files that must be used together (external), for more + files that a management application must track. + </dd> + + <dt>Impact to guest definition</dt> + <dd>Capturing state may require temporary changes to the guest + definition, such as associating new files into the domain + definition. While state capture should never impact the + running guest, a change to the domain's active XML may have + impact on other host operations being performed on the domain. + </dd> + + <dt>Third-party integration</dt> + <dd>When capturing state, there are tradeoffs to how much of the + process must be done directly by the hypervisor, and how much + can be off-loaded to third-party software. Since capturing + state is not instantaneous, it is essential that any + third-party integration see consistent data even if the + running guest continues to modify that data after the point in + time of the capture.</dd> + + <dt>Full vs. incremental</dt> + <dd>When periodically repeating the action of state capture, it + is useful to minimize the amount of state that must be + captured by exploiting the relation to a previous capture, + such as focusing only on the portions of the disk that the + guest has modified in the meantime. Some approaches are able + to take advantage of checkpoints to provide an incremental + backup, while others are only capable of a full backup even if + that means re-capturing unchanged portions of the disk.</dd> + + <dt>Local vs. remote</dt> + <dd>Domains that completely use remote storage may only need + some mechanism to keep track of guest memory state while using + external means to manage storage. Still, hypervisor and guest + cooperation to ensure points in time when no I/O is in flight + across the network can be important for properly capturing + disk state.</dd> + + <dt>Network latency</dt> + <dd>Whether it's domain storage or saving domain state into + remote storage, network latency has an impact on snapshot + data. Having dedicated network capacity, bandwidth, or quality + of service levels may play a role, as well as planning for how + much of the backup process needs to be local.</dd> + </dl> + + <p> + An example of the various facets in action is migration of a + running guest. In order for the guest to be able to resume on + the destination at the same place it left off at the source, the + hypervisor has to get to a point where execution on the source + is stopped, the last remaining changes occurring since the + migration started are then transferred, and the guest is started + on the target. The management software thus must keep track of + the starting point and any changes since the starting + point. These last changes are often referred to as dirty page + tracking or dirty disk block bitmaps. At some point in time + during the migration, the management software must freeze the + source guest, transfer the dirty data, and then start the guest + on the target. This period of time must be minimal. To minimize + overall migration time, one is advised to use a dedicated + network connection with a high quality of service. Alternatively + saving the current state of the running guest can just be a + point in time type operation which doesn't require updating the + "last vestiges" of state prior to writing out the saved state + file. The state file is the point in time of whatever is current + and may contain incomplete data which if used to restart the + guest could cause confusion or problems because some operation + wasn't completed depending upon where in time the operation was + commenced. + </p> + + <h2><a id="apis">State capture APIs</a></h2> + <p>With those definitions, the following libvirt APIs related to + state capture have these properties:</p> + <dl> + <dt><a href="html/libvirt-libvirt-domain.html#virDomainManagedSave"><code>virDomainManagedSave</code></a></dt> + <dd>This API saves guest memory, with libvirt managing all of + the saved state, then stops the guest. While stopped, the + disks can be copied by a third party. However, since any + subsequent restart of the guest by libvirt API will restore + the memory state (which typically only works if the disk state + is unchanged in the meantime), and since it is not possible to + get at the memory state that libvirt is managing, this is not + viable as a means for rolling back to earlier saved states, + but is rather more suited to situations such as suspending a + guest prior to rebooting the host in order to resume the guest + when the host is back up. This API also has a drawback of + potentially long guest downtime, and therefore does not lend + itself well to live backups.</dd> + + <dt><a href="html/libvirt-libvirt-domain.html#virDomainSave"><code>virDomainSave</code></a></dt> + <dd>This API is similar to virDomainManagedSave(), but moves the + burden on managing the stored memory state to the user. As + such, the user can now couple saved state with copies of the + disks to perform a revert to an arbitrary earlier saved state. + However, changing who manages the memory state does not change + the drawback of potentially long guest downtime when capturing + state.</dd> + + <dt><a href="html/libvirt-libvirt-domain-snapshot.html#virDomainSnapshotCreateXML"><code>virDomainSnapshotCreateXML</code></a></dt> + <dd>This API wraps several approaches for capturing guest state, + with a general premise of creating a snapshot (where the + current guest resources are frozen in time and a new wrapper + layer is opened for tracking subsequent guest changes). It + can operate on both offline and running guests, can choose + whether to capture the state of memory, disk, or both when + used on a running guest, and can choose between internal and + external storage for captured state. However, it is geared + towards post-event captures (when capturing both memory and + disk state, the disk state is not captured until all memory + state has been collected first). Using QEMU as the + hypervisor, internal snapshots currently have lengthy downtime + that is incompatible with freezing guest I/O, but external + snapshots are quick when memory contents are not also saved. + Since creating an external snapshot changes which disk image + resource is in use by the guest, this API can be coupled + with <a href="html/libvirt-libvirt-domain.html#virDomainBlockCommit"><code>virDomainBlockCommit()</code></a> + to restore things back to the guest using its original disk + image, where a third-party tool can read the backing file + prior to the live commit. See also + the <a href="formatsnapshot.html">XML details</a> used with + this command.</dd> + + <dt><a href="html/libvirt-libvirt-domain.html#virDomainFSFreeze"><code>virDomainFSFreeze</code></a>, <a href="html/libvirt-libvirt-domain.html#virDomainFSThaw"><code>virDomainFSThaw</code></a></dt> + <dd>This pair of APIs does not directly capture guest state, but + can be used to coordinate with a trusted live guest that state + capture is about to happen, and therefore guest I/O should be + quiesced so that the state capture is fully consistent, rather + than merely crash consistent. Some APIs are able to + automatically perform a freeze and thaw via a flags parameter, + rather than having to make separate calls to these + functions. Also, note that freezing guest I/O is only possible + with trusted guests running a guest agent, and that some + guests place maximum time limits on how long I/O can be + frozen.</dd> + + <dt><a href="html/libvirt-libvirt-domain-checkpoint.html#virDomainCheckpointCreateXML"><code>virDomainCheckpointCreateXML</code></a></dt> + <dd>This API does not actually capture guest state, rather it + makes it possible to track which portions of guest disks have + changed between a checkpoint and the current live execution of + the guest. However, while it is possible use this API to + create checkpoints in isolation, it is more typical to create + a checkpoint as a side-effect of starting a new incremental + backup with <code>virDomainBackupBegin()</code> or at the + creation of an external snapshot + with <code>virDomainSnapshotCreateXML2()</code>, since a + second incremental backup is most useful when using the + checkpoint created during the first. See also + the <a href="formatcheckpoint.html">XML details</a> used with + this command.</dd> + + <dt><a href="html/libvirt-libvirt-domain.html#virDomainBackupBegin"><code>virDomainBackupBegin</code></a>, <a href="html/libvirt-libvirt-domain.html#virDomainBackupEnd"><code>virDomainBackupEnd</code></a></dt> + <dd>This API wraps approaches for capturing the state of disks + of a running guest, but does not track accompanying guest + memory state. The capture is consistent to the start of the + operation, where the captured state is stored independently + from the disk image in use with the guest and where it can be + easily integrated with a third-party for capturing the disk + state. Since the backup operation is stored externally from + the guest resources, there is no need to commit data back in + at the completion of the operation. When coupled with + checkpoints, this can be used to capture incremental backups + instead of full.</dd> + </dl> + + <h2><a id="examples">Examples</a></h2> + <p>The following two sequences both accomplish the task of + capturing the disk state of a running guest, then wrapping + things up so that the guest is still running with the same file + as its disk image as before the sequence of operations began. + The difference between the two sequences boils down to the + impact of an unexpected interruption made at any point in the + middle of the sequence: with such an interruption, the first + example leaves the guest tied to a temporary wrapper file rather + than the original disk, and requires manual clean up of the + domain definition; while the second example has no impact to the + domain definition.</p> + + <p>1. Backup via temporary snapshot + <pre> +virDomainFSFreeze() +virDomainSnapshotCreateXML(VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY) +virDomainFSThaw() +third-party copy the backing file to backup storage # most time spent here +virDomainBlockCommit(VIR_DOMAIN_BLOCK_COMMIT_ACTIVE) per disk +wait for commit ready event per disk +virDomainBlockJobAbort() per disk + </pre></p> + + <p>2. Direct backup + <pre> +virDomainFSFreeze() +virDomainBackupBegin() +virDomainFSThaw() +wait for push mode event, or pull data over NBD # most time spent here +virDomainBackupEnd() + </pre></p> + + </body> +</html> -- 2.20.1

Add a new file checkpoint_conf.c that performs the translation to and from new XML describing a checkpoint. The code shares a common base class with snapshots, since a checkpoint similarly represents the domain state at a moment in time. Add some basic testing of round trip XML handling through the new code. Of note - this code intentionally differs from snapshots in that XML schema validation is unconditional, rather than based on a public API flag. Also, the redefine flag requires the <domain> sub-element to be present, rather than catering to historical back-compat to older versions. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 84 +++ src/conf/virconftypes.h | 3 + po/POTFILES | 1 + src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 535 ++++++++++++++++++ src/libvirt_private.syms | 8 + tests/Makefile.am | 9 +- .../internal-active-invalid.xml | 53 ++ .../internal-inactive-invalid.xml | 53 ++ tests/qemudomaincheckpointxml2xmltest.c | 213 +++++++ 10 files changed, 959 insertions(+), 2 deletions(-) create mode 100644 src/conf/checkpoint_conf.h create mode 100644 src/conf/checkpoint_conf.c create mode 100644 tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmltest.c diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h new file mode 100644 index 0000000000..0fac521efe --- /dev/null +++ b/src/conf/checkpoint_conf.h @@ -0,0 +1,84 @@ +/* + * checkpoint_conf.h: domain checkpoint XML processing + * (based on snapshot_conf.h) + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "internal.h" +#include "domain_conf.h" +#include "moment_conf.h" +#include "virobject.h" + +/* Items related to checkpoint state */ + +typedef enum { + VIR_DOMAIN_CHECKPOINT_TYPE_DEFAULT = 0, + VIR_DOMAIN_CHECKPOINT_TYPE_NONE, + VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP, + + VIR_DOMAIN_CHECKPOINT_TYPE_LAST +} virDomainCheckpointType; + +/* Stores disk-checkpoint information */ +typedef struct _virDomainCheckpointDiskDef virDomainCheckpointDiskDef; +typedef virDomainCheckpointDiskDef *virDomainCheckpointDiskDefPtr; +struct _virDomainCheckpointDiskDef { + char *name; /* name matching the <target dev='...' of the domain */ + int idx; /* index within checkpoint->dom->disks that matches name */ + int type; /* virDomainCheckpointType */ + char *bitmap; /* bitmap name, if type is bitmap */ + unsigned long long size; /* current checkpoint size in bytes */ +}; + +/* Stores the complete checkpoint metadata */ +struct _virDomainCheckpointDef { + virDomainMomentDef parent; + + /* Additional Public XML. */ + size_t ndisks; /* should not exceed dom->ndisks */ + virDomainCheckpointDiskDef *disks; +}; + + +typedef enum { + VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE = 1 << 0, +} virDomainCheckpointParseFlags; + +typedef enum { + VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE = 1 << 0, + VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN = 1 << 1, + VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE = 1 << 2, +} virDomainCheckpointFormatFlags; + +unsigned int virDomainCheckpointFormatConvertXMLFlags(unsigned int flags); + +virDomainCheckpointDefPtr virDomainCheckpointDefParseString(const char *xmlStr, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +virDomainCheckpointDefPtr virDomainCheckpointDefNew(void); +char *virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +int virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr checkpoint); + +VIR_ENUM_DECL(virDomainCheckpoint); diff --git a/src/conf/virconftypes.h b/src/conf/virconftypes.h index b8f553f7fb..fe3f0af14f 100644 --- a/src/conf/virconftypes.h +++ b/src/conf/virconftypes.h @@ -102,6 +102,9 @@ typedef virDomainBlkiotune *virDomainBlkiotunePtr; typedef struct _virDomainBlockIoTuneInfo virDomainBlockIoTuneInfo; typedef virDomainBlockIoTuneInfo *virDomainBlockIoTuneInfoPtr; +typedef struct _virDomainCheckpointDef virDomainCheckpointDef; +typedef virDomainCheckpointDef *virDomainCheckpointDefPtr; + typedef struct _virDomainChrDef virDomainChrDef; typedef virDomainChrDef *virDomainChrDefPtr; diff --git a/po/POTFILES b/po/POTFILES index 5d0bf43f02..2c27049d55 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -15,6 +15,7 @@ src/bhyve/bhyve_monitor.c src/bhyve/bhyve_parse_command.c src/bhyve/bhyve_process.c src/conf/capabilities.c +src/conf/checkpoint_conf.c src/conf/cpu_conf.c src/conf/device_conf.c src/conf/domain_addr.c diff --git a/src/conf/Makefile.inc.am b/src/conf/Makefile.inc.am index 16a21c34f3..f656725e3a 100644 --- a/src/conf/Makefile.inc.am +++ b/src/conf/Makefile.inc.am @@ -14,6 +14,8 @@ NETDEV_CONF_SOURCES = \ DOMAIN_CONF_SOURCES = \ conf/capabilities.c \ conf/capabilities.h \ + conf/checkpoint_conf.c \ + conf/checkpoint_conf.h \ conf/domain_addr.c \ conf/domain_addr.h \ conf/domain_capabilities.c \ diff --git a/src/conf/checkpoint_conf.c b/src/conf/checkpoint_conf.c new file mode 100644 index 0000000000..69c9040a43 --- /dev/null +++ b/src/conf/checkpoint_conf.c @@ -0,0 +1,535 @@ +/* + * checkpoint_conf.c: domain checkpoint XML processing + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "configmake.h" +#include "internal.h" +#include "virbitmap.h" +#include "virbuffer.h" +#include "datatypes.h" +#include "domain_conf.h" +#include "virlog.h" +#include "viralloc.h" +#include "checkpoint_conf.h" +#include "virstoragefile.h" +#include "viruuid.h" +#include "virfile.h" +#include "virerror.h" +#include "virxml.h" +#include "virstring.h" + +#define VIR_FROM_THIS VIR_FROM_DOMAIN_CHECKPOINT + +VIR_LOG_INIT("conf.checkpoint_conf"); + +static virClassPtr virDomainCheckpointDefClass; +static void virDomainCheckpointDefDispose(void *obj); + +static int +virDomainCheckpointOnceInit(void) +{ + if (!VIR_CLASS_NEW(virDomainCheckpointDef, virClassForDomainMomentDef())) + return -1; + + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virDomainCheckpoint); + +VIR_ENUM_IMPL(virDomainCheckpoint, + VIR_DOMAIN_CHECKPOINT_TYPE_LAST, + "default", "no", "bitmap"); + + +/* Checkpoint Def functions */ +static void +virDomainCheckpointDiskDefClear(virDomainCheckpointDiskDefPtr disk) +{ + VIR_FREE(disk->name); + VIR_FREE(disk->bitmap); +} + +/* Allocate a new virDomainCheckpointDef; free with virObjectUnref() */ +virDomainCheckpointDefPtr +virDomainCheckpointDefNew(void) +{ + virDomainCheckpointDefPtr def; + + if (virDomainCheckpointInitialize() < 0) + return NULL; + + def = virObjectNew(virDomainCheckpointDefClass); + return def; +} + +static void +virDomainCheckpointDefDispose(void *obj) +{ + virDomainCheckpointDefPtr def = obj; + size_t i; + + for (i = 0; i < def->ndisks; i++) + virDomainCheckpointDiskDefClear(&def->disks[i]); + VIR_FREE(def->disks); +} + +static int +virDomainCheckpointDiskDefParseXML(xmlNodePtr node, + xmlXPathContextPtr ctxt, + virDomainCheckpointDiskDefPtr def) +{ + VIR_AUTOFREE(char *) checkpoint = NULL; + VIR_XPATH_NODE_AUTORESTORE(ctxt); + + ctxt->node = node; + + /* Schema guarantees this is non-NULL: */ + def->name = virXMLPropString(node, "name"); + + checkpoint = virXMLPropString(node, "checkpoint"); + if (checkpoint) + /* Schema guarantees this is in range: */ + def->type = virDomainCheckpointTypeFromString(checkpoint); + else + def->type = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + + def->bitmap = virXMLPropString(node, "bitmap"); + + return 0; +} + +/* flags is bitwise-or of virDomainCheckpointParseFlags. If flags + * does not include VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE, then caps + * is ignored. + */ +static virDomainCheckpointDefPtr +virDomainCheckpointDefParse(xmlXPathContextPtr ctxt, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainCheckpointDefPtr ret = NULL; + size_t i; + int n; + char *tmp; + VIR_AUTOFREE(xmlNodePtr *) nodes = NULL; + VIR_AUTOFREE(char *)creation = NULL; + VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL; + + if (!(def = virDomainCheckpointDefNew())) + return NULL; + + def->parent.name = virXPathString("string(./name)", ctxt); + + if (def->parent.name == NULL) { + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("a redefined checkpoint must have a name")); + return NULL; + } + } + + def->parent.description = virXPathString("string(./description)", ctxt); + + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE) { + if (virXPathLongLong("string(./creationTime)", ctxt, + &def->parent.creationTime) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing creationTime from existing checkpoint")); + return NULL; + } + + def->parent.parent_name = virXPathString("string(./parent/name)", ctxt); + + if ((tmp = virXPathString("string(./domain/@type)", ctxt))) { + int domainflags = VIR_DOMAIN_DEF_PARSE_INACTIVE | + VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE; + xmlNodePtr domainNode = virXPathNode("./domain", ctxt); + + VIR_FREE(tmp); + if (!domainNode) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint")); + return NULL; + } + def->parent.dom = virDomainDefParseNode(ctxt->node->doc, domainNode, + caps, xmlopt, NULL, + domainflags); + if (!def->parent.dom) + return NULL; + } else { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint redefine")); + return NULL; + } + } else if (virDomainXMLOptionRunMomentPostParse(xmlopt, &def->parent) < 0) { + return NULL; + } + + if ((n = virXPathNodeSet("./disks/*", ctxt, &nodes)) < 0) + return NULL; + if (n && VIR_ALLOC_N(def->disks, n) < 0) + return NULL; + def->ndisks = n; + for (i = 0; i < def->ndisks; i++) { + if (virDomainCheckpointDiskDefParseXML(nodes[i], ctxt, + &def->disks[i]) < 0) + return NULL; + } + + VIR_STEAL_PTR(ret, def); + return ret; +} + +static virDomainCheckpointDefPtr +virDomainCheckpointDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + xmlXPathContextPtr ctxt = NULL; + virDomainCheckpointDefPtr def = NULL; + VIR_AUTOFREE(char *) schema = NULL; + + if (!virXMLNodeNameEqual(root, "domaincheckpoint")) { + virReportError(VIR_ERR_XML_ERROR, "%s", _("domaincheckpoint")); + goto cleanup; + } + + /* This is a new enough API to make schema validation unconditional */ + schema = virFileFindResource("domaincheckpoint.rng", + abs_top_srcdir "/docs/schemas", + PKGDATADIR "/schemas"); + if (!schema) + return NULL; + if (virXMLValidateAgainstSchema(schema, xml) < 0) + return NULL; + + ctxt = xmlXPathNewContext(xml); + if (ctxt == NULL) { + virReportOOMError(); + goto cleanup; + } + + ctxt->node = root; + def = virDomainCheckpointDefParse(ctxt, caps, xmlopt, flags); + cleanup: + xmlXPathFreeContext(ctxt); + return def; +} + +virDomainCheckpointDefPtr +virDomainCheckpointDefParseString(const char *xmlStr, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainCheckpointDefPtr ret = NULL; + xmlDocPtr xml; + int keepBlanksDefault = xmlKeepBlanksDefault(0); + + if ((xml = virXMLParse(NULL, xmlStr, _("(domain_checkpoint)")))) { + xmlKeepBlanksDefault(keepBlanksDefault); + ret = virDomainCheckpointDefParseNode(xml, xmlDocGetRootElement(xml), + caps, xmlopt, flags); + xmlFreeDoc(xml); + } + xmlKeepBlanksDefault(keepBlanksDefault); + + return ret; +} + + +/** + * virDomainCheckpointDefAssignBitmapNames: + * @def: checkpoint def object + * + * Generate default bitmap names for checkpoint targets. Returns 0 on + * success, -1 on error. + */ +static int +virDomainCheckpointDefAssignBitmapNames(virDomainCheckpointDefPtr def) +{ + size_t i; + + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &def->disks[i]; + + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP || + disk->bitmap) + continue; + + if (VIR_STRDUP(disk->bitmap, def->parent.name) < 0) + return -1; + } + + return 0; +} + + +static int +virDomainCheckpointCompareDiskIndex(const void *a, const void *b) +{ + const virDomainCheckpointDiskDef *diska = a; + const virDomainCheckpointDiskDef *diskb = b; + + /* Integer overflow shouldn't be a problem here. */ + return diska->idx - diskb->idx; +} + +/* Align def->disks to def->domain. Sort the list of def->disks, + * filling in any missing disks with appropriate default. Convert + * paths to disk targets for uniformity. Issue an error and return -1 + * if any def->disks[n]->name appears more than once or does not map + * to dom->disks. */ +int +virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr def) +{ + int ret = -1; + virBitmapPtr map = NULL; + size_t i; + int ndisks; + int checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_NONE; + + if (!def->parent.dom) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint")); + goto cleanup; + } + + if (def->ndisks > def->parent.dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("too many disk checkpoint requests for domain")); + goto cleanup; + } + + /* Unlikely to have a guest without disks but technically possible. */ + if (!def->parent.dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("domain must have at least one disk to perform " + "checkpoints")); + goto cleanup; + } + + /* If <disks> omitted, do bitmap on all writeable disks; + * otherwise, do nothing for omitted disks */ + if (!def->ndisks) + checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + + if (!(map = virBitmapNew(def->parent.dom->ndisks))) + goto cleanup; + + /* Double check requested disks. */ + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &def->disks[i]; + int idx = virDomainDiskIndexByName(def->parent.dom, disk->name, false); + + if (idx < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("no disk named '%s'"), disk->name); + goto cleanup; + } + + if (virBitmapIsBitSet(map, idx)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk '%s' specified twice"), + disk->name); + goto cleanup; + } + if ((virStorageSourceIsEmpty(def->parent.dom->disks[idx]->src) || + def->parent.dom->disks[idx]->src->readonly) && + disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_NONE) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk '%s' is empty or readonly"), + disk->name); + goto cleanup; + } + ignore_value(virBitmapSetBit(map, idx)); + disk->idx = idx; + + if (STRNEQ(disk->name, def->parent.dom->disks[idx]->dst)) { + VIR_FREE(disk->name); + if (VIR_STRDUP(disk->name, def->parent.dom->disks[idx]->dst) < 0) + goto cleanup; + } + } + + /* Provide defaults for all remaining disks. */ + ndisks = def->ndisks; + if (VIR_EXPAND_N(def->disks, def->ndisks, + def->parent.dom->ndisks - def->ndisks) < 0) + goto cleanup; + + for (i = 0; i < def->parent.dom->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk; + + if (virBitmapIsBitSet(map, i)) + continue; + disk = &def->disks[ndisks++]; + if (VIR_STRDUP(disk->name, def->parent.dom->disks[i]->dst) < 0) + goto cleanup; + disk->idx = i; + + /* Don't checkpoint empty or readonly drives */ + if (virStorageSourceIsEmpty(def->parent.dom->disks[i]->src) || + def->parent.dom->disks[i]->src->readonly) + disk->type = VIR_DOMAIN_CHECKPOINT_TYPE_NONE; + else + disk->type = checkpoint_default; + } + + qsort(&def->disks[0], def->ndisks, sizeof(def->disks[0]), + virDomainCheckpointCompareDiskIndex); + + /* Generate default bitmap names for checkpoint */ + if (virDomainCheckpointDefAssignBitmapNames(def) < 0) + goto cleanup; + + ret = 0; + + cleanup: + virBitmapFree(map); + return ret; +} + + +/* Converts public VIR_DOMAIN_CHECKPOINT_XML_* into + * VIR_DOMAIN_CHECKPOINT_FORMAT_* flags, and silently ignores any other + * flags. */ +unsigned int virDomainCheckpointFormatConvertXMLFlags(unsigned int flags) +{ + unsigned int formatFlags = 0; + + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SECURE) + formatFlags |= VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE; + if (flags & VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN) + formatFlags |= VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN; + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SIZE) + formatFlags |= VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE; + + return formatFlags; +} + + +static int +virDomainCheckpointDiskDefFormat(virBufferPtr buf, + virDomainCheckpointDiskDefPtr disk, + unsigned int flags) +{ + if (!disk->name) + return 0; + + virBufferEscapeString(buf, "<disk name='%s'", disk->name); + if (disk->type) + virBufferAsprintf(buf, " checkpoint='%s'", + virDomainCheckpointTypeToString(disk->type)); + if (disk->bitmap) { + virBufferEscapeString(buf, " bitmap='%s'", disk->bitmap); + if (flags & VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE) + virBufferAsprintf(buf, " size='%llu'", disk->size); + } + virBufferAddLit(buf, "/>\n"); + return 0; +} + + +static int +virDomainCheckpointDefFormatInternal(virBufferPtr buf, + virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + size_t i; + unsigned int domainflags = VIR_DOMAIN_DEF_FORMAT_INACTIVE; + + if (flags & VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE) + domainflags |= VIR_DOMAIN_DEF_FORMAT_SECURE; + + virBufferAddLit(buf, "<domaincheckpoint>\n"); + virBufferAdjustIndent(buf, 2); + + virBufferEscapeString(buf, "<name>%s</name>\n", def->parent.name); + virBufferEscapeString(buf, "<description>%s</description>\n", + def->parent.description); + + if (def->parent.parent_name) { + virBufferAddLit(buf, "<parent>\n"); + virBufferAdjustIndent(buf, 2); + virBufferEscapeString(buf, "<name>%s</name>\n", + def->parent.parent_name); + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</parent>\n"); + } + + if (def->parent.creationTime) + virBufferAsprintf(buf, "<creationTime>%lld</creationTime>\n", + def->parent.creationTime); + + if (def->ndisks) { + virBufferAddLit(buf, "<disks>\n"); + virBufferAdjustIndent(buf, 2); + for (i = 0; i < def->ndisks; i++) { + if (virDomainCheckpointDiskDefFormat(buf, &def->disks[i], + flags) < 0) + goto error; + } + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</disks>\n"); + } + + if (!(flags & VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN) && + virDomainDefFormatInternal(def->parent.dom, caps, domainflags, buf, + xmlopt) < 0) + goto error; + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</domaincheckpoint>\n"); + + if (virBufferCheckError(buf) < 0) + goto error; + + return 0; + + error: + virBufferFreeAndReset(buf); + return -1; +} + +char * +virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE | + VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN | + VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE, NULL); + if (virDomainCheckpointDefFormatInternal(&buf, def, caps, xmlopt, + flags) < 0) + return NULL; + + return virBufferContentAndReset(&buf); +} diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ff5a77b0e2..693c75b45d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -69,6 +69,14 @@ virCapabilitiesSetHostCPU; virCapabilitiesSetNetPrefix; +# conf/checkpoint_conf.h +virDomainCheckpointDefFormat; +virDomainCheckpointDefNew; +virDomainCheckpointDefParseString; +virDomainCheckpointTypeFromString; +virDomainCheckpointTypeToString; + + # conf/cpu_conf.h virCPUCacheModeTypeFromString; virCPUCacheModeTypeToString; diff --git a/tests/Makefile.am b/tests/Makefile.am index 9e2e57459e..544328bb18 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -276,7 +276,7 @@ endif WITH_LIBXL if WITH_QEMU test_programs += qemuxml2argvtest qemuxml2xmltest \ - qemudomainsnapshotxml2xmltest \ + qemudomaincheckpointxml2xmltest qemudomainsnapshotxml2xmltest \ qemumonitorjsontest qemuhotplugtest \ qemuagenttest qemucapabilitiestest qemucaps2xmltest \ qemumemlocktest \ @@ -647,6 +647,11 @@ qemublocktest_LDADD = \ $(qemu_LDADDS) \ $(NULL) +qemudomaincheckpointxml2xmltest_SOURCES = \ + qemudomaincheckpointxml2xmltest.c testutilsqemu.c testutilsqemu.h \ + testutils.c testutils.h +qemudomaincheckpointxml2xmltest_LDADD = $(qemu_LDADDS) + qemudomainsnapshotxml2xmltest_SOURCES = \ qemudomainsnapshotxml2xmltest.c testutilsqemu.c testutilsqemu.h \ testutils.c testutils.h @@ -682,7 +687,7 @@ qemufirmwaretest_LDADD = $(qemu_LDADDS) else ! WITH_QEMU EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c \ - qemudomainsnapshotxml2xmltest.c \ + qemudomaincheckpointxml2xmltest.c qemudomainsnapshotxml2xmltest.c \ testutilsqemu.c testutilsqemu.h \ testutilsqemuschema.c testutilsqemuschema.h \ qemumonitorjsontest.c qemuhotplugtest.c \ diff --git a/tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml b/tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml new file mode 100644 index 0000000000..a518c58915 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml @@ -0,0 +1,53 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <parent> + <name>1525111885</name> + </parent> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + <disk name='vdb' checkpoint='no'/> + </disks> + <domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu-system-i686</emulator> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/data.img'/> + <target dev='vda' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> + </disk> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/logs.img'/> + <target dev='vdb' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> + </disk> + <controller type='usb' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> + </controller> + <controller type='ide' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> + </controller> + <controller type='pci' index='0' model='pci-root'/> + <input type='mouse' bus='ps2'/> + <input type='keyboard' bus='ps2'/> + <memballoon model='none'/> + </devices> + </domain> + <active>1</active> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml b/tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml new file mode 100644 index 0000000000..df14c97836 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml @@ -0,0 +1,53 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <parent> + <name>1525111885</name> + </parent> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + <disk name='vdb' checkpoint='no'/> + </disks> + <domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu-system-i686</emulator> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/data.img'/> + <target dev='vda' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> + </disk> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/logs.img'/> + <target dev='vdb' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> + </disk> + <controller type='usb' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> + </controller> + <controller type='ide' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> + </controller> + <controller type='pci' index='0' model='pci-root'/> + <input type='mouse' bus='ps2'/> + <input type='keyboard' bus='ps2'/> + <memballoon model='none'/> + </devices> + </domain> + <active>0</active> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmltest.c b/tests/qemudomaincheckpointxml2xmltest.c new file mode 100644 index 0000000000..8a7c0922c7 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmltest.c @@ -0,0 +1,213 @@ +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include <sys/types.h> +#include <fcntl.h> + +#include "testutils.h" + +#ifdef WITH_QEMU + +# include "internal.h" +# include "qemu/qemu_conf.h" +# include "qemu/qemu_domain.h" +# include "checkpoint_conf.h" +# include "testutilsqemu.h" +# include "virstring.h" + +# define VIR_FROM_THIS VIR_FROM_NONE + +static virQEMUDriver driver; + +enum { + TEST_REDEFINE = 1 << 0, /* Test use of REDEFINE parse flag */ + TEST_PARENT = 1 << 1, /* hard-code parent after parse */ + TEST_VDA_BITMAP = 1 << 2, /* hard-code disk vda after parse */ + TEST_SIZE = 1 << 3, /* Test use of SIZE format flag */ + TEST_INVALID = 1 << 4, /* Test that input fails parse */ +}; + +static int +testCompareXMLToXMLFiles(const char *inxml, + const char *outxml, + unsigned int flags) +{ + unsigned int parseflags = 0; + unsigned int formatflags = VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE; + VIR_AUTOFREE(char *) inXmlData = NULL; + VIR_AUTOFREE(char *) outXmlData = NULL; + VIR_AUTOFREE(char *) actual = NULL; + VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL; + + if (flags & TEST_REDEFINE) + parseflags |= VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + + if (virTestLoadFile(inxml, &inXmlData) < 0) + return -1; + + if (!(flags & TEST_INVALID) && + virTestLoadFile(outxml, &outXmlData) < 0) + return -1; + + if (!(def = virDomainCheckpointDefParseString(inXmlData, driver.caps, + driver.xmlopt, + parseflags))) { + if (flags & TEST_INVALID) + return 0; + return -1; + } + if (flags & TEST_PARENT) { + if (def->parent.parent_name) + return -1; + if (VIR_STRDUP(def->parent.parent_name, "1525111885") < 0) + return -1; + } + if (flags & TEST_VDA_BITMAP) { + virDomainCheckpointDiskDefPtr disk; + + if (VIR_EXPAND_N(def->disks, def->ndisks, 1) < 0) + return -1; + disk = &def->disks[0]; + if (disk->bitmap) + return -1; + if (!disk->name) { + disk->type = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + if (VIR_STRDUP(disk->name, "vda") < 0) + return -1; + } else if (STRNEQ(disk->name, "vda")) { + return -1; + } + if (VIR_STRDUP(disk->bitmap, def->parent.name) < 0) + return -1; + } + if (flags & TEST_SIZE) { + def->disks[0].size = 1048576; + formatflags |= VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE; + } + + /* Parsing XML does not populate the domain definition; work + * around that by not requesting domain on output */ + if (!def->parent.dom) + formatflags |= VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN; + + if (!(actual = virDomainCheckpointDefFormat(def, driver.caps, + driver.xmlopt, + formatflags))) + return -1; + + if (STRNEQ(outXmlData, actual)) { + virTestDifferenceFull(stderr, outXmlData, outxml, actual, inxml); + return -1; + } + + return 0; +} + +struct testInfo { + const char *inxml; + const char *outxml; + long long creationTime; + unsigned int flags; +}; +static long long mocktime; + +static int +testCheckpointPostParse(virDomainMomentDefPtr def) +{ + if (!mocktime) + return 0; + if (def->creationTime) + return -1; + def->creationTime = mocktime; + if (!def->name && + virAsprintf(&def->name, "%lld", def->creationTime) < 0) + return -1; + return 0; +} + +static int +testCompareXMLToXMLHelper(const void *data) +{ + const struct testInfo *info = data; + + mocktime = info->creationTime; + return testCompareXMLToXMLFiles(info->inxml, info->outxml, info->flags); +} + + +static int +mymain(void) +{ + int ret = 0; + + if (qemuTestDriverInit(&driver) < 0) + return EXIT_FAILURE; + + virDomainXMLOptionSetMomentPostParse(driver.xmlopt, + testCheckpointPostParse); + +# define DO_TEST(prefix, name, inpath, outpath, time, flags) \ + do { \ + const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml", \ + abs_srcdir "/" outpath "/" name ".xml", \ + time, flags}; \ + if (virTestRun("CHECKPOINT XML-2-XML " prefix " " name, \ + testCompareXMLToXMLHelper, &info) < 0) \ + ret = -1; \ + } while (0) + +# define DO_TEST_INOUT(name, time, flags) \ + DO_TEST("in->out", name, \ + "qemudomaincheckpointxml2xmlin", \ + "qemudomaincheckpointxml2xmlout", \ + time, flags) +# define DO_TEST_OUT(name, flags) \ + DO_TEST("out->out", name, \ + "qemudomaincheckpointxml2xmlout", \ + "qemudomaincheckpointxml2xmlout", \ + 0, flags | TEST_REDEFINE) +# define DO_TEST_INVALID(name) \ + DO_TEST("in->out", name, \ + "qemudomaincheckpointxml2xmlin", \ + "qemudomaincheckpointxml2xmlout", \ + 0, TEST_INVALID) + + /* Unset or set all envvars here that are copied in qemudBuildCommandLine + * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected + * values for these envvars */ + setenv("PATH", "/bin", 1); + + /* Test a normal user redefine */ + DO_TEST_OUT("redefine", 0); + + /* Tests of valid user input, and resulting output */ + DO_TEST_INOUT("empty", 1525889631, TEST_VDA_BITMAP); + DO_TEST_INOUT("disk-default", 1525889631, TEST_PARENT | TEST_VDA_BITMAP); + DO_TEST_INOUT("sample", 1525889631, TEST_PARENT | TEST_VDA_BITMAP); + DO_TEST_INOUT("size", 1553648510, + TEST_PARENT | TEST_VDA_BITMAP | TEST_SIZE); + + /* Tests of invalid user input */ + DO_TEST_INVALID("disk-invalid"); + DO_TEST_INVALID("name-invalid"); + + qemuTestDriverFree(&driver); + + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIR_TEST_MAIN(mymain) + +#else + +int +main(void) +{ + return EXIT_AM_SKIP; +} + +#endif /* WITH_QEMU */ -- 2.20.1

On Wed, Jul 24, 2019 at 12:55:58AM -0500, Eric Blake wrote:
Add a new file checkpoint_conf.c that performs the translation to and from new XML describing a checkpoint. The code shares a common base class with snapshots, since a checkpoint similarly represents the domain state at a moment in time. Add some basic testing of round trip XML handling through the new code.
Of note - this code intentionally differs from snapshots in that XML schema validation is unconditional, rather than based on a public API flag.
I'm in two minds as to whether this is really a good idea. I always have a strong tendancy to prefer consistency across the APIs we expose, which would point to an opt-in flag for validation. If we did later find we need a way to skip validation, then we end up having to add a NO_VALIDATION API flag, which makes inconsitency even more glaring to app devs I understand that this can help catch mistakes in apps, but I'm not sold on it as a default, when it is easy for apps to opt-in if they want to.
Also, the redefine flag requires the <domain> sub-element to be present, rather than catering to historical back-compat to older versions.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 84 +++ src/conf/virconftypes.h | 3 + po/POTFILES | 1 + src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 535 ++++++++++++++++++ src/libvirt_private.syms | 8 + tests/Makefile.am | 9 +- .../internal-active-invalid.xml | 53 ++ .../internal-inactive-invalid.xml | 53 ++ tests/qemudomaincheckpointxml2xmltest.c | 213 +++++++ 10 files changed, 959 insertions(+), 2 deletions(-) create mode 100644 src/conf/checkpoint_conf.h create mode 100644 src/conf/checkpoint_conf.c create mode 100644 tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml create mode 100644 tests/qemudomaincheckpointxml2xmltest.c
diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h new file mode 100644 index 0000000000..0fac521efe --- /dev/null +++ b/src/conf/checkpoint_conf.h @@ -0,0 +1,84 @@ +/* + * checkpoint_conf.h: domain checkpoint XML processing + * (based on snapshot_conf.h) + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "internal.h" +#include "domain_conf.h" +#include "moment_conf.h" +#include "virobject.h" + +/* Items related to checkpoint state */ + +typedef enum { + VIR_DOMAIN_CHECKPOINT_TYPE_DEFAULT = 0, + VIR_DOMAIN_CHECKPOINT_TYPE_NONE, + VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP, + + VIR_DOMAIN_CHECKPOINT_TYPE_LAST +} virDomainCheckpointType; + +/* Stores disk-checkpoint information */ +typedef struct _virDomainCheckpointDiskDef virDomainCheckpointDiskDef; +typedef virDomainCheckpointDiskDef *virDomainCheckpointDiskDefPtr; +struct _virDomainCheckpointDiskDef { + char *name; /* name matching the <target dev='...' of the domain */ + int idx; /* index within checkpoint->dom->disks that matches name */ + int type; /* virDomainCheckpointType */ + char *bitmap; /* bitmap name, if type is bitmap */ + unsigned long long size; /* current checkpoint size in bytes */ +}; + +/* Stores the complete checkpoint metadata */ +struct _virDomainCheckpointDef { + virDomainMomentDef parent; + + /* Additional Public XML. */ + size_t ndisks; /* should not exceed dom->ndisks */ + virDomainCheckpointDiskDef *disks; +}; + + +typedef enum { + VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE = 1 << 0, +} virDomainCheckpointParseFlags; + +typedef enum { + VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE = 1 << 0, + VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN = 1 << 1, + VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE = 1 << 2, +} virDomainCheckpointFormatFlags; + +unsigned int virDomainCheckpointFormatConvertXMLFlags(unsigned int flags); + +virDomainCheckpointDefPtr virDomainCheckpointDefParseString(const char *xmlStr, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +virDomainCheckpointDefPtr virDomainCheckpointDefNew(void); +char *virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +int virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr checkpoint); + +VIR_ENUM_DECL(virDomainCheckpoint); diff --git a/src/conf/virconftypes.h b/src/conf/virconftypes.h index b8f553f7fb..fe3f0af14f 100644 --- a/src/conf/virconftypes.h +++ b/src/conf/virconftypes.h @@ -102,6 +102,9 @@ typedef virDomainBlkiotune *virDomainBlkiotunePtr; typedef struct _virDomainBlockIoTuneInfo virDomainBlockIoTuneInfo; typedef virDomainBlockIoTuneInfo *virDomainBlockIoTuneInfoPtr;
+typedef struct _virDomainCheckpointDef virDomainCheckpointDef; +typedef virDomainCheckpointDef *virDomainCheckpointDefPtr; + typedef struct _virDomainChrDef virDomainChrDef; typedef virDomainChrDef *virDomainChrDefPtr;
diff --git a/po/POTFILES b/po/POTFILES index 5d0bf43f02..2c27049d55 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -15,6 +15,7 @@ src/bhyve/bhyve_monitor.c src/bhyve/bhyve_parse_command.c src/bhyve/bhyve_process.c src/conf/capabilities.c +src/conf/checkpoint_conf.c src/conf/cpu_conf.c src/conf/device_conf.c src/conf/domain_addr.c diff --git a/src/conf/Makefile.inc.am b/src/conf/Makefile.inc.am index 16a21c34f3..f656725e3a 100644 --- a/src/conf/Makefile.inc.am +++ b/src/conf/Makefile.inc.am @@ -14,6 +14,8 @@ NETDEV_CONF_SOURCES = \ DOMAIN_CONF_SOURCES = \ conf/capabilities.c \ conf/capabilities.h \ + conf/checkpoint_conf.c \ + conf/checkpoint_conf.h \ conf/domain_addr.c \ conf/domain_addr.h \ conf/domain_capabilities.c \ diff --git a/src/conf/checkpoint_conf.c b/src/conf/checkpoint_conf.c new file mode 100644 index 0000000000..69c9040a43 --- /dev/null +++ b/src/conf/checkpoint_conf.c @@ -0,0 +1,535 @@ +/* + * checkpoint_conf.c: domain checkpoint XML processing + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "configmake.h" +#include "internal.h" +#include "virbitmap.h" +#include "virbuffer.h" +#include "datatypes.h" +#include "domain_conf.h" +#include "virlog.h" +#include "viralloc.h" +#include "checkpoint_conf.h" +#include "virstoragefile.h" +#include "viruuid.h" +#include "virfile.h" +#include "virerror.h" +#include "virxml.h" +#include "virstring.h" + +#define VIR_FROM_THIS VIR_FROM_DOMAIN_CHECKPOINT + +VIR_LOG_INIT("conf.checkpoint_conf"); + +static virClassPtr virDomainCheckpointDefClass; +static void virDomainCheckpointDefDispose(void *obj); + +static int +virDomainCheckpointOnceInit(void) +{ + if (!VIR_CLASS_NEW(virDomainCheckpointDef, virClassForDomainMomentDef())) + return -1; + + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virDomainCheckpoint); + +VIR_ENUM_IMPL(virDomainCheckpoint, + VIR_DOMAIN_CHECKPOINT_TYPE_LAST, + "default", "no", "bitmap"); + + +/* Checkpoint Def functions */ +static void +virDomainCheckpointDiskDefClear(virDomainCheckpointDiskDefPtr disk) +{ + VIR_FREE(disk->name); + VIR_FREE(disk->bitmap); +} + +/* Allocate a new virDomainCheckpointDef; free with virObjectUnref() */ +virDomainCheckpointDefPtr +virDomainCheckpointDefNew(void) +{ + virDomainCheckpointDefPtr def; + + if (virDomainCheckpointInitialize() < 0) + return NULL; + + def = virObjectNew(virDomainCheckpointDefClass); + return def; +} + +static void +virDomainCheckpointDefDispose(void *obj) +{ + virDomainCheckpointDefPtr def = obj; + size_t i; + + for (i = 0; i < def->ndisks; i++) + virDomainCheckpointDiskDefClear(&def->disks[i]); + VIR_FREE(def->disks); +} + +static int +virDomainCheckpointDiskDefParseXML(xmlNodePtr node, + xmlXPathContextPtr ctxt, + virDomainCheckpointDiskDefPtr def) +{ + VIR_AUTOFREE(char *) checkpoint = NULL; + VIR_XPATH_NODE_AUTORESTORE(ctxt); + + ctxt->node = node; + + /* Schema guarantees this is non-NULL: */ + def->name = virXMLPropString(node, "name"); + + checkpoint = virXMLPropString(node, "checkpoint"); + if (checkpoint) + /* Schema guarantees this is in range: */ + def->type = virDomainCheckpointTypeFromString(checkpoint); + else + def->type = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + + def->bitmap = virXMLPropString(node, "bitmap"); + + return 0; +} + +/* flags is bitwise-or of virDomainCheckpointParseFlags. If flags + * does not include VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE, then caps + * is ignored. + */ +static virDomainCheckpointDefPtr +virDomainCheckpointDefParse(xmlXPathContextPtr ctxt, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainCheckpointDefPtr ret = NULL; + size_t i; + int n; + char *tmp; + VIR_AUTOFREE(xmlNodePtr *) nodes = NULL; + VIR_AUTOFREE(char *)creation = NULL; + VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL; + + if (!(def = virDomainCheckpointDefNew())) + return NULL; + + def->parent.name = virXPathString("string(./name)", ctxt); + + if (def->parent.name == NULL) { + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("a redefined checkpoint must have a name")); + return NULL; + } + } + + def->parent.description = virXPathString("string(./description)", ctxt); + + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE) { + if (virXPathLongLong("string(./creationTime)", ctxt, + &def->parent.creationTime) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing creationTime from existing checkpoint")); + return NULL; + } + + def->parent.parent_name = virXPathString("string(./parent/name)", ctxt); + + if ((tmp = virXPathString("string(./domain/@type)", ctxt))) { + int domainflags = VIR_DOMAIN_DEF_PARSE_INACTIVE | + VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE; + xmlNodePtr domainNode = virXPathNode("./domain", ctxt); + + VIR_FREE(tmp); + if (!domainNode) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint")); + return NULL; + } + def->parent.dom = virDomainDefParseNode(ctxt->node->doc, domainNode, + caps, xmlopt, NULL, + domainflags); + if (!def->parent.dom) + return NULL; + } else { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint redefine")); + return NULL; + } + } else if (virDomainXMLOptionRunMomentPostParse(xmlopt, &def->parent) < 0) { + return NULL; + } + + if ((n = virXPathNodeSet("./disks/*", ctxt, &nodes)) < 0) + return NULL; + if (n && VIR_ALLOC_N(def->disks, n) < 0) + return NULL; + def->ndisks = n; + for (i = 0; i < def->ndisks; i++) { + if (virDomainCheckpointDiskDefParseXML(nodes[i], ctxt, + &def->disks[i]) < 0) + return NULL; + } + + VIR_STEAL_PTR(ret, def); + return ret; +} + +static virDomainCheckpointDefPtr +virDomainCheckpointDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + xmlXPathContextPtr ctxt = NULL; + virDomainCheckpointDefPtr def = NULL; + VIR_AUTOFREE(char *) schema = NULL; + + if (!virXMLNodeNameEqual(root, "domaincheckpoint")) { + virReportError(VIR_ERR_XML_ERROR, "%s", _("domaincheckpoint")); + goto cleanup; + } + + /* This is a new enough API to make schema validation unconditional */ + schema = virFileFindResource("domaincheckpoint.rng", + abs_top_srcdir "/docs/schemas", + PKGDATADIR "/schemas"); + if (!schema) + return NULL; + if (virXMLValidateAgainstSchema(schema, xml) < 0) + return NULL; + + ctxt = xmlXPathNewContext(xml); + if (ctxt == NULL) { + virReportOOMError(); + goto cleanup; + } + + ctxt->node = root; + def = virDomainCheckpointDefParse(ctxt, caps, xmlopt, flags); + cleanup: + xmlXPathFreeContext(ctxt); + return def; +} + +virDomainCheckpointDefPtr +virDomainCheckpointDefParseString(const char *xmlStr, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainCheckpointDefPtr ret = NULL; + xmlDocPtr xml; + int keepBlanksDefault = xmlKeepBlanksDefault(0); + + if ((xml = virXMLParse(NULL, xmlStr, _("(domain_checkpoint)")))) { + xmlKeepBlanksDefault(keepBlanksDefault); + ret = virDomainCheckpointDefParseNode(xml, xmlDocGetRootElement(xml), + caps, xmlopt, flags); + xmlFreeDoc(xml); + } + xmlKeepBlanksDefault(keepBlanksDefault); + + return ret; +} + + +/** + * virDomainCheckpointDefAssignBitmapNames: + * @def: checkpoint def object + * + * Generate default bitmap names for checkpoint targets. Returns 0 on + * success, -1 on error. + */ +static int +virDomainCheckpointDefAssignBitmapNames(virDomainCheckpointDefPtr def) +{ + size_t i; + + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &def->disks[i]; + + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP || + disk->bitmap) + continue; + + if (VIR_STRDUP(disk->bitmap, def->parent.name) < 0) + return -1; + } + + return 0; +} + + +static int +virDomainCheckpointCompareDiskIndex(const void *a, const void *b) +{ + const virDomainCheckpointDiskDef *diska = a; + const virDomainCheckpointDiskDef *diskb = b; + + /* Integer overflow shouldn't be a problem here. */ + return diska->idx - diskb->idx; +} + +/* Align def->disks to def->domain. Sort the list of def->disks, + * filling in any missing disks with appropriate default. Convert + * paths to disk targets for uniformity. Issue an error and return -1 + * if any def->disks[n]->name appears more than once or does not map + * to dom->disks. */ +int +virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr def) +{ + int ret = -1; + virBitmapPtr map = NULL; + size_t i; + int ndisks; + int checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_NONE; + + if (!def->parent.dom) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint")); + goto cleanup; + } + + if (def->ndisks > def->parent.dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("too many disk checkpoint requests for domain")); + goto cleanup; + } + + /* Unlikely to have a guest without disks but technically possible. */ + if (!def->parent.dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("domain must have at least one disk to perform " + "checkpoints")); + goto cleanup; + } + + /* If <disks> omitted, do bitmap on all writeable disks; + * otherwise, do nothing for omitted disks */ + if (!def->ndisks) + checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + + if (!(map = virBitmapNew(def->parent.dom->ndisks))) + goto cleanup; + + /* Double check requested disks. */ + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &def->disks[i]; + int idx = virDomainDiskIndexByName(def->parent.dom, disk->name, false); + + if (idx < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("no disk named '%s'"), disk->name); + goto cleanup; + } + + if (virBitmapIsBitSet(map, idx)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk '%s' specified twice"), + disk->name); + goto cleanup; + } + if ((virStorageSourceIsEmpty(def->parent.dom->disks[idx]->src) || + def->parent.dom->disks[idx]->src->readonly) && + disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_NONE) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk '%s' is empty or readonly"), + disk->name); + goto cleanup; + } + ignore_value(virBitmapSetBit(map, idx)); + disk->idx = idx; + + if (STRNEQ(disk->name, def->parent.dom->disks[idx]->dst)) { + VIR_FREE(disk->name); + if (VIR_STRDUP(disk->name, def->parent.dom->disks[idx]->dst) < 0) + goto cleanup; + } + } + + /* Provide defaults for all remaining disks. */ + ndisks = def->ndisks; + if (VIR_EXPAND_N(def->disks, def->ndisks, + def->parent.dom->ndisks - def->ndisks) < 0) + goto cleanup; + + for (i = 0; i < def->parent.dom->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk; + + if (virBitmapIsBitSet(map, i)) + continue; + disk = &def->disks[ndisks++]; + if (VIR_STRDUP(disk->name, def->parent.dom->disks[i]->dst) < 0) + goto cleanup; + disk->idx = i; + + /* Don't checkpoint empty or readonly drives */ + if (virStorageSourceIsEmpty(def->parent.dom->disks[i]->src) || + def->parent.dom->disks[i]->src->readonly) + disk->type = VIR_DOMAIN_CHECKPOINT_TYPE_NONE; + else + disk->type = checkpoint_default; + } + + qsort(&def->disks[0], def->ndisks, sizeof(def->disks[0]), + virDomainCheckpointCompareDiskIndex); + + /* Generate default bitmap names for checkpoint */ + if (virDomainCheckpointDefAssignBitmapNames(def) < 0) + goto cleanup; + + ret = 0; + + cleanup: + virBitmapFree(map); + return ret; +} + + +/* Converts public VIR_DOMAIN_CHECKPOINT_XML_* into + * VIR_DOMAIN_CHECKPOINT_FORMAT_* flags, and silently ignores any other + * flags. */ +unsigned int virDomainCheckpointFormatConvertXMLFlags(unsigned int flags) +{ + unsigned int formatFlags = 0; + + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SECURE) + formatFlags |= VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE; + if (flags & VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN) + formatFlags |= VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN; + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SIZE) + formatFlags |= VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE; + + return formatFlags; +} + + +static int +virDomainCheckpointDiskDefFormat(virBufferPtr buf, + virDomainCheckpointDiskDefPtr disk, + unsigned int flags) +{ + if (!disk->name) + return 0; + + virBufferEscapeString(buf, "<disk name='%s'", disk->name); + if (disk->type) + virBufferAsprintf(buf, " checkpoint='%s'", + virDomainCheckpointTypeToString(disk->type)); + if (disk->bitmap) { + virBufferEscapeString(buf, " bitmap='%s'", disk->bitmap); + if (flags & VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE) + virBufferAsprintf(buf, " size='%llu'", disk->size); + } + virBufferAddLit(buf, "/>\n"); + return 0; +} + + +static int +virDomainCheckpointDefFormatInternal(virBufferPtr buf, + virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + size_t i; + unsigned int domainflags = VIR_DOMAIN_DEF_FORMAT_INACTIVE; + + if (flags & VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE) + domainflags |= VIR_DOMAIN_DEF_FORMAT_SECURE; + + virBufferAddLit(buf, "<domaincheckpoint>\n"); + virBufferAdjustIndent(buf, 2); + + virBufferEscapeString(buf, "<name>%s</name>\n", def->parent.name); + virBufferEscapeString(buf, "<description>%s</description>\n", + def->parent.description); + + if (def->parent.parent_name) { + virBufferAddLit(buf, "<parent>\n"); + virBufferAdjustIndent(buf, 2); + virBufferEscapeString(buf, "<name>%s</name>\n", + def->parent.parent_name); + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</parent>\n"); + } + + if (def->parent.creationTime) + virBufferAsprintf(buf, "<creationTime>%lld</creationTime>\n", + def->parent.creationTime); + + if (def->ndisks) { + virBufferAddLit(buf, "<disks>\n"); + virBufferAdjustIndent(buf, 2); + for (i = 0; i < def->ndisks; i++) { + if (virDomainCheckpointDiskDefFormat(buf, &def->disks[i], + flags) < 0) + goto error; + } + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</disks>\n"); + } + + if (!(flags & VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN) && + virDomainDefFormatInternal(def->parent.dom, caps, domainflags, buf, + xmlopt) < 0) + goto error; + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</domaincheckpoint>\n"); + + if (virBufferCheckError(buf) < 0) + goto error; + + return 0; + + error: + virBufferFreeAndReset(buf); + return -1; +} + +char * +virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE | + VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN | + VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE, NULL); + if (virDomainCheckpointDefFormatInternal(&buf, def, caps, xmlopt, + flags) < 0) + return NULL; + + return virBufferContentAndReset(&buf); +} diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ff5a77b0e2..693c75b45d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -69,6 +69,14 @@ virCapabilitiesSetHostCPU; virCapabilitiesSetNetPrefix;
+# conf/checkpoint_conf.h +virDomainCheckpointDefFormat; +virDomainCheckpointDefNew; +virDomainCheckpointDefParseString; +virDomainCheckpointTypeFromString; +virDomainCheckpointTypeToString; + + # conf/cpu_conf.h virCPUCacheModeTypeFromString; virCPUCacheModeTypeToString; diff --git a/tests/Makefile.am b/tests/Makefile.am index 9e2e57459e..544328bb18 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -276,7 +276,7 @@ endif WITH_LIBXL
if WITH_QEMU test_programs += qemuxml2argvtest qemuxml2xmltest \ - qemudomainsnapshotxml2xmltest \ + qemudomaincheckpointxml2xmltest qemudomainsnapshotxml2xmltest \ qemumonitorjsontest qemuhotplugtest \ qemuagenttest qemucapabilitiestest qemucaps2xmltest \ qemumemlocktest \ @@ -647,6 +647,11 @@ qemublocktest_LDADD = \ $(qemu_LDADDS) \ $(NULL)
+qemudomaincheckpointxml2xmltest_SOURCES = \ + qemudomaincheckpointxml2xmltest.c testutilsqemu.c testutilsqemu.h \ + testutils.c testutils.h +qemudomaincheckpointxml2xmltest_LDADD = $(qemu_LDADDS) + qemudomainsnapshotxml2xmltest_SOURCES = \ qemudomainsnapshotxml2xmltest.c testutilsqemu.c testutilsqemu.h \ testutils.c testutils.h @@ -682,7 +687,7 @@ qemufirmwaretest_LDADD = $(qemu_LDADDS)
else ! WITH_QEMU EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c \ - qemudomainsnapshotxml2xmltest.c \ + qemudomaincheckpointxml2xmltest.c qemudomainsnapshotxml2xmltest.c \ testutilsqemu.c testutilsqemu.h \ testutilsqemuschema.c testutilsqemuschema.h \ qemumonitorjsontest.c qemuhotplugtest.c \ diff --git a/tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml b/tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml new file mode 100644 index 0000000000..a518c58915 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/internal-active-invalid.xml @@ -0,0 +1,53 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <parent> + <name>1525111885</name> + </parent> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + <disk name='vdb' checkpoint='no'/> + </disks> + <domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu-system-i686</emulator> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/data.img'/> + <target dev='vda' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> + </disk> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/logs.img'/> + <target dev='vdb' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> + </disk> + <controller type='usb' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> + </controller> + <controller type='ide' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> + </controller> + <controller type='pci' index='0' model='pci-root'/> + <input type='mouse' bus='ps2'/> + <input type='keyboard' bus='ps2'/> + <memballoon model='none'/> + </devices> + </domain> + <active>1</active> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml b/tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml new file mode 100644 index 0000000000..df14c97836 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmlout/internal-inactive-invalid.xml @@ -0,0 +1,53 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <parent> + <name>1525111885</name> + </parent> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + <disk name='vdb' checkpoint='no'/> + </disks> + <domain type='qemu'> + <name>QEMUGuest1</name> + <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid> + <memory unit='KiB'>219136</memory> + <currentMemory unit='KiB'>219136</currentMemory> + <vcpu placement='static'>1</vcpu> + <os> + <type arch='i686' machine='pc'>hvm</type> + <boot dev='hd'/> + </os> + <clock offset='utc'/> + <on_poweroff>destroy</on_poweroff> + <on_reboot>restart</on_reboot> + <on_crash>destroy</on_crash> + <devices> + <emulator>/usr/bin/qemu-system-i686</emulator> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/data.img'/> + <target dev='vda' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> + </disk> + <disk type='file' device='disk'> + <driver name='qemu' type='qcow2'/> + <source file='/tmp/logs.img'/> + <target dev='vdb' bus='virtio'/> + <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> + </disk> + <controller type='usb' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> + </controller> + <controller type='ide' index='0'> + <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> + </controller> + <controller type='pci' index='0' model='pci-root'/> + <input type='mouse' bus='ps2'/> + <input type='keyboard' bus='ps2'/> + <memballoon model='none'/> + </devices> + </domain> + <active>0</active> +</domaincheckpoint> diff --git a/tests/qemudomaincheckpointxml2xmltest.c b/tests/qemudomaincheckpointxml2xmltest.c new file mode 100644 index 0000000000..8a7c0922c7 --- /dev/null +++ b/tests/qemudomaincheckpointxml2xmltest.c @@ -0,0 +1,213 @@ +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include <sys/types.h> +#include <fcntl.h> + +#include "testutils.h" + +#ifdef WITH_QEMU + +# include "internal.h" +# include "qemu/qemu_conf.h" +# include "qemu/qemu_domain.h" +# include "checkpoint_conf.h" +# include "testutilsqemu.h" +# include "virstring.h" + +# define VIR_FROM_THIS VIR_FROM_NONE + +static virQEMUDriver driver; + +enum { + TEST_REDEFINE = 1 << 0, /* Test use of REDEFINE parse flag */ + TEST_PARENT = 1 << 1, /* hard-code parent after parse */ + TEST_VDA_BITMAP = 1 << 2, /* hard-code disk vda after parse */ + TEST_SIZE = 1 << 3, /* Test use of SIZE format flag */ + TEST_INVALID = 1 << 4, /* Test that input fails parse */ +}; + +static int +testCompareXMLToXMLFiles(const char *inxml, + const char *outxml, + unsigned int flags) +{ + unsigned int parseflags = 0; + unsigned int formatflags = VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE; + VIR_AUTOFREE(char *) inXmlData = NULL; + VIR_AUTOFREE(char *) outXmlData = NULL; + VIR_AUTOFREE(char *) actual = NULL; + VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL; + + if (flags & TEST_REDEFINE) + parseflags |= VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + + if (virTestLoadFile(inxml, &inXmlData) < 0) + return -1; + + if (!(flags & TEST_INVALID) && + virTestLoadFile(outxml, &outXmlData) < 0) + return -1; + + if (!(def = virDomainCheckpointDefParseString(inXmlData, driver.caps, + driver.xmlopt, + parseflags))) { + if (flags & TEST_INVALID) + return 0; + return -1; + } + if (flags & TEST_PARENT) { + if (def->parent.parent_name) + return -1; + if (VIR_STRDUP(def->parent.parent_name, "1525111885") < 0) + return -1; + } + if (flags & TEST_VDA_BITMAP) { + virDomainCheckpointDiskDefPtr disk; + + if (VIR_EXPAND_N(def->disks, def->ndisks, 1) < 0) + return -1; + disk = &def->disks[0]; + if (disk->bitmap) + return -1; + if (!disk->name) { + disk->type = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + if (VIR_STRDUP(disk->name, "vda") < 0) + return -1; + } else if (STRNEQ(disk->name, "vda")) { + return -1; + } + if (VIR_STRDUP(disk->bitmap, def->parent.name) < 0) + return -1; + } + if (flags & TEST_SIZE) { + def->disks[0].size = 1048576; + formatflags |= VIR_DOMAIN_CHECKPOINT_FORMAT_SIZE; + } + + /* Parsing XML does not populate the domain definition; work + * around that by not requesting domain on output */ + if (!def->parent.dom) + formatflags |= VIR_DOMAIN_CHECKPOINT_FORMAT_NO_DOMAIN; + + if (!(actual = virDomainCheckpointDefFormat(def, driver.caps, + driver.xmlopt, + formatflags))) + return -1; + + if (STRNEQ(outXmlData, actual)) { + virTestDifferenceFull(stderr, outXmlData, outxml, actual, inxml); + return -1; + } + + return 0; +} + +struct testInfo { + const char *inxml; + const char *outxml; + long long creationTime; + unsigned int flags; +}; +static long long mocktime; + +static int +testCheckpointPostParse(virDomainMomentDefPtr def) +{ + if (!mocktime) + return 0; + if (def->creationTime) + return -1; + def->creationTime = mocktime; + if (!def->name && + virAsprintf(&def->name, "%lld", def->creationTime) < 0) + return -1; + return 0; +} + +static int +testCompareXMLToXMLHelper(const void *data) +{ + const struct testInfo *info = data; + + mocktime = info->creationTime; + return testCompareXMLToXMLFiles(info->inxml, info->outxml, info->flags); +} + + +static int +mymain(void) +{ + int ret = 0; + + if (qemuTestDriverInit(&driver) < 0) + return EXIT_FAILURE; + + virDomainXMLOptionSetMomentPostParse(driver.xmlopt, + testCheckpointPostParse); + +# define DO_TEST(prefix, name, inpath, outpath, time, flags) \ + do { \ + const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml", \ + abs_srcdir "/" outpath "/" name ".xml", \ + time, flags}; \ + if (virTestRun("CHECKPOINT XML-2-XML " prefix " " name, \ + testCompareXMLToXMLHelper, &info) < 0) \ + ret = -1; \ + } while (0) + +# define DO_TEST_INOUT(name, time, flags) \ + DO_TEST("in->out", name, \ + "qemudomaincheckpointxml2xmlin", \ + "qemudomaincheckpointxml2xmlout", \ + time, flags) +# define DO_TEST_OUT(name, flags) \ + DO_TEST("out->out", name, \ + "qemudomaincheckpointxml2xmlout", \ + "qemudomaincheckpointxml2xmlout", \ + 0, flags | TEST_REDEFINE) +# define DO_TEST_INVALID(name) \ + DO_TEST("in->out", name, \ + "qemudomaincheckpointxml2xmlin", \ + "qemudomaincheckpointxml2xmlout", \ + 0, TEST_INVALID) + + /* Unset or set all envvars here that are copied in qemudBuildCommandLine + * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected + * values for these envvars */ + setenv("PATH", "/bin", 1); + + /* Test a normal user redefine */ + DO_TEST_OUT("redefine", 0); + + /* Tests of valid user input, and resulting output */ + DO_TEST_INOUT("empty", 1525889631, TEST_VDA_BITMAP); + DO_TEST_INOUT("disk-default", 1525889631, TEST_PARENT | TEST_VDA_BITMAP); + DO_TEST_INOUT("sample", 1525889631, TEST_PARENT | TEST_VDA_BITMAP); + DO_TEST_INOUT("size", 1553648510, + TEST_PARENT | TEST_VDA_BITMAP | TEST_SIZE); + + /* Tests of invalid user input */ + DO_TEST_INVALID("disk-invalid"); + DO_TEST_INVALID("name-invalid"); + + qemuTestDriverFree(&driver); + + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIR_TEST_MAIN(mymain) + +#else + +int +main(void) +{ + return EXIT_AM_SKIP; +} + +#endif /* WITH_QEMU */ -- 2.20.1
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Jul 24, 2019 at 17:11:19 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 12:55:58AM -0500, Eric Blake wrote:
Add a new file checkpoint_conf.c that performs the translation to and from new XML describing a checkpoint. The code shares a common base class with snapshots, since a checkpoint similarly represents the domain state at a moment in time. Add some basic testing of round trip XML handling through the new code.
Of note - this code intentionally differs from snapshots in that XML schema validation is unconditional, rather than based on a public API flag.
I'm in two minds as to whether this is really a good idea.
I always have a strong tendancy to prefer consistency across the APIs we expose, which would point to an opt-in flag for validation. If we did later find we need a way to skip validation, then we end up having to add a NO_VALIDATION API flag, which makes inconsitency even more glaring to app devs
I understand that this can help catch mistakes in apps, but I'm not sold on it as a default, when it is easy for apps to opt-in if they want to.
I've specifically requested this behaviour as I don't think that there's value in not validating the XML. The XML parser is tied to what we parse anyways and the checks done when parsing are _WAY_ simpler if we know that all the values conform to the schema. Also if we'd ever want to do a generated parser, we will need to strictly adhere to the schema first. Consistency is great as long as it does not hinder progress. And in this case I think that validating everything right away is progress.

On Wed, Jul 24, 2019 at 06:17:24PM +0200, Peter Krempa wrote:
On Wed, Jul 24, 2019 at 17:11:19 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 12:55:58AM -0500, Eric Blake wrote:
Add a new file checkpoint_conf.c that performs the translation to and from new XML describing a checkpoint. The code shares a common base class with snapshots, since a checkpoint similarly represents the domain state at a moment in time. Add some basic testing of round trip XML handling through the new code.
Of note - this code intentionally differs from snapshots in that XML schema validation is unconditional, rather than based on a public API flag.
I'm in two minds as to whether this is really a good idea.
I always have a strong tendancy to prefer consistency across the APIs we expose, which would point to an opt-in flag for validation. If we did later find we need a way to skip validation, then we end up having to add a NO_VALIDATION API flag, which makes inconsitency even more glaring to app devs
I understand that this can help catch mistakes in apps, but I'm not sold on it as a default, when it is easy for apps to opt-in if they want to.
I've specifically requested this behaviour as I don't think that there's value in not validating the XML. The XML parser is tied to what we parse anyways and the checks done when parsing are _WAY_ simpler if we know that all the values conform to the schema.
If there is no downside to validation though, why only do it for one schema. We should do it for all of the XML parsers we have. We didn't do this originally because we didn't have confidence in accuracy of the schemas, which was shown a sensible decision many times over as we found a great many schema bugs. None the less, it would still be possible to turn on validation for all our schemas, as simply declare the current _VALIDATE flags are now a no-op. There is the possibility that apps are lazy in creating XML documents adding elements that may not be supported with the version of libvirt they are talking to. That might be caught at parsing time or might be silently ignored with little ill effect. So enforcing validation would mean apps have to be more careful with the XML they feed libvirt. Such enforcement could be considered a good thing even if it risks highlighting existing broken behaviour in apps.
Also if we'd ever want to do a generated parser, we will need to strictly adhere to the schema first. Consistency is great as long as it does not hinder progress. And in this case I think that validating everything right away is progress.
If & when we attempt generated parsers, we still have the option to enforce schema validation at that time, so I don't see this as blocking. So overall, either we should turn on validation for all our schemas, or we should require a VALIDATE flag for this new API. Both avoid special case behaviour with the checkpoint APIs. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Jul 24, 2019 at 17:40:51 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 06:17:24PM +0200, Peter Krempa wrote:
On Wed, Jul 24, 2019 at 17:11:19 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 12:55:58AM -0500, Eric Blake wrote:
[...]
So overall, either we should turn on validation for all our schemas, or we should require a VALIDATE flag for this new API. Both avoid special case behaviour with the checkpoint APIs.
I definitely do not want to add a new API with no XML validation. Whether we'll require bundling a way bigger change which actually may break existing APPS using invalid XML is worth together with this, I'm not sure. But adding more legacy cruft seems to be pointless.

On Wed, Jul 24, 2019 at 06:55:18PM +0200, Peter Krempa wrote:
On Wed, Jul 24, 2019 at 17:40:51 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 06:17:24PM +0200, Peter Krempa wrote:
On Wed, Jul 24, 2019 at 17:11:19 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 12:55:58AM -0500, Eric Blake wrote:
[...]
So overall, either we should turn on validation for all our schemas, or we should require a VALIDATE flag for this new API. Both avoid special case behaviour with the checkpoint APIs.
I definitely do not want to add a new API with no XML validation. Whether we'll require bundling a way bigger change which actually may break existing APPS using invalid XML is worth together with this, I'm not sure.
But adding more legacy cruft seems to be pointless.
I don't think its legacy cruft, when it is our normal practice, including in the new XML we added just in the release last month. Validation as standard is only a compelling thing if we're going todo it universally. When only 1 out of 14 schemas does validation that doesn't justify the divergance in behaviour IMHO. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Jul 24, 2019 at 18:10:47 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 06:55:18PM +0200, Peter Krempa wrote:
On Wed, Jul 24, 2019 at 17:40:51 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 06:17:24PM +0200, Peter Krempa wrote:
On Wed, Jul 24, 2019 at 17:11:19 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 12:55:58AM -0500, Eric Blake wrote:
[...]
So overall, either we should turn on validation for all our schemas, or we should require a VALIDATE flag for this new API. Both avoid special case behaviour with the checkpoint APIs.
I definitely do not want to add a new API with no XML validation. Whether we'll require bundling a way bigger change which actually may break existing APPS using invalid XML is worth together with this, I'm not sure.
But adding more legacy cruft seems to be pointless.
I don't think its legacy cruft, when it is our normal practice, including in the new XML we added just in the release last month.
Validation as standard is only a compelling thing if we're going todo it universally. When only 1 out of 14 schemas does validation that doesn't justify the divergance in behaviour IMHO.
Given that we've done VERY badly when adding validation and basically the only object supporting validation is the domain object (no, not even the snapshot object supported validation until earlier this month) having schemas is basically a joke. If we don't mandate it from the beginning we will never be able to do it without the risk of breaking users which try to use it wrongly. In my opinion the only sane approach is to validate always and the only exception is historical reasons. Note that requesting dropping mandatory validation will also require re-introducing the checks that Eric removed as pointles after I asked twice to add mandatory validation in recent reviews. At any rate I'm not going to object to dropping mandatory validation, but it will disappoint me greatly if we do that.

On 7/24/19 11:40 AM, Daniel P. Berrangé wrote:
On Wed, Jul 24, 2019 at 06:17:24PM +0200, Peter Krempa wrote:
On Wed, Jul 24, 2019 at 17:11:19 +0100, Daniel Berrange wrote:
On Wed, Jul 24, 2019 at 12:55:58AM -0500, Eric Blake wrote:
Add a new file checkpoint_conf.c that performs the translation to and from new XML describing a checkpoint. The code shares a common base class with snapshots, since a checkpoint similarly represents the domain state at a moment in time. Add some basic testing of round trip XML handling through the new code.
Of note - this code intentionally differs from snapshots in that XML schema validation is unconditional, rather than based on a public API flag.
I'm in two minds as to whether this is really a good idea.
I always have a strong tendancy to prefer consistency across the APIs we expose, which would point to an opt-in flag for validation. If we did later find we need a way to skip validation, then we end up having to add a NO_VALIDATION API flag, which makes inconsitency even more glaring to app devs
I understand that this can help catch mistakes in apps, but I'm not sold on it as a default, when it is easy for apps to opt-in if they want to.
The opt-in argument allowing us to skip something if we run into an RNG bug is slightly tempting, but
I've specifically requested this behaviour as I don't think that there's value in not validating the XML. The XML parser is tied to what we parse anyways and the checks done when parsing are _WAY_ simpler if we know that all the values conform to the schema.
the amount of simplifications when you can rely on the data already being validated was rather substantial.
If there is no downside to validation though, why only do it for one schema. We should do it for all of the XML parsers we have. We didn't do this originally because we didn't have confidence in accuracy of the schemas, which was shown a sensible decision many times over as we found a great many schema bugs.
Yes, I'd love to add more validation to more of our existing legacy APIs, but one project at a time. Maybe a libvirt.conf or qemu.conf switch to opt in to validation (and ignore the flags, where flags exist) is worthwhile (then, if you do run into a schema bug, you can temporarily weaken qemu.conf and rely on the flags on a per-API basis).
None the less, it would still be possible to turn on validation for all our schemas, as simply declare the current _VALIDATE flags are now a no-op.
It would also be possible to add a _VALIDATE flag for checkpoints that is initially treated as a no-op (ie. I perform the validation using the RNG schema no matter what, regardless of the flag's presence, because it makes the rest of my code shorter), but if we run into an RNG schema bug down the road, we could then still have the position of omitting the flag to bypass validation at that time. Then again, if we run into an RNG bug where we reject something that looks like it should be valid, an older libvirt will fail to parse the XML no matter what, then by the time you upgrade to a version of libvirt that honors the flag to allow a bypass, you've also upgraded to a version of libvirt that fixes the bug, so why do you need the bypass?
There is the possibility that apps are lazy in creating XML documents adding elements that may not be supported with the version of libvirt they are talking to. That might be caught at parsing time or might be silently ignored with little ill effect. So enforcing validation would mean apps have to be more careful with the XML they feed libvirt. Such enforcement could be considered a good thing even if it risks highlighting existing broken behaviour in apps.
Existing APIs are produced by existing apps (and yes, they have been lazy, where strict validation would probably break some apps). But checkpoints are a brand-new API, and as there are no existing apps producing checkpoint XML (only new stuff), now is a great time to enforce that the new stuff is sane from the get-go.
So overall, either we should turn on validation for all our schemas, or we should require a VALIDATE flag for this new API. Both avoid special case behaviour with the checkpoint APIs.
There's still time to add it in prior to the 5.6 release if we decide we need the opt-out capability. I'm probably going to commit this patch as-is while we continue the discussion, where we can decide if a follow-up patch needs to make it opt-in rather than mandatory. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

On 7/24/19 5:42 PM, Eric Blake wrote:
the amount of simplifications when you can rely on the data already being validated was rather substantial.
If there is no downside to validation though, why only do it for one schema. We should do it for all of the XML parsers we have. We didn't do this originally because we didn't have confidence in accuracy of the schemas, which was shown a sensible decision many times over as we found a great many schema bugs.
Yes, I'd love to add more validation to more of our existing legacy APIs, but one project at a time. Maybe a libvirt.conf or qemu.conf switch to opt in to validation (and ignore the flags, where flags exist) is worthwhile (then, if you do run into a schema bug, you can temporarily weaken qemu.conf and rely on the flags on a per-API basis).
None the less, it would still be possible to turn on validation for all our schemas, as simply declare the current _VALIDATE flags are now a no-op.
It would also be possible to add a _VALIDATE flag for checkpoints that is initially treated as a no-op (ie. I perform the validation using the RNG schema no matter what, regardless of the flag's presence, because it makes the rest of my code shorter), but if we run into an RNG schema bug down the road, we could then still have the position of omitting the flag to bypass validation at that time. Then again, if we run into an RNG bug where we reject something that looks like it should be valid, an older libvirt will fail to parse the XML no matter what, then by the time you upgrade to a version of libvirt that honors the flag to allow a bypass, you've also upgraded to a version of libvirt that fixes the bug, so why do you need the bypass?
Thinking just a bit more: for the case of defining a new checkpoint, mandatory schema validation is still appealing (the new RNG file is pretty small, and easy enough to compare to the code in checkpoint_conf.c). But for the case of the VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE flag, which includes a full <domain> sub-element, there _is_ a risk that we still have inconsistencies between the domain XML we produce and what the schema parser will require during the REDEFINE (because we don't have schema validation always enabled for domains in general, and because <domain> is so much of a bigger beast to begin with). The last thing we need is for restarting libvirtd to fail to reload all checkpoints merely because of a schema failure on the <domain> subelement. I may still end up implementing a VALIDATE flag (maybe just at the checkpoint_conf.c level for internal use, rather than as a public API flag) that is a no-op for checkpoint creation but allows bypass of the validation of a <domain> sub-element during REDEFINE (and I would actually code it up that way too: try to validate the XML as-is; if it fails, then edit a copy of the XML to remove the <domain> element and try to revalidate the rest, rather than skipping validation altogether - so that the rest of the checkpoint code can still rely on validation rather than reinstating redundant checking code). But it is definitely material for a separate patch:
There's still time to add it in prior to the 5.6 release if we decide we need the opt-out capability. I'm probably going to commit this patch as-is while we continue the discussion, where we can decide if a follow-up patch needs to make it opt-in rather than mandatory.
-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

[adding bug-gnulib] On 7/24/19 12:55 AM, Eric Blake wrote:
Add a new file checkpoint_conf.c that performs the translation to and from new XML describing a checkpoint. The code shares a common base class with snapshots, since a checkpoint similarly represents the domain state at a moment in time. Add some basic testing of round trip XML handling through the new code.
+++ b/src/conf/checkpoint_conf.c
+#include <config.h> + +#include "configmake.h" +#include "internal.h" +#include "virbitmap.h" +#include "virbuffer.h" +#include "datatypes.h"
This causes a compilation failure on mingw, due to libvirt's "datatypes.h" including <winsock.h> after the point at which gnulib's "configmake.h" has already #define'd DATADIR into a string, but mingw's winsock.h tries to use DATADIR as a data-type tag name: make all-am make[1]: Entering directory '/home/berrange/src/virt/libvirt/src' CC conf/libvirt_conf_la-checkpoint_conf.lo In file included from /usr/i686-w64-mingw32/sys-root/mingw/include/objbase.h:66, from /usr/i686-w64-mingw32/sys-root/mingw/include/ole2.h:17, from /usr/i686-w64-mingw32/sys-root/mingw/include/wtypes.h:12, from /usr/i686-w64-mingw32/sys-root/mingw/include/winscard.h:10, from /usr/i686-w64-mingw32/sys-root/mingw/include/windows.h:97, from /usr/i686-w64-mingw32/sys-root/mingw/include/winsock2.h:23, from ../gnulib/lib/unistd.h:48, from ./driver.h:24, from ./datatypes.h:26, from conf/checkpoint_conf.c:28: /usr/i686-w64-mingw32/sys-root/mingw/include/objidl.h:12275:2: error: expected identifier or '(' before string constant } DATADIR; ^~~~~~~~~ make[1]: *** [Makefile:10127: conf/libvirt_conf_la-checkpoint_conf.lo] Error 1 And it's not the first time libvirt has run into this issue; I've found the following commits in 2015 that worked around it: https://libvirt.org/git/?p=libvirt.git;a=commitdiff;h=976abdf6 https://libvirt.org/git/?p=libvirt.git;a=commitdiff;h=bd205a90 Gnulib should work around this: perhaps "configmake.h" should include <unistd.h> first when built on mingw, as that is sufficient to trigger enough other headers to be included such that a later inclusion of <winsock.h> after "configmake.h" no longer runs into an issue with the DATADIR pollution breaking compilation, or perhaps gnulib can wrap <winsock.h> in such a way that it no longer depends on a tag name DATADIR. In the meantime, I'll push an obvious fix to libvirt to reorder the header inclusions to work around the problem. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

Create a new file for managing a list of checkpoint objects, borrowing heavily from existing virDomainSnapshotObjList paradigms. Note that while snapshots definitely have a use case for multiple children to a single parent (create a base snapshot, create a child snapshot, revert to the base, then create another child snapshot), it's harder to predict how checkpoints will play out with reverting to prior points in time. Thus, in initial use, given a list of checkpoints, you never have more than one child, and we can treat the most-recent leaf node as the parent of the next node creation, without having to expose a notion of a current node in XML or public API. However, as the snapshot machinery is already generic, it is easier to reuse the generic machinery that tracks relations between domain moments than it is to open-code a new list-management scheme just for checkpoints (hence, we still have internal functions related to a current checkpoint, even though that has no observable effect externally). Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 7 + src/conf/domain_conf.h | 2 + src/conf/virconftypes.h | 6 + src/conf/virdomaincheckpointobjlist.h | 72 ++++++++ src/conf/virdomainmomentobjlist.h | 1 + src/conf/virdomainobjlist.h | 7 +- src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 62 +++++++ src/conf/domain_conf.c | 6 + src/conf/virdomaincheckpointobjlist.c | 243 ++++++++++++++++++++++++++ src/conf/virdomainmomentobjlist.c | 17 +- src/conf/virdomainobjlist.c | 11 ++ src/libvirt_private.syms | 19 ++ 13 files changed, 453 insertions(+), 2 deletions(-) create mode 100644 src/conf/virdomaincheckpointobjlist.h create mode 100644 src/conf/virdomaincheckpointobjlist.c diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h index 0fac521efe..018e9f53b1 100644 --- a/src/conf/checkpoint_conf.h +++ b/src/conf/checkpoint_conf.h @@ -81,4 +81,11 @@ char *virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, unsigned int flags); int virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr checkpoint); +int virDomainCheckpointRedefinePrep(virDomainPtr domain, + virDomainObjPtr vm, + virDomainCheckpointDefPtr *def, + virDomainMomentObjPtr *checkpoint, + virDomainXMLOptionPtr xmlopt, + bool *update_current); + VIR_ENUM_DECL(virDomainCheckpoint); diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 48b0af4b04..b7a553b249 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -2562,6 +2562,8 @@ struct _virDomainObj { bool hasManagedSave; + virDomainCheckpointObjListPtr checkpoints; + void *privateData; void (*privateDataFreeFunc)(void *); diff --git a/src/conf/virconftypes.h b/src/conf/virconftypes.h index fe3f0af14f..e8e9b6c314 100644 --- a/src/conf/virconftypes.h +++ b/src/conf/virconftypes.h @@ -105,6 +105,12 @@ typedef virDomainBlockIoTuneInfo *virDomainBlockIoTuneInfoPtr; typedef struct _virDomainCheckpointDef virDomainCheckpointDef; typedef virDomainCheckpointDef *virDomainCheckpointDefPtr; +typedef struct _virDomainCheckpointObj virDomainCheckpointObj; +typedef virDomainCheckpointObj *virDomainCheckpointObjPtr; + +typedef struct _virDomainCheckpointObjList virDomainCheckpointObjList; +typedef virDomainCheckpointObjList *virDomainCheckpointObjListPtr; + typedef struct _virDomainChrDef virDomainChrDef; typedef virDomainChrDef *virDomainChrDefPtr; diff --git a/src/conf/virdomaincheckpointobjlist.h b/src/conf/virdomaincheckpointobjlist.h new file mode 100644 index 0000000000..3a51a46e68 --- /dev/null +++ b/src/conf/virdomaincheckpointobjlist.h @@ -0,0 +1,72 @@ +/* + * virdomaincheckpointobjlist.h: handle a tree of checkpoint objects + * (derived from virdomainsnapshotobjlist.h) + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "internal.h" +#include "virdomainmomentobjlist.h" +#include "virbuffer.h" + +virDomainCheckpointObjListPtr virDomainCheckpointObjListNew(void); +void virDomainCheckpointObjListFree(virDomainCheckpointObjListPtr checkpoints); + +virDomainMomentObjPtr virDomainCheckpointAssignDef(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def); + +virDomainMomentObjPtr virDomainCheckpointFindByName(virDomainCheckpointObjListPtr checkpoints, + const char *name); +virDomainMomentObjPtr virDomainCheckpointGetCurrent(virDomainCheckpointObjListPtr checkpoints); +const char *virDomainCheckpointGetCurrentName(virDomainCheckpointObjListPtr checkpoints); +void virDomainCheckpointSetCurrent(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr checkpoint); +bool virDomainCheckpointObjListRemove(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr checkpoint); +void virDomainCheckpointObjListRemoveAll(virDomainCheckpointObjListPtr checkpoints); +int virDomainCheckpointForEach(virDomainCheckpointObjListPtr checkpoints, + virHashIterator iter, + void *data); +void virDomainCheckpointLinkParent(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr chk); +int virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr *leaf); +int virDomainCheckpointCheckCycles(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def, + const char *domname); + +#define VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES \ + (VIR_DOMAIN_CHECKPOINT_LIST_LEAVES | \ + VIR_DOMAIN_CHECKPOINT_LIST_NO_LEAVES) + +#define VIR_DOMAIN_CHECKPOINT_FILTERS_ALL \ + (VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) + +int virDomainListCheckpoints(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr from, + virDomainPtr dom, + virDomainCheckpointPtr **objs, + unsigned int flags); + +static inline virDomainCheckpointDefPtr +virDomainCheckpointObjGetDef(virDomainMomentObjPtr obj) +{ + return (virDomainCheckpointDefPtr) obj->def; +} diff --git a/src/conf/virdomainmomentobjlist.h b/src/conf/virdomainmomentobjlist.h index 5b73175e8d..75198909ba 100644 --- a/src/conf/virdomainmomentobjlist.h +++ b/src/conf/virdomainmomentobjlist.h @@ -121,3 +121,4 @@ int virDomainMomentUpdateRelations(virDomainMomentObjListPtr moments); int virDomainMomentCheckCycles(virDomainMomentObjListPtr list, virDomainMomentDefPtr def, const char *domname); +virDomainMomentObjPtr virDomainMomentFindLeaf(virDomainMomentObjListPtr list); diff --git a/src/conf/virdomainobjlist.h b/src/conf/virdomainobjlist.h index bf3ab396fa..7d71bc54d0 100644 --- a/src/conf/virdomainobjlist.h +++ b/src/conf/virdomainobjlist.h @@ -120,13 +120,18 @@ int virDomainObjListForEach(virDomainObjListPtr doms, (VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT | \ VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT) +#define VIR_CONNECT_LIST_DOMAINS_FILTERS_CHECKPOINT \ + (VIR_CONNECT_LIST_DOMAINS_HAS_CHECKPOINT | \ + VIR_CONNECT_LIST_DOMAINS_NO_CHECKPOINT) + #define VIR_CONNECT_LIST_DOMAINS_FILTERS_ALL \ (VIR_CONNECT_LIST_DOMAINS_FILTERS_ACTIVE | \ VIR_CONNECT_LIST_DOMAINS_FILTERS_PERSISTENT | \ VIR_CONNECT_LIST_DOMAINS_FILTERS_STATE | \ VIR_CONNECT_LIST_DOMAINS_FILTERS_MANAGEDSAVE | \ VIR_CONNECT_LIST_DOMAINS_FILTERS_AUTOSTART | \ - VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT) + VIR_CONNECT_LIST_DOMAINS_FILTERS_SNAPSHOT | \ + VIR_CONNECT_LIST_DOMAINS_FILTERS_CHECKPOINT) int virDomainObjListCollect(virDomainObjListPtr doms, virConnectPtr conn, diff --git a/src/conf/Makefile.inc.am b/src/conf/Makefile.inc.am index f656725e3a..5035b9b524 100644 --- a/src/conf/Makefile.inc.am +++ b/src/conf/Makefile.inc.am @@ -36,6 +36,8 @@ DOMAIN_CONF_SOURCES = \ conf/numa_conf.c \ conf/numa_conf.h \ conf/virconftypes.h \ + conf/virdomaincheckpointobjlist.c \ + conf/virdomaincheckpointobjlist.h \ conf/virdomainobjlist.c \ conf/virdomainobjlist.h \ conf/virdomainmomentobjlist.c \ diff --git a/src/conf/checkpoint_conf.c b/src/conf/checkpoint_conf.c index 69c9040a43..30c6d2e717 100644 --- a/src/conf/checkpoint_conf.c +++ b/src/conf/checkpoint_conf.c @@ -36,6 +36,7 @@ #include "virerror.h" #include "virxml.h" #include "virstring.h" +#include "virdomaincheckpointobjlist.h" #define VIR_FROM_THIS VIR_FROM_DOMAIN_CHECKPOINT @@ -533,3 +534,64 @@ virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, return virBufferContentAndReset(&buf); } + + +int +virDomainCheckpointRedefinePrep(virDomainPtr domain, + virDomainObjPtr vm, + virDomainCheckpointDefPtr *defptr, + virDomainMomentObjPtr *chk, + virDomainXMLOptionPtr xmlopt, + bool *update_current) +{ + virDomainCheckpointDefPtr def = *defptr; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + virDomainMomentObjPtr other = NULL; + virDomainCheckpointDefPtr otherdef = NULL; + + virUUIDFormat(domain->uuid, uuidstr); + + if (virDomainCheckpointCheckCycles(vm->checkpoints, def, vm->def->name) < 0) + return -1; + + if (!def->parent.dom || + memcmp(def->parent.dom->uuid, domain->uuid, VIR_UUID_BUFLEN)) { + virReportError(VIR_ERR_INVALID_ARG, + _("definition for checkpoint %s must use uuid %s"), + def->parent.name, uuidstr); + return -1; + } + if (virDomainCheckpointAlignDisks(def) < 0) + return -1; + + if (def->parent.parent_name) + other = virDomainCheckpointFindByName(vm->checkpoints, + def->parent.parent_name); + if (other == virDomainCheckpointGetCurrent(vm->checkpoints)) { + *update_current = true; + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + } + + other = virDomainCheckpointFindByName(vm->checkpoints, def->parent.name); + if (other) { + otherdef = virDomainCheckpointObjGetDef(other); + if (!virDomainDefCheckABIStability(otherdef->parent.dom, + def->parent.dom, xmlopt)) + return -1; + + if (other == virDomainCheckpointGetCurrent(vm->checkpoints)) { + *update_current = true; + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + } + + /* Drop and rebuild the parent relationship, but keep all + * child relations by reusing chk. */ + virDomainMomentDropParent(other); + virObjectUnref(otherdef); + other->def = &(*defptr)->parent; + *defptr = NULL; + *chk = other; + } + + return 0; +} diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 0574c69a46..cc3bb4110f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -29,6 +29,7 @@ #include "configmake.h" #include "internal.h" #include "virerror.h" +#include "checkpoint_conf.h" #include "datatypes.h" #include "domain_addr.h" #include "domain_conf.h" @@ -60,6 +61,7 @@ #include "virhostdev.h" #include "virmdev.h" #include "virdomainsnapshotobjlist.h" +#include "virdomaincheckpointobjlist.h" #define VIR_FROM_THIS VIR_FROM_DOMAIN @@ -3486,6 +3488,7 @@ static void virDomainObjDispose(void *obj) (dom->privateDataFreeFunc)(dom->privateData); virDomainSnapshotObjListFree(dom->snapshots); + virDomainCheckpointObjListFree(dom->checkpoints); } virDomainObjPtr @@ -3515,6 +3518,9 @@ virDomainObjNew(virDomainXMLOptionPtr xmlopt) if (!(domain->snapshots = virDomainSnapshotObjListNew())) goto error; + if (!(domain->checkpoints = virDomainCheckpointObjListNew())) + goto error; + virObjectLock(domain); virDomainObjSetState(domain, VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_SHUTOFF_UNKNOWN); diff --git a/src/conf/virdomaincheckpointobjlist.c b/src/conf/virdomaincheckpointobjlist.c new file mode 100644 index 0000000000..03aad9f5ef --- /dev/null +++ b/src/conf/virdomaincheckpointobjlist.c @@ -0,0 +1,243 @@ +/* + * virdomaincheckpointobjlist.c: handle a tree of checkpoint objects + * (derived from virdomainsnapshotobjlist.c) + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include "internal.h" +#include "virdomaincheckpointobjlist.h" +#include "checkpoint_conf.h" +#include "virlog.h" +#include "virerror.h" +#include "datatypes.h" +#include "virstring.h" +#include "viralloc.h" + +#define VIR_FROM_THIS VIR_FROM_DOMAIN_CHECKPOINT + +VIR_LOG_INIT("conf.virdomaincheckpointobjlist"); + +struct _virDomainCheckpointObjList { + virDomainMomentObjListPtr base; +}; + +virDomainMomentObjPtr +virDomainCheckpointAssignDef(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def) +{ + return virDomainMomentAssignDef(checkpoints->base, &def->parent); +} + + +static bool +virDomainCheckpointFilter(virDomainMomentObjPtr obj ATTRIBUTE_UNUSED, + unsigned int flags) +{ + /* For now, we have no further filters than what the common code handles. */ + virCheckFlags(0, false); + return true; +} + + +virDomainCheckpointObjListPtr +virDomainCheckpointObjListNew(void) +{ + virDomainCheckpointObjListPtr checkpoints; + + if (VIR_ALLOC(checkpoints) < 0) + return NULL; + checkpoints->base = virDomainMomentObjListNew(); + if (!checkpoints->base) { + VIR_FREE(checkpoints); + return NULL; + } + return checkpoints; +} + + +void +virDomainCheckpointObjListFree(virDomainCheckpointObjListPtr checkpoints) +{ + if (!checkpoints) + return; + virDomainMomentObjListFree(checkpoints->base); + VIR_FREE(checkpoints); +} + + +static int +virDomainCheckpointObjListGetNames(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr from, + char **const names, + int maxnames, + unsigned int flags) +{ + /* We intentionally chose our public flags to match the common flags */ + verify(VIR_DOMAIN_CHECKPOINT_LIST_ROOTS == + (int) VIR_DOMAIN_MOMENT_LIST_ROOTS); + verify(VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL == + (int) VIR_DOMAIN_MOMENT_LIST_TOPOLOGICAL); + verify(VIR_DOMAIN_CHECKPOINT_LIST_LEAVES == + (int) VIR_DOMAIN_MOMENT_LIST_LEAVES); + verify(VIR_DOMAIN_CHECKPOINT_LIST_NO_LEAVES == + (int) VIR_DOMAIN_MOMENT_LIST_NO_LEAVES); + + return virDomainMomentObjListGetNames(checkpoints->base, from, names, + maxnames, flags, + virDomainCheckpointFilter, 0); +} + + +virDomainMomentObjPtr +virDomainCheckpointFindByName(virDomainCheckpointObjListPtr checkpoints, + const char *name) +{ + return virDomainMomentFindByName(checkpoints->base, name); +} + + +/* Return the current checkpoint, or NULL */ +virDomainMomentObjPtr +virDomainCheckpointGetCurrent(virDomainCheckpointObjListPtr checkpoints) +{ + return virDomainMomentGetCurrent(checkpoints->base); +} + + +/* Return the current checkpoint's name, or NULL */ +const char * +virDomainCheckpointGetCurrentName(virDomainCheckpointObjListPtr checkpoints) +{ + return virDomainMomentGetCurrentName(checkpoints->base); +} + + +/* Update the current checkpoint, using NULL if no current remains */ +void +virDomainCheckpointSetCurrent(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr checkpoint) +{ + virDomainMomentSetCurrent(checkpoints->base, checkpoint); +} + + +/* Remove checkpoint from the list; return true if it was current */ +bool +virDomainCheckpointObjListRemove(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr checkpoint) +{ + return virDomainMomentObjListRemove(checkpoints->base, checkpoint); +} + + +/* Remove all checkpoints tracked in the list */ +void +virDomainCheckpointObjListRemoveAll(virDomainCheckpointObjListPtr checkpoints) +{ + return virDomainMomentObjListRemoveAll(checkpoints->base); +} + + +int +virDomainCheckpointForEach(virDomainCheckpointObjListPtr checkpoints, + virHashIterator iter, + void *data) +{ + return virDomainMomentForEach(checkpoints->base, iter, data); +} + + +/* Populate parent link of a given checkpoint. */ +void +virDomainCheckpointLinkParent(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr chk) +{ + return virDomainMomentLinkParent(checkpoints->base, chk); +} + + +/* Populate parent link and child count of all checkpoints, with all + * assigned defs having relations starting as 0/NULL. Return 0 on + * success, -1 if a parent is missing or if a circular relationship + * was requested. Set leaf to the end of the chain, if there is + * exactly one such leaf. */ +int +virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr *leaf) +{ + int ret = virDomainMomentUpdateRelations(checkpoints->base); + + if (ret == 0) + *leaf = virDomainMomentFindLeaf(checkpoints->base); + return ret; +} + + +int +virDomainCheckpointCheckCycles(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def, + const char *domname) +{ + return virDomainMomentCheckCycles(checkpoints->base, &def->parent, domname); +} + + +int +virDomainListCheckpoints(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr from, + virDomainPtr dom, + virDomainCheckpointPtr **chks, + unsigned int flags) +{ + int count = virDomainCheckpointObjListGetNames(checkpoints, from, NULL, + 0, flags); + virDomainCheckpointPtr *list = NULL; + char **names; + int ret = -1; + size_t i; + + if (!chks || count < 0) + return count; + if (VIR_ALLOC_N(names, count) < 0 || + VIR_ALLOC_N(list, count + 1) < 0) + goto cleanup; + + if (virDomainCheckpointObjListGetNames(checkpoints, from, names, count, + flags) < 0) + goto cleanup; + for (i = 0; i < count; i++) + if ((list[i] = virGetDomainCheckpoint(dom, names[i])) == NULL) + goto cleanup; + + ret = count; + *chks = list; + + cleanup: + for (i = 0; i < count; i++) + VIR_FREE(names[i]); + VIR_FREE(names); + if (ret < 0 && list) { + for (i = 0; i < count; i++) + virObjectUnref(list[i]); + VIR_FREE(list); + } + return ret; +} diff --git a/src/conf/virdomainmomentobjlist.c b/src/conf/virdomainmomentobjlist.c index c8dd8f8e01..d02efb420a 100644 --- a/src/conf/virdomainmomentobjlist.c +++ b/src/conf/virdomainmomentobjlist.c @@ -31,8 +31,8 @@ #include "viralloc.h" /* FIXME: using virObject would allow us to not need this */ -#include "snapshot_conf.h" #include "virdomainsnapshotobjlist.h" +#include "virdomaincheckpointobjlist.h" #define VIR_FROM_THIS VIR_FROM_DOMAIN @@ -589,3 +589,18 @@ virDomainMomentCheckCycles(virDomainMomentObjListPtr list, } return 0; } + +/* If there is exactly one leaf node, return that node. */ +virDomainMomentObjPtr +virDomainMomentFindLeaf(virDomainMomentObjListPtr list) +{ + virDomainMomentObjPtr moment = &list->metaroot; + + if (moment->nchildren != 1) + return NULL; + while (moment->nchildren == 1) + moment = moment->first_child; + if (moment->nchildren == 0) + return moment; + return NULL; +} diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c index d58d25f847..d640da6205 100644 --- a/src/conf/virdomainobjlist.c +++ b/src/conf/virdomainobjlist.c @@ -25,12 +25,14 @@ #include "internal.h" #include "datatypes.h" #include "virdomainobjlist.h" +#include "checkpoint_conf.h" #include "snapshot_conf.h" #include "viralloc.h" #include "virfile.h" #include "virlog.h" #include "virstring.h" #include "virdomainsnapshotobjlist.h" +#include "virdomaincheckpointobjlist.h" #define VIR_FROM_THIS VIR_FROM_DOMAIN @@ -887,6 +889,15 @@ virDomainObjMatchFilter(virDomainObjPtr vm, return false; } + /* filter by checkpoint existence */ + if (MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_CHECKPOINT)) { + int nchk = virDomainListCheckpoints(vm->checkpoints, NULL, NULL, + NULL, 0); + if (!((MATCH(VIR_CONNECT_LIST_DOMAINS_HAS_CHECKPOINT) && nchk > 0) || + (MATCH(VIR_CONNECT_LIST_DOMAINS_NO_CHECKPOINT) && nchk <= 0))) + return false; + } + return true; } #undef MATCH diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 693c75b45d..3350cb963c 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -70,9 +70,12 @@ virCapabilitiesSetNetPrefix; # conf/checkpoint_conf.h +virDomainCheckpointAlignDisks; virDomainCheckpointDefFormat; virDomainCheckpointDefNew; virDomainCheckpointDefParseString; +virDomainCheckpointFormatConvertXMLFlags; +virDomainCheckpointRedefinePrep; virDomainCheckpointTypeFromString; virDomainCheckpointTypeToString; @@ -980,6 +983,22 @@ virChrdevFree; virChrdevOpen; +# conf/virdomaincheckpointobjlist.h +virDomainCheckpointAssignDef; +virDomainCheckpointFindByName; +virDomainCheckpointForEach; +virDomainCheckpointGetCurrent; +virDomainCheckpointGetCurrentName; +virDomainCheckpointLinkParent; +virDomainCheckpointObjListFree; +virDomainCheckpointObjListNew; +virDomainCheckpointObjListRemove; +virDomainCheckpointObjListRemoveAll; +virDomainCheckpointSetCurrent; +virDomainCheckpointUpdateRelations; +virDomainListCheckpoints; + + # conf/virdomainmomentobjlist.h virDomainMomentDropChildren; virDomainMomentDropParent; -- 2.20.1

On Wed, Jul 24, 2019 at 12:55:59AM -0500, Eric Blake wrote:
Create a new file for managing a list of checkpoint objects, borrowing heavily from existing virDomainSnapshotObjList paradigms.
Note that while snapshots definitely have a use case for multiple children to a single parent (create a base snapshot, create a child snapshot, revert to the base, then create another child snapshot), it's harder to predict how checkpoints will play out with reverting to prior points in time. Thus, in initial use, given a list of checkpoints, you never have more than one child, and we can treat the most-recent leaf node as the parent of the next node creation, without having to expose a notion of a current node in XML or public API. However, as the snapshot machinery is already generic, it is easier to reuse the generic machinery that tracks relations between domain moments than it is to open-code a new list-management scheme just for checkpoints (hence, we still have internal functions related to a current checkpoint, even though that has no observable effect externally).
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 7 + src/conf/domain_conf.h | 2 + src/conf/virconftypes.h | 6 + src/conf/virdomaincheckpointobjlist.h | 72 ++++++++ src/conf/virdomainmomentobjlist.h | 1 + src/conf/virdomainobjlist.h | 7 +- src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 62 +++++++ src/conf/domain_conf.c | 6 + src/conf/virdomaincheckpointobjlist.c | 243 ++++++++++++++++++++++++++ src/conf/virdomainmomentobjlist.c | 17 +- src/conf/virdomainobjlist.c | 11 ++ src/libvirt_private.syms | 19 ++ 13 files changed, 453 insertions(+), 2 deletions(-) create mode 100644 src/conf/virdomaincheckpointobjlist.h create mode 100644 src/conf/virdomaincheckpointobjlist.c
I probably would have split the patch in two - first introducing the virDomainCheckpointObjListPtr and APIs, the second doing the virDomainObj integration, but that's not a big deal. Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
diff --git a/src/conf/virdomaincheckpointobjlist.h b/src/conf/virdomaincheckpointobjlist.h new file mode 100644 index 0000000000..3a51a46e68 --- /dev/null +++ b/src/conf/virdomaincheckpointobjlist.h @@ -0,0 +1,72 @@ +/* + * virdomaincheckpointobjlist.h: handle a tree of checkpoint objects + * (derived from virdomainsnapshotobjlist.h) + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "internal.h" +#include "virdomainmomentobjlist.h" +#include "virbuffer.h" + +virDomainCheckpointObjListPtr virDomainCheckpointObjListNew(void); +void virDomainCheckpointObjListFree(virDomainCheckpointObjListPtr checkpoints); + +virDomainMomentObjPtr virDomainCheckpointAssignDef(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def); + +virDomainMomentObjPtr virDomainCheckpointFindByName(virDomainCheckpointObjListPtr checkpoints, + const char *name); +virDomainMomentObjPtr virDomainCheckpointGetCurrent(virDomainCheckpointObjListPtr checkpoints); +const char *virDomainCheckpointGetCurrentName(virDomainCheckpointObjListPtr checkpoints); +void virDomainCheckpointSetCurrent(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr checkpoint); +bool virDomainCheckpointObjListRemove(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr checkpoint); +void virDomainCheckpointObjListRemoveAll(virDomainCheckpointObjListPtr checkpoints); +int virDomainCheckpointForEach(virDomainCheckpointObjListPtr checkpoints, + virHashIterator iter, + void *data); +void virDomainCheckpointLinkParent(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr chk); +int virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints, + virDomainMomentObjPtr *leaf); +int virDomainCheckpointCheckCycles(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def, + const char *domname);
I'd suggest putting the return types all on a separate line so these don't get so wide. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Creating a checkpoint does not modify guest-visible state, but does modify host resources. Rather than reuse existing domain:write, domain:block_write, or domain:snapshot access controls, it seems better to introduce a new access control specific to tasks related to checkpoints and incremental backups of guest disk state. Signed-off-by: Eric Blake <eblake@redhat.com> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> --- src/access/viraccessperm.h | 6 ++++++ src/access/viraccessperm.c | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/access/viraccessperm.h b/src/access/viraccessperm.h index d4b9c69c1e..a42512d5e0 100644 --- a/src/access/viraccessperm.h +++ b/src/access/viraccessperm.h @@ -186,6 +186,12 @@ typedef enum { */ VIR_ACCESS_PERM_DOMAIN_MIGRATE, /* Host migration */ + /** + * @desc: Checkpoint domain + * @message: Checkpointing domain requires authorization + */ + VIR_ACCESS_PERM_DOMAIN_CHECKPOINT, /* Checkpoint disks */ + /** * @desc: Snapshot domain * @message: Snapshotting domain requires authorization diff --git a/src/access/viraccessperm.c b/src/access/viraccessperm.c index 74993e9f29..33db7752b6 100644 --- a/src/access/viraccessperm.c +++ b/src/access/viraccessperm.c @@ -39,7 +39,8 @@ VIR_ENUM_IMPL(virAccessPermDomain, "getattr", "read", "write", "read_secure", "start", "stop", "reset", "save", "delete", - "migrate", "snapshot", "suspend", "hibernate", "core_dump", "pm_control", + "migrate", "checkpoint", "snapshot", "suspend", "hibernate", + "core_dump", "pm_control", "init_control", "inject_nmi", "send_input", "send_signal", "fs_trim", "fs_freeze", "block_read", "block_write", "mem_read", -- 2.20.1

The remote code generator had to be taught about the new virDomainCheckpointPtr type, at which point the remote driver code for checkpoints can be generated. Signed-off-by: Eric Blake <eblake@redhat.com> Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> --- src/remote/remote_daemon_dispatch.c | 20 +++++ src/remote/remote_driver.c | 24 +++++- src/remote/remote_protocol.x | 123 +++++++++++++++++++++++++++- src/remote_protocol-structs | 69 ++++++++++++++++ src/rpc/gendispatch.pl | 32 ++++---- 5 files changed, 252 insertions(+), 16 deletions(-) diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index 856c5e48e7..444c03a644 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -90,6 +90,7 @@ static virStorageVolPtr get_nonnull_storage_vol(virConnectPtr conn, remote_nonnu static virSecretPtr get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret); static virNWFilterPtr get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter); static virNWFilterBindingPtr get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding); +static virDomainCheckpointPtr get_nonnull_domain_checkpoint(virDomainPtr dom, remote_nonnull_domain_checkpoint checkpoint); static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot); static virNodeDevicePtr get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev); static int make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src) ATTRIBUTE_RETURN_CHECK; @@ -102,6 +103,7 @@ static int make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNode static int make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src) ATTRIBUTE_RETURN_CHECK; static int make_nonnull_nwfilter(remote_nonnull_nwfilter *net_dst, virNWFilterPtr nwfilter_src) ATTRIBUTE_RETURN_CHECK; static int make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src) ATTRIBUTE_RETURN_CHECK; +static int make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src) ATTRIBUTE_RETURN_CHECK; static int make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src) ATTRIBUTE_RETURN_CHECK; static int @@ -7301,6 +7303,12 @@ get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding return virGetNWFilterBinding(conn, binding.portdev, binding.filtername); } +static virDomainCheckpointPtr +get_nonnull_domain_checkpoint(virDomainPtr dom, remote_nonnull_domain_checkpoint checkpoint) +{ + return virGetDomainCheckpoint(dom, checkpoint.name); +} + static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot) { @@ -7421,6 +7429,18 @@ make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virN return 0; } +static int +make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src) +{ + if (VIR_STRDUP(checkpoint_dst->name, checkpoint_src->name) < 0) + return -1; + if (make_nonnull_domain(&checkpoint_dst->dom, checkpoint_src->domain) < 0) { + VIR_FREE(checkpoint_dst->name); + return -1; + } + return 0; +} + static int make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src) { diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 925ada1cac..72c2336b7a 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -146,6 +146,7 @@ static virStoragePoolPtr get_nonnull_storage_pool(virConnectPtr conn, remote_non static virStorageVolPtr get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol); static virNodeDevicePtr get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev); static virSecretPtr get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret); +static virDomainCheckpointPtr get_nonnull_domain_checkpoint(virDomainPtr domain, remote_nonnull_domain_checkpoint checkpoint); static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr domain, remote_nonnull_domain_snapshot snapshot); static void make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src); static void make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src); @@ -158,6 +159,7 @@ make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr d static void make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src); static void make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src); static void make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src); +static void make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src); static void make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src); /*----------------------------------------------------------------------*/ @@ -8242,6 +8244,12 @@ get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding return virGetNWFilterBinding(conn, binding.portdev, binding.filtername); } +static virDomainCheckpointPtr +get_nonnull_domain_checkpoint(virDomainPtr domain, remote_nonnull_domain_checkpoint checkpoint) +{ + return virGetDomainCheckpoint(domain, checkpoint.name); +} + static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr domain, remote_nonnull_domain_snapshot snapshot) { @@ -8324,6 +8332,13 @@ make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virN binding_dst->filtername = binding_src->filtername; } +static void +make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src) +{ + checkpoint_dst->name = checkpoint_src->name; + make_nonnull_domain(&checkpoint_dst->dom, checkpoint_src->domain); +} + static void make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src) { @@ -8573,7 +8588,14 @@ static virHypervisorDriver hypervisor_driver = { .connectCompareHypervisorCPU = remoteConnectCompareHypervisorCPU, /* 4.4.0 */ .connectBaselineHypervisorCPU = remoteConnectBaselineHypervisorCPU, /* 4.4.0 */ .nodeGetSEVInfo = remoteNodeGetSEVInfo, /* 4.5.0 */ - .domainGetLaunchSecurityInfo = remoteDomainGetLaunchSecurityInfo /* 4.5.0 */ + .domainGetLaunchSecurityInfo = remoteDomainGetLaunchSecurityInfo, /* 4.5.0 */ + .domainCheckpointCreateXML = remoteDomainCheckpointCreateXML, /* 5.6.0 */ + .domainCheckpointGetXMLDesc = remoteDomainCheckpointGetXMLDesc, /* 5.6.0 */ + .domainListAllCheckpoints = remoteDomainListAllCheckpoints, /* 5.6.0 */ + .domainCheckpointListAllChildren = remoteDomainCheckpointListAllChildren, /* 5.6.0 */ + .domainCheckpointLookupByName = remoteDomainCheckpointLookupByName, /* 5.6.0 */ + .domainCheckpointGetParent = remoteDomainCheckpointGetParent, /* 5.6.0 */ + .domainCheckpointDelete = remoteDomainCheckpointDelete, /* 5.6.0 */ }; static virNetworkDriver network_driver = { diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index 2e45b5cef0..2f91bd1921 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -140,6 +140,9 @@ const REMOTE_AUTH_TYPE_LIST_MAX = 20; /* Upper limit on list of memory stats */ const REMOTE_DOMAIN_MEMORY_STATS_MAX = 1024; +/* Upper limit on lists of domain checkpoints. */ +const REMOTE_DOMAIN_CHECKPOINT_LIST_MAX = 16384; + /* Upper limit on lists of domain snapshots. */ const REMOTE_DOMAIN_SNAPSHOT_LIST_MAX = 16384; @@ -336,6 +339,12 @@ struct remote_nonnull_secret { remote_nonnull_string usageID; }; +/* A checkpoint which may not be NULL. */ +struct remote_nonnull_domain_checkpoint { + remote_nonnull_string name; + remote_nonnull_domain dom; +}; + /* A snapshot which may not be NULL. */ struct remote_nonnull_domain_snapshot { remote_nonnull_string name; @@ -3649,6 +3658,70 @@ struct remote_network_port_delete_args { unsigned int flags; }; +struct remote_domain_checkpoint_create_xml_args { + remote_nonnull_domain dom; + remote_nonnull_string xml_desc; + unsigned int flags; +}; + +struct remote_domain_checkpoint_create_xml_ret { + remote_nonnull_domain_checkpoint checkpoint; +}; + +struct remote_domain_checkpoint_get_xml_desc_args { + remote_nonnull_domain_checkpoint checkpoint; + unsigned int flags; +}; + +struct remote_domain_checkpoint_get_xml_desc_ret { + remote_nonnull_string xml; +}; + +struct remote_domain_list_all_checkpoints_args { + remote_nonnull_domain dom; + int need_results; + unsigned int flags; +}; + +struct remote_domain_list_all_checkpoints_ret { /* insert@1 */ + remote_nonnull_domain_checkpoint checkpoints<REMOTE_DOMAIN_CHECKPOINT_LIST_MAX>; + int ret; +}; + +struct remote_domain_checkpoint_list_all_children_args { + remote_nonnull_domain_checkpoint checkpoint; + int need_results; + unsigned int flags; +}; + +struct remote_domain_checkpoint_list_all_children_ret { /* insert@1 */ + remote_nonnull_domain_checkpoint checkpoints<REMOTE_DOMAIN_CHECKPOINT_LIST_MAX>; + int ret; +}; + +struct remote_domain_checkpoint_lookup_by_name_args { + remote_nonnull_domain dom; + remote_nonnull_string name; + unsigned int flags; +}; + +struct remote_domain_checkpoint_lookup_by_name_ret { + remote_nonnull_domain_checkpoint checkpoint; +}; + +struct remote_domain_checkpoint_get_parent_args { + remote_nonnull_domain_checkpoint checkpoint; + unsigned int flags; +}; + +struct remote_domain_checkpoint_get_parent_ret { + remote_nonnull_domain_checkpoint parent; +}; + +struct remote_domain_checkpoint_delete_args { + remote_nonnull_domain_checkpoint checkpoint; + unsigned int flags; +}; /*----- Protocol. -----*/ @@ -6463,5 +6536,53 @@ enum remote_procedure { * @generate: both * @acl: network_port:delete */ - REMOTE_PROC_NETWORK_PORT_DELETE = 410 + REMOTE_PROC_NETWORK_PORT_DELETE = 410, + + /** + * @generate: both + * @acl: domain:checkpoint + * @acl: domain:fs_freeze:VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_CREATE_XML = 411, + + /** + * @generate: both + * @acl: domain:read + * @acl: domain:read_secure:VIR_DOMAIN_CHECKPOINT_XML_SECURE + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_GET_XML_DESC = 412, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_LIST_ALL_CHECKPOINTS = 413, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_LIST_ALL_CHILDREN = 414, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_LOOKUP_BY_NAME = 415, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_GET_PARENT = 416, + + /** + * @generate: both + * @acl: domain:checkpoint + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_DELETE = 417 }; diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs index 2398494520..a42b4a9671 100644 --- a/src/remote_protocol-structs +++ b/src/remote_protocol-structs @@ -50,6 +50,10 @@ struct remote_nonnull_secret { int usageType; remote_nonnull_string usageID; }; +struct remote_nonnull_domain_checkpoint { + remote_nonnull_string name; + remote_nonnull_domain dom; +}; struct remote_nonnull_domain_snapshot { remote_nonnull_string name; remote_nonnull_domain dom; @@ -3043,6 +3047,64 @@ struct remote_network_port_delete_args { remote_nonnull_network_port port; u_int flags; }; +struct remote_domain_checkpoint_create_xml_args { + remote_nonnull_domain dom; + remote_nonnull_string xml_desc; + u_int flags; +}; +struct remote_domain_checkpoint_create_xml_ret { + remote_nonnull_domain_checkpoint checkpoint; +}; +struct remote_domain_checkpoint_get_xml_desc_args { + remote_nonnull_domain_checkpoint checkpoint; + u_int flags; +}; +struct remote_domain_checkpoint_get_xml_desc_ret { + remote_nonnull_string xml; +}; +struct remote_domain_list_all_checkpoints_args { + remote_nonnull_domain dom; + int need_results; + u_int flags; +}; +struct remote_domain_list_all_checkpoints_ret { + struct { + u_int checkpoints_len; + remote_nonnull_domain_checkpoint * checkpoints_val; + } checkpoints; + int ret; +}; +struct remote_domain_checkpoint_list_all_children_args { + remote_nonnull_domain_checkpoint checkpoint; + int need_results; + u_int flags; +}; +struct remote_domain_checkpoint_list_all_children_ret { + struct { + u_int checkpoints_len; + remote_nonnull_domain_checkpoint * checkpoints_val; + } checkpoints; + int ret; +}; +struct remote_domain_checkpoint_lookup_by_name_args { + remote_nonnull_domain dom; + remote_nonnull_string name; + u_int flags; +}; +struct remote_domain_checkpoint_lookup_by_name_ret { + remote_nonnull_domain_checkpoint checkpoint; +}; +struct remote_domain_checkpoint_get_parent_args { + remote_nonnull_domain_checkpoint checkpoint; + u_int flags; +}; +struct remote_domain_checkpoint_get_parent_ret { + remote_nonnull_domain_checkpoint parent; +}; +struct remote_domain_checkpoint_delete_args { + remote_nonnull_domain_checkpoint checkpoint; + u_int flags; +}; enum remote_procedure { REMOTE_PROC_CONNECT_OPEN = 1, REMOTE_PROC_CONNECT_CLOSE = 2, @@ -3454,4 +3516,11 @@ enum remote_procedure { REMOTE_PROC_NETWORK_PORT_SET_PARAMETERS = 408, REMOTE_PROC_NETWORK_PORT_GET_XML_DESC = 409, REMOTE_PROC_NETWORK_PORT_DELETE = 410, + REMOTE_PROC_DOMAIN_CHECKPOINT_CREATE_XML = 411, + REMOTE_PROC_DOMAIN_CHECKPOINT_GET_XML_DESC = 412, + REMOTE_PROC_DOMAIN_LIST_ALL_CHECKPOINTS = 413, + REMOTE_PROC_DOMAIN_CHECKPOINT_LIST_ALL_CHILDREN = 414, + REMOTE_PROC_DOMAIN_CHECKPOINT_LOOKUP_BY_NAME = 415, + REMOTE_PROC_DOMAIN_CHECKPOINT_GET_PARENT = 416, + REMOTE_PROC_DOMAIN_CHECKPOINT_DELETE = 417, }; diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index f683e711ce..4e548e14cd 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -58,6 +58,7 @@ sub fixup_name { $name =~ s/Nwfilter/NWFilter/; $name =~ s/Xml$/XML/; + $name =~ s/Xml2$/XML2/; $name =~ s/Uri$/URI/; $name =~ s/Uuid$/UUID/; $name =~ s/Id$/ID/; @@ -567,18 +568,20 @@ elsif ($mode eq "server") { push(@args_list, "$2"); push(@free_list, " virObjectUnref($2);"); - } elsif ($args_member =~ m/^remote_nonnull_domain_snapshot (\S+);$/) { + } elsif ($args_member =~ m/^remote_nonnull_domain_(checkpoint|snapshot) (\S+);$/) { + my $type_name = name_to_TypeName($1); + push(@vars_list, "virDomainPtr dom = NULL"); - push(@vars_list, "virDomainSnapshotPtr snapshot = NULL"); + push(@vars_list, "virDomain${type_name}Ptr ${1} = NULL"); push(@getters_list, - " if (!(dom = get_nonnull_domain($conn, args->${1}.dom)))\n" . + " if (!(dom = get_nonnull_domain($conn, args->${2}.dom)))\n" . " goto cleanup;\n" . "\n" . - " if (!(snapshot = get_nonnull_domain_snapshot(dom, args->${1})))\n" . + " if (!($1 = get_nonnull_domain_${1}(dom, args->$2)))\n" . " goto cleanup;\n"); - push(@args_list, "snapshot"); + push(@args_list, "$1"); push(@free_list, - " virObjectUnref(snapshot);\n" . + " virObjectUnref($1);\n" . " virObjectUnref(dom);"); } elsif ($args_member =~ m/^(?:(?:admin|remote)_string|remote_uuid) (\S+)<\S+>;/) { push(@args_list, $conn) if !@args_list; @@ -722,7 +725,7 @@ elsif ($mode eq "server") { if (!$modern_ret_as_list) { push(@ret_list, "ret->$3 = tmp.$3;"); } - } elsif ($ret_member =~ m/(?:admin|remote)_nonnull_(secret|nwfilter|nwfilter_binding|node_device|interface|network|network_port|storage_vol|storage_pool|domain_snapshot|domain|server|client) (\S+)<(\S+)>;/) { + } elsif ($ret_member =~ m/(?:admin|remote)_nonnull_(secret|nwfilter|nwfilter_binding|node_device|interface|network|network_port|storage_vol|storage_pool|domain_checkpoint|domain_snapshot|domain|server|client) (\S+)<(\S+)>;/) { $modern_ret_struct_name = $1; $single_ret_list_error_msg_type = $1; $single_ret_list_name = $2; @@ -780,7 +783,7 @@ elsif ($mode eq "server") { $single_ret_var = $1; $single_ret_by_ref = 0; $single_ret_check = " == NULL"; - } elsif ($ret_member =~ m/^remote_nonnull_(domain|network|network_port|storage_pool|storage_vol|interface|node_device|secret|nwfilter|nwfilter_binding|domain_snapshot) (\S+);/) { + } elsif ($ret_member =~ m/^remote_nonnull_(domain|network|network_port|storage_pool|storage_vol|interface|node_device|secret|nwfilter|nwfilter_binding|domain_checkpoint|domain_snapshot) (\S+);/) { my $type_name = name_to_TypeName($1); if ($call->{ProcName} eq "DomainCreateWithFlags") { @@ -1328,13 +1331,14 @@ elsif ($mode eq "client") { $priv_src = "dev->conn"; push(@args_list, "virNodeDevicePtr dev"); push(@setters_list, "args.name = dev->name;"); - } elsif ($args_member =~ m/^remote_nonnull_(domain|network|network_port|storage_pool|storage_vol|interface|secret|nwfilter|nwfilter_binding|domain_snapshot) (\S+);/) { + } elsif ($args_member =~ m/^remote_nonnull_(domain|network|network_port|storage_pool|storage_vol|interface|secret|nwfilter|nwfilter_binding|domain_checkpoint|domain_snapshot) (\S+);/) { my $name = $1; my $arg_name = $2; my $type_name = name_to_TypeName($name); if ($is_first_arg) { - if ($name eq "domain_snapshot") { + if ($name eq "domain_snapshot" || + $name eq "domain_checkpoint") { $priv_src = "$arg_name->domain->conn"; } elsif ($name eq "network_port") { $priv_src = "$arg_name->net->conn"; @@ -1523,7 +1527,7 @@ elsif ($mode eq "client") { } push(@ret_list, "memcpy(result->$3, ret.$3, sizeof(result->$3));"); - } elsif ($ret_member =~ m/(?:admin|remote)_nonnull_(secret|nwfilter|nwfilter_binding|node_device|interface|network|network_port|storage_vol|storage_pool|domain_snapshot|domain|server|client) (\S+)<(\S+)>;/) { + } elsif ($ret_member =~ m/(?:admin|remote)_nonnull_(secret|nwfilter|nwfilter_binding|node_device|interface|network|network_port|storage_vol|storage_pool|domain_checkpoint|domain_snapshot|domain|server|client) (\S+)<(\S+)>;/) { my $proc_name = name_to_TypeName($1); if ($structprefix eq "admin") { @@ -1576,7 +1580,7 @@ elsif ($mode eq "client") { push(@ret_list, "VIR_FREE(ret.$1);"); $single_ret_var = "char *rv = NULL"; $single_ret_type = "char *"; - } elsif ($ret_member =~ m/^remote_nonnull_(domain|network|network_port|storage_pool|storage_vol|node_device|interface|secret|nwfilter|nwfilter_binding|domain_snapshot) (\S+);/) { + } elsif ($ret_member =~ m/^remote_nonnull_(domain|network|network_port|storage_pool|storage_vol|node_device|interface|secret|nwfilter|nwfilter_binding|domain_checkpoint|domain_snapshot) (\S+);/) { my $name = $1; my $arg_name = $2; my $type_name = name_to_TypeName($name); @@ -1590,7 +1594,7 @@ elsif ($mode eq "client") { $single_ret_var = "int rv = -1"; $single_ret_type = "int"; } else { - if ($name eq "domain_snapshot") { + if ($name =~ m/^domain_.*/) { my $dom = "$priv_src"; $dom =~ s/->conn//; push(@ret_list, "rv = get_nonnull_$name($dom, ret.$arg_name);"); @@ -1934,7 +1938,7 @@ elsif ($mode eq "client") { print " }\n"; print "\n"; } elsif ($modern_ret_as_list) { - if ($modern_ret_struct_name =~ m/domain_snapshot|client/) { + if ($modern_ret_struct_name =~ m/domain_checkpoint|domain_snapshot|client/) { $priv_src =~ s/->conn//; } print " if (result) {\n"; -- 2.20.1

Introduce a bunch of new virsh commands for managing checkpoints in isolation. More commands are needed for performing incremental backups, but these commands were easy to implement by modeling heavily after virsh-snapshot.c. There is no need for checkpoint-revert or checkpoint-current since those snapshot APIs have no checkpoint counterpart. Similarly, it is not necessary to change which checkpoint is current when redefining from XML, since checkpoints expose whether they are current in the public XML (rather than the way snapshots did it behind the scenese). checkpoint-list is a bit simpler, in part because we don't have to cater to back-compat to older API. checkpoint-info is a bit trickier, because it requires parsing XML (maybe we'll want virDomainCheckpointIsCurrent() as an API after all). Signed-off-by: Eric Blake <eblake@redhat.com> --- tools/virsh-checkpoint.h | 26 + tools/virsh-completer.h | 4 + tools/virsh-util.h | 3 + tools/virsh.h | 1 + po/POTFILES | 1 + tools/Makefile.am | 1 + tools/virsh-checkpoint.c | 1153 ++++++++++++++++++++++++++++++++++ tools/virsh-completer.c | 51 ++ tools/virsh-domain-monitor.c | 23 + tools/virsh-domain.c | 7 + tools/virsh-util.c | 11 + tools/virsh.c | 2 + tools/virsh.pod | 185 +++++- 13 files changed, 1461 insertions(+), 7 deletions(-) create mode 100644 tools/virsh-checkpoint.h create mode 100644 tools/virsh-checkpoint.c diff --git a/tools/virsh-checkpoint.h b/tools/virsh-checkpoint.h new file mode 100644 index 0000000000..7cc998638f --- /dev/null +++ b/tools/virsh-checkpoint.h @@ -0,0 +1,26 @@ +/* + * virsh-checkpoint.h: Commands to manage domain checkpoints + * + * Copyright (C) 2005-2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + * + */ + +#pragma once + +#include "virsh.h" + +extern const vshCmdDef checkpointCmds[]; diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index cb7aafcc8c..2e54dddc05 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -78,6 +78,10 @@ char ** virshSecretUUIDCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); +char ** virshCheckpointNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + char ** virshSnapshotNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); diff --git a/tools/virsh-util.h b/tools/virsh-util.h index 55520302ff..9005aa9d36 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -42,6 +42,9 @@ virshCommandOptDomain(vshControl *ctl, void virshDomainFree(virDomainPtr dom); +void +virshDomainCheckpointFree(virDomainCheckpointPtr chk); + void virshDomainSnapshotFree(virDomainSnapshotPtr snap); diff --git a/tools/virsh.h b/tools/virsh.h index 847ed25151..b4e610b2a4 100644 --- a/tools/virsh.h +++ b/tools/virsh.h @@ -40,6 +40,7 @@ /* * Command group types */ +#define VIRSH_CMD_GRP_CHECKPOINT "Checkpoint" #define VIRSH_CMD_GRP_DOM_MANAGEMENT "Domain Management" #define VIRSH_CMD_GRP_DOM_MONITORING "Domain Monitoring" #define VIRSH_CMD_GRP_STORAGE_POOL "Storage Pool" diff --git a/po/POTFILES b/po/POTFILES index 2c27049d55..c62bc32bb2 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -296,6 +296,7 @@ src/xenconfig/xen_xl.c src/xenconfig/xen_xm.c tests/virpolkittest.c tools/libvirt-guests.sh.in +tools/virsh-checkpoint.c tools/virsh-console.c tools/virsh-domain-monitor.c tools/virsh-domain.c diff --git a/tools/Makefile.am b/tools/Makefile.am index c6064dee08..2807b9f6fd 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -216,6 +216,7 @@ virt_login_shell_CFLAGS = \ virsh_SOURCES = \ virsh.c virsh.h \ + virsh-checkpoint.c virsh-checkpoint.h \ virsh-completer.c virsh-completer.h \ virsh-console.c virsh-console.h \ virsh-domain.c virsh-domain.h \ diff --git a/tools/virsh-checkpoint.c b/tools/virsh-checkpoint.c new file mode 100644 index 0000000000..f6401f2cf0 --- /dev/null +++ b/tools/virsh-checkpoint.c @@ -0,0 +1,1153 @@ +/* + * virsh-checkpoint.c: Commands to manage domain checkpoints + * + * Copyright (C) 2005-2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + * + * Daniel Veillard <veillard@redhat.com> + * Karel Zak <kzak@redhat.com> + * Daniel P. Berrange <berrange@redhat.com> + * + */ + +#include <config.h> +#include "virsh-checkpoint.h" + +#include <assert.h> + +#include <libxml/parser.h> +#include <libxml/tree.h> +#include <libxml/xpath.h> +#include <libxml/xmlsave.h> + +#include "internal.h" +#include "virbuffer.h" +#include "viralloc.h" +#include "virfile.h" +#include "virsh-util.h" +#include "virstring.h" +#include "virxml.h" +#include "conf/checkpoint_conf.h" +#include "vsh-table.h" + +/* Helper for checkpoint-create and checkpoint-create-as */ +static bool +virshCheckpointCreate(vshControl *ctl, + virDomainPtr dom, + const char *buffer, + unsigned int flags, + const char *from) +{ + bool ret = false; + virDomainCheckpointPtr checkpoint; + const char *name = NULL; + + checkpoint = virDomainCheckpointCreateXML(dom, buffer, flags); + + if (checkpoint == NULL) + goto cleanup; + + name = virDomainCheckpointGetName(checkpoint); + if (!name) { + vshError(ctl, "%s", _("Could not get checkpoint name")); + goto cleanup; + } + + if (from) + vshPrintExtra(ctl, _("Domain checkpoint %s created from '%s'"), + name, from); + else + vshPrintExtra(ctl, _("Domain checkpoint %s created"), name); + + ret = true; + + cleanup: + virshDomainCheckpointFree(checkpoint); + return ret; +} + + +/* + * "checkpoint-create" command + */ +static const vshCmdInfo info_checkpoint_create[] = { + {.name = "help", + .data = N_("Create a checkpoint from XML") + }, + {.name = "desc", + .data = N_("Create a checkpoint from XML for use in " + "future incremental backups") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_create[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "xmlfile", + .type = VSH_OT_STRING, + .help = N_("domain checkpoint XML") + }, + {.name = "redefine", + .type = VSH_OT_BOOL, + .help = N_("redefine metadata for existing checkpoint") + }, + {.name = "quiesce", + .type = VSH_OT_BOOL, + .help = N_("quiesce guest's file systems") + }, + {.name = NULL} +}; + +static bool +cmdCheckpointCreate(vshControl *ctl, + const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + const char *from = NULL; + char *buffer = NULL; + unsigned int flags = 0; + + if (vshCommandOptBool(cmd, "redefine")) + flags |= VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE; + if (vshCommandOptBool(cmd, "quiesce")) + flags |= VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + goto cleanup; + + if (vshCommandOptStringReq(ctl, cmd, "xmlfile", &from) < 0) + goto cleanup; + if (!from) { + buffer = vshStrdup(ctl, "<domaincheckpoint/>"); + } else { + if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) { + vshSaveLibvirtError(); + goto cleanup; + } + } + + ret = virshCheckpointCreate(ctl, dom, buffer, flags, from); + + cleanup: + VIR_FREE(buffer); + virshDomainFree(dom); + + return ret; +} + + +/* + * "checkpoint-create-as" command + */ +static int +virshParseCheckpointDiskspec(vshControl *ctl, + virBufferPtr buf, + const char *str) +{ + int ret = -1; + const char *name = NULL; + const char *checkpoint = NULL; + const char *bitmap = NULL; + char **array = NULL; + int narray; + size_t i; + + narray = vshStringToArray(str, &array); + if (narray <= 0) + goto cleanup; + + name = array[0]; + for (i = 1; i < narray; i++) { + if (!checkpoint && STRPREFIX(array[i], "checkpoint=")) + checkpoint = array[i] + strlen("checkpoint="); + else if (!bitmap && STRPREFIX(array[i], "bitmap=")) + bitmap = array[i] + strlen("bitmap="); + else + goto cleanup; + } + + virBufferEscapeString(buf, "<disk name='%s'", name); + if (checkpoint) + virBufferAsprintf(buf, " checkpoint='%s'", checkpoint); + if (bitmap) + virBufferAsprintf(buf, " bitmap='%s'", bitmap); + virBufferAddLit(buf, "/>\n"); + ret = 0; + cleanup: + if (ret < 0) + vshError(ctl, _("unable to parse diskspec: %s"), str); + virStringListFree(array); + return ret; +} + +static const vshCmdInfo info_checkpoint_create_as[] = { + {.name = "help", + .data = N_("Create a checkpoint from a set of args") + }, + {.name = "desc", + .data = N_("Create a checkpoint from arguments for use in " + "future incremental backups") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_create_as[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "name", + .type = VSH_OT_STRING, + .help = N_("name of checkpoint") + }, + {.name = "description", + .type = VSH_OT_STRING, + .help = N_("description of checkpoint") + }, + {.name = "print-xml", + .type = VSH_OT_BOOL, + .help = N_("print XML document rather than create") + }, + {.name = "quiesce", + .type = VSH_OT_BOOL, + .help = N_("quiesce guest's file systems") + }, + {.name = "diskspec", + .type = VSH_OT_ARGV, + .help = N_("disk attributes: disk[,checkpoint=type][,bitmap=name]") + }, + {.name = NULL} +}; + + +static bool +cmdCheckpointCreateAs(vshControl *ctl, + const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + char *buffer = NULL; + const char *name = NULL; + const char *desc = NULL; + virBuffer buf = VIR_BUFFER_INITIALIZER; + unsigned int flags = 0; + const vshCmdOpt *opt = NULL; + + if (vshCommandOptBool(cmd, "quiesce")) + flags |= VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0 || + vshCommandOptStringReq(ctl, cmd, "description", &desc) < 0) + goto cleanup; + + virBufferAddLit(&buf, "<domaincheckpoint>\n"); + virBufferAdjustIndent(&buf, 2); + virBufferEscapeString(&buf, "<name>%s</name>\n", name); + virBufferEscapeString(&buf, "<description>%s</description>\n", desc); + + if (vshCommandOptBool(cmd, "diskspec")) { + virBufferAddLit(&buf, "<disks>\n"); + virBufferAdjustIndent(&buf, 2); + while ((opt = vshCommandOptArgv(ctl, cmd, opt))) { + if (virshParseCheckpointDiskspec(ctl, &buf, opt->data) < 0) + goto cleanup; + } + virBufferAdjustIndent(&buf, -2); + virBufferAddLit(&buf, "</disks>\n"); + } + virBufferAdjustIndent(&buf, -2); + virBufferAddLit(&buf, "</domaincheckpoint>\n"); + + if (virBufferError(&buf)) { + vshError(ctl, "%s", _("Out of memory")); + goto cleanup; + } + + buffer = virBufferContentAndReset(&buf); + + if (vshCommandOptBool(cmd, "print-xml")) { + vshPrint(ctl, "%s\n", buffer); + ret = true; + goto cleanup; + } + + ret = virshCheckpointCreate(ctl, dom, buffer, flags, NULL); + + cleanup: + virBufferFreeAndReset(&buf); + VIR_FREE(buffer); + virshDomainFree(dom); + + return ret; +} + + +/* Helper for resolving --ARG name into a checkpoint + * belonging to DOM. On success, populate *CHK and *NAME, before + * returning 0. On failure, return -1 after issuing an error + * message. */ +static int +virshLookupCheckpoint(vshControl *ctl, + const vshCmd *cmd, + const char *arg, + virDomainPtr dom, + virDomainCheckpointPtr *chk, + const char **name) +{ + const char *chkname = NULL; + + if (vshCommandOptStringReq(ctl, cmd, arg, &chkname) < 0) + return -1; + + if (chkname) { + *chk = virDomainCheckpointLookupByName(dom, chkname, 0); + } else { + vshError(ctl, _("--%s is required"), arg); + return -1; + } + if (!*chk) { + vshReportError(ctl); + return -1; + } + + *name = virDomainCheckpointGetName(*chk); + return 0; +} + + +/* + * "checkpoint-edit" command + */ +static const vshCmdInfo info_checkpoint_edit[] = { + {.name = "help", + .data = N_("edit XML for a checkpoint") + }, + {.name = "desc", + .data = N_("Edit the domain checkpoint XML for a named checkpoint") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_edit[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "checkpointname", + .type = VSH_OT_STRING, + .help = N_("checkpoint name"), + .completer = virshCheckpointNameCompleter, + }, + {.name = NULL} +}; + +static bool +cmdCheckpointEdit(vshControl *ctl, + const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + virDomainCheckpointPtr checkpoint = NULL; + virDomainCheckpointPtr edited = NULL; + const char *name = NULL; + const char *edited_name; + bool ret = false; + unsigned int getxml_flags = VIR_DOMAIN_CHECKPOINT_XML_SECURE; + unsigned int define_flags = VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, + &checkpoint, &name) < 0) + goto cleanup; + +#define EDIT_GET_XML \ + virDomainCheckpointGetXMLDesc(checkpoint, getxml_flags) +#define EDIT_NOT_CHANGED \ + do { \ + vshPrintExtra(ctl, \ + _("Checkpoint %s XML configuration not changed.\n"), \ + name); \ + ret = true; \ + goto edit_cleanup; \ + } while (0) +#define EDIT_DEFINE \ + edited = virDomainCheckpointCreateXML(dom, doc_edited, define_flags) +#include "virsh-edit.c" + + edited_name = virDomainCheckpointGetName(edited); + if (STREQ(name, edited_name)) { + vshPrintExtra(ctl, _("Checkpoint %s edited.\n"), name); + } else { + unsigned int delete_flags = VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY; + + if (virDomainCheckpointDelete(edited, delete_flags) < 0) { + vshReportError(ctl); + vshError(ctl, _("Failed to clean up %s"), edited_name); + goto cleanup; + } + vshError(ctl, _("Cannot rename checkpoint %s to %s"), + name, edited_name); + goto cleanup; + } + + ret = true; + + cleanup: + if (!ret && name) + vshError(ctl, _("Failed to update %s"), name); + virshDomainCheckpointFree(edited); + virshDomainCheckpointFree(checkpoint); + virshDomainFree(dom); + return ret; +} + + +/* Helper function to get the name of a checkpoint's parent. Caller + * must free the result. Returns 0 on success (including when it was + * proven no parent exists), and -1 on failure with error reported + * (such as no checkpoint support or domain deleted in meantime). */ +static int +virshGetCheckpointParent(vshControl *ctl, + virDomainCheckpointPtr checkpoint, + char **parent_name) +{ + virDomainCheckpointPtr parent = NULL; + int ret = -1; + + *parent_name = NULL; + + parent = virDomainCheckpointGetParent(checkpoint, 0); + if (parent) { + /* API works, and virDomainCheckpointGetName will succeed */ + *parent_name = vshStrdup(ctl, virDomainCheckpointGetName(parent)); + ret = 0; + } else if (last_error->code == VIR_ERR_NO_DOMAIN_CHECKPOINT) { + /* API works, and we found a root with no parent */ + ret = 0; + } + + if (ret < 0) { + vshReportError(ctl); + vshError(ctl, "%s", _("unable to determine if checkpoint has parent")); + } else { + vshResetLibvirtError(); + } + virshDomainCheckpointFree(parent); + return ret; +} + + +/* + * "checkpoint-info" command + */ +static const vshCmdInfo info_checkpoint_info[] = { + {.name = "help", + .data = N_("checkpoint information") + }, + {.name = "desc", + .data = N_("Returns basic information about a checkpoint.") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_info[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "checkpointname", + .type = VSH_OT_STRING, + .help = N_("checkpoint name"), + .completer = virshCheckpointNameCompleter, + }, + {.name = NULL} +}; + + +static bool +cmdCheckpointInfo(vshControl *ctl, + const vshCmd *cmd) +{ + virDomainPtr dom; + virDomainCheckpointPtr checkpoint = NULL; + const char *name; + char *parent = NULL; + char *xml = NULL; + xmlDocPtr xmldoc = NULL; + xmlXPathContextPtr ctxt = NULL; + bool ret = false; + int count; + unsigned int flags; + + dom = virshCommandOptDomain(ctl, cmd, NULL); + if (dom == NULL) + return false; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, + &checkpoint, &name) < 0) + goto cleanup; + + vshPrint(ctl, "%-15s %s\n", _("Name:"), name); + vshPrint(ctl, "%-15s %s\n", _("Domain:"), virDomainGetName(dom)); + + if (virshGetCheckpointParent(ctl, checkpoint, &parent) < 0) { + vshError(ctl, "%s", + _("unexpected problem querying checkpoint state")); + goto cleanup; + } + vshPrint(ctl, "%-15s %s\n", _("Parent:"), parent ? parent : "-"); + + /* Children, Descendants. */ + flags = 0; + count = virDomainCheckpointListAllChildren(checkpoint, NULL, flags); + if (count < 0) { + if (last_error->code == VIR_ERR_NO_SUPPORT) { + vshResetLibvirtError(); + ret = true; + } + goto cleanup; + } + vshPrint(ctl, "%-15s %d\n", _("Children:"), count); + flags = VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS; + count = virDomainCheckpointListAllChildren(checkpoint, NULL, flags); + if (count < 0) + goto cleanup; + vshPrint(ctl, "%-15s %d\n", _("Descendants:"), count); + + ret = true; + + cleanup: + xmlXPathFreeContext(ctxt); + xmlFreeDoc(xmldoc); + VIR_FREE(xml); + VIR_FREE(parent); + virshDomainCheckpointFree(checkpoint); + virshDomainFree(dom); + return ret; +} + + +/* Helpers for collecting a list of checkpoints. */ +struct virshChk { + virDomainCheckpointPtr chk; + char *parent; +}; +struct virshCheckpointList { + struct virshChk *chks; + int nchks; +}; +typedef struct virshCheckpointList *virshCheckpointListPtr; + +static void +virshCheckpointListFree(virshCheckpointListPtr checkpointlist) +{ + size_t i; + + if (!checkpointlist) + return; + if (checkpointlist->chks) { + for (i = 0; i < checkpointlist->nchks; i++) { + virshDomainCheckpointFree(checkpointlist->chks[i].chk); + VIR_FREE(checkpointlist->chks[i].parent); + } + VIR_FREE(checkpointlist->chks); + } + VIR_FREE(checkpointlist); +} + + +static int +virshChkSorter(const void *a, + const void *b) +{ + const struct virshChk *sa = a; + const struct virshChk *sb = b; + + if (sa->chk && !sb->chk) + return -1; + if (!sa->chk) + return sb->chk != NULL; + + return vshStrcasecmp(virDomainCheckpointGetName(sa->chk), + virDomainCheckpointGetName(sb->chk)); +} + + +/* Compute a list of checkpoints from DOM. If FROM is provided, the + * list is limited to descendants of the given checkpoint. If FLAGS is + * given, the list is filtered. If TREE is specified, then all but + * FROM or the roots will also have parent information. */ +static virshCheckpointListPtr +virshCheckpointListCollect(vshControl *ctl, + virDomainPtr dom, + virDomainCheckpointPtr from, + unsigned int orig_flags, + bool tree) +{ + size_t i; + char **names = NULL; + int count = -1; + virDomainCheckpointPtr *chks; + virshCheckpointListPtr checkpointlist = vshMalloc(ctl, + sizeof(*checkpointlist)); + virshCheckpointListPtr ret = NULL; + unsigned int flags = orig_flags; + + if (from) + count = virDomainCheckpointListAllChildren(from, &chks, flags); + else + count = virDomainListAllCheckpoints(dom, &chks, flags); + if (count < 0) { + vshError(ctl, "%s", + _("unexpected problem querying checkpoints")); + goto cleanup; + } + + /* When mixing --from and --tree, we also want a copy of from + * in the list, but with no parent for that one entry. */ + checkpointlist->chks = vshCalloc(ctl, count + (tree && from), + sizeof(*checkpointlist->chks)); + checkpointlist->nchks = count; + for (i = 0; i < count; i++) + checkpointlist->chks[i].chk = chks[i]; + VIR_FREE(chks); + if (tree) { + for (i = 0; i < count; i++) { + if (virshGetCheckpointParent(ctl, checkpointlist->chks[i].chk, + &checkpointlist->chks[i].parent) < 0) + goto cleanup; + } + if (from) { + checkpointlist->chks[checkpointlist->nchks++].chk = from; + virDomainCheckpointRef(from); + } + } + + if (!(orig_flags & VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL)) + qsort(checkpointlist->chks, checkpointlist->nchks, + sizeof(*checkpointlist->chks), virshChkSorter); + + ret = checkpointlist; + checkpointlist = NULL; + + cleanup: + virshCheckpointListFree(checkpointlist); + if (names && count > 0) + for (i = 0; i < count; i++) + VIR_FREE(names[i]); + VIR_FREE(names); + return ret; +} + + +static const char * +virshCheckpointListLookup(int id, + bool parent, + void *opaque) +{ + virshCheckpointListPtr checkpointlist = opaque; + if (parent) + return checkpointlist->chks[id].parent; + return virDomainCheckpointGetName(checkpointlist->chks[id].chk); +} + + +/* + * "checkpoint-list" command + */ +static const vshCmdInfo info_checkpoint_list[] = { + {.name = "help", + .data = N_("List checkpoints for a domain") + }, + {.name = "desc", + .data = N_("Checkpoint List") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_list[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "parent", + .type = VSH_OT_BOOL, + .help = N_("add a column showing parent checkpoint") + }, + {.name = "roots", + .type = VSH_OT_BOOL, + .help = N_("list only checkpoints without parents") + }, + {.name = "leaves", + .type = VSH_OT_BOOL, + .help = N_("list only checkpoints without children") + }, + {.name = "no-leaves", + .type = VSH_OT_BOOL, + .help = N_("list only checkpoints that are not leaves (with children)") + }, + {.name = "tree", + .type = VSH_OT_BOOL, + .help = N_("list checkpoints in a tree") + }, + {.name = "from", + .type = VSH_OT_STRING, + .help = N_("limit list to children of given checkpoint"), + .completer = virshCheckpointNameCompleter, + }, + {.name = "descendants", + .type = VSH_OT_BOOL, + .help = N_("with --from, list all descendants") + }, + {.name = "name", + .type = VSH_OT_BOOL, + .help = N_("list checkpoint names only") + }, + {.name = "topological", + .type = VSH_OT_BOOL, + .help = N_("sort list topologically rather than by name"), + }, + {.name = NULL} +}; + +static bool +cmdCheckpointList(vshControl *ctl, + const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + unsigned int flags = 0; + size_t i; + xmlDocPtr xml = NULL; + xmlXPathContextPtr ctxt = NULL; + char *doc = NULL; + virDomainCheckpointPtr checkpoint = NULL; + long long creation_longlong; + time_t creation_time_t; + char timestr[100]; + struct tm time_info; + bool tree = vshCommandOptBool(cmd, "tree"); + bool name = vshCommandOptBool(cmd, "name"); + bool from = vshCommandOptBool(cmd, "from"); + bool parent = vshCommandOptBool(cmd, "parent"); + bool roots = vshCommandOptBool(cmd, "roots"); + const char *from_chk = NULL; + char *parent_chk = NULL; + virDomainCheckpointPtr start = NULL; + virshCheckpointListPtr checkpointlist = NULL; + vshTablePtr table = NULL; + + VSH_EXCLUSIVE_OPTIONS_VAR(tree, name); + VSH_EXCLUSIVE_OPTIONS_VAR(parent, roots); + VSH_EXCLUSIVE_OPTIONS_VAR(parent, tree); + VSH_EXCLUSIVE_OPTIONS_VAR(roots, tree); + VSH_EXCLUSIVE_OPTIONS_VAR(roots, from); + +#define FILTER(option, flag) \ + do { \ + if (vshCommandOptBool(cmd, option)) { \ + if (tree) { \ + vshError(ctl, \ + _("--%s and --tree are mutually exclusive"), \ + option); \ + return false; \ + } \ + flags |= VIR_DOMAIN_CHECKPOINT_LIST_ ## flag; \ + } \ + } while (0) + + FILTER("leaves", LEAVES); + FILTER("no-leaves", NO_LEAVES); +#undef FILTER + + if (vshCommandOptBool(cmd, "topological")) + flags |= VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL; + + if (roots) + flags |= VIR_DOMAIN_CHECKPOINT_LIST_ROOTS; + + if (vshCommandOptBool(cmd, "descendants")) { + if (!from) { + vshError(ctl, "%s", + _("--descendants requires --from")); + return false; + } + flags |= VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS; + } + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (from && + virshLookupCheckpoint(ctl, cmd, "from", dom, &start, &from_chk) < 0) + goto cleanup; + + if (!(checkpointlist = virshCheckpointListCollect(ctl, dom, start, flags, + tree))) + goto cleanup; + + if (!tree && !name) { + if (parent) + table = vshTableNew(_("Name"), _("Creation Time"), _("Parent"), + NULL); + else + table = vshTableNew(_("Name"), _("Creation Time"), NULL); + if (!table) + goto cleanup; + } + + if (tree) { + for (i = 0; i < checkpointlist->nchks; i++) { + if (!checkpointlist->chks[i].parent && + vshTreePrint(ctl, virshCheckpointListLookup, checkpointlist, + checkpointlist->nchks, i) < 0) + goto cleanup; + } + ret = true; + goto cleanup; + } + + for (i = 0; i < checkpointlist->nchks; i++) { + const char *chk_name; + + /* free up memory from previous iterations of the loop */ + VIR_FREE(parent_chk); + xmlXPathFreeContext(ctxt); + xmlFreeDoc(xml); + VIR_FREE(doc); + + checkpoint = checkpointlist->chks[i].chk; + chk_name = virDomainCheckpointGetName(checkpoint); + assert(chk_name); + + if (name) { + /* just print the checkpoint name */ + vshPrint(ctl, "%s\n", chk_name); + continue; + } + + if (!(doc = virDomainCheckpointGetXMLDesc(checkpoint, 0))) + continue; + + if (!(xml = virXMLParseStringCtxt(doc, _("(domain_checkpoint)"), &ctxt))) + continue; + + if (parent) + parent_chk = virXPathString("string(/domaincheckpoint/parent/name)", + ctxt); + + if (virXPathLongLong("string(/domaincheckpoint/creationTime)", ctxt, + &creation_longlong) < 0) + continue; + creation_time_t = creation_longlong; + if (creation_time_t != creation_longlong) { + vshError(ctl, "%s", _("time_t overflow")); + continue; + } + localtime_r(&creation_time_t, &time_info); + strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S %z", + &time_info); + + if (parent) { + if (vshTableRowAppend(table, chk_name, timestr, + NULLSTR_EMPTY(parent_chk), NULL) < 0) + goto cleanup; + } else { + if (vshTableRowAppend(table, chk_name, timestr, NULL) < 0) + goto cleanup; + } + } + + if (table) + vshTablePrintToStdout(table, ctl); + + ret = true; + + cleanup: + /* this frees up memory from the last iteration of the loop */ + virshCheckpointListFree(checkpointlist); + VIR_FREE(parent_chk); + virshDomainCheckpointFree(start); + xmlXPathFreeContext(ctxt); + xmlFreeDoc(xml); + VIR_FREE(doc); + virshDomainFree(dom); + vshTableFree(table); + + return ret; +} + + +/* + * "checkpoint-dumpxml" command + */ +static const vshCmdInfo info_checkpoint_dumpxml[] = { + {.name = "help", + .data = N_("Dump XML for a domain checkpoint") + }, + {.name = "desc", + .data = N_("Checkpoint Dump XML") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_dumpxml[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "checkpointname", + .type = VSH_OT_STRING, + .help = N_("checkpoint name"), + .completer = virshCheckpointNameCompleter, + }, + {.name = "security-info", + .type = VSH_OT_BOOL, + .help = N_("include security sensitive information in XML dump") + }, + {.name = "no-domain", + .type = VSH_OT_BOOL, + .help = N_("exclude <domain> from XML") + }, + {.name = "size", + .type = VSH_OT_BOOL, + .help = N_("include backup size estimate in XML dump") + }, + {.name = NULL} +}; + +static bool +cmdCheckpointDumpXML(vshControl *ctl, + const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + const char *name = NULL; + virDomainCheckpointPtr checkpoint = NULL; + char *xml = NULL; + unsigned int flags = 0; + + if (vshCommandOptBool(cmd, "security-info")) + flags |= VIR_DOMAIN_CHECKPOINT_XML_SECURE; + if (vshCommandOptBool(cmd, "no-domain")) + flags |= VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN; + if (vshCommandOptBool(cmd, "size")) + flags |= VIR_DOMAIN_CHECKPOINT_XML_SIZE; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, + &checkpoint, &name) < 0) + goto cleanup; + + if (!(xml = virDomainCheckpointGetXMLDesc(checkpoint, flags))) + goto cleanup; + + vshPrint(ctl, "%s", xml); + ret = true; + + cleanup: + VIR_FREE(xml); + virshDomainCheckpointFree(checkpoint); + virshDomainFree(dom); + + return ret; +} + + +/* + * "checkpoint-parent" command + */ +static const vshCmdInfo info_checkpoint_parent[] = { + {.name = "help", + .data = N_("Get the name of the parent of a checkpoint") + }, + {.name = "desc", + .data = N_("Extract the checkpoint's parent, if any") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_parent[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "checkpointname", + .type = VSH_OT_STRING, + .help = N_("find parent of checkpoint name"), + .completer = virshCheckpointNameCompleter, + }, + {.name = NULL} +}; + +static bool +cmdCheckpointParent(vshControl *ctl, + const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + const char *name = NULL; + virDomainCheckpointPtr checkpoint = NULL; + char *parent = NULL; + + dom = virshCommandOptDomain(ctl, cmd, NULL); + if (dom == NULL) + goto cleanup; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, + &checkpoint, &name) < 0) + goto cleanup; + + if (virshGetCheckpointParent(ctl, checkpoint, &parent) < 0) + goto cleanup; + if (!parent) { + vshError(ctl, _("checkpoint '%s' has no parent"), name); + goto cleanup; + } + + vshPrint(ctl, "%s", parent); + + ret = true; + + cleanup: + VIR_FREE(parent); + virshDomainCheckpointFree(checkpoint); + virshDomainFree(dom); + + return ret; +} + + +/* + * "checkpoint-delete" command + */ +static const vshCmdInfo info_checkpoint_delete[] = { + {.name = "help", + .data = N_("Delete a domain checkpoint") + }, + {.name = "desc", + .data = N_("Checkpoint Delete") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_delete[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "checkpointname", + .type = VSH_OT_STRING, + .help = N_("checkpoint name"), + .completer = virshCheckpointNameCompleter, + }, + {.name = "children", + .type = VSH_OT_BOOL, + .help = N_("delete checkpoint and all children") + }, + {.name = "children-only", + .type = VSH_OT_BOOL, + .help = N_("delete children but not checkpoint") + }, + {.name = "metadata", + .type = VSH_OT_BOOL, + .help = N_("delete only libvirt metadata, leaving checkpoint contents behind") + }, + {.name = NULL} +}; + +static bool +cmdCheckpointDelete(vshControl *ctl, + const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + const char *name = NULL; + virDomainCheckpointPtr checkpoint = NULL; + unsigned int flags = 0; + + dom = virshCommandOptDomain(ctl, cmd, NULL); + if (dom == NULL) + goto cleanup; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", dom, + &checkpoint, &name) < 0) + goto cleanup; + + if (vshCommandOptBool(cmd, "children")) + flags |= VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN; + if (vshCommandOptBool(cmd, "children-only")) + flags |= VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY; + if (vshCommandOptBool(cmd, "metadata")) + flags |= VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY; + + if (virDomainCheckpointDelete(checkpoint, flags) == 0) { + if (flags & VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY) + vshPrintExtra(ctl, _("Domain checkpoint %s children deleted\n"), name); + else + vshPrintExtra(ctl, _("Domain checkpoint %s deleted\n"), name); + } else { + vshError(ctl, _("Failed to delete checkpoint %s"), name); + goto cleanup; + } + + ret = true; + + cleanup: + virshDomainCheckpointFree(checkpoint); + virshDomainFree(dom); + + return ret; +} + + +const vshCmdDef checkpointCmds[] = { + {.name = "checkpoint-create", + .handler = cmdCheckpointCreate, + .opts = opts_checkpoint_create, + .info = info_checkpoint_create, + .flags = 0 + }, + {.name = "checkpoint-create-as", + .handler = cmdCheckpointCreateAs, + .opts = opts_checkpoint_create_as, + .info = info_checkpoint_create_as, + .flags = 0 + }, + {.name = "checkpoint-delete", + .handler = cmdCheckpointDelete, + .opts = opts_checkpoint_delete, + .info = info_checkpoint_delete, + .flags = 0 + }, + {.name = "checkpoint-dumpxml", + .handler = cmdCheckpointDumpXML, + .opts = opts_checkpoint_dumpxml, + .info = info_checkpoint_dumpxml, + .flags = 0 + }, + {.name = "checkpoint-edit", + .handler = cmdCheckpointEdit, + .opts = opts_checkpoint_edit, + .info = info_checkpoint_edit, + .flags = 0 + }, + {.name = "checkpoint-info", + .handler = cmdCheckpointInfo, + .opts = opts_checkpoint_info, + .info = info_checkpoint_info, + .flags = 0 + }, + {.name = "checkpoint-list", + .handler = cmdCheckpointList, + .opts = opts_checkpoint_list, + .info = info_checkpoint_list, + .flags = 0 + }, + {.name = "checkpoint-parent", + .handler = cmdCheckpointParent, + .opts = opts_checkpoint_parent, + .info = info_checkpoint_parent, + .flags = 0 + }, + {.name = NULL} +}; diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 37f946d4b6..4fa6b19225 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -693,6 +693,57 @@ virshSecretUUIDCompleter(vshControl *ctl, } +char ** +virshCheckpointNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + virDomainPtr dom = NULL; + virDomainCheckpointPtr *checkpoints = NULL; + int ncheckpoints = 0; + size_t i = 0; + char **ret = NULL; + + virCheckFlags(0, NULL); + + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) + return NULL; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return NULL; + + if ((ncheckpoints = virDomainListAllCheckpoints(dom, &checkpoints, + flags)) < 0) + goto error; + + if (VIR_ALLOC_N(ret, ncheckpoints + 1) < 0) + goto error; + + for (i = 0; i < ncheckpoints; i++) { + const char *name = virDomainCheckpointGetName(checkpoints[i]); + + if (VIR_STRDUP(ret[i], name) < 0) + goto error; + + virshDomainCheckpointFree(checkpoints[i]); + } + VIR_FREE(checkpoints); + virshDomainFree(dom); + + return ret; + + error: + for (; i < ncheckpoints; i++) + virshDomainCheckpointFree(checkpoints[i]); + VIR_FREE(checkpoints); + for (i = 0; i < ncheckpoints; i++) + VIR_FREE(ret[i]); + VIR_FREE(ret); + virshDomainFree(dom); + return NULL; +} + char ** virshSnapshotNameCompleter(vshControl *ctl, const vshCmd *cmd, diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index e399195deb..0e2c4191d7 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -1621,6 +1621,7 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags) int autostart; int state; int nsnap; + int nchk; int mansave; virshControlPtr priv = ctl->privData; @@ -1788,6 +1789,17 @@ virshDomainListCollect(vshControl *ctl, unsigned int flags) goto remove_entry; } + /* checkpoint filter */ + if (VSH_MATCH(VIR_CONNECT_LIST_DOMAINS_FILTERS_CHECKPOINT)) { + if ((nchk = virDomainListAllCheckpoints(dom, NULL, 0)) < 0) { + vshError(ctl, "%s", _("Failed to get checkpoint count")); + goto cleanup; + } + if (!((VSH_MATCH(VIR_CONNECT_LIST_DOMAINS_HAS_CHECKPOINT) && nchk > 0) || + (VSH_MATCH(VIR_CONNECT_LIST_DOMAINS_NO_CHECKPOINT) && nchk == 0))) + goto remove_entry; + } + /* the domain matched all filters, it may stay */ continue; @@ -1849,6 +1861,14 @@ static const vshCmdOptDef opts_list[] = { .type = VSH_OT_BOOL, .help = N_("list domains without a snapshot") }, + {.name = "with-checkpoint", + .type = VSH_OT_BOOL, + .help = N_("list domains with existing checkpoint") + }, + {.name = "without-checkpoint", + .type = VSH_OT_BOOL, + .help = N_("list domains without a checkpoint") + }, {.name = "state-running", .type = VSH_OT_BOOL, .help = N_("list domains in running state") @@ -1948,6 +1968,9 @@ cmdList(vshControl *ctl, const vshCmd *cmd) FILTER("with-snapshot", VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT); FILTER("without-snapshot", VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT); + FILTER("with-checkpoint", VIR_CONNECT_LIST_DOMAINS_HAS_CHECKPOINT); + FILTER("without-checkpoint", VIR_CONNECT_LIST_DOMAINS_NO_CHECKPOINT); + FILTER("state-running", VIR_CONNECT_LIST_DOMAINS_RUNNING); FILTER("state-paused", VIR_CONNECT_LIST_DOMAINS_PAUSED); FILTER("state-shutoff", VIR_CONNECT_LIST_DOMAINS_SHUTOFF); diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 2ad73959ec..e79dc75342 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -3633,6 +3633,10 @@ static const vshCmdOptDef opts_undefine[] = { .type = VSH_OT_BOOL, .help = N_("remove all domain snapshot metadata (vm must be inactive)") }, + {.name = "checkpoints-metadata", + .type = VSH_OT_BOOL, + .help = N_("remove all domain checkpoint metadata (vm must be inactive)") + }, {.name = "nvram", .type = VSH_OT_BOOL, .help = N_("remove nvram file, if inactive") @@ -3662,6 +3666,7 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd) /* User-requested actions. */ bool managed_save = vshCommandOptBool(cmd, "managed-save"); bool snapshots_metadata = vshCommandOptBool(cmd, "snapshots-metadata"); + bool checkpoints_metadata = vshCommandOptBool(cmd, "checkpoints-metadata"); bool wipe_storage = vshCommandOptBool(cmd, "wipe-storage"); bool remove_all_storage = vshCommandOptBool(cmd, "remove-all-storage"); bool delete_snapshots = vshCommandOptBool(cmd, "delete-snapshots"); @@ -3716,6 +3721,8 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd) flags |= VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA; snapshots_safe = true; } + if (checkpoints_metadata) + flags |= VIR_DOMAIN_UNDEFINE_CHECKPOINTS_METADATA; if (nvram) flags |= VIR_DOMAIN_UNDEFINE_NVRAM; if (keep_nvram) diff --git a/tools/virsh-util.c b/tools/virsh-util.c index aa88397d61..933d1c825d 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -228,6 +228,17 @@ virshDomainFree(virDomainPtr dom) } +void +virshDomainCheckpointFree(virDomainCheckpointPtr chk) +{ + if (!chk) + return; + + vshSaveLibvirtHelperError(); + virDomainCheckpointFree(chk); /* sc_prohibit_obj_free_apis_in_virsh */ +} + + void virshDomainSnapshotFree(virDomainSnapshotPtr snap) { diff --git a/tools/virsh.c b/tools/virsh.c index b41304a888..0de41e33b8 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -50,6 +50,7 @@ #include "virstring.h" #include "virgettext.h" +#include "virsh-checkpoint.h" #include "virsh-console.h" #include "virsh-domain.h" #include "virsh-domain-monitor.h" @@ -832,6 +833,7 @@ static const vshCmdGrp cmdGroups[] = { {VIRSH_CMD_GRP_DOM_MANAGEMENT, "domain", domManagementCmds}, {VIRSH_CMD_GRP_DOM_MONITORING, "monitor", domMonitoringCmds}, {VIRSH_CMD_GRP_HOST_AND_HV, "host", hostAndHypervisorCmds}, + {VIRSH_CMD_GRP_CHECKPOINT, "checkpoint", checkpointCmds}, {VIRSH_CMD_GRP_IFACE, "interface", ifaceCmds}, {VIRSH_CMD_GRP_NWFILTER, "filter", nwfilterCmds}, {VIRSH_CMD_GRP_NETWORK, "network", networkCmds}, diff --git a/tools/virsh.pod b/tools/virsh.pod index 40c7b638f7..f58578e3b3 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -409,6 +409,7 @@ Inject NMI to the guest. [I<--with-managed-save>] [I<--without-managed-save>] [I<--autostart>] [I<--no-autostart>] [I<--with-snapshot>] [I<--without-snapshot>] + [I<--with-checkpoint>] [I<--without-checkpoint>] [I<--state-running>] [I<--state-paused>] [I<--state-shutoff>] [I<--state-other>] @@ -514,6 +515,11 @@ this feature disabled use I<--no-autostart>. Domains that have snapshot images can be listed using flag I<--with-snapshot>, domains without a snapshot I<--without-snapshot>. +=item B<Checkpoint existence> + +Domains that have checkpoints can be listed using flag I<--with-checkpoint>, +domains without a checkpoint I<--without-checkpoint>. + =back When talking to older servers, this command is forced to use a series of API @@ -809,7 +815,8 @@ can be restarted later. If I<domain> is transient, then the metadata of any snapshots will be lost once the guest stops running, but the snapshot contents still exist, and a new domain with the same name and UUID can restore the -snapshot metadata with B<snapshot-create>. +snapshot metadata with B<snapshot-create>. Similarly, the metadata of +any checkpoints will be lost, but can be restored with B<checkpoint-create>. If I<--graceful> is specified, don't resort to extreme measures (e.g. SIGKILL) when the guest doesn't stop after a reasonable timeout; @@ -1574,7 +1581,7 @@ Convert a domain Id (or UUID) to domain name Rename a domain. This command changes current domain name to the new name specified in the second argument. -B<Note>: Domain must be inactive and without snapshots. +B<Note>: Domain must be inactive and without snapshots or checkpoints. =item B<domstate> I<domain> [I<--reason>] @@ -2815,10 +2822,11 @@ services must be shutdown in the domain. The exact behavior of a domain when it shuts down is set by the I<on_poweroff> parameter in the domain's XML definition. -If I<domain> is transient, then the metadata of any snapshots will -be lost once the guest stops running, but the snapshot contents still -exist, and a new domain with the same name and UUID can restore the -snapshot metadata with B<snapshot-create>. +If I<domain> is transient, then the metadata of any snapshots and +checkpoints will be lost once the guest stops running, but the underlying +contents still exist, and a new domain with the same name and UUID can +restore the snapshot metadata with B<snapshot-create>, and the checkpoint +metadata with B<checkpoint-create>. By default the hypervisor will try to pick a suitable shutdown method. To specify an alternative method, the I<--mode> parameter @@ -2895,7 +2903,7 @@ Output the device used for the TTY console of the domain. If the information is not available the processes will provide an exit code of 1. =item B<undefine> I<domain> [I<--managed-save>] [I<--snapshots-metadata>] -[I<--nvram>] [I<--keep-nvram>] +[I<--checkpoints-metadata>] [I<--nvram>] [I<--keep-nvram>] [ {I<--storage> B<volumes> | I<--remove-all-storage> [I<--delete-storage-volume-snapshots>]} I<--wipe-storage>] @@ -2913,6 +2921,12 @@ domain. Without the flag, attempts to undefine an inactive domain with snapshot metadata will fail. If the domain is active, this flag is ignored. +The I<--checkpoints-metadata> flag guarantees that any checkpoints (see the +B<checkpoint-list> command) are also cleaned up when undefining an inactive +domain. Without the flag, attempts to undefine an inactive domain with +checkpoint metadata will fail. If the domain is active, this flag is +ignored. + I<--nvram> and I<--keep-nvram> specify accordingly to delete or keep nvram (/domain/os/nvram/) file. If the domain has an nvram file and the flags are omitted, the undefine will fail. @@ -4890,6 +4904,163 @@ the data contents from that point in time. =back +=head1 CHECKPOINT COMMANDS + +The following commands manipulate domain checkpoints. Checkpoints serve as +a point in time to identify which portions of a guest's disks have changed +after that time, making it possible to perform incremental and differential +backups. Checkpoints are identified with a unique name. See +L<https://libvirt.org/formatcheckpoint.html> for documentation of the XML +format used to represent properties of checkpoints. + +=over 4 + +=item B<checkpoint-create> I<domain> [I<xmlfile>] { I<--redefine> +| [I<--quiesce>]} + +Create a checkpoint for domain I<domain> with the properties specified +in I<xmlfile> describing a <domaincheckpoint> top-level element. The +format of the input XML file will be validated against an internal RNG +schema (idential to using the L<virt-xml-validate(1)> tool). If +I<xmlfile> is completely omitted, then libvirt will create a +checkpoint with a name based on the current time. + +If I<--redefine> is specified, then all XML elements produced by +B<checkpoint-dumpxml> are valid; this can be used to migrate +checkpoint hierarchy from one machine to another, to recreate +hierarchy for the case of a transient domain that goes away and is +later recreated with the same name and UUID, or to make slight +alterations in the checkpoint metadata (such as host-specific aspects +of the domain XML embedded in the checkpoint). When this flag is +supplied, the I<xmlfile> argument is mandatory. + +If I<--quiesce> is specified, libvirt will try to use guest agent +to freeze and unfreeze domain's mounted file systems. However, +if domain has no guest agent, checkpoint creation will fail. + +Existence of checkpoint metadata will prevent attempts to B<undefine> +a persistent domain. However, for transient domains, checkpoint +metadata is silently lost when the domain quits running (whether +by command such as B<destroy> or by internal guest action). + +=item B<checkpoint-create-as> I<domain> [I<--print-xml>] +[I<name>] [I<description>] [I<--quiesce>] [I<--diskspec>] B<diskspec>]... + +Create a checkpoint 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<checkpoint-create> is output, rather than actually creating a +checkpoint. + +The I<--diskspec> option can be used to control which guest disks +participate in the checkpoint. This option can occur multiple times, +according to the number of <disk> elements in the domain xml. Each +<diskspec> is in the form B<disk[,checkpoint=type][,bitmap=name]>. A +literal I<--diskspec> must precede each B<diskspec> unless +all three of I<domain>, I<name>, and I<description> are also present. +For example, a diskspec of "vda,checkpoint=bitmap,bitmap=map1" +results in the following XML: + <disk name='vda' checkpoint='bitmap' bitmap='map1'/> + +If I<--quiesce> is specified, libvirt will try to use guest agent +to freeze and unfreeze domain's mounted file systems. However, +if domain has no guest agent, checkpoint creation will fail. + +=item B<checkpoint-edit> I<domain> I<checkpointname> + +Edit the XML configuration file for I<checkpointname> of a domain. + +This is equivalent to: + + virsh checkpoint-dumpxml dom name > checkpoint.xml + vi checkpoint.xml (or make changes with your other text editor) + virsh checkpoint-create dom checkpoint.xml --redefine + +except that it does some error checking, including that the edits +should not attempt to change the checkpoint name. + +The editor used can be supplied by the C<$VISUAL> or C<$EDITOR> environment +variables, and defaults to C<vi>. + +=item B<checkpoint-info> I<domain> I<checkpoint> + +Output basic information about a named <checkpoint>. + +=item B<checkpoint-list> I<domain> [{I<--parent> | I<--roots> | +[{I<--tree> | I<--name>}]}] [I<--topological>] +[[I<--from>] B<checkpoint> | [I<--descendants>]] +[I<--leaves>] [I<--no-leaves>] + +List all of the available checkpoints for the given domain, defaulting +to show columns for the checkpoint name and creation time. + +Normally, table form output is sorted by checkpoint name; using +I<--topological> instead sorts so that no child is listed before its +ancestors (although there may be more than one possible ordering with +this property). + +If I<--parent> is specified, add a column to the output table giving +the name of the parent of each checkpoint. If I<--roots> is +specified, the list will be filtered to just checkpoints that have no +parents. If I<--tree> is specified, the output will be in a tree +format, listing just checkpoint names. These three options are +mutually exclusive. If I<--name> is specified only the checkpoint name +is printed. This option is mutually exclusive with I<--tree>. + +If I<--from> is provided, filter the list to checkpoints which are +children of the given B<checkpoint>. When used in isolation or with +I<--parent>, the list is limited to direct children unless +I<--descendants> is also present. When used with I<--tree>, the use +of I<--descendants> is implied. This option is not compatible with +I<--roots>. Note that the starting point of I<--from> +is not included in the list unless the I<--tree> option is also +present. + +If I<--leaves> is specified, the list will be filtered to just +checkpoints that have no children. Likewise, if I<--no-leaves> is +specified, the list will be filtered to just checkpoints with +children. (Note that omitting both options does no filtering, while +providing both options will either produce the same list or error out +depending on whether the server recognizes the flags). Filtering +options are not compatible with I<--tree>. + +=item B<checkpoint-dumpxml> I<domain> I<checkpoint> +[I<--security-info>] [I<--no-domain>] [I<--size>] + +Output the checkpoint XML for the domain's checkpoint named +I<checkpoint>. Using +I<--security-info> will also include security sensitive information. +Using I<--size> will add XML indicating the current size in bytes of +guest data that has changed since the checkpoint was created (although +remember that guest activity between a size check and actually +creating a backup can result in the backup needing slightly more +space). Using I<--no-domain> will omit the <domain> element from the +output for a more compact view. + +=item B<checkpoint-parent> I<domain> I<checkpoint> + +Output the name of the parent checkpoint, if any, for the given +I<checkpoint>. + +=item B<checkpoint-delete> I<domain> I<checkpoint> +[I<--metadata>] [{I<--children> | I<--children-only>}] + +Delete the checkpoint for the domain named I<checkpoint>. The +record of which portions of +the disk changed since the checkpoint are merged into the parent +checkpoint (if any). If I<--children> is passed, then delete this +checkpoint and any children of this checkpoint. If I<--children-only> +is passed, then delete any children of this checkpoint, but leave this +checkpoint intact. These two flags are mutually exclusive. + +If I<--metadata> is specified, then only delete the checkpoint +metadata maintained by libvirt, while leaving the checkpoint contents +intact for access by external tools; otherwise deleting a checkpoint +also removes the ability to perform an incremental backup from that +point in time. + +=back + =head1 NWFILTER COMMANDS The following commands manipulate network filters. Network filters allow -- 2.20.1

On Wed, Jul 24, 2019 at 12:56:02AM -0500, Eric Blake wrote:
Introduce a bunch of new virsh commands for managing checkpoints in isolation. More commands are needed for performing incremental backups, but these commands were easy to implement by modeling heavily after virsh-snapshot.c. There is no need for checkpoint-revert or checkpoint-current since those snapshot APIs have no checkpoint counterpart. Similarly, it is not necessary to change which checkpoint is current when redefining from XML, since checkpoints expose whether they are current in the public XML (rather than the way snapshots did it behind the scenese). checkpoint-list is a bit simpler, in part because we don't have to cater to back-compat to older API. checkpoint-info is a bit trickier, because it requires parsing XML (maybe we'll want virDomainCheckpointIsCurrent() as an API after all).
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
diff --git a/tools/virsh-checkpoint.c b/tools/virsh-checkpoint.c new file mode 100644 index 0000000000..f6401f2cf0 --- /dev/null +++ b/tools/virsh-checkpoint.c @@ -0,0 +1,1153 @@ +/* + * virsh-checkpoint.c: Commands to manage domain checkpoints + * + * Copyright (C) 2005-2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + * + * Daniel Veillard <veillard@redhat.com> + * Karel Zak <kzak@redhat.com> + * Daniel P. Berrange <berrange@redhat.com>
We purged all authors from the source, only copyright & license lines need to be preserved when copying files. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

A lot of this work heavily copies from the existing snapshot APIs. The test driver doesn't really have to do anything more than just expose an interface into libvirt metadata, making it possible to test saving and restoring XML, and tracking relations between multiple checkpoints. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/test/test_driver.c | 369 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 369 insertions(+) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index d96dd638e7..abe51f252a 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -39,6 +39,7 @@ #include "viralloc.h" #include "virnetworkobj.h" #include "interface_conf.h" +#include "checkpoint_conf.h" #include "domain_conf.h" #include "domain_event.h" #include "network_event.h" @@ -63,6 +64,7 @@ #include "virdomainobjlist.h" #include "virinterfaceobj.h" #include "virhostcpu.h" +#include "virdomaincheckpointobjlist.h" #include "virdomainsnapshotobjlist.h" #include "virkeycode.h" @@ -7692,7 +7694,366 @@ testDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, return ret; } +/* + * Checkpoint APIs + */ +static int +testDomainCheckpointDiscardAll(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *data) +{ + virDomainMomentObjPtr chk = payload; + testMomentRemoveDataPtr curr = data; + + curr->current |= virDomainCheckpointObjListRemove(curr->vm->checkpoints, + chk); + return 0; +} + +static virDomainObjPtr +testDomObjFromCheckpoint(virDomainCheckpointPtr checkpoint) +{ + return testDomObjFromDomain(checkpoint->domain); +} + +static virDomainMomentObjPtr +testCheckpointObjFromName(virDomainObjPtr vm, + const char *name) +{ + virDomainMomentObjPtr chk = NULL; + + chk = virDomainCheckpointFindByName(vm->checkpoints, name); + if (!chk) + virReportError(VIR_ERR_NO_DOMAIN_CHECKPOINT, + _("no domain checkpoint with matching name '%s'"), + name); + + return chk; +} + +static virDomainMomentObjPtr +testCheckpointObjFromCheckpoint(virDomainObjPtr vm, + virDomainCheckpointPtr checkpoint) +{ + return testCheckpointObjFromName(vm, checkpoint->name); +} + +static virDomainCheckpointPtr +testDomainCheckpointCreateXML(virDomainPtr domain, + const char *xmlDesc, + unsigned int flags) +{ + testDriverPtr privconn = domain->conn->privateData; + virDomainObjPtr vm = NULL; + char *xml = NULL; + virDomainMomentObjPtr chk = NULL; + virDomainCheckpointPtr checkpoint = NULL; + virDomainMomentObjPtr current = NULL; + bool update_current = true; + bool redefine = flags & VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE; + unsigned int parse_flags = 0; + VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE | + VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE, NULL); + + if (redefine) { + parse_flags |= VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + update_current = false; + } + + if (!(vm = testDomObjFromDomain(domain))) + goto cleanup; + + if (!virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create checkpoint for inactive domain")); + goto cleanup; + } + + if (!(def = virDomainCheckpointDefParseString(xmlDesc, privconn->caps, + privconn->xmlopt, + parse_flags))) + goto cleanup; + + if (redefine) { + if (virDomainCheckpointRedefinePrep(domain, vm, &def, &chk, + privconn->xmlopt, + &update_current) < 0) + goto cleanup; + } else { + if (!(def->parent.dom = virDomainDefCopy(vm->def, + privconn->caps, + privconn->xmlopt, + NULL, + true))) + goto cleanup; + + if (virDomainCheckpointAlignDisks(def) < 0) + goto cleanup; + } + + if (!chk) { + if (!(chk = virDomainCheckpointAssignDef(vm->checkpoints, def))) + goto cleanup; + + def = NULL; + } + + current = virDomainCheckpointGetCurrent(vm->checkpoints); + if (current) { + if (!redefine && + VIR_STRDUP(chk->def->parent_name, current->def->name) < 0) + goto cleanup; + if (update_current) + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + } + + /* actually do the checkpoint - except the test driver has nothing + * to actually do here */ + + /* If we fail after this point, there's not a whole lot we can do; + * we've successfully created the checkpoint, so we have to go + * forward the best we can. + */ + checkpoint = virGetDomainCheckpoint(domain, chk->def->name); + + cleanup: + if (checkpoint) { + if (update_current) + virDomainCheckpointSetCurrent(vm->checkpoints, chk); + virDomainCheckpointLinkParent(vm->checkpoints, chk); + } else if (chk) { + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + } + + virDomainObjEndAPI(&vm); + VIR_FREE(xml); + return checkpoint; +} + + +static int +testDomainListAllCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **chks, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + int n = -1; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_LIST_ROOTS | + VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL | + VIR_DOMAIN_CHECKPOINT_FILTERS_ALL, -1); + + if (!(vm = testDomObjFromDomain(domain))) + return -1; + + n = virDomainListCheckpoints(vm->checkpoints, NULL, domain, chks, flags); + + virDomainObjEndAPI(&vm); + return n; +} + + +static int +testDomainCheckpointListAllChildren(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **chks, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + virDomainMomentObjPtr chk = NULL; + int n = -1; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS | + VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL | + VIR_DOMAIN_CHECKPOINT_FILTERS_ALL, -1); + + if (!(vm = testDomObjFromCheckpoint(checkpoint))) + return -1; + + if (!(chk = testCheckpointObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + n = virDomainListCheckpoints(vm->checkpoints, chk, checkpoint->domain, + chks, flags); + + cleanup: + virDomainObjEndAPI(&vm); + return n; +} + + +static virDomainCheckpointPtr +testDomainCheckpointLookupByName(virDomainPtr domain, + const char *name, + unsigned int flags) +{ + virDomainObjPtr vm; + virDomainMomentObjPtr chk = NULL; + virDomainCheckpointPtr checkpoint = NULL; + + virCheckFlags(0, NULL); + + if (!(vm = testDomObjFromDomain(domain))) + return NULL; + + if (!(chk = testCheckpointObjFromName(vm, name))) + goto cleanup; + + checkpoint = virGetDomainCheckpoint(domain, chk->def->name); + + cleanup: + virDomainObjEndAPI(&vm); + return checkpoint; +} + + +static virDomainCheckpointPtr +testDomainCheckpointGetParent(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virDomainObjPtr vm; + virDomainMomentObjPtr chk = NULL; + virDomainCheckpointPtr parent = NULL; + + virCheckFlags(0, NULL); + + if (!(vm = testDomObjFromCheckpoint(checkpoint))) + return NULL; + + if (!(chk = testCheckpointObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + if (!chk->def->parent_name) { + virReportError(VIR_ERR_NO_DOMAIN_CHECKPOINT, + _("checkpoint '%s' does not have a parent"), + chk->def->name); + goto cleanup; + } + + parent = virGetDomainCheckpoint(checkpoint->domain, chk->def->parent_name); + + cleanup: + virDomainObjEndAPI(&vm); + return parent; +} + + +static char * +testDomainCheckpointGetXMLDesc(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + testDriverPtr privconn = checkpoint->domain->conn->privateData; + virDomainObjPtr vm = NULL; + char *xml = NULL; + virDomainMomentObjPtr chk = NULL; + size_t i; + virDomainCheckpointDefPtr chkdef; + unsigned int format_flags; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_XML_SECURE | + VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN | + VIR_DOMAIN_CHECKPOINT_XML_SIZE, NULL); + + if (!(vm = testDomObjFromCheckpoint(checkpoint))) + return NULL; + + if (!(chk = testCheckpointObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + chkdef = virDomainCheckpointObjGetDef(chk); + + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SIZE) { + if (virDomainObjCheckActive(vm) < 0) + goto cleanup; + + for (i = 0; i < chkdef->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &chkdef->disks[i]; + + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + disk->size = 1024; /* Any number will do... */ + } + } + + format_flags = virDomainCheckpointFormatConvertXMLFlags(flags); + xml = virDomainCheckpointDefFormat(chkdef, privconn->caps, + privconn->xmlopt, format_flags); + + cleanup: + virDomainObjEndAPI(&vm); + return xml; +} + + +static int +testDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + int ret = -1; + virDomainMomentObjPtr chk = NULL; + virDomainMomentObjPtr parentchk = NULL; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN | + VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY | + VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY, -1); + + if (!(vm = testDomObjFromCheckpoint(checkpoint))) + return -1; + + if (!(chk = testCheckpointObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + if (flags & (VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN | + VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY)) { + testMomentRemoveData rem; + + rem.vm = vm; + rem.current = false; + virDomainMomentForEachDescendant(chk, testDomainCheckpointDiscardAll, + &rem); + if (rem.current) + virDomainCheckpointSetCurrent(vm->checkpoints, chk); + } else if (chk->nchildren) { + testMomentReparentData rep; + + rep.parent = chk->parent; + rep.vm = vm; + rep.err = 0; + virDomainMomentForEachChild(chk, testDomainMomentReparentChildren, + &rep); + if (rep.err < 0) + goto cleanup; + virDomainMomentMoveChildren(chk, chk->parent); + } + + if (flags & VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY) { + virDomainMomentDropChildren(chk); + } else { + virDomainMomentDropParent(chk); + if (chk == virDomainCheckpointGetCurrent(vm->checkpoints)) { + if (chk->def->parent_name) { + parentchk = virDomainCheckpointFindByName(vm->checkpoints, + chk->def->parent_name); + if (!parentchk) + VIR_WARN("missing parent checkpoint matching name '%s'", + chk->def->parent_name); + } + virDomainCheckpointSetCurrent(vm->checkpoints, parentchk); + } + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + } + + ret = 0; + cleanup: + virDomainObjEndAPI(&vm); + return ret; +} + +/* + * Test driver + */ static virHypervisorDriver testHypervisorDriver = { .name = "Test", .connectOpen = testConnectOpen, /* 0.1.1 */ @@ -7817,6 +8178,14 @@ static virHypervisorDriver testHypervisorDriver = { .domainSnapshotDelete = testDomainSnapshotDelete, /* 1.1.4 */ .connectBaselineCPU = testConnectBaselineCPU, /* 1.2.0 */ + .domainCheckpointCreateXML = testDomainCheckpointCreateXML, /* 5.6.0 */ + .domainCheckpointGetXMLDesc = testDomainCheckpointGetXMLDesc, /* 5.6.0 */ + + .domainListAllCheckpoints = testDomainListAllCheckpoints, /* 5.6.0 */ + .domainCheckpointListAllChildren = testDomainCheckpointListAllChildren, /* 5.6.0 */ + .domainCheckpointLookupByName = testDomainCheckpointLookupByName, /* 5.6.0 */ + .domainCheckpointGetParent = testDomainCheckpointGetParent, /* 5.6.0 */ + .domainCheckpointDelete = testDomainCheckpointDelete, /* 5.6.0 */ }; static virNetworkDriver testNetworkDriver = { -- 2.20.1

On Wed, Jul 24, 2019 at 12:56:03AM -0500, Eric Blake wrote:
A lot of this work heavily copies from the existing snapshot APIs. The test driver doesn't really have to do anything more than just expose an interface into libvirt metadata, making it possible to test saving and restoring XML, and tracking relations between multiple checkpoints.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/test/test_driver.c | 369 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 369 insertions(+)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Similar to virsh-snapshots (which also gets a minor tweak, to prove we properly lock out one API from the other until later integration efforts figure out the best way to proceed). Provides decent coverage of the checkpoint API, the test driver implementation, and the virsh access to the API. Signed-off-by: Eric Blake <eblake@redhat.com> --- tests/Makefile.am | 1 + tests/virsh-checkpoint | 164 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100755 tests/virsh-checkpoint diff --git a/tests/Makefile.am b/tests/Makefile.am index 544328bb18..1c92e3ca6f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -413,6 +413,7 @@ libvirtd_test_scripts = \ virsh-schedinfo \ virsh-self-test \ virt-admin-self-test \ + virsh-checkpoint \ virsh-snapshot \ virsh-start \ virsh-undefine \ diff --git a/tests/virsh-checkpoint b/tests/virsh-checkpoint new file mode 100755 index 0000000000..6fe5aa4c05 --- /dev/null +++ b/tests/virsh-checkpoint @@ -0,0 +1,164 @@ +#!/bin/sh +# simple testing of checkpoint APIs on test driver + +# Copyright (C) 2019 Red Hat, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# <http://www.gnu.org/licenses/>. + +. "$(dirname $0)/test-lib.sh" + +if test "$VERBOSE" = yes; then + set -x + $abs_top_builddir/tools/virsh --version +fi + +fail=0 + +mock_xdg_ || framework_failure + +# The test driver loses states between restarts, so we perform a script +# with some convenient markers for later post-processing of output. +$abs_top_builddir/tools/virsh --connect test:///default >out 2>err ' + # Create a series of checkpoints, with names that intentionally sort + # differently by topology than by name. For now, it is not possible + # to create fanout without hacking through redefines. + checkpoint-create-as test c1 + checkpoint-create-as test c1 + checkpoint-create-as test c3 + checkpoint-create-as test c2 + # Checking tree view (siblings sorted alphabetically) + checkpoint-list test --tree + # Demonstrate list filtering + checkpoint-list test --roots + checkpoint-list test --leaves + checkpoint-list test --parent --no-leaves + checkpoint-list test --from c3 + checkpoint-list test --from c1 --descendants --name + # Now the tree is linear, so we have an unambiguous topological order + checkpoint-list test --name + checkpoint-list test --name --topological + # Capture some XML for later redefine + checkpoint-delete test c1 + echo "<!--MarkerA-->" + checkpoint-dumpxml test c3 + echo "<!--MarkerB-->" + checkpoint-dumpxml test c2 + echo "<!--MarkerC-->" + # Deleting current checkpoint moves current up to remaining parent + checkpoint-delete test --children-only c3 + checkpoint-list test --leaves --name + checkpoint-delete test --children c3 + checkpoint-list test --leaves --name + # All done +' || fail=1 + +# First part is expected output, --tree results in trailing spaces, +# and checkpoint-list produces timestamps +sed 's/ *$//; s/[0-9-]\{10\} [0-9:.]* .[0-9]\{4\}/TIMESTAMP/; + /MarkerA/,/MarkerC/d' < out > out.cooked || fail=1 +# Second part holds domain checkpoint XMLs +sed -n '/MarkerA/,/MarkerB/p' < out > c3.xml || fail=1 +sed -n '/MarkerB/,/MarkerC/p' < out > c2.xml || fail=1 + +cat <<\EOF > exp || fail=1 +Domain checkpoint c1 created + +Domain checkpoint c3 created +Domain checkpoint c2 created +c1 + | + +- c3 + | + +- c2 + + + Name Creation Time +----------------------------------- + c1 TIMESTAMP + + Name Creation Time +----------------------------------- + c2 TIMESTAMP + + Name Creation Time Parent +-------------------------------------------- + c1 TIMESTAMP + c3 TIMESTAMP c1 + + Name Creation Time +----------------------------------- + c2 TIMESTAMP + +c2 +c3 + +c1 +c2 +c3 + +c1 +c3 +c2 + +Domain checkpoint c1 deleted + +Domain checkpoint c3 children deleted + +c3 + +Domain checkpoint c3 deleted + + +EOF +compare exp out.cooked || fail=1 + +cat <<EOF > exp || fail=1 +error: operation failed: domain moment c1 already exists +EOF +compare exp err || fail=1 + +# Restore state with redefine +$abs_top_builddir/tools/virsh -c test:///default >out 2>err <<EOF || fail=1 + # Redefine must be in topological order; this will fail + checkpoint-create test --redefine c2.xml + echo --err marker + # This is the right order + checkpoint-create test --redefine c3.xml + checkpoint-create test --redefine c2.xml + checkpoint-list test --leaves + checkpoint-info test c2 +EOF + +cat <<\EOF > exp || fail=1 + + +Domain checkpoint s3 created from 's3.xml' +Domain checkpoint s2 created from 's2.xml' +Name: s2 +Domain: test +Current: yes +Parent: s3 +Children: 0 +Descendants: 0 + +EOF + +cat <<EOF > exp || fail=1 +error: invalid argument: parent c3 for moment c2 not found +error: marker +EOF +compare exp err || fail=1 + +(exit $fail); exit $fail -- 2.20.1

On Wed, Jul 24, 2019 at 12:56:04AM -0500, Eric Blake wrote:
Similar to virsh-snapshots (which also gets a minor tweak, to prove we properly lock out one API from the other until later integration efforts figure out the best way to proceed). Provides decent coverage of the checkpoint API, the test driver implementation, and the virsh access to the API.
Signed-off-by: Eric Blake <eblake@redhat.com> --- tests/Makefile.am | 1 + tests/virsh-checkpoint | 164 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100755 tests/virsh-checkpoint
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

This is similar to the existing directory for snapshots; the domain will save one xml file per checkpoint, for reloading on the next libvirtd restart. Fortunately, since checkpoints mandate RNG validation, we are assured that the checkpoint name will be usable as a file name (no abuse of '../escape' as a checkpoint name, for example). Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_conf.h | 2 ++ src/qemu/qemu_conf.c | 5 +++++ src/qemu/qemu_driver.c | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index 2229b76e89..962df4f52c 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -28,6 +28,7 @@ #include "capabilities.h" #include "network_conf.h" #include "domain_conf.h" +#include "checkpoint_conf.h" #include "snapshot_conf.h" #include "domain_event.h" #include "virthread.h" @@ -101,6 +102,7 @@ struct _virQEMUDriverConfig { char *cacheDir; char *saveDir; char *snapshotDir; + char *checkpointDir; char *channelTargetDir; char *nvramDir; char *swtpmStorageDir; diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index e0195dac29..7a02ea5b07 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -160,6 +160,8 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged) goto error; if (virAsprintf(&cfg->snapshotDir, "%s/snapshot", cfg->libDir) < 0) goto error; + if (virAsprintf(&cfg->checkpointDir, "%s/checkpoint", cfg->libDir) < 0) + goto error; if (virAsprintf(&cfg->autoDumpPath, "%s/dump", cfg->libDir) < 0) goto error; if (virAsprintf(&cfg->channelTargetDir, @@ -223,6 +225,8 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged) goto error; if (virAsprintf(&cfg->snapshotDir, "%s/qemu/snapshot", cfg->configBaseDir) < 0) goto error; + if (virAsprintf(&cfg->checkpointDir, "%s/qemu/checkpoint", cfg->configBaseDir) < 0) + goto error; if (virAsprintf(&cfg->autoDumpPath, "%s/qemu/dump", cfg->configBaseDir) < 0) goto error; if (virAsprintf(&cfg->channelTargetDir, @@ -335,6 +339,7 @@ static void virQEMUDriverConfigDispose(void *obj) VIR_FREE(cfg->cacheDir); VIR_FREE(cfg->saveDir); VIR_FREE(cfg->snapshotDir); + VIR_FREE(cfg->checkpointDir); VIR_FREE(cfg->channelTargetDir); VIR_FREE(cfg->nvramDir); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 065e0a1bd8..fae2bd3b08 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -655,6 +655,11 @@ qemuStateInitialize(bool privileged, cfg->snapshotDir); goto error; } + if (virFileMakePath(cfg->checkpointDir) < 0) { + virReportSystemError(errno, _("Failed to create checkpoint dir %s"), + cfg->checkpointDir); + goto error; + } if (virFileMakePath(cfg->autoDumpPath) < 0) { virReportSystemError(errno, _("Failed to create dump dir %s"), cfg->autoDumpPath); @@ -766,6 +771,13 @@ qemuStateInitialize(bool privileged, (int)cfg->group); goto error; } + if (chown(cfg->checkpointDir, cfg->user, cfg->group) < 0) { + virReportSystemError(errno, + _("unable to set ownership of '%s' to %d:%d"), + cfg->checkpointDir, (int)cfg->user, + (int)cfg->group); + goto error; + } if (chown(cfg->autoDumpPath, cfg->user, cfg->group) < 0) { virReportSystemError(errno, _("unable to set ownership of '%s' to %d:%d"), -- 2.20.1

On Wed, Jul 24, 2019 at 12:56:05AM -0500, Eric Blake wrote:
This is similar to the existing directory for snapshots; the domain will save one xml file per checkpoint, for reloading on the next libvirtd restart. Fortunately, since checkpoints mandate RNG validation, we are assured that the checkpoint name will be usable as a file name (no abuse of '../escape' as a checkpoint name, for example).
but see my question earlier. I would rather have an explicit check in the code forbidding '/' even if validation is also checking this, but that's not dealt with in this patch anyway. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Jul 24, 2019 at 12:56:05AM -0500, Eric Blake wrote:
This is similar to the existing directory for snapshots; the domain will save one xml file per checkpoint, for reloading on the next libvirtd restart. Fortunately, since checkpoints mandate RNG validation, we are assured that the checkpoint name will be usable as a file name (no abuse of '../escape' as a checkpoint name, for example).
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_conf.h | 2 ++ src/qemu/qemu_conf.c | 5 +++++ src/qemu/qemu_driver.c | 12 ++++++++++++ 3 files changed, 19 insertions(+)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

A lot of this work heavily copies from the existing snapshot APIs. The interaction with qemu during create/delete still needs to be implemented, but this takes care of all the libvirt metadata (saving and restoring XML, and tracking the relations between multiple checkpoints). Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.h | 15 + src/qemu/qemu_domain.c | 131 +++++++++ src/qemu/qemu_driver.c | 606 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 752 insertions(+) diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index b0c465cb22..eeb07180b7 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -755,6 +755,21 @@ int qemuDomainMomentDiscardAll(void *payload, int qemuDomainSnapshotDiscardAllMetadata(virQEMUDriverPtr driver, virDomainObjPtr vm); +int qemuDomainCheckpointWriteMetadata(virDomainObjPtr vm, + virDomainMomentObjPtr checkpoint, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + const char *checkpointDir); + +int qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainMomentObjPtr chk, + bool update_current, + bool metadata_only); + +int qemuDomainCheckpointDiscardAllMetadata(virQEMUDriverPtr driver, + virDomainObjPtr vm); + void qemuDomainRemoveInactive(virQEMUDriverPtr driver, virDomainObjPtr vm); diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index e7f28aa2b8..44ac7eb73e 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -56,6 +56,7 @@ #include "logging/log_manager.h" #include "locking/domain_lock.h" #include "virdomainsnapshotobjlist.h" +#include "virdomaincheckpointobjlist.h" #ifdef MAJOR_IN_MKDEV # include <sys/mkdev.h> @@ -9019,6 +9020,37 @@ qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm, return ret; } +int +qemuDomainCheckpointWriteMetadata(virDomainObjPtr vm, + virDomainMomentObjPtr checkpoint, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + const char *checkpointDir) +{ + unsigned int flags = VIR_DOMAIN_CHECKPOINT_FORMAT_SECURE; + virDomainCheckpointDefPtr def = virDomainCheckpointObjGetDef(checkpoint); + VIR_AUTOFREE(char *) newxml = NULL; + VIR_AUTOFREE(char *) chkDir = NULL; + VIR_AUTOFREE(char *) chkFile = NULL; + + newxml = virDomainCheckpointDefFormat(def, caps, xmlopt, flags); + if (newxml == NULL) + return -1; + + if (virAsprintf(&chkDir, "%s/%s", checkpointDir, vm->def->name) < 0) + return -1; + if (virFileMakePath(chkDir) < 0) { + virReportSystemError(errno, _("cannot create checkpoint directory '%s'"), + chkDir); + return -1; + } + + if (virAsprintf(&chkFile, "%s/%s.xml", chkDir, def->parent.name) < 0) + return -1; + + return virXMLSaveFile(chkFile, NULL, "checkpoint-edit", newxml); +} + /* The domain is expected to be locked and inactive. Return -1 on normal * failure, 1 if we skipped a disk due to try_all. */ static int @@ -9215,12 +9247,100 @@ qemuDomainSnapshotDiscardAllMetadata(virQEMUDriverPtr driver, } +/* Discard one checkpoint (or its metadata), without reparenting any children. */ +int +qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainMomentObjPtr chk, + bool update_parent, + bool metadata_only) +{ + char *chkFile = NULL; + int ret = -1; + virDomainMomentObjPtr parentchk = NULL; + virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver); + + if (!metadata_only) { + if (!virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot remove checkpoint from inactive domain")); + goto cleanup; + } else { + /* TODO: Implement QMP sequence to merge bitmaps */ + // qemuDomainObjPrivatePtr priv; + // priv = vm->privateData; + // qemuDomainObjEnterMonitor(driver, vm); + // /* we continue on even in the face of error */ + // qemuMonitorDeleteCheckpoint(priv->mon, chk->def->name); + // ignore_value(qemuDomainObjExitMonitor(driver, vm)); + } + } + + if (virAsprintf(&chkFile, "%s/%s/%s.xml", cfg->checkpointDir, + vm->def->name, chk->def->name) < 0) + goto cleanup; + + if (chk == virDomainCheckpointGetCurrent(vm->checkpoints)) { + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + if (update_parent && chk->def->parent_name) { + parentchk = virDomainCheckpointFindByName(vm->checkpoints, + chk->def->parent_name); + if (!parentchk) { + VIR_WARN("missing parent checkpoint matching name '%s'", + chk->def->parent_name); + } else { + virDomainCheckpointSetCurrent(vm->checkpoints, parentchk); + if (qemuDomainCheckpointWriteMetadata(vm, parentchk, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) { + VIR_WARN("failed to set parent checkpoint '%s' as current", + chk->def->parent_name); + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + } + } + } + } + + if (unlink(chkFile) < 0) + VIR_WARN("Failed to unlink %s", chkFile); + if (update_parent) + virDomainMomentDropParent(chk); + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + + ret = 0; + + cleanup: + VIR_FREE(chkFile); + virObjectUnref(cfg); + return ret; +} + +int +qemuDomainCheckpointDiscardAllMetadata(virQEMUDriverPtr driver, + virDomainObjPtr vm) +{ + virQEMUMomentRemove rem = { + .driver = driver, + .vm = vm, + .metadata_only = true, + .momentDiscard = qemuDomainCheckpointDiscard, + }; + + virDomainCheckpointForEach(vm->checkpoints, qemuDomainMomentDiscardAll, + &rem); + virDomainCheckpointObjListRemoveAll(vm->checkpoints); + + return rem.err; +} + + static void qemuDomainRemoveInactiveCommon(virQEMUDriverPtr driver, virDomainObjPtr vm) { virQEMUDriverConfigPtr cfg; VIR_AUTOFREE(char *) snapDir = NULL; + VIR_AUTOFREE(char *) chkDir = NULL; cfg = virQEMUDriverGetConfig(driver); @@ -9235,6 +9355,17 @@ qemuDomainRemoveInactiveCommon(virQEMUDriverPtr driver, } else if (rmdir(snapDir) < 0 && errno != ENOENT) { VIR_WARN("unable to remove snapshot directory %s", snapDir); } + /* Remove any checkpoint metadata prior to removing the domain */ + if (qemuDomainCheckpointDiscardAllMetadata(driver, vm) < 0) { + VIR_WARN("unable to remove all checkpoints for domain %s", + vm->def->name); + } else if (virAsprintf(&chkDir, "%s/%s", cfg->checkpointDir, + vm->def->name) < 0) { + VIR_WARN("unable to remove checkpoint directory %s/%s", + cfg->checkpointDir, vm->def->name); + } else if (rmdir(chkDir) < 0 && errno != ENOENT) { + VIR_WARN("unable to remove checkpoint directory %s", chkDir); + } qemuExtDevicesCleanupHost(driver, vm->def); virObjectUnref(cfg); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index fae2bd3b08..a75a16492b 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -103,6 +103,7 @@ #include "virqemu.h" #include "virdomainsnapshotobjlist.h" #include "virenum.h" +#include "virdomaincheckpointobjlist.h" #define VIR_FROM_THIS VIR_FROM_QEMU @@ -222,6 +223,40 @@ qemuSnapObjFromSnapshot(virDomainObjPtr vm, return qemuSnapObjFromName(vm, snapshot->name); } +/* Looks up the domain object from checkpoint and unlocks the + * driver. The returned domain object is locked and ref'd and the + * caller must call virDomainObjEndAPI() on it. */ +static virDomainObjPtr +qemuDomObjFromCheckpoint(virDomainCheckpointPtr checkpoint) +{ + return qemuDomObjFromDomain(checkpoint->domain); +} + + +/* Looks up checkpoint object from VM and name */ +static virDomainMomentObjPtr +qemuCheckpointObjFromName(virDomainObjPtr vm, + const char *name) +{ + virDomainMomentObjPtr chk = NULL; + chk = virDomainCheckpointFindByName(vm->checkpoints, name); + if (!chk) + virReportError(VIR_ERR_NO_DOMAIN_CHECKPOINT, + _("no domain checkpoint with matching name '%s'"), + name); + + return chk; +} + + +/* Looks up checkpoint object from VM and checkpointPtr */ +static virDomainMomentObjPtr +qemuCheckpointObjFromCheckpoint(virDomainObjPtr vm, + virDomainCheckpointPtr checkpoint) +{ + return qemuCheckpointObjFromName(vm, checkpoint->name); +} + static int qemuAutostartDomain(virDomainObjPtr vm, void *opaque) @@ -525,6 +560,113 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm, } +static int +qemuDomainCheckpointLoad(virDomainObjPtr vm, + void *data) +{ + char *baseDir = (char *)data; + char *chkDir = NULL; + DIR *dir = NULL; + struct dirent *entry; + char *xmlStr; + char *fullpath; + virDomainCheckpointDefPtr def = NULL; + virDomainMomentObjPtr chk = NULL; + virDomainMomentObjPtr current = NULL; + unsigned int flags = VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + int ret = -1; + virCapsPtr caps = NULL; + int direrr; + + virObjectLock(vm); + if (virAsprintf(&chkDir, "%s/%s", baseDir, vm->def->name) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to allocate memory for " + "checkpoint directory for domain %s"), + vm->def->name); + goto cleanup; + } + + if (!(caps = virQEMUDriverGetCapabilities(qemu_driver, false))) + goto cleanup; + + VIR_INFO("Scanning for checkpoints for domain %s in %s", vm->def->name, + chkDir); + + if (virDirOpenIfExists(&dir, chkDir) <= 0) + goto cleanup; + + while ((direrr = virDirRead(dir, &entry, NULL)) > 0) { + /* NB: ignoring errors, so one malformed config doesn't + kill the whole process */ + VIR_INFO("Loading checkpoint file '%s'", entry->d_name); + + if (virAsprintf(&fullpath, "%s/%s", chkDir, entry->d_name) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to allocate memory for path")); + continue; + } + + if (virFileReadAll(fullpath, 1024*1024*1, &xmlStr) < 0) { + /* Nothing we can do here, skip this one */ + virReportSystemError(errno, + _("Failed to read checkpoint file %s"), + fullpath); + VIR_FREE(fullpath); + continue; + } + + def = virDomainCheckpointDefParseString(xmlStr, caps, + qemu_driver->xmlopt, + flags); + if (!def || virDomainCheckpointAlignDisks(def) < 0) { + /* Nothing we can do here, skip this one */ + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to parse checkpoint XML from file '%s'"), + fullpath); + VIR_FREE(fullpath); + VIR_FREE(xmlStr); + virObjectUnref(def); + continue; + } + + chk = virDomainCheckpointAssignDef(vm->checkpoints, def); + if (chk == NULL) + virObjectUnref(def); + + VIR_FREE(fullpath); + VIR_FREE(xmlStr); + } + if (direrr < 0) + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to fully read directory %s"), + chkDir); + + if (virDomainCheckpointUpdateRelations(vm->checkpoints, ¤t) < 0) + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Checkpoints have inconsistent relations for domain %s"), + vm->def->name); + virDomainCheckpointSetCurrent(vm->checkpoints, current); + + /* Note that it is not practical to automatically construct + * checkpoints based solely on qcow2 bitmaps, since qemu does not + * track parent relations which we find important in our metadata. + * Perhaps we could double-check that our just-loaded checkpoint + * metadata is consistent with existing qcow2 bitmaps, but a user + * that changes things behind our backs deserves what happens. */ + + virResetLastError(); + + ret = 0; + cleanup: + VIR_DIR_CLOSE(dir); + VIR_FREE(chkDir); + virObjectUnref(caps); + virObjectUnlock(vm); + return ret; +} + + static int qemuDomainNetsRestart(virDomainObjPtr vm, void *data ATTRIBUTE_UNUSED) @@ -911,6 +1053,10 @@ qemuStateInitialize(bool privileged, qemuDomainSnapshotLoad, cfg->snapshotDir); + virDomainObjListForEach(qemu_driver->domains, + qemuDomainCheckpointLoad, + cfg->checkpointDir); + virDomainObjListForEach(qemu_driver->domains, qemuDomainManagedSaveLoad, qemu_driver); @@ -7711,10 +7857,12 @@ qemuDomainUndefineFlags(virDomainPtr dom, char *name = NULL; int ret = -1; int nsnapshots; + int ncheckpoints; virQEMUDriverConfigPtr cfg = NULL; virCheckFlags(VIR_DOMAIN_UNDEFINE_MANAGED_SAVE | VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA | + VIR_DOMAIN_UNDEFINE_CHECKPOINTS_METADATA | VIR_DOMAIN_UNDEFINE_NVRAM | VIR_DOMAIN_UNDEFINE_KEEP_NVRAM, -1); @@ -7754,6 +7902,19 @@ qemuDomainUndefineFlags(virDomainPtr dom, if (qemuDomainSnapshotDiscardAllMetadata(driver, vm) < 0) goto endjob; } + if (!virDomainObjIsActive(vm) && + (ncheckpoints = virDomainListCheckpoints(vm->checkpoints, NULL, dom, + NULL, flags)) > 0) { + if (!(flags & VIR_DOMAIN_UNDEFINE_CHECKPOINTS_METADATA)) { + virReportError(VIR_ERR_OPERATION_INVALID, + _("cannot delete inactive domain with %d " + "checkpoints"), + ncheckpoints); + goto endjob; + } + if (qemuDomainCheckpointDiscardAllMetadata(driver, vm) < 0) + goto endjob; + } name = qemuDomainManagedSavePath(driver, vm); if (name == NULL) @@ -16786,6 +16947,443 @@ qemuDomainSnapshotDelete(virDomainSnapshotPtr snapshot, return ret; } + +/* Called inside job lock */ +static int +qemuDomainCheckpointPrepare(virQEMUDriverPtr driver, virCapsPtr caps, + virDomainObjPtr vm, + virDomainCheckpointDefPtr def) +{ + int ret = -1; + size_t i; + char *xml = NULL; + qemuDomainObjPrivatePtr priv = vm->privateData; + + /* Easiest way to clone inactive portion of vm->def is via + * conversion in and back out of xml. */ + if (!(xml = qemuDomainDefFormatLive(driver, vm->def, priv->origCPU, + true, true)) || + !(def->parent.dom = virDomainDefParseString(xml, caps, driver->xmlopt, NULL, + VIR_DOMAIN_DEF_PARSE_INACTIVE | + VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE))) + goto cleanup; + + if (virDomainCheckpointAlignDisks(def) < 0) + goto cleanup; + + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &def->disks[i]; + + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + + if (vm->def->disks[i]->src->format != VIR_STORAGE_FILE_QCOW2) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("checkpoint for disk %s unsupported " + "for storage type %s"), + disk->name, + virStorageFileFormatTypeToString( + vm->def->disks[i]->src->format)); + goto cleanup; + } + } + + ret = 0; + + cleanup: + VIR_FREE(xml); + return ret; +} + + +static virDomainCheckpointPtr +qemuDomainCheckpointCreateXML(virDomainPtr domain, + const char *xmlDesc, + unsigned int flags) +{ + virQEMUDriverPtr driver = domain->conn->privateData; + virDomainObjPtr vm = NULL; + char *xml = NULL; + virDomainMomentObjPtr chk = NULL; + virDomainCheckpointPtr checkpoint = NULL; + bool update_current = true; + bool redefine = flags & VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE; + unsigned int parse_flags = 0; + virDomainMomentObjPtr other = NULL; + virQEMUDriverConfigPtr cfg = NULL; + virCapsPtr caps = NULL; + VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, NULL); + /* TODO: VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE */ + + if (redefine) { + parse_flags |= VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + update_current = false; + } + + if (!(vm = qemuDomObjFromDomain(domain))) + goto cleanup; + + if (virDomainSnapshotObjListNum(vm->snapshots, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create checkpoint while snapshot exists")); + goto cleanup; + } + + cfg = virQEMUDriverGetConfig(driver); + + if (virDomainCheckpointCreateXMLEnsureACL(domain->conn, vm->def, flags) < 0) + goto cleanup; + + if (!(caps = virQEMUDriverGetCapabilities(driver, false))) + goto cleanup; + + if (qemuProcessAutoDestroyActive(driver, vm)) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("domain is marked for auto destroy")); + goto cleanup; + } + + if (!virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create checkpoint for inactive domain")); + goto cleanup; + } + + if (!(def = virDomainCheckpointDefParseString(xmlDesc, caps, driver->xmlopt, + parse_flags))) + goto cleanup; + /* Unlike snapshots, the RNG schema already ensured a sane filename. */ + + /* We are going to modify the domain below. */ + if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) + goto cleanup; + + if (redefine) { + if (virDomainCheckpointRedefinePrep(domain, vm, &def, &chk, + driver->xmlopt, + &update_current) < 0) + goto endjob; + } else if (qemuDomainCheckpointPrepare(driver, caps, vm, def) < 0) { + goto endjob; + } + + if (!chk) { + if (!(chk = virDomainCheckpointAssignDef(vm->checkpoints, def))) + goto endjob; + + def = NULL; + } + + other = virDomainCheckpointGetCurrent(vm->checkpoints); + if (other) { + if (!redefine && + VIR_STRDUP(chk->def->parent_name, other->def->name) < 0) + goto endjob; + if (update_current) { + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + if (qemuDomainCheckpointWriteMetadata(vm, other, + driver->caps, driver->xmlopt, + cfg->checkpointDir) < 0) + goto endjob; + } + } + + /* actually do the checkpoint */ + if (redefine) { + /* XXX Should we validate that the redefined checkpoint even + * makes sense, such as checking that qemu-img recognizes the + * checkpoint bitmap name in at least one of the domain's disks? */ + } else { + /* TODO: issue QMP transaction command */ + } + + /* If we fail after this point, there's not a whole lot we can do; + * we've successfully created the checkpoint, so we have to go + * forward the best we can. + */ + checkpoint = virGetDomainCheckpoint(domain, chk->def->name); + + endjob: + if (checkpoint) { + if (update_current) + virDomainCheckpointSetCurrent(vm->checkpoints, chk); + if (qemuDomainCheckpointWriteMetadata(vm, chk, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) { + /* if writing of metadata fails, error out rather than trying + * to silently carry on without completing the checkpoint */ + virObjectUnref(checkpoint); + checkpoint = NULL; + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unable to save metadata for checkpoint %s"), + chk->def->name); + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + } else { + virDomainCheckpointLinkParent(vm->checkpoints, chk); + } + } else if (chk) { + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + } + + qemuDomainObjEndJob(driver, vm); + + cleanup: + virDomainObjEndAPI(&vm); + VIR_FREE(xml); + virObjectUnref(caps); + virObjectUnref(cfg); + return checkpoint; +} + + +static int +qemuDomainListAllCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **chks, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + int n = -1; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_LIST_ROOTS | + VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL | + VIR_DOMAIN_CHECKPOINT_FILTERS_ALL, -1); + + if (!(vm = qemuDomObjFromDomain(domain))) + return -1; + + if (virDomainListAllCheckpointsEnsureACL(domain->conn, vm->def) < 0) + goto cleanup; + + n = virDomainListCheckpoints(vm->checkpoints, NULL, domain, chks, flags); + + cleanup: + virDomainObjEndAPI(&vm); + return n; +} + + +static int +qemuDomainCheckpointListAllChildren(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **chks, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + virDomainMomentObjPtr chk = NULL; + int n = -1; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS | + VIR_DOMAIN_CHECKPOINT_LIST_TOPOLOGICAL | + VIR_DOMAIN_CHECKPOINT_FILTERS_ALL, -1); + + if (!(vm = qemuDomObjFromCheckpoint(checkpoint))) + return -1; + + if (virDomainCheckpointListAllChildrenEnsureACL(checkpoint->domain->conn, + vm->def) < 0) + goto cleanup; + + if (!(chk = qemuCheckpointObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + n = virDomainListCheckpoints(vm->checkpoints, chk, checkpoint->domain, + chks, flags); + + cleanup: + virDomainObjEndAPI(&vm); + return n; +} + + +static virDomainCheckpointPtr +qemuDomainCheckpointLookupByName(virDomainPtr domain, + const char *name, + unsigned int flags) +{ + virDomainObjPtr vm; + virDomainMomentObjPtr chk = NULL; + virDomainCheckpointPtr checkpoint = NULL; + + virCheckFlags(0, NULL); + + if (!(vm = qemuDomObjFromDomain(domain))) + return NULL; + + if (virDomainCheckpointLookupByNameEnsureACL(domain->conn, vm->def) < 0) + goto cleanup; + + if (!(chk = qemuCheckpointObjFromName(vm, name))) + goto cleanup; + + checkpoint = virGetDomainCheckpoint(domain, chk->def->name); + + cleanup: + virDomainObjEndAPI(&vm); + return checkpoint; +} + + +static virDomainCheckpointPtr +qemuDomainCheckpointGetParent(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virDomainObjPtr vm; + virDomainMomentObjPtr chk = NULL; + virDomainCheckpointPtr parent = NULL; + + virCheckFlags(0, NULL); + + if (!(vm = qemuDomObjFromCheckpoint(checkpoint))) + return NULL; + + if (virDomainCheckpointGetParentEnsureACL(checkpoint->domain->conn, vm->def) < 0) + goto cleanup; + + if (!(chk = qemuCheckpointObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + if (!chk->def->parent_name) { + virReportError(VIR_ERR_NO_DOMAIN_CHECKPOINT, + _("checkpoint '%s' does not have a parent"), + chk->def->name); + goto cleanup; + } + + parent = virGetDomainCheckpoint(checkpoint->domain, chk->def->parent_name); + + cleanup: + virDomainObjEndAPI(&vm); + return parent; +} + + +static char * +qemuDomainCheckpointGetXMLDesc(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virQEMUDriverPtr driver = checkpoint->domain->conn->privateData; + virDomainObjPtr vm = NULL; + char *xml = NULL; + virDomainMomentObjPtr chk = NULL; + virDomainCheckpointDefPtr chkdef; + unsigned int format_flags; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_XML_SECURE | + VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN, NULL); + + if (!(vm = qemuDomObjFromCheckpoint(checkpoint))) + return NULL; + + if (virDomainCheckpointGetXMLDescEnsureACL(checkpoint->domain->conn, vm->def, flags) < 0) + goto cleanup; + + if (!(chk = qemuCheckpointObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + chkdef = virDomainCheckpointObjGetDef(chk); + + format_flags = virDomainCheckpointFormatConvertXMLFlags(flags); + xml = virDomainCheckpointDefFormat(chkdef, driver->caps, driver->xmlopt, + format_flags); + + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SIZE) + qemuDomainObjEndJob(driver, vm); + + cleanup: + virDomainObjEndAPI(&vm); + return xml; +} + + +static int +qemuDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virQEMUDriverPtr driver = checkpoint->domain->conn->privateData; + virDomainObjPtr vm = NULL; + int ret = -1; + virDomainMomentObjPtr chk = NULL; + virQEMUMomentRemove rem; + virQEMUMomentReparent rep; + bool metadata_only = !!(flags & VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY); + virQEMUDriverConfigPtr cfg = NULL; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN | + VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY | + VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY, -1); + + if (!(vm = qemuDomObjFromCheckpoint(checkpoint))) + return -1; + + cfg = virQEMUDriverGetConfig(driver); + + if (virDomainCheckpointDeleteEnsureACL(checkpoint->domain->conn, vm->def) < 0) + goto cleanup; + + if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) + goto cleanup; + + if (!(chk = qemuCheckpointObjFromCheckpoint(vm, checkpoint))) + goto endjob; + + if (flags & (VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN | + VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY)) { + rem.driver = driver; + rem.vm = vm; + rem.metadata_only = metadata_only; + rem.err = 0; + rem.current = virDomainCheckpointGetCurrent(vm->checkpoints); + rem.found = false; + rem.momentDiscard = qemuDomainCheckpointDiscard; + virDomainMomentForEachDescendant(chk, qemuDomainMomentDiscardAll, + &rem); + if (rem.err < 0) + goto endjob; + if (rem.found) { + virDomainCheckpointSetCurrent(vm->checkpoints, chk); + if (flags & VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY) { + if (qemuDomainCheckpointWriteMetadata(vm, chk, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("failed to set checkpoint '%s' as current"), + chk->def->name); + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + goto endjob; + } + } + } + } else if (chk->nchildren) { + rep.dir = cfg->checkpointDir; + rep.parent = chk->parent; + rep.vm = vm; + rep.err = 0; + rep.caps = driver->caps; + rep.xmlopt = driver->xmlopt; + rep.writeMetadata = qemuDomainCheckpointWriteMetadata; + virDomainMomentForEachChild(chk, qemuDomainMomentReparentChildren, + &rep); + if (rep.err < 0) + goto endjob; + virDomainMomentMoveChildren(chk, chk->parent); + } + + if (flags & VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY) { + virDomainMomentDropChildren(chk); + ret = 0; + } else { + ret = qemuDomainCheckpointDiscard(driver, vm, chk, true, metadata_only); + } + + endjob: + qemuDomainObjEndJob(driver, vm); + + cleanup: + virDomainObjEndAPI(&vm); + virObjectUnref(cfg); + return ret; +} + static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, char **result, unsigned int flags) { @@ -22458,6 +23056,14 @@ static virHypervisorDriver qemuHypervisorDriver = { .connectBaselineHypervisorCPU = qemuConnectBaselineHypervisorCPU, /* 4.4.0 */ .nodeGetSEVInfo = qemuNodeGetSEVInfo, /* 4.5.0 */ .domainGetLaunchSecurityInfo = qemuDomainGetLaunchSecurityInfo, /* 4.5.0 */ + .domainCheckpointCreateXML = qemuDomainCheckpointCreateXML, /* 5.6.0 */ + .domainCheckpointGetXMLDesc = qemuDomainCheckpointGetXMLDesc, /* 5.6.0 */ + + .domainListAllCheckpoints = qemuDomainListAllCheckpoints, /* 5.6.0 */ + .domainCheckpointListAllChildren = qemuDomainCheckpointListAllChildren, /* 5.6.0 */ + .domainCheckpointLookupByName = qemuDomainCheckpointLookupByName, /* 5.6.0 */ + .domainCheckpointGetParent = qemuDomainCheckpointGetParent, /* 5.6.0 */ + .domainCheckpointDelete = qemuDomainCheckpointDelete, /* 5.6.0 */ }; -- 2.20.1

On Wed, Jul 24, 2019 at 12:56:06AM -0500, Eric Blake wrote:
A lot of this work heavily copies from the existing snapshot APIs. The interaction with qemu during create/delete still needs to be implemented, but this takes care of all the libvirt metadata (saving and restoring XML, and tracking the relations between multiple checkpoints).
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.h | 15 + src/qemu/qemu_domain.c | 131 +++++++++ src/qemu/qemu_driver.c | 606 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 752 insertions(+)
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index e7f28aa2b8..44ac7eb73e 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c
+/* Discard one checkpoint (or its metadata), without reparenting any children. */ +int +qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainMomentObjPtr chk, + bool update_parent, + bool metadata_only) +{ + char *chkFile = NULL; + int ret = -1; + virDomainMomentObjPtr parentchk = NULL; + virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver); + + if (!metadata_only) { + if (!virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot remove checkpoint from inactive domain")); + goto cleanup; + } else { + /* TODO: Implement QMP sequence to merge bitmaps */ + // qemuDomainObjPrivatePtr priv; + // priv = vm->privateData; + // qemuDomainObjEnterMonitor(driver, vm); + // /* we continue on even in the face of error */ + // qemuMonitorDeleteCheckpoint(priv->mon, chk->def->name); + // ignore_value(qemuDomainObjExitMonitor(driver, vm));
Shouldn't we have a virReportError + goto cleamnup here until this is actually implemented for real ?
+ } + } + + if (virAsprintf(&chkFile, "%s/%s/%s.xml", cfg->checkpointDir, + vm->def->name, chk->def->name) < 0) + goto cleanup; + + if (chk == virDomainCheckpointGetCurrent(vm->checkpoints)) { + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + if (update_parent && chk->def->parent_name) { + parentchk = virDomainCheckpointFindByName(vm->checkpoints, + chk->def->parent_name); + if (!parentchk) { + VIR_WARN("missing parent checkpoint matching name '%s'", + chk->def->parent_name); + } else { + virDomainCheckpointSetCurrent(vm->checkpoints, parentchk); + if (qemuDomainCheckpointWriteMetadata(vm, parentchk, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) { + VIR_WARN("failed to set parent checkpoint '%s' as current", + chk->def->parent_name); + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + } + } + } + } + + if (unlink(chkFile) < 0) + VIR_WARN("Failed to unlink %s", chkFile); + if (update_parent) + virDomainMomentDropParent(chk); + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + + ret = 0; + + cleanup: + VIR_FREE(chkFile); + virObjectUnref(cfg); + return ret; +}
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index fae2bd3b08..a75a16492b 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c
+static virDomainCheckpointPtr +qemuDomainCheckpointCreateXML(virDomainPtr domain, + const char *xmlDesc, + unsigned int flags) +{ + virQEMUDriverPtr driver = domain->conn->privateData; + virDomainObjPtr vm = NULL; + char *xml = NULL; + virDomainMomentObjPtr chk = NULL; + virDomainCheckpointPtr checkpoint = NULL; + bool update_current = true; + bool redefine = flags & VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE; + unsigned int parse_flags = 0; + virDomainMomentObjPtr other = NULL; + virQEMUDriverConfigPtr cfg = NULL; + virCapsPtr caps = NULL; + VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, NULL); + /* TODO: VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE */ + + if (redefine) { + parse_flags |= VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + update_current = false; + } + + if (!(vm = qemuDomObjFromDomain(domain))) + goto cleanup; + + if (virDomainSnapshotObjListNum(vm->snapshots, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create checkpoint while snapshot exists")); + goto cleanup; + } + + cfg = virQEMUDriverGetConfig(driver); + + if (virDomainCheckpointCreateXMLEnsureACL(domain->conn, vm->def, flags) < 0) + goto cleanup; + + if (!(caps = virQEMUDriverGetCapabilities(driver, false))) + goto cleanup; + + if (qemuProcessAutoDestroyActive(driver, vm)) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("domain is marked for auto destroy")); + goto cleanup; + } + + if (!virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create checkpoint for inactive domain")); + goto cleanup; + } + + if (!(def = virDomainCheckpointDefParseString(xmlDesc, caps, driver->xmlopt, + parse_flags))) + goto cleanup; + /* Unlike snapshots, the RNG schema already ensured a sane filename. */ + + /* We are going to modify the domain below. */ + if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) + goto cleanup; + + if (redefine) { + if (virDomainCheckpointRedefinePrep(domain, vm, &def, &chk, + driver->xmlopt, + &update_current) < 0) + goto endjob; + } else if (qemuDomainCheckpointPrepare(driver, caps, vm, def) < 0) { + goto endjob; + } + + if (!chk) { + if (!(chk = virDomainCheckpointAssignDef(vm->checkpoints, def))) + goto endjob; + + def = NULL; + } + + other = virDomainCheckpointGetCurrent(vm->checkpoints); + if (other) { + if (!redefine && + VIR_STRDUP(chk->def->parent_name, other->def->name) < 0) + goto endjob; + if (update_current) { + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); + if (qemuDomainCheckpointWriteMetadata(vm, other, + driver->caps, driver->xmlopt, + cfg->checkpointDir) < 0) + goto endjob; + } + } + + /* actually do the checkpoint */ + if (redefine) { + /* XXX Should we validate that the redefined checkpoint even + * makes sense, such as checking that qemu-img recognizes the + * checkpoint bitmap name in at least one of the domain's disks? */ + } else { + /* TODO: issue QMP transaction command */ + }
If we want to merge this now, is there something we should be doing in these XXX/TODO branches ? Is the code doing anything useful with both of those if/else branches being no-ops ? Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Qemu bitmap operations require knowing the node name associated with the format layer (the qcow2 file); as upcoming patches will be grabbing that information frequently, make a helper function to access it. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.h | 3 +++ src/qemu/qemu_domain.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index eeb07180b7..b76d3cace9 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -804,6 +804,9 @@ int qemuDomainDetermineDiskChain(virQEMUDriverPtr driver, bool qemuDomainDiskChangeSupported(virDomainDiskDefPtr disk, virDomainDiskDefPtr orig_disk); +const char *qemuDomainDiskNodeFormatLookup(virDomainObjPtr vm, + const char *disk); + int qemuDomainStorageFileInit(virQEMUDriverPtr driver, virDomainObjPtr vm, virStorageSourcePtr src, diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 44ac7eb73e..d71b7d1984 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -10260,6 +10260,21 @@ qemuDomainDiskChangeSupported(virDomainDiskDefPtr disk, return true; } + +/* Return the format node name for a given disk of an online guest */ +const char * +qemuDomainDiskNodeFormatLookup(virDomainObjPtr vm, + const char *disk) +{ + size_t i; + + for (i = 0; i < vm->def->ndisks; i++) { + if (STREQ(vm->def->disks[i]->dst, disk)) + return vm->def->disks[i]->src->nodeformat; + } + return NULL; +} + bool qemuDomainDiskBlockJobIsActive(virDomainDiskDefPtr disk) { -- 2.20.1

On Wed, Jul 24, 2019 at 12:56:07AM -0500, Eric Blake wrote:
Qemu bitmap operations require knowing the node name associated with the format layer (the qcow2 file); as upcoming patches will be grabbing that information frequently, make a helper function to access it.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.h | 3 +++ src/qemu/qemu_domain.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Time to actually issue the QMP transactions that create and delete persistent checkpoints. For create, we only need one transaction: inside, we visit all disks affected by the checkpoint, and create a new enabled bitmap, as well as disabling the bitmap of the first ancestor checkpoint (if any) that also had a bitmap. For deletion, we need multiple calls: for each disk, if there is an ancestor checkpoint with a bitmap, then the bitmap must be merged (including activating the ancestor bitmap), all before deleting the bitmap from the checkpoint being removed. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.c | 122 ++++++++++++++++++++++++++++------------- src/qemu/qemu_driver.c | 98 ++++++++++++++++++++++++++++++++- 2 files changed, 180 insertions(+), 40 deletions(-) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index d71b7d1984..062a08ed97 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -9255,48 +9255,97 @@ qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, bool update_parent, bool metadata_only) { - char *chkFile = NULL; - int ret = -1; - virDomainMomentObjPtr parentchk = NULL; - virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver); + virDomainMomentObjPtr parent = NULL; + virDomainMomentObjPtr moment; + virDomainCheckpointDefPtr parentdef = NULL; + size_t i, j; + VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver); + VIR_AUTOFREE(char *) chkFile = NULL; - if (!metadata_only) { - if (!virDomainObjIsActive(vm)) { - virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", - _("cannot remove checkpoint from inactive domain")); - goto cleanup; - } else { - /* TODO: Implement QMP sequence to merge bitmaps */ - // qemuDomainObjPrivatePtr priv; - // priv = vm->privateData; - // qemuDomainObjEnterMonitor(driver, vm); - // /* we continue on even in the face of error */ - // qemuMonitorDeleteCheckpoint(priv->mon, chk->def->name); - // ignore_value(qemuDomainObjExitMonitor(driver, vm)); - } + if (!metadata_only && !virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot remove checkpoint from inactive domain")); + return -1; } if (virAsprintf(&chkFile, "%s/%s/%s.xml", cfg->checkpointDir, vm->def->name, chk->def->name) < 0) - goto cleanup; + return -1; + + if (!metadata_only) { + qemuDomainObjPrivatePtr priv = vm->privateData; + bool success = true; + bool search_parents; + virDomainCheckpointDefPtr chkdef = virDomainCheckpointObjGetDef(chk); + + qemuDomainObjEnterMonitor(driver, vm); + parent = virDomainCheckpointFindByName(vm->checkpoints, + chk->def->parent_name); + for (i = 0; i < chkdef->ndisks; i++) { + virDomainCheckpointDiskDef *disk = &chkdef->disks[i]; + const char *node; + + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + + node = qemuDomainDiskNodeFormatLookup(vm, disk->name); + /* If any ancestor checkpoint has a bitmap for the same + * disk, then this bitmap must be merged to the + * ancestor. */ + search_parents = true; + for (moment = parent; + search_parents && moment; + moment = virDomainCheckpointFindByName(vm->checkpoints, + parentdef->parent.parent_name)) { + parentdef = virDomainCheckpointObjGetDef(moment); + for (j = 0; j < parentdef->ndisks; j++) { + virDomainCheckpointDiskDef *disk2; + VIR_AUTOPTR(virJSONValue) arr = NULL; + + disk2 = &parentdef->disks[j]; + if (STRNEQ(disk->name, disk2->name) || + disk2->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + search_parents = false; + + arr = virJSONValueNewArray(); + if (!arr || + virJSONValueArrayAppendString(arr, disk->bitmap) < 0) { + success = false; + break; + } + if (chk == virDomainCheckpointGetCurrent(vm->checkpoints) && + qemuMonitorEnableBitmap(priv->mon, node, + disk2->bitmap) < 0) { + success = false; + break; + } + if (qemuMonitorMergeBitmaps(priv->mon, node, + disk2->bitmap, &arr) < 0) { + success = false; + break; + } + } + } + if (qemuMonitorDeleteBitmap(priv->mon, node, disk->bitmap) < 0) { + success = false; + break; + } + } + if (qemuDomainObjExitMonitor(driver, vm) < 0 || !success) + return -1; + } if (chk == virDomainCheckpointGetCurrent(vm->checkpoints)) { virDomainCheckpointSetCurrent(vm->checkpoints, NULL); - if (update_parent && chk->def->parent_name) { - parentchk = virDomainCheckpointFindByName(vm->checkpoints, - chk->def->parent_name); - if (!parentchk) { - VIR_WARN("missing parent checkpoint matching name '%s'", + if (update_parent && parent) { + virDomainCheckpointSetCurrent(vm->checkpoints, parent); + if (qemuDomainCheckpointWriteMetadata(vm, parent, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) { + VIR_WARN("failed to set parent checkpoint '%s' as current", chk->def->parent_name); - } else { - virDomainCheckpointSetCurrent(vm->checkpoints, parentchk); - if (qemuDomainCheckpointWriteMetadata(vm, parentchk, driver->caps, - driver->xmlopt, - cfg->checkpointDir) < 0) { - VIR_WARN("failed to set parent checkpoint '%s' as current", - chk->def->parent_name); - virDomainCheckpointSetCurrent(vm->checkpoints, NULL); - } + virDomainCheckpointSetCurrent(vm->checkpoints, NULL); } } } @@ -9307,12 +9356,7 @@ qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, virDomainMomentDropParent(chk); virDomainCheckpointObjListRemove(vm->checkpoints, chk); - ret = 0; - - cleanup: - VIR_FREE(chkFile); - virObjectUnref(cfg); - return ret; + return 0; } int diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index a75a16492b..d0377d78a9 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -47,6 +47,7 @@ #include "qemu_hostdev.h" #include "qemu_hotplug.h" #include "qemu_monitor.h" +#include "qemu_monitor_json.h" #include "qemu_process.h" #include "qemu_migration.h" #include "qemu_migration_params.h" @@ -16995,6 +16996,65 @@ qemuDomainCheckpointPrepare(virQEMUDriverPtr driver, virCapsPtr caps, return ret; } +static int +qemuDomainCheckpointAddActions(virDomainObjPtr vm, + virJSONValuePtr actions, + virDomainMomentObjPtr old_current, + virDomainCheckpointDefPtr def) +{ + size_t i, j; + virDomainCheckpointDefPtr olddef; + virDomainMomentObjPtr parent; + bool search_parents; + + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDef *disk = &def->disks[i]; + const char *node; + + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + node = qemuDomainDiskNodeFormatLookup(vm, disk->name); + if (qemuMonitorJSONTransactionAdd(actions, + "block-dirty-bitmap-add", + "s:node", node, + "s:name", disk->bitmap, + "b:persistent", true, + NULL) < 0) + return -1; + + /* We only want one active bitmap for a disk along the + * checkpoint chain, then later differential backups will + * merge the bitmaps (only one active) between the bounding + * checkpoint and the leaf checkpoint. If the same disks are + * involved in each checkpoint, this search terminates in one + * iteration; but it is also possible to have to search + * further than the immediate parent to find another + * checkpoint with a bitmap on the same disk. */ + search_parents = true; + for (parent = old_current; search_parents && parent; + parent = virDomainCheckpointFindByName(vm->checkpoints, + olddef->parent.parent_name)) { + olddef = virDomainCheckpointObjGetDef(parent); + for (j = 0; j < olddef->ndisks; j++) { + virDomainCheckpointDiskDef *disk2; + + disk2 = &olddef->disks[j]; + if (STRNEQ(disk->name, disk2->name) || + disk2->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + if (qemuMonitorJSONTransactionAdd(actions, + "block-dirty-bitmap-disable", + "s:node", node, + "s:name", disk2->bitmap, + NULL) < 0) + return -1; + search_parents = false; + break; + } + } + } + return 0; +} static virDomainCheckpointPtr qemuDomainCheckpointCreateXML(virDomainPtr domain, @@ -17012,6 +17072,9 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, virDomainMomentObjPtr other = NULL; virQEMUDriverConfigPtr cfg = NULL; virCapsPtr caps = NULL; + qemuDomainObjPrivatePtr priv; + virJSONValuePtr actions = NULL; + int ret; VIR_AUTOUNREF(virDomainCheckpointDefPtr) def = NULL; virCheckFlags(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, NULL); @@ -17031,11 +17094,18 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, goto cleanup; } + priv = vm->privateData; cfg = virQEMUDriverGetConfig(driver); if (virDomainCheckpointCreateXMLEnsureACL(domain->conn, vm->def, flags) < 0) goto cleanup; + if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BITMAP_MERGE)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("qemu binary lacks persistent bitmaps support")); + goto cleanup; + } + if (!(caps = virQEMUDriverGetCapabilities(driver, false))) goto cleanup; @@ -17096,7 +17166,15 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, * makes sense, such as checking that qemu-img recognizes the * checkpoint bitmap name in at least one of the domain's disks? */ } else { - /* TODO: issue QMP transaction command */ + if (!(actions = virJSONValueNewArray())) + goto endjob; + if (qemuDomainCheckpointAddActions(vm, actions, other, + virDomainCheckpointObjGetDef(chk)) < 0) + goto endjob; + qemuDomainObjEnterMonitor(driver, vm); + ret = qemuMonitorTransaction(priv->mon, &actions); + if (qemuDomainObjExitMonitor(driver, vm) < 0 || ret < 0) + goto endjob; } /* If we fail after this point, there's not a whole lot we can do; @@ -17130,6 +17208,7 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, qemuDomainObjEndJob(driver, vm); cleanup: + virJSONValueFree(actions); virDomainObjEndAPI(&vm); VIR_FREE(xml); virObjectUnref(caps); @@ -17301,6 +17380,7 @@ qemuDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, { virQEMUDriverPtr driver = checkpoint->domain->conn->privateData; virDomainObjPtr vm = NULL; + qemuDomainObjPrivatePtr priv; int ret = -1; virDomainMomentObjPtr chk = NULL; virQEMUMomentRemove rem; @@ -17323,6 +17403,22 @@ qemuDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) goto cleanup; + priv = vm->privateData; + if (!metadata_only) { + /* Until qemu-img supports offline bitmap deletion, we are stuck + * with requiring a running guest */ + if (!virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot delete checkpoint for inactive domain")); + goto endjob; + } + if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BITMAP_MERGE)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("qemu binary lacks persistent bitmaps support")); + goto endjob; + } + } + if (!(chk = qemuCheckpointObjFromCheckpoint(vm, checkpoint))) goto endjob; -- 2.20.1

On Wed, Jul 24, 2019 at 12:56:08AM -0500, Eric Blake wrote:
Time to actually issue the QMP transactions that create and delete persistent checkpoints. For create, we only need one transaction: inside, we visit all disks affected by the checkpoint, and create a new enabled bitmap, as well as disabling the bitmap of the first ancestor checkpoint (if any) that also had a bitmap. For deletion, we need multiple calls: for each disk, if there is an ancestor checkpoint with a bitmap, then the bitmap must be merged (including activating the ancestor bitmap), all before deleting the bitmap from the checkpoint being removed.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.c | 122 ++++++++++++++++++++++++++++------------- src/qemu/qemu_driver.c | 98 ++++++++++++++++++++++++++++++++- 2 files changed, 180 insertions(+), 40 deletions(-)
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index d71b7d1984..062a08ed97 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -9255,48 +9255,97 @@ qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, bool update_parent, bool metadata_only) { - char *chkFile = NULL; - int ret = -1; - virDomainMomentObjPtr parentchk = NULL; - virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver); + virDomainMomentObjPtr parent = NULL; + virDomainMomentObjPtr moment; + virDomainCheckpointDefPtr parentdef = NULL; + size_t i, j; + VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver); + VIR_AUTOFREE(char *) chkFile = NULL;
- if (!metadata_only) { - if (!virDomainObjIsActive(vm)) { - virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", - _("cannot remove checkpoint from inactive domain")); - goto cleanup; - } else { - /* TODO: Implement QMP sequence to merge bitmaps */ - // qemuDomainObjPrivatePtr priv; - // priv = vm->privateData; - // qemuDomainObjEnterMonitor(driver, vm); - // /* we continue on even in the face of error */ - // qemuMonitorDeleteCheckpoint(priv->mon, chk->def->name); - // ignore_value(qemuDomainObjExitMonitor(driver, vm)); - } + if (!metadata_only && !virDomainObjIsActive(vm)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot remove checkpoint from inactive domain")); + return -1; }
This semi-answerss my previous question on earlier patch. I guess we just shouldn't add the commented out lines at all in teh first place, given you are deleting them entirely here.
@@ -17096,7 +17166,15 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, * makes sense, such as checking that qemu-img recognizes the * checkpoint bitmap name in at least one of the domain's disks? */ } else { - /* TODO: issue QMP transaction command */ + if (!(actions = virJSONValueNewArray())) + goto endjob; + if (qemuDomainCheckpointAddActions(vm, actions, other, + virDomainCheckpointObjGetDef(chk)) < 0) + goto endjob; + qemuDomainObjEnterMonitor(driver, vm); + ret = qemuMonitorTransaction(priv->mon, &actions); + if (qemuDomainObjExitMonitor(driver, vm) < 0 || ret < 0) + goto endjob; }
Ah, and this now answers my other question. Makes me wonder if there's really a benefit to splitting the code across two patches, as opposed to just one. As long as it git bisect's with a clean built though its not a show stopper. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

Earlier patches mentioned that the initial implementation will prevent snapshots and checkpoints from being used on the same domain at once. However, the actual restriction is done in this separate patch to make it easier to lift that restriction via a revert, when we are finally ready to tackle that integration in the future. Signed-off-by: Eric Blake <eblake@redhat.com> --- docs/formatsnapshot.html.in | 4 +++- src/qemu/qemu_driver.c | 12 ++++++++++++ src/test/test_driver.c | 12 ++++++++++++ tests/virsh-checkpoint | 10 ++++++++++ tests/virsh-snapshot | 10 ++++++++++ tools/virsh.pod | 16 ++++++++++++++++ 6 files changed, 63 insertions(+), 1 deletion(-) diff --git a/docs/formatsnapshot.html.in b/docs/formatsnapshot.html.in index ced8bbd6db..1935e7351a 100644 --- a/docs/formatsnapshot.html.in +++ b/docs/formatsnapshot.html.in @@ -93,7 +93,9 @@ sets that snapshot as current, and the prior current snapshot is the parent of the new snapshot. Branches in the hierarchy can be formed by reverting to a snapshot with a child, then creating - another snapshot. + another snapshot. For now, the creation of external snapshots + when checkpoints exist is forbidden, although future work will + make it possible to integrate these two concepts. </p> <p> The top-level <code>domainsnapshot</code> element may contain diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index d0377d78a9..ca707815d9 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -15749,6 +15749,12 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain, if (!(vm = qemuDomObjFromDomain(domain))) goto cleanup; + if (virDomainListCheckpoints(vm->checkpoints, NULL, domain, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create snapshot while checkpoint exists")); + goto cleanup; + } + cfg = virQEMUDriverGetConfig(driver); if (virDomainSnapshotCreateXMLEnsureACL(domain->conn, vm->def, flags) < 0) @@ -22332,6 +22338,12 @@ static int qemuDomainRename(virDomainPtr dom, goto endjob; } + if (virDomainListCheckpoints(vm->checkpoints, NULL, dom, NULL, flags) > 0) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("cannot rename domain with checkpoints")); + goto endjob; + } + if (virDomainObjListRename(driver->domains, vm, new_name, flags, qemuDomainRenameCallback, driver) < 0) goto endjob; diff --git a/src/test/test_driver.c b/src/test/test_driver.c index abe51f252a..886d6720db 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -7306,6 +7306,12 @@ testDomainSnapshotCreateXML(virDomainPtr domain, if (!(vm = testDomObjFromDomain(domain))) goto cleanup; + if (virDomainListCheckpoints(vm->checkpoints, NULL, domain, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create snapshot while checkpoint exists")); + goto cleanup; + } + if (!vm->persistent && (flags & VIR_DOMAIN_SNAPSHOT_CREATE_HALT)) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("cannot halt after transient domain snapshot")); @@ -7766,6 +7772,12 @@ testDomainCheckpointCreateXML(virDomainPtr domain, if (!(vm = testDomObjFromDomain(domain))) goto cleanup; + if (virDomainSnapshotObjListNum(vm->snapshots, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create checkpoint while snapshot exists")); + goto cleanup; + } + if (!virDomainObjIsActive(vm)) { virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", _("cannot create checkpoint for inactive domain")); diff --git a/tests/virsh-checkpoint b/tests/virsh-checkpoint index 6fe5aa4c05..8f964b5822 100755 --- a/tests/virsh-checkpoint +++ b/tests/virsh-checkpoint @@ -38,6 +38,10 @@ $abs_top_builddir/tools/virsh --connect test:///default >out 2>err ' checkpoint-create-as test c1 checkpoint-create-as test c3 checkpoint-create-as test c2 + # snapshots cannot be created while checkpoints exist + echo --err marker + snapshot-create-as test s1 + echo --err marker # Checking tree view (siblings sorted alphabetically) checkpoint-list test --tree # Demonstrate list filtering @@ -77,6 +81,9 @@ Domain checkpoint c1 created Domain checkpoint c3 created Domain checkpoint c2 created + + + c1 | +- c3 @@ -126,6 +133,9 @@ compare exp out.cooked || fail=1 cat <<EOF > exp || fail=1 error: operation failed: domain moment c1 already exists +error: marker +error: Operation not supported: cannot create snapshot while checkpoint exists +error: marker EOF compare exp err || fail=1 diff --git a/tests/virsh-snapshot b/tests/virsh-snapshot index b23d949f62..fa321d3354 100755 --- a/tests/virsh-snapshot +++ b/tests/virsh-snapshot @@ -45,6 +45,10 @@ $abs_top_builddir/tools/virsh --connect test:///default >out 2>err ' snapshot-revert test s1 snapshot-create-as test s7 snapshot-create-as test s8 + # checkpoints cannot be created while snapshots exist + echo --err marker + checkpoint-create-as test c1 + echo --err marker # Checking tree view (siblings sorted alphabetically) snapshot-list test --tree # Current was last one created, but we can change that @@ -102,6 +106,9 @@ Domain snapshot s4 created Domain snapshot s7 created Domain snapshot s8 created + + + s1 | +- s3 @@ -175,6 +182,9 @@ compare exp out.cooked || fail=1 cat <<EOF > exp || fail=1 error: operation failed: domain moment s1 already exists error: marker +error: Operation not supported: cannot create checkpoint while snapshot exists +error: marker +error: marker error: domain 'test' has no current snapshot error: marker EOF diff --git a/tools/virsh.pod b/tools/virsh.pod index f58578e3b3..af739bec24 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -4669,6 +4669,10 @@ 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). +For now, it is not possible to create snapshots in a domain that has +checkpoints, although this restriction will be lifted in a future +release. + =item B<snapshot-create-as> I<domain> {[I<--print-xml>] [I<--no-metadata>] [I<--halt>] [I<--reuse-external>]} [I<name>] [I<description>] [I<--disk-only> [I<--quiesce>]] [I<--atomic>] @@ -4735,6 +4739,10 @@ If I<--live> is specified, libvirt takes the snapshot while the guest is running. This increases the size of the memory image of the external snapshot. This is currently supported only for external full system snapshots. +For now, it is not possible to create snapshots in a domain that has +checkpoints, although this restriction will be lifted in a future +release. + =item B<snapshot-current> I<domain> {[I<--name>] | [I<--security-info>] | [I<snapshotname>]} @@ -4943,6 +4951,10 @@ a persistent domain. However, for transient domains, checkpoint metadata is silently lost when the domain quits running (whether by command such as B<destroy> or by internal guest action). +For now, it is not possible to create checkpoints in a domain that has +snapshots, although this restriction will be lifted in a future +release. + =item B<checkpoint-create-as> I<domain> [I<--print-xml>] [I<name>] [I<description>] [I<--quiesce>] [I<--diskspec>] B<diskspec>]... @@ -4966,6 +4978,10 @@ If I<--quiesce> is specified, libvirt will try to use guest agent to freeze and unfreeze domain's mounted file systems. However, if domain has no guest agent, checkpoint creation will fail. +For now, it is not possible to create checkpoints in a domain that has +snapshots, although this restriction will be lifted in a future +release. + =item B<checkpoint-edit> I<domain> I<checkpointname> Edit the XML configuration file for I<checkpointname> of a domain. -- 2.20.1

On Wed, Jul 24, 2019 at 00:56:09 -0500, Eric Blake wrote:
Earlier patches mentioned that the initial implementation will prevent snapshots and checkpoints from being used on the same domain at once. However, the actual restriction is done in this separate patch to make it easier to lift that restriction via a revert, when we are finally ready to tackle that integration in the future.
Signed-off-by: Eric Blake <eblake@redhat.com> --- docs/formatsnapshot.html.in | 4 +++- src/qemu/qemu_driver.c | 12 ++++++++++++ src/test/test_driver.c | 12 ++++++++++++ tests/virsh-checkpoint | 10 ++++++++++ tests/virsh-snapshot | 10 ++++++++++ tools/virsh.pod | 16 ++++++++++++++++ 6 files changed, 63 insertions(+), 1 deletion(-)
[...]
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index d0377d78a9..ca707815d9 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -15749,6 +15749,12 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain, if (!(vm = qemuDomObjFromDomain(domain))) goto cleanup;
+ if (virDomainListCheckpoints(vm->checkpoints, NULL, domain, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create snapshot while checkpoint exists")); + goto cleanup; + } + cfg = virQEMUDriverGetConfig(driver);
if (virDomainSnapshotCreateXMLEnsureACL(domain->conn, vm->def, flags) < 0)
Note that also any of the block jobs (block pull, block commit) will mess up the bitmaps thus should be forbidden with checkpoints. Also block copy has the same problem, but it's questionable whether we'll want to copy over the bitmaps (which is way easier than with snapshots, which break if you use that API). If the idea is to eventually be able to copy bitmaps, then we should also forbid that one until it's implemented.

On 7/24/19 10:40 AM, Peter Krempa wrote:
On Wed, Jul 24, 2019 at 00:56:09 -0500, Eric Blake wrote:
Earlier patches mentioned that the initial implementation will prevent snapshots and checkpoints from being used on the same domain at once. However, the actual restriction is done in this separate patch to make it easier to lift that restriction via a revert, when we are finally ready to tackle that integration in the future.
Signed-off-by: Eric Blake <eblake@redhat.com>
if (virDomainSnapshotCreateXMLEnsureACL(domain->conn, vm->def, flags) < 0)
Note that also any of the block jobs (block pull, block commit) will mess up the bitmaps thus should be forbidden with checkpoints. Also block copy has the same problem, but it's questionable whether we'll want to copy over the bitmaps (which is way easier than with snapshots, which break if you use that API). If the idea is to eventually be able to copy bitmaps, then we should also forbid that one until it's implemented.
Here's what I'm squashing in to this patch. I've made a few other adjustments through the series based on review comments and my last-minute once-over, and am now pushing this series; any further changes will need to be followup patches. diff --git i/src/qemu/qemu_driver.c w/src/qemu/qemu_driver.c index 3e29e54cea..8fa928afc1 100644 --- i/src/qemu/qemu_driver.c +++ w/src/qemu/qemu_driver.c @@ -18476,6 +18476,12 @@ qemuDomainBlockRebase(virDomainPtr dom, const char *path, const char *base, if (virDomainBlockRebaseEnsureACL(dom->conn, vm->def) < 0) goto cleanup; + if (virDomainListCheckpoints(vm->checkpoints, NULL, dom, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot perform block rebase while checkpoint exists")); + goto cleanup; + } + /* For normal rebase (enhanced blockpull), the common code handles * everything, including vm cleanup. */ if (!(flags & VIR_DOMAIN_BLOCK_REBASE_COPY)) @@ -18560,6 +18566,12 @@ qemuDomainBlockCopy(virDomainPtr dom, const char *disk, const char *destxml, if (virDomainBlockCopyEnsureACL(dom->conn, vm->def) < 0) goto cleanup; + if (virDomainListCheckpoints(vm->checkpoints, NULL, dom, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot perform block copy while checkpoint exists")); + goto cleanup; + } + for (i = 0; i < nparams; i++) { virTypedParameterPtr param = ¶ms[i]; @@ -18622,6 +18634,13 @@ qemuDomainBlockPull(virDomainPtr dom, const char *path, unsigned long bandwidth, return -1; } + if (virDomainListCheckpoints(vm->checkpoints, NULL, dom, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot perform block pull while checkpoint exists")); + virDomainObjEndAPI(&vm); + return -1; + } + return qemuDomainBlockPullCommon(dom->conn->privateData, vm, path, NULL, bandwidth, flags); } @@ -18668,6 +18687,12 @@ qemuDomainBlockCommit(virDomainPtr dom, if (virDomainBlockCommitEnsureACL(dom->conn, vm->def) < 0) goto cleanup; + if (virDomainListCheckpoints(vm->checkpoints, NULL, dom, NULL, 0) > 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot perform block commit while checkpoint exists")); + goto cleanup; + } + if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) goto cleanup; -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

On Wed, Jul 24, 2019 at 12:56:09AM -0500, Eric Blake wrote:
Earlier patches mentioned that the initial implementation will prevent snapshots and checkpoints from being used on the same domain at once. However, the actual restriction is done in this separate patch to make it easier to lift that restriction via a revert, when we are finally ready to tackle that integration in the future.
Signed-off-by: Eric Blake <eblake@redhat.com> --- docs/formatsnapshot.html.in | 4 +++- src/qemu/qemu_driver.c | 12 ++++++++++++ src/test/test_driver.c | 12 ++++++++++++ tests/virsh-checkpoint | 10 ++++++++++ tests/virsh-snapshot | 10 ++++++++++ tools/virsh.pod | 16 ++++++++++++++++ 6 files changed, 63 insertions(+), 1 deletion(-)
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|

On Wed, Jul 24, 2019 at 12:55:50AM -0500, Eric Blake wrote:
This is a subset of v10 of incremental backup, fixing Peter's review comments from v9 (and even some from v8 that I had missed earlier). There's still a lot more rebasing churn to resolve in the backup portion of the series before I can post a full backup-v10 label.
There's still a decision to make: do we want this series in 5.6 (possibly with the addition of just patch 2/10 at [1] to introduce the backup API to make it possible for downstream to backport features without bumping .so)? This series has checkpoint support for test and qemu drivers, and I think is probably clean enough to finally satisfy everything Peter has been pointing out;
In general my preference is to incrementally merge code as it is ready. For large development tasks, I think it is counterproductive to try to wait until everything is absolutely perfect & able to merge in a big bang. Longer patch series tend to scare off reviewers, making them take even longer to turn around & merge, and increase the burden on the developer to continually rebase wich also delays things. IOW, assuming positive review on this v10 series, I see no compelling reason to artificially delay merge of these checkpoint patches waiting for the followup backup patch series.
but while the backup API itself seems reasonable, the qemu implementation will likely miss 5.6 (as that half of the v9 posting was further behind, and still has a lot of rebase churn to resolve). Or do we delay the checkpoint API to 5.7, to only go in with backup API? There's also still an outstanding question of whether the backup API needs a tweak to use 'const char *' instead of 'int' for the job identifier, given that Peter has a proposal for overhauling the representation of libvirt jobs.
I definitely think we do *not* want to use an 'int'. The only place we've used an int for a unique identifier was the virDomainPtr and even that's largely a historical mistake from early days of our Xen support. A 'const char*' is reasonable, though I have raised the question in Peter proposal whether we should go all the way and pick a UUID as the most unique identifier, so that we don't acceidentally confuse jobs between domains. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
participants (3)
-
Daniel P. Berrangé
-
Eric Blake
-
Peter Krempa