[libvirt] [PATCH v4 00/20] Incremental Backup API additions

The following is the latest version of my API proposal for incremental backups, and follows along from the demo I presented as part of my KVM Forum 2018 talk: https://kvmforum2018.sched.com/event/FzuB/facilitating-incremental-backup-er... The patches are also available via https://repo.or.cz/libvirt/ericb.git at the tag backup-v4. Integration with external snapshots is still not included in these patches, although I have been playing more with that locally, and I still haven't gotten a working demonstration of a push-mode incremental backup. At this point, I'm posting mainly because there have been enough changes in qemu (the backup APIs are now stable! and in qemu.git in time for 4.0) and libvirt (several releases and code cleanups in between, such that the v3 no longer applies easily), while I continue to work locally on more features, addressing the review comments I got on v3, and remove the 'wip' tag on several of the later patches. Among other things, I still haven't determined how we want to integrate checkpoints with external snapshots; we could either have: virDomainSnapshotCreateXML("<domainsnapshot>...") # existing virDomainBackupBegin("<domainbackup>...", "<domaincheckpoint>...") # this series virDomainSnapshotCheckpointCreateXML("<domainsnapshot>...", "<domaincheckpoint>...") # new or to make checkpoint creation part of the snapshot and backup XML, as in: virDomainSnapshotCreateXML("<domainsnapshot><domaincheckpoint>...</domainsnapshot>") # extension of existing virDomainBackupBegin("<domainbackup><domaincheckpoint>...</domainbackup>") # tweak to this series I'm also planning to add an API to query which job ids are currently running (right now, the code only supports one job at a time, and always calls it job 1, but that is not future-friendly) and an update to the redefine XML command to make it easier to redefine multiple checkpoints in one API call. Since v3: 001/20:[----] [--] 'snapshots: Avoid term 'checkpoint' for full system snapshot' 002/20:[----] [--] 'domain_conf: Expose virDomainStorageNetworkParseHost' 003/20:[----] [--] 'qemu: Allow optional export name during NBD export' 004/20:[----] [--] 'backup: Document nuances between different state capture APIs' 005/20:[0032] [FC] 'backup: Introduce virDomainCheckpointPtr' 006/20:[----] [--] 'backup: Document new XML for backups' 007/20:[0013] [FC] 'backup: Introduce virDomainCheckpoint APIs' 008/20:[0017] [FC] 'backup: Introduce virDomainBackup APIs' 009/20:[----] [--] 'backup: Add new domain:checkpoint access control' 010/20:[0104] [FC] 'backup: Implement backup APIs for remote driver' 011/20:[0034] [FC] 'wip: backup: Parse and output checkpoint XML' 012/20:[0013] [FC] 'wip: backup: Parse and output backup XML' 013/20:[0006] [FC] 'backup: Implement virsh support for checkpoints' 014/20:[----] [--] 'wip: backup: virsh support for backup' 015/20:[0126] [FC] 'backup: Add new qemu monitor interactions' 016/20:[0072] [FC] 'backup: qemu: Implement metadata tracking for checkpoint APIs' 017/20:[0069] [FC] 'wip: backup: Wire up qemu checkpoint commands over QMP' 018/20:[0018] [FC] 'wip: backup: qemu: Implement framework for backup job APIs' 019/20:[0045] [FC] 'backup: Wire up qemu full pull backup commands over QMP' 020/20:[0051] [FC] 'backup: implement qemu incremental pull backup' Eric Blake (20): snapshots: Avoid term 'checkpoint' for full system snapshot domain_conf: Expose virDomainStorageNetworkParseHost qemu: Allow optional export name during NBD export backup: Document nuances between different state capture APIs backup: Introduce virDomainCheckpointPtr backup: Document new XML for backups backup: Introduce virDomainCheckpoint APIs backup: Introduce virDomainBackup APIs backup: Add new domain:checkpoint access control backup: Implement backup APIs for remote driver wip: backup: Parse and output checkpoint XML wip: backup: Parse and output backup XML backup: Implement virsh support for checkpoints wip: backup: virsh support for backup backup: Add new qemu monitor interactions backup: qemu: Implement metadata tracking for checkpoint APIs wip: backup: Wire up qemu checkpoint commands over QMP wip: backup: qemu: Implement framework for backup job APIs backup: Wire up qemu full pull backup commands over QMP backup: implement qemu incremental pull backup include/libvirt/virterror.h | 7 +- src/util/virerror.c | 12 +- include/libvirt/libvirt-domain-checkpoint.h | 176 ++ include/libvirt/libvirt-domain-snapshot.h | 2 +- include/libvirt/libvirt-domain.h | 17 +- include/libvirt/libvirt.h | 3 +- src/access/viraccessperm.h | 8 +- src/conf/checkpoint_conf.h | 215 +++ src/conf/domain_conf.h | 16 +- src/datatypes.h | 31 +- src/driver-hypervisor.h | 74 +- src/qemu/qemu_block.h | 3 + src/qemu/qemu_blockjob.h | 1 + src/qemu/qemu_capabilities.h | 2 + src/qemu/qemu_conf.h | 2 + src/qemu/qemu_domain.h | 29 +- src/qemu/qemu_monitor.h | 21 +- src/qemu/qemu_monitor_json.h | 19 +- tools/virsh-checkpoint.h | 29 + 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 | 9 +- docs/domainstatecapture.html.in | 314 ++++ docs/format.html.in | 1 + docs/formatcheckpoint.html.in | 291 +++ docs/formatsnapshot.html.in | 33 +- docs/index.html.in | 3 +- docs/schemas/domainbackup.rng | 185 ++ docs/schemas/domaincheckpoint.rng | 94 + examples/object-events/event-test.c | 3 + libvirt.spec.in | 3 + mingw-libvirt.spec.in | 6 + po/POTFILES | 3 + src/Makefile.am | 2 + src/access/viraccessperm.c | 5 +- src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 1533 ++++++++++++++++ src/conf/domain_conf.c | 52 +- src/conf/snapshot_conf.c | 4 +- src/datatypes.c | 62 +- src/libvirt-domain-checkpoint.c | 925 ++++++++++ src/libvirt-domain-snapshot.c | 7 +- src/libvirt-domain.c | 8 +- src/libvirt_private.syms | 30 + src/libvirt_public.syms | 23 + src/qemu/qemu_block.c | 12 + src/qemu/qemu_capabilities.c | 4 + src/qemu/qemu_conf.c | 5 + src/qemu/qemu_domain.c | 217 ++- src/qemu/qemu_driver.c | 1552 +++++++++++++++- src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_monitor.c | 65 +- src/qemu/qemu_monitor_json.c | 197 +- src/qemu/qemu_process.c | 7 + src/remote/remote_daemon_dispatch.c | 22 +- src/remote/remote_driver.c | 33 +- src/remote/remote_protocol.x | 238 ++- src/remote_protocol-structs | 129 ++ src/rpc/gendispatch.pl | 32 +- tests/Makefile.am | 15 +- tests/domainbackupxml2xmlin/backup-pull.xml | 9 + tests/domainbackupxml2xmlin/backup-push.xml | 9 + tests/domainbackupxml2xmlin/empty.xml | 1 + tests/domainbackupxml2xmlout/backup-pull.xml | 9 + tests/domainbackupxml2xmlout/backup-push.xml | 9 + tests/domainbackupxml2xmlout/empty.xml | 7 + tests/domaincheckpointxml2xmlin/empty.xml | 1 + tests/domaincheckpointxml2xmlin/sample.xml | 7 + tests/domaincheckpointxml2xmlout/empty.xml | 10 + tests/domaincheckpointxml2xmlout/sample.xml | 16 + tests/domaincheckpointxml2xmltest.c | 231 +++ .../caps_4.0.0.riscv32.xml | 2 + .../caps_4.0.0.riscv64.xml | 2 + tests/qemumonitorjsontest.c | 2 +- tests/virschematest.c | 4 + tools/Makefile.am | 3 +- tools/virsh-checkpoint.c | 1578 +++++++++++++++++ tools/virsh-completer.c | 52 +- tools/virsh-domain.c | 8 +- tools/virsh-snapshot.c | 2 +- tools/virsh-util.c | 11 + tools/virsh.c | 2 + tools/virsh.pod | 14 +- 86 files changed, 8674 insertions(+), 123 deletions(-) create mode 100644 include/libvirt/libvirt-domain-checkpoint.h create mode 100644 src/conf/checkpoint_conf.h create mode 100644 tools/virsh-checkpoint.h create mode 100644 docs/domainstatecapture.html.in create mode 100644 docs/formatcheckpoint.html.in create mode 100644 docs/schemas/domainbackup.rng create mode 100644 docs/schemas/domaincheckpoint.rng create mode 100644 src/conf/checkpoint_conf.c create mode 100644 src/libvirt-domain-checkpoint.c create mode 100644 tests/domainbackupxml2xmlin/backup-pull.xml create mode 100644 tests/domainbackupxml2xmlin/backup-push.xml create mode 100644 tests/domainbackupxml2xmlin/empty.xml create mode 100644 tests/domainbackupxml2xmlout/backup-pull.xml create mode 100644 tests/domainbackupxml2xmlout/backup-push.xml create mode 100644 tests/domainbackupxml2xmlout/empty.xml create mode 100644 tests/domaincheckpointxml2xmlin/empty.xml create mode 100644 tests/domaincheckpointxml2xmlin/sample.xml create mode 100644 tests/domaincheckpointxml2xmlout/empty.xml create mode 100644 tests/domaincheckpointxml2xmlout/sample.xml create mode 100644 tests/domaincheckpointxml2xmltest.c create mode 100644 tools/virsh-checkpoint.c -- 2.20.1

Upcoming patches plan to introduce virDomainCheckpointPtr as a new object for use in incremental backups, along with documentation on how incremental backups differ from snapshots. But first, we need to rename any existing mention of a 'system checkpoint' to instead be a 'full system snapshot', so that we aren't overloading the term checkpoint. Signed-off-by: Eric Blake <eblake@redhat.com> --- v2: wording improvements based on review --- include/libvirt/libvirt-domain-snapshot.h | 2 +- docs/formatsnapshot.html.in | 31 +++++++++++------------ src/conf/snapshot_conf.c | 4 +-- src/libvirt-domain-snapshot.c | 7 ++--- src/qemu/qemu_driver.c | 12 ++++----- tools/virsh-snapshot.c | 2 +- tools/virsh.pod | 14 +++++----- 7 files changed, 36 insertions(+), 36 deletions(-) diff --git a/include/libvirt/libvirt-domain-snapshot.h b/include/libvirt/libvirt-domain-snapshot.h index 0c9985f7f4..67dd0f62ca 100644 --- a/include/libvirt/libvirt-domain-snapshot.h +++ b/include/libvirt/libvirt-domain-snapshot.h @@ -59,7 +59,7 @@ typedef enum { VIR_DOMAIN_SNAPSHOT_CREATE_HALT = (1 << 3), /* Stop running guest after snapshot */ VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY = (1 << 4), /* disk snapshot, not - system checkpoint */ + full system */ VIR_DOMAIN_SNAPSHOT_CREATE_REUSE_EXT = (1 << 5), /* reuse any existing external files */ VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE = (1 << 6), /* use guest agent to diff --git a/docs/formatsnapshot.html.in b/docs/formatsnapshot.html.in index fbbecfd242..c60b4fb7c9 100644 --- a/docs/formatsnapshot.html.in +++ b/docs/formatsnapshot.html.in @@ -33,7 +33,7 @@ resume in a consistent state; but if the disks are modified externally in the meantime, this is likely to lead to data corruption.</dd> - <dt>system checkpoint</dt> + <dt>full system</dt> <dd>A combination of disk snapshots for all disks as well as VM memory state, which can be used to resume the guest from where it left off with symptoms similar to hibernation (that is, TCP @@ -55,11 +55,12 @@ as <code>virDomainSaveImageGetXMLDesc()</code> to work with those files. </p> - <p>System checkpoints are created - by <code>virDomainSnapshotCreateXML()</code> with no flags, and + <p>Full system snapshots are created + by <code>virDomainSnapshotCreateXML()</code> with no flags, while disk snapshots are created by the same function with - the <code>VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY</code> flag; in - both cases, they are restored by + the <code>VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY</code> + flag. Regardless of the flags provided, restoration of the + snapshot is handled by the <code>virDomainRevertToSnapshot()</code> function. For these types of snapshots, libvirt tracks each snapshot as a separate <code>virDomainSnapshotPtr</code> object, and maintains @@ -128,13 +129,10 @@ what file name is created in an external snapshot. On output, this is fully populated to show the state of each disk in the snapshot, including any properties that were generated by the - hypervisor defaults. For system checkpoints, this field is - ignored on input and omitted on output (a system checkpoint - implies that all disks participate in the snapshot process, - and since the current implementation only does internal system - checkpoints, there are no extra details to add); a future - release may allow the use of <code>disks</code> with a system - checkpoint. This element has a list of <code>disk</code> + hypervisor defaults. For full system snapshots, this field is + ignored on input and omitted on output (a full system snapshot + implies that all disks participate in the snapshot process). + This element has a list of <code>disk</code> sub-elements, describing anywhere from zero to all of the disks associated with the domain. <span class="since">Since 0.9.5</span> @@ -206,11 +204,12 @@ </dd> <dt><code>state</code></dt> <dd>The state of the domain at the time this snapshot was taken. - If the snapshot was created as a system checkpoint, then this - is the state of the domain at that time; when the domain is + If a full system snapshot was created, then this + is the state of the domain at that time. When the domain is reverted to this snapshot, the domain's state will default to - whatever is in this field unless additional flags are passed - to <code>virDomainRevertToSnapshot()</code>. Additionally, + this state, unless overridden + by <code>virDomainRevertToSnapshot()</code> flags to revert to + a running or paused state. Additionally, this field can be the value "disk-snapshot" (<span class="since">since 0.9.5</span>) when it represents only a disk snapshot (no VM memory state), and reverting to this diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c index b16f450a01..d0471c9f5b 100644 --- a/src/conf/snapshot_conf.c +++ b/src/conf/snapshot_conf.c @@ -1296,8 +1296,8 @@ virDomainSnapshotRedefinePrep(virDomainPtr domain, if ((other->def->state == VIR_DOMAIN_DISK_SNAPSHOT) != (def->state == VIR_DOMAIN_DISK_SNAPSHOT)) { virReportError(VIR_ERR_INVALID_ARG, - _("cannot change between disk snapshot and " - "system checkpoint in snapshot %s"), + _("cannot change between disk only and " + "full system in snapshot %s"), def->name); goto cleanup; } diff --git a/src/libvirt-domain-snapshot.c b/src/libvirt-domain-snapshot.c index 100326a5e7..1cc0188928 100644 --- a/src/libvirt-domain-snapshot.c +++ b/src/libvirt-domain-snapshot.c @@ -105,8 +105,9 @@ virDomainSnapshotGetConnect(virDomainSnapshotPtr snapshot) * contained in xmlDesc. * * If @flags is 0, the domain can be active, in which case the - * snapshot will be a system checkpoint (both disk state and runtime - * VM state such as RAM contents), where reverting to the snapshot is + * snapshot will be a full system snapshot (capturing both disk state, + * and runtime VM state such as RAM contents), where reverting to the + * snapshot is * the same as resuming from hibernation (TCP connections may have * timed out, but everything else picks up where it left off); or * the domain can be inactive, in which case the snapshot includes @@ -149,7 +150,7 @@ virDomainSnapshotGetConnect(virDomainSnapshotPtr snapshot) * is not paused while creating the snapshot. This increases the size * of the memory dump file, but reduces downtime of the guest while * taking the snapshot. Some hypervisors only support this flag during - * external checkpoints. + * external snapshots. * * If @flags includes VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY, then the * snapshot will be limited to the disks described in @xmlDesc, and no diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 427c1d02a8..2d39e9de96 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -2151,7 +2151,7 @@ qemuDomainReset(virDomainPtr dom, unsigned int flags) } -/* Count how many snapshots in a set are external snapshots or checkpoints. */ +/* Count how many snapshots in a set are external snapshots. */ static int qemuDomainSnapshotCountExternal(void *payload, const void *name ATTRIBUTE_UNUSED, @@ -15005,7 +15005,7 @@ qemuDomainSnapshotPrepare(virDomainObjPtr vm, if ((def->memory == VIR_DOMAIN_SNAPSHOT_LOCATION_INTERNAL && !found_internal) || (found_internal && forbid_internal)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("internal snapshots and checkpoints require all " + _("internal and full system snapshots require all " "disks to be selected for snapshot")); goto cleanup; } @@ -15455,7 +15455,7 @@ qemuDomainSnapshotCreateActiveExternal(virQEMUDriverPtr driver, if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_PMSUSPENDED) { pmsuspended = true; } else if (virDomainObjGetState(vm, NULL) == VIR_DOMAIN_RUNNING) { - /* For external checkpoints (those with memory), the guest + /* For full system external snapshots (those with memory), the guest * must pause (either by libvirt up front, or by qemu after * _LIVE converges). */ if (memory) @@ -15683,7 +15683,7 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain, redefine)) { virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", _("live snapshot creation is supported only " - "with external checkpoints")); + "during full system snapshots")); goto cleanup; } @@ -15803,12 +15803,12 @@ qemuDomainSnapshotCreateXML(virDomainPtr domain, } else if (virDomainObjIsActive(vm)) { if (flags & VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY || snap->def->memory == VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) { - /* external checkpoint or disk snapshot */ + /* external full system or disk snapshot */ if (qemuDomainSnapshotCreateActiveExternal(driver, vm, snap, flags) < 0) goto endjob; } else { - /* internal checkpoint */ + /* internal full system */ if (qemuDomainSnapshotCreateActiveInternal(driver, vm, snap, flags) < 0) goto endjob; diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c index 6d8e2b299b..1322a01038 100644 --- a/tools/virsh-snapshot.c +++ b/tools/virsh-snapshot.c @@ -1421,7 +1421,7 @@ static const vshCmdOptDef opts_snapshot_list[] = { }, {.name = "active", .type = VSH_OT_BOOL, - .help = N_("filter by snapshots taken while active (system checkpoints)") + .help = N_("filter by snapshots taken while active (full system snapshots)") }, {.name = "disk-only", .type = VSH_OT_BOOL, diff --git a/tools/virsh.pod b/tools/virsh.pod index 59a5900162..67a29630b5 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -4552,8 +4552,8 @@ If I<--halt> is specified, the domain will be left in an inactive state after the snapshot is created. If I<--disk-only> is specified, the snapshot will only include disk -state rather than the usual system checkpoint with vm state. Disk -snapshots are faster than full system checkpoints, but reverting to a +content rather than the usual full system snapshot with vm state. Disk +snapshots are captured faster than full system snapshots, but reverting to a disk snapshot may require fsck or journal replays, since it is like the disk state at the point when the power cord is abruptly pulled; and mixing I<--halt> and I<--disk-only> loses any data that was not @@ -4592,10 +4592,10 @@ this. If this flag is not specified, then some hypervisors may fail after partially performing the action, and B<dumpxml> must be used to see whether any partial changes occurred. -If I<--live> is specified, libvirt takes the snapshot (checkpoint) while +If I<--live> is specified, libvirt takes the snapshot while the guest is running. Both disk snapshot and domain memory snapshot are taken. This increases the size of the memory image of the external -checkpoint. This is currently supported only for external checkpoints. +snapshot. This is currently supported only for full system external snapshots. Existence of snapshot metadata will prevent attempts to B<undefine> a persistent domain. However, for transient domains, snapshot @@ -4615,7 +4615,7 @@ Otherwise, if I<--halt> is specified, the domain will be left in an inactive state after the snapshot is created, and if I<--disk-only> is specified, the snapshot will not include vm state. -The I<--memspec> option can be used to control whether a checkpoint +The I<--memspec> option can be used to control whether a full system snapshot is internal or external. The I<--memspec> flag is mandatory, followed by a B<memspec> of the form B<[file=]name[,snapshot=type]>, where type can be B<no>, B<internal>, or B<external>. To include a literal @@ -4623,7 +4623,7 @@ comma in B<file=name>, escape it with a second comma. I<--memspec> cannot be used together with I<--disk-only>. The I<--diskspec> option can be used to control how I<--disk-only> and -external checkpoints create external files. This option can occur +external full system snapshots create external files. 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[,snapshot=type][,driver=type][,file=name]>. A I<diskspec> @@ -4663,7 +4663,7 @@ see whether any partial changes occurred. 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 -checkpoint. This is currently supported only for external checkpoints. +snapshot. This is currently supported only for external full system snapshots. =item B<snapshot-current> I<domain> {[I<--name>] | [I<--security-info>] | [I<snapshotname>]} -- 2.20.1

On 2/6/19 2:17 PM, Eric Blake wrote:
Upcoming patches plan to introduce virDomainCheckpointPtr as a new object for use in incremental backups, along with documentation on how incremental backups differ from snapshots. But first, we need to rename any existing mention of a 'system checkpoint' to instead be a 'full system snapshot', so that we aren't overloading the term checkpoint.
Signed-off-by: Eric Blake <eblake@redhat.com>
--- v2: wording improvements based on review --- include/libvirt/libvirt-domain-snapshot.h | 2 +- docs/formatsnapshot.html.in | 31 +++++++++++------------ src/conf/snapshot_conf.c | 4 +-- src/libvirt-domain-snapshot.c | 7 ++--- src/qemu/qemu_driver.c | 12 ++++----- tools/virsh-snapshot.c | 2 +- tools/virsh.pod | 14 +++++----- 7 files changed, 36 insertions(+), 36 deletions(-)
Figured I'd start this and continue on Monday In my "scan" of this I found it "interesting" that eventually as we get closer to the user docs there's an emphasis to know the difference between external and internal snapshots... Reviewed-by: John Ferlan <jferlan@redhat.com> John

On 2/9/19 9:40 AM, John Ferlan wrote:
On 2/6/19 2:17 PM, Eric Blake wrote:
Upcoming patches plan to introduce virDomainCheckpointPtr as a new object for use in incremental backups, along with documentation on how incremental backups differ from snapshots. But first, we need to rename any existing mention of a 'system checkpoint' to instead be a 'full system snapshot', so that we aren't overloading the term checkpoint.
Signed-off-by: Eric Blake <eblake@redhat.com>
--- v2: wording improvements based on review --- include/libvirt/libvirt-domain-snapshot.h | 2 +- docs/formatsnapshot.html.in | 31 +++++++++++------------ src/conf/snapshot_conf.c | 4 +-- src/libvirt-domain-snapshot.c | 7 ++--- src/qemu/qemu_driver.c | 12 ++++----- tools/virsh-snapshot.c | 2 +- tools/virsh.pod | 14 +++++----- 7 files changed, 36 insertions(+), 36 deletions(-)
Figured I'd start this and continue on Monday
In my "scan" of this I found it "interesting" that eventually as we get closer to the user docs there's an emphasis to know the difference between external and internal snapshots...
Reviewed-by: John Ferlan <jferlan@redhat.com>
I think I'll go ahead and push this one now, regardless of the work remaining on the rest of the series, as it is a nice cleanup to have in the release. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

An upcoming patch wants to reuse XML parsing of both unix and tcp network host descriptions in the context of setting up a backup NBD server. Make that easier by refactoring the existing parser. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/domain_conf.h | 2 ++ src/conf/domain_conf.c | 43 ++++++++++++++++++++-------------------- src/libvirt_private.syms | 1 + 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 2bc3f879f7..5db4396fd5 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3170,6 +3170,8 @@ int virDomainDiskInsert(virDomainDefPtr def, ATTRIBUTE_RETURN_CHECK; void virDomainDiskInsertPreAlloced(virDomainDefPtr def, virDomainDiskDefPtr disk); +int virDomainStorageNetworkParseHost(xmlNodePtr hostnode, + virStorageNetHostDefPtr host); int virDomainDiskDefAssignAddress(virDomainXMLOptionPtr xmlopt, virDomainDiskDefPtr def, const virDomainDef *vmdef); diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 1fc4c8a35a..0221eb0634 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -7396,23 +7396,21 @@ virDomainHostdevSubsysPCIDefParseXML(xmlNodePtr node, } -static int +int virDomainStorageNetworkParseHost(xmlNodePtr hostnode, - virStorageNetHostDefPtr *hosts, - size_t *nhosts) + virStorageNetHostDefPtr host) { int ret = -1; char *transport = NULL; char *port = NULL; - virStorageNetHostDef host; - memset(&host, 0, sizeof(host)); - host.transport = VIR_STORAGE_NET_HOST_TRANS_TCP; + memset(host, 0, sizeof(*host)); + host->transport = VIR_STORAGE_NET_HOST_TRANS_TCP; /* transport can be tcp (default), unix or rdma. */ if ((transport = virXMLPropString(hostnode, "transport"))) { - host.transport = virStorageNetHostTransportTypeFromString(transport); - if (host.transport < 0) { + host->transport = virStorageNetHostTransportTypeFromString(transport); + if (host->transport < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("unknown protocol transport type '%s'"), transport); @@ -7420,17 +7418,17 @@ virDomainStorageNetworkParseHost(xmlNodePtr hostnode, } } - host.socket = virXMLPropString(hostnode, "socket"); + host->socket = virXMLPropString(hostnode, "socket"); - if (host.transport == VIR_STORAGE_NET_HOST_TRANS_UNIX && - host.socket == NULL) { + if (host->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX && + host->socket == NULL) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing socket for unix transport")); goto cleanup; } - if (host.transport != VIR_STORAGE_NET_HOST_TRANS_UNIX && - host.socket != NULL) { + if (host->transport != VIR_STORAGE_NET_HOST_TRANS_UNIX && + host->socket != NULL) { virReportError(VIR_ERR_XML_ERROR, _("transport '%s' does not support " "socket attribute"), @@ -7438,26 +7436,24 @@ virDomainStorageNetworkParseHost(xmlNodePtr hostnode, goto cleanup; } - if (host.transport != VIR_STORAGE_NET_HOST_TRANS_UNIX) { - if (!(host.name = virXMLPropString(hostnode, "name"))) { + if (host->transport != VIR_STORAGE_NET_HOST_TRANS_UNIX) { + if (!(host->name = virXMLPropString(hostnode, "name"))) { virReportError(VIR_ERR_XML_ERROR, "%s", _("missing name for host")); goto cleanup; } if ((port = virXMLPropString(hostnode, "port"))) { - if (virStringParsePort(port, &host.port) < 0) + if (virStringParsePort(port, &host->port) < 0) goto cleanup; } } - if (VIR_APPEND_ELEMENT(*hosts, *nhosts, host) < 0) - goto cleanup; - ret = 0; cleanup: - virStorageNetHostDefClear(&host); + if (ret < 0) + virStorageNetHostDefClear(host); VIR_FREE(transport); VIR_FREE(port); return ret; @@ -7474,9 +7470,14 @@ virDomainStorageNetworkParseHosts(xmlNodePtr node, for (child = node->children; child; child = child->next) { if (child->type == XML_ELEMENT_NODE && virXMLNodeNameEqual(child, "host")) { + virStorageNetHostDef host; - if (virDomainStorageNetworkParseHost(child, hosts, nhosts) < 0) + if (virDomainStorageNetworkParseHost(child, &host) < 0) return -1; + if (VIR_APPEND_ELEMENT(*hosts, *nhosts, host) < 0) { + virStorageNetHostDefClear(&host); + return -1; + } } } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6b401aa5e0..28a2ef707e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -555,6 +555,7 @@ virDomainStateReasonFromString; virDomainStateReasonToString; virDomainStateTypeFromString; virDomainStateTypeToString; +virDomainStorageNetworkParseHost; virDomainStorageSourceFormat; virDomainStorageSourceParse; virDomainTaintTypeFromString; -- 2.20.1

On 2/6/19 2:18 PM, Eric Blake wrote:
An upcoming patch wants to reuse XML parsing of both unix and tcp network host descriptions in the context of setting up a backup NBD server. Make that easier by refactoring the existing parser.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/domain_conf.h | 2 ++ src/conf/domain_conf.c | 43 ++++++++++++++++++++-------------------- src/libvirt_private.syms | 1 + 3 files changed, 25 insertions(+), 21 deletions(-)
Reviewed-by: John Ferlan <jferlan@redhat.com> John

Right now, we only use NBD exports during storage migration, where we control the NBD client and therefore don't care about the export name (qemu's default of naming the export after the device is fine). But upcoming patches for exposing backups over NBD wants to use a different export name (matching the libvirt domain XML rather than the qemu device name); so enhance the QMP glue code to allow this flexibility. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.h | 1 + src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_monitor.c | 5 +++-- src/qemu/qemu_monitor_json.c | 2 ++ tests/qemumonitorjsontest.c | 2 +- 6 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index fd7dcc9196..55acd60380 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -1099,6 +1099,7 @@ int qemuMonitorNBDServerStart(qemuMonitorPtr mon, const char *tls_alias); int qemuMonitorNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, + const char *export, bool writable); int qemuMonitorNBDServerStop(qemuMonitorPtr); int qemuMonitorGetTPMModels(qemuMonitorPtr mon, diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index 62772228fe..b105964ed6 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -465,6 +465,7 @@ int qemuMonitorJSONNBDServerStart(qemuMonitorPtr mon, const char *tls_alias); int qemuMonitorJSONNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, + const char *export, bool writable); int qemuMonitorJSONNBDServerStop(qemuMonitorPtr mon); int qemuMonitorJSONGetTPMModels(qemuMonitorPtr mon, diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index 1433b2c2f3..39a228d977 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -417,7 +417,7 @@ qemuMigrationDstStartNBDServer(virQEMUDriverPtr driver, goto exit_monitor; } - if (qemuMonitorNBDServerAdd(priv->mon, diskAlias, true) < 0) + if (qemuMonitorNBDServerAdd(priv->mon, diskAlias, NULL, true) < 0) goto exit_monitor; if (qemuDomainObjExitMonitor(driver, vm) < 0) goto cleanup; diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 8bd4d4d761..296563ce1c 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -3945,13 +3945,14 @@ qemuMonitorNBDServerStart(qemuMonitorPtr mon, int qemuMonitorNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, + const char *export, bool writable) { - VIR_DEBUG("deviceID=%s", deviceID); + VIR_DEBUG("deviceID=%s, export=%s", deviceID, NULLSTR(export)); QEMU_CHECK_MONITOR(mon); - return qemuMonitorJSONNBDServerAdd(mon, deviceID, writable); + return qemuMonitorJSONNBDServerAdd(mon, deviceID, export, writable); } diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 8bafa93c8d..37626b64ea 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -6767,6 +6767,7 @@ qemuMonitorJSONNBDServerStart(qemuMonitorPtr mon, int qemuMonitorJSONNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, + const char *export, bool writable) { int ret = -1; @@ -6775,6 +6776,7 @@ qemuMonitorJSONNBDServerAdd(qemuMonitorPtr mon, if (!(cmd = qemuMonitorJSONMakeCommand("nbd-server-add", "s:device", deviceID, + "S:name", export, "b:writable", writable, NULL))) return ret; diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index 1a8a31717f..1bdef11d15 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -1354,7 +1354,7 @@ GEN_TEST_FUNC(qemuMonitorJSONDrivePivot, "vdb") GEN_TEST_FUNC(qemuMonitorJSONScreendump, "devicename", 1, "/foo/bar") GEN_TEST_FUNC(qemuMonitorJSONOpenGraphics, "spice", "spicefd", false) GEN_TEST_FUNC(qemuMonitorJSONNBDServerStart, "localhost", 12345, "test-alias") -GEN_TEST_FUNC(qemuMonitorJSONNBDServerAdd, "vda", true) +GEN_TEST_FUNC(qemuMonitorJSONNBDServerAdd, "vda", NULL, true) GEN_TEST_FUNC(qemuMonitorJSONDetachCharDev, "serial1") GEN_TEST_FUNC(qemuMonitorJSONBlockdevTrayOpen, "foodev", true) GEN_TEST_FUNC(qemuMonitorJSONBlockdevTrayClose, "foodev") -- 2.20.1

On 2/6/19 2:18 PM, Eric Blake wrote:
Right now, we only use NBD exports during storage migration, where we control the NBD client and therefore don't care about the export name (qemu's default of naming the export after the device is fine). But upcoming patches for exposing backups over NBD wants to use a different export name (matching the libvirt domain XML rather than the qemu device name); so enhance the QMP glue code to allow this flexibility.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.h | 1 + src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_monitor.c | 5 +++-- src/qemu/qemu_monitor_json.c | 2 ++ tests/qemumonitorjsontest.c | 2 +- 6 files changed, 9 insertions(+), 4 deletions(-)
I assume/hope some day a test will be added that will show what the @export variable does. I haven't read ahead at all - I'm trying to just go in order and get some feedback sooner so you can start pushing stuff. Reviewed-by: John Ferlan <jferlan@redhat.com> John

On 2/9/19 9:41 AM, John Ferlan wrote:
On 2/6/19 2:18 PM, Eric Blake wrote:
Right now, we only use NBD exports during storage migration, where we control the NBD client and therefore don't care about the export name (qemu's default of naming the export after the device is fine). But upcoming patches for exposing backups over NBD wants to use a different export name (matching the libvirt domain XML rather than the qemu device name); so enhance the QMP glue code to allow this flexibility.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.h | 1 + src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_monitor.c | 5 +++-- src/qemu/qemu_monitor_json.c | 2 ++ tests/qemumonitorjsontest.c | 2 +- 6 files changed, 9 insertions(+), 4 deletions(-)
I assume/hope some day a test will be added that will show what the @export variable does. I haven't read ahead at all - I'm trying to just go in order and get some feedback sooner so you can start pushing stuff.
Reviewed-by: John Ferlan <jferlan@redhat.com>
Yes, I need to do better at in-tree tests (and not just stuff I've mentioned in the commit messages) for my v5. The export name gets used during pull-style backups, where I purposefully went with naming the exports 'vda' and 'vdb' for a guest with two disks, rather than based on any file name or block node name; 'qemu-nbd --list' connected to the NBD server during a pull backup is an easy way of testing the export names made it through the stack. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

On 2/25/19 9:53 AM, Eric Blake wrote:
On 2/9/19 9:41 AM, John Ferlan wrote:
On 2/6/19 2:18 PM, Eric Blake wrote:
Right now, we only use NBD exports during storage migration, where we control the NBD client and therefore don't care about the export name (qemu's default of naming the export after the device is fine). But upcoming patches for exposing backups over NBD wants to use a different export name (matching the libvirt domain XML rather than the qemu device name); so enhance the QMP glue code to allow this flexibility.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.h | 1 + src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_monitor.c | 5 +++-- src/qemu/qemu_monitor_json.c | 2 ++ tests/qemumonitorjsontest.c | 2 +- 6 files changed, 9 insertions(+), 4 deletions(-)
I assume/hope some day a test will be added that will show what the @export variable does. I haven't read ahead at all - I'm trying to just go in order and get some feedback sooner so you can start pushing stuff.
Reviewed-by: John Ferlan <jferlan@redhat.com>
Yes, I need to do better at in-tree tests (and not just stuff I've mentioned in the commit messages) for my v5. The export name gets used during pull-style backups, where I purposefully went with naming the exports 'vda' and 'vdb' for a guest with two disks, rather than based on any file name or block node name; 'qemu-nbd --list' connected to the NBD server during a pull backup is an easy way of testing the export names made it through the stack.
For that matter, I'm probably just going to squash this patch in with 15/20, and do all the QMP monitor changes in one patch. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

Upcoming patches will add support for incremental backups via a new API; but first, we need 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> --- v2: wording improvements based on review --- docs/docs.html.in | 5 + docs/domainstatecapture.html.in | 314 ++++++++++++++++++++++++++++++++ docs/formatsnapshot.html.in | 2 + 3 files changed, 321 insertions(+) create mode 100644 docs/domainstatecapture.html.in diff --git a/docs/docs.html.in b/docs/docs.html.in index 40e0e3b82e..4c46b74980 100644 --- a/docs/docs.html.in +++ b/docs/docs.html.in @@ -120,6 +120,11 @@ <dt><a href="secureusage.html">Secure usage</a></dt> <dd>Secure usage of the libvirt APIs</dd> + + <dt><a href="domainstatecapture.html">Domain state + capture</a></dt> + <dd>Comparison between different methods of capturing domain + state</dd> </dl> </div> diff --git a/docs/domainstatecapture.html.in b/docs/domainstatecapture.html.in new file mode 100644 index 0000000000..f7f2fe0b98 --- /dev/null +++ b/docs/domainstatecapture.html.in @@ -0,0 +1,314 @@ +<?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 with disks restored to that + state. Some of the topics presented below may relate to inactive + state collection, but it is not the primary focus of this page. + </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 + correpsonding 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>virDomainManagedSave</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>virDomainSave</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>virDomainSnapshotCreateXML()</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. Since creating an external snapshot + changes which disk image resource is in use by the guest, this + API can be coupled with <code>virDomainBlockCommit()</code> 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>virDomainFSFreeze(), virDomainFSThaw()</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>virDomainBlockCopy()</dt> + <dd>This API wraps approaches for capturing the disk state (but + not memory) of a running guest, but does not track + accompanying guest memory state, but can only operate on one + block device per job. To get a consistent copy of multiple + disks, multiple jobs just be run in parallel, then the domain + must be paused before ending all of the jobs. The capture is + consistent only at the end of the operation with a choice for + future guest changes to either pivot to the new file or to + resume to just using the original file. The resulting backup + file is thus the other file no longer in use by the + guest.</dd> + + <dt>virDomainCheckpointCreateXML()</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>, 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>virDomainBackupBegin(), virDomainBackupEnd()</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 +virDomainBackeupEnd() + </pre></p> + + </body> +</html> diff --git a/docs/formatsnapshot.html.in b/docs/formatsnapshot.html.in index c60b4fb7c9..9ee355198f 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> -- 2.20.1

On 2/6/19 2:18 PM, Eric Blake wrote:
Upcoming patches will add support for incremental backups via a new API; but first, we need 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>
--- v2: wording improvements based on review --- docs/docs.html.in | 5 + docs/domainstatecapture.html.in | 314 ++++++++++++++++++++++++++++++++ docs/formatsnapshot.html.in | 2 + 3 files changed, 321 insertions(+) create mode 100644 docs/domainstatecapture.html.in
Anything inside [...] are my wise-ass comments. I couldn't help myself and hopefully they don't offend anyone ;-)
diff --git a/docs/docs.html.in b/docs/docs.html.in index 40e0e3b82e..4c46b74980 100644 --- a/docs/docs.html.in +++ b/docs/docs.html.in @@ -120,6 +120,11 @@
<dt><a href="secureusage.html">Secure usage</a></dt> <dd>Secure usage of the libvirt APIs</dd> + + <dt><a href="domainstatecapture.html">Domain state + capture</a></dt> + <dd>Comparison between different methods of capturing domain + state</dd> </dl> </div>
diff --git a/docs/domainstatecapture.html.in b/docs/domainstatecapture.html.in new file mode 100644 index 0000000000..f7f2fe0b98 --- /dev/null +++ b/docs/domainstatecapture.html.in @@ -0,0 +1,314 @@ +<?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 with disks restored to that + state. Some of the topics presented below may relate to inactive + state collection, but it is not the primary focus of this page. + </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
s/, and/ and/
+ guest's view of the disks are backed by local storage on the + host, or by the hypervisor accessing shared storage over a
s/, or/ or/
+ 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
s/, or/ or/
+ files, and may require synchronization with third-party tools
s/, and/ and/
+ built around managing the amount of data resulting from + capturing the state of multiple guests that each use multiple + disks.
[and the smart-ass observer will note all these combinations are very hard for engineering to get just right, so don't whine when you try some edge condition thing and it doesn't work exactly like you expected ;-)]
+ </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 + correpsonding to something the guest was actually executing,
corresponding executing.
+ capturing state tends to focus on minimizing guest downtime
Capturing
+ 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>
[so if you have a 1 Mb/s connection, then you will be very susceptible for issues related to duration. So don't try migrating ;-)]
+ + <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.
from a partial state
+ </dd>
[and definitely don't try to migrate your multi GB guest with mulit Tb storage over that 1 Mb/s connection - you may be able to take a week long vacation before the migration completes ;-)]
+ + <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>
[lucky mgmt application - let's hope all the I/O got to all those files and well they are named appropriately so that someone doesn't delete one or more of them outside the mgmt applications control 8-/]
+ + <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>
[so if you're taking a snapshot, for the love of your favorite diety, don't make other changes that will be incompatible]
+ + <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
s/, and/ and/
+ 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>
[oy - good luck on anything remote]
+ + <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>
[so be sure to always tip your bartender... err network manager... really, really well - because you are going to need bandwidth that is wider than the drink stirrer straws.]
+ </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
stopped. The
+ migration started are then transferred, and the guest is started
s/, and/ and/
+ 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
[absolute emphasis on minimal]
+ 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
are related
+ state capture have these properties:</p> + <dl> + <dt>virDomainManagedSave</dt> + <dd>This API saves guest memory, with libvirt managing all of
s/, with/ with/
+ the saved state, then stops the guest. While stopped, the
s/state, then/state and/
+ 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
s/, and since/. Since/
+ 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
s/, and/ and/
+ itself well to live backups.</dd>
s/to/for usage for/
+ + <dt>virDomainSave</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>virDomainSnapshotCreateXML()</dt> + <dd>This API wraps several approaches for capturing guest state,
s/state,/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
s/state,/state/
+ 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. Since creating an external snapshot + changes which disk image resource is in use by the guest, this + API can be coupled with <code>virDomainBlockCommit()</code> 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>virDomainFSFreeze(), virDomainFSThaw()</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
s/, and/ and/
+ 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,
s/parameter,/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
s/agent,/agent/
+ guests place maximum time limits on how long I/O can be + frozen.</dd> + + <dt>virDomainBlockCopy()</dt> + <dd>This API wraps approaches for capturing the disk state (but + not memory) of a running guest, but does not track + accompanying guest memory state, but can only operate on one + block device per job. To get a consistent copy of multiple
too many "buts"... s/state, but/state. This API/
+ disks, multiple jobs just be run in parallel, then the domain
s/disks,/disks/ s/parallel,/parallel and/
+ must be paused before ending all of the jobs. The capture is + consistent only at the end of the operation with a choice for + future guest changes to either pivot to the new file or to + resume to just using the original file. The resulting backup + file is thus the other file no longer in use by the + guest.</dd> + + <dt>virDomainCheckpointCreateXML()</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
s/,/;/
+ a checkpoint as a side-effect of starting a new incremental + backup with <code>virDomainBackupBegin()</code>, since a
s/,//
+ 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>virDomainBackupBegin(), virDomainBackupEnd()</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
s/,//
+ 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 +virDomainBackeupEnd() + </pre></p> + + </body> +</html> diff --git a/docs/formatsnapshot.html.in b/docs/formatsnapshot.html.in index c60b4fb7c9..9ee355198f 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>
Reviewed-by: John Ferlan <jferlan@redhat.com> John

Prepare for introducing a bunch of new public APIs related to backup checkpoints by first introducing a new internal type and errors associated with that type. 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. Signed-off-by: Eric Blake <eblake@redhat.com> --- v2: fix copy-and-paste issue in virerror.c [John] --- include/libvirt/virterror.h | 7 +++-- src/util/virerror.c | 12 ++++++- include/libvirt/libvirt.h | 2 ++ src/datatypes.h | 31 ++++++++++++++++++- src/datatypes.c | 62 ++++++++++++++++++++++++++++++++++++- src/libvirt_private.syms | 2 ++ 6 files changed, 111 insertions(+), 5 deletions(-) diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h index 3c19ff5e2e..acbf03d0ea 100644 --- a/include/libvirt/virterror.h +++ b/include/libvirt/virterror.h @@ -4,7 +4,7 @@ * Description: Provides the interfaces of the libvirt library to handle * errors raised while using the library. * - * Copyright (C) 2006-2016 Red Hat, Inc. + * 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 @@ -132,6 +132,7 @@ typedef enum { VIR_FROM_LIBSSH = 66, /* Error from libssh connection transport */ VIR_FROM_RESCTRL = 67, /* Error from resource control */ VIR_FROM_FIREWALLD = 68, /* Error from firewalld */ + VIR_FROM_DOMAIN_CHECKPOINT = 69,/* Error from domain checkpoint */ # ifdef VIR_ENUM_SENTINELS VIR_ERR_DOMAIN_LAST @@ -322,11 +323,13 @@ typedef enum { VIR_ERR_DEVICE_MISSING = 99, /* fail to find the desired device */ VIR_ERR_INVALID_NWFILTER_BINDING = 100, /* invalid nwfilter binding */ VIR_ERR_NO_NWFILTER_BINDING = 101, /* no nwfilter binding */ + VIR_ERR_INVALID_DOMAIN_CHECKPOINT = 102,/* invalid domain checkpoint */ + VIR_ERR_NO_DOMAIN_CHECKPOINT = 103, /* domain checkpoint not found */ + VIR_ERR_NO_DOMAIN_BACKUP = 104, /* domain backup job id not found */ # ifdef VIR_ENUM_SENTINELS VIR_ERR_NUMBER_LAST # endif - } virErrorNumber; /** diff --git a/src/util/virerror.c b/src/util/virerror.c index 63de0cb278..325e8df346 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -1,7 +1,7 @@ /* * virerror.c: error handling and reporting code for libvirt * - * Copyright (C) 2006, 2008-2016 Red Hat, Inc. + * Copyright (C) 2006, 2008-2018 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 @@ -139,6 +139,7 @@ VIR_ENUM_IMPL(virErrorDomain, VIR_ERR_DOMAIN_LAST, "Libssh transport layer", "Resource control", "FirewallD", + "Domain Checkpoint", ); @@ -1214,6 +1215,15 @@ const virErrorMsgTuple virErrorMsgStrings[VIR_ERR_NUMBER_LAST] = { [VIR_ERR_NO_NWFILTER_BINDING] = { N_("Network filter binding not found"), N_("Network filter binding not found: %s") }, + [VIR_ERR_INVALID_DOMAIN_CHECKPOINT] = { + N_("Invalid checkpoint"), + N_("Invalid checkpoint: %s") }, + [VIR_ERR_NO_DOMAIN_CHECKPOINT] = { + N_("Domain checkpoint not found"), + N_("Domain checkpoint not found: %s") }, + [VIR_ERR_NO_DOMAIN_BACKUP] = { + N_("Domain backup job id not found"), + N_("Domain backup job id not found: %s") }, }; diff --git a/include/libvirt/libvirt.h b/include/libvirt/libvirt.h index 20e5d276a7..b7238bd96e 100644 --- a/include/libvirt/libvirt.h +++ b/include/libvirt/libvirt.h @@ -34,6 +34,8 @@ extern "C" { # include <libvirt/libvirt-common.h> # include <libvirt/libvirt-host.h> # include <libvirt/libvirt-domain.h> +typedef struct _virDomainCheckpoint virDomainCheckpoint; +typedef virDomainCheckpoint *virDomainCheckpointPtr; # include <libvirt/libvirt-domain-snapshot.h> # include <libvirt/libvirt-event.h> # include <libvirt/libvirt-interface.h> diff --git a/src/datatypes.h b/src/datatypes.h index 529b340587..f42ad5f989 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -1,7 +1,7 @@ /* * datatypes.h: management of structs for public data types * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 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 @@ -31,6 +31,7 @@ extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; +extern virClassPtr virDomainCheckpointClass; extern virClassPtr virDomainSnapshotClass; extern virClassPtr virInterfaceClass; extern virClassPtr virNetworkClass; @@ -307,6 +308,21 @@ extern virClassPtr virAdmClientClass; } \ } while (0) +# define virCheckDomainCheckpointReturn(obj, retval) \ + do { \ + virDomainCheckpointPtr _check = (obj); \ + if (!virObjectIsClass(_check, virDomainCheckpointClass) || \ + !virObjectIsClass(_check->domain, virDomainClass) || \ + !virObjectIsClass(_check->domain->conn, virConnectClass)) { \ + virReportErrorHelper(VIR_FROM_DOMAIN_CHECKPOINT, \ + VIR_ERR_INVALID_DOMAIN_CHECKPOINT, \ + __FILE__, __FUNCTION__, __LINE__, \ + __FUNCTION__); \ + virDispatchError(NULL); \ + return retval; \ + } \ + } while (0) + /* Helper macros to implement VIR_DOMAIN_DEBUG using just C99. This * assumes you pass fewer than 15 arguments to VIR_DOMAIN_DEBUG, but @@ -667,6 +683,17 @@ struct _virStream { void *privateData; }; +/** + * _virDomainCheckpoint + * + * Internal structure associated with a domain checkpoint + */ +struct _virDomainCheckpoint { + virObject parent; + char *name; + virDomainPtr domain; +}; + /** * _virDomainSnapshot * @@ -743,6 +770,8 @@ virNWFilterPtr virGetNWFilter(virConnectPtr conn, virNWFilterBindingPtr virGetNWFilterBinding(virConnectPtr conn, const char *portdev, const char *filtername); +virDomainCheckpointPtr virGetDomainCheckpoint(virDomainPtr domain, + const char *name); virDomainSnapshotPtr virGetDomainSnapshot(virDomainPtr domain, const char *name); diff --git a/src/datatypes.c b/src/datatypes.c index 09f63d9e15..68f7d86661 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -1,7 +1,7 @@ /* * datatypes.c: management of structs for public data types * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 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 @@ -36,6 +36,7 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; +virClassPtr virDomainCheckpointClass; virClassPtr virDomainSnapshotClass; virClassPtr virInterfaceClass; virClassPtr virNetworkClass; @@ -50,6 +51,7 @@ virClassPtr virStoragePoolClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); +static void virDomainCheckpointDispose(void *obj); static void virDomainSnapshotDispose(void *obj); static void virInterfaceDispose(void *obj); static void virNetworkDispose(void *obj); @@ -86,6 +88,7 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); + DECLARE_CLASS(virDomainCheckpoint); DECLARE_CLASS(virDomainSnapshot); DECLARE_CLASS(virInterface); DECLARE_CLASS(virNetwork); @@ -955,6 +958,63 @@ virDomainSnapshotDispose(void *obj) } +/** + * virGetDomainCheckpoint: + * @domain: the domain to checkpoint + * @name: pointer to the domain checkpoint name + * + * Allocates a new domain checkpoint object. When the object is no longer needed, + * virObjectUnref() must be called in order to not leak data. + * + * Returns a pointer to the domain checkpoint object, or NULL on error. + */ +virDomainCheckpointPtr +virGetDomainCheckpoint(virDomainPtr domain, const char *name) +{ + virDomainCheckpointPtr ret = NULL; + + if (virDataTypesInitialize() < 0) + return NULL; + + virCheckDomainGoto(domain, error); + virCheckNonNullArgGoto(name, error); + + if (!(ret = virObjectNew(virDomainCheckpointClass))) + goto error; + if (VIR_STRDUP(ret->name, name) < 0) + goto error; + + ret->domain = virObjectRef(domain); + + return ret; + + error: + virObjectUnref(ret); + return NULL; +} + + +/** + * virDomainCheckpointDispose: + * @obj: the domain checkpoint to release + * + * Unconditionally release all memory associated with a checkpoint. + * The checkpoint object must not be used once this method returns. + * + * It will also unreference the associated connection object, + * which may also be released if its ref count hits zero. + */ +static void +virDomainCheckpointDispose(void *obj) +{ + virDomainCheckpointPtr checkpoint = obj; + VIR_DEBUG("release checkpoint %p %s", checkpoint, checkpoint->name); + + VIR_FREE(checkpoint->name); + virObjectUnref(checkpoint->domain); +} + + virAdmConnectPtr virAdmConnectNew(void) { diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 28a2ef707e..5e22acb059 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1219,10 +1219,12 @@ virConnectCloseCallbackDataClass; virConnectCloseCallbackDataGetCallback; virConnectCloseCallbackDataRegister; virConnectCloseCallbackDataUnregister; +virDomainCheckpointClass; virDomainClass; virDomainSnapshotClass; virGetConnect; virGetDomain; +virGetDomainCheckpoint; virGetDomainSnapshot; virGetInterface; virGetNetwork; -- 2.20.1

On 2/6/19 2:18 PM, Eric Blake wrote:
Prepare for introducing a bunch of new public APIs related to backup checkpoints by first introducing a new internal type and errors associated with that type. 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.
Signed-off-by: Eric Blake <eblake@redhat.com>
--- v2: fix copy-and-paste issue in virerror.c [John] --- include/libvirt/virterror.h | 7 +++-- src/util/virerror.c | 12 ++++++- include/libvirt/libvirt.h | 2 ++ src/datatypes.h | 31 ++++++++++++++++++- src/datatypes.c | 62 ++++++++++++++++++++++++++++++++++++- src/libvirt_private.syms | 2 ++ 6 files changed, 111 insertions(+), 5 deletions(-)
Naturally something changed recently in datatypes.h... so you will have a merge conflict here.
diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h index 3c19ff5e2e..acbf03d0ea 100644 --- a/include/libvirt/virterror.h +++ b/include/libvirt/virterror.h @@ -4,7 +4,7 @@ * Description: Provides the interfaces of the libvirt library to handle * errors raised while using the library. * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2019 Red Hat, Inc.
I'll let someone else complain about your emacs macro ;-)
* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -132,6 +132,7 @@ typedef enum { VIR_FROM_LIBSSH = 66, /* Error from libssh connection transport */ VIR_FROM_RESCTRL = 67, /* Error from resource control */ VIR_FROM_FIREWALLD = 68, /* Error from firewalld */ + VIR_FROM_DOMAIN_CHECKPOINT = 69,/* Error from domain checkpoint */
# ifdef VIR_ENUM_SENTINELS VIR_ERR_DOMAIN_LAST @@ -322,11 +323,13 @@ typedef enum { VIR_ERR_DEVICE_MISSING = 99, /* fail to find the desired device */ VIR_ERR_INVALID_NWFILTER_BINDING = 100, /* invalid nwfilter binding */ VIR_ERR_NO_NWFILTER_BINDING = 101, /* no nwfilter binding */ + VIR_ERR_INVALID_DOMAIN_CHECKPOINT = 102,/* invalid domain checkpoint */ + VIR_ERR_NO_DOMAIN_CHECKPOINT = 103, /* domain checkpoint not found */ + VIR_ERR_NO_DOMAIN_BACKUP = 104, /* domain backup job id not found */
# ifdef VIR_ENUM_SENTINELS VIR_ERR_NUMBER_LAST # endif - } virErrorNumber;
/** diff --git a/src/util/virerror.c b/src/util/virerror.c index 63de0cb278..325e8df346 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -1,7 +1,7 @@ /* * virerror.c: error handling and reporting code for libvirt * - * Copyright (C) 2006, 2008-2016 Red Hat, Inc. + * Copyright (C) 2006, 2008-2018 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 @@ -139,6 +139,7 @@ VIR_ENUM_IMPL(virErrorDomain, VIR_ERR_DOMAIN_LAST, "Libssh transport layer", "Resource control", "FirewallD", + "Domain Checkpoint", );
@@ -1214,6 +1215,15 @@ const virErrorMsgTuple virErrorMsgStrings[VIR_ERR_NUMBER_LAST] = { [VIR_ERR_NO_NWFILTER_BINDING] = { N_("Network filter binding not found"), N_("Network filter binding not found: %s") }, + [VIR_ERR_INVALID_DOMAIN_CHECKPOINT] = { + N_("Invalid checkpoint"), + N_("Invalid checkpoint: %s") },
Invalid domain checkpoint
+ [VIR_ERR_NO_DOMAIN_CHECKPOINT] = { + N_("Domain checkpoint not found"), + N_("Domain checkpoint not found: %s") }, + [VIR_ERR_NO_DOMAIN_BACKUP] = { + N_("Domain backup job id not found"), + N_("Domain backup job id not found: %s") }, };
diff --git a/include/libvirt/libvirt.h b/include/libvirt/libvirt.h index 20e5d276a7..b7238bd96e 100644 --- a/include/libvirt/libvirt.h +++ b/include/libvirt/libvirt.h @@ -34,6 +34,8 @@ extern "C" { # include <libvirt/libvirt-common.h> # include <libvirt/libvirt-host.h> # include <libvirt/libvirt-domain.h> +typedef struct _virDomainCheckpoint virDomainCheckpoint; +typedef virDomainCheckpoint *virDomainCheckpointPtr;
This is a weird construct for this file... I think this should be in a libvirt-domain-checkpoint.h type file which I see is generated a few patches later and these lines moved. In the long run I guess as long as it's in a file it doesn't matter.
# include <libvirt/libvirt-domain-snapshot.h> # include <libvirt/libvirt-event.h> # include <libvirt/libvirt-interface.h> diff --git a/src/datatypes.h b/src/datatypes.h index 529b340587..f42ad5f989 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -1,7 +1,7 @@ /* * datatypes.h: management of structs for public data types * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 Red Hat, Inc.
haha 2018 - I know when you last touched this ;-)
* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -31,6 +31,7 @@
extern virClassPtr virConnectClass; extern virClassPtr virDomainClass; +extern virClassPtr virDomainCheckpointClass; extern virClassPtr virDomainSnapshotClass; extern virClassPtr virInterfaceClass; extern virClassPtr virNetworkClass; @@ -307,6 +308,21 @@ extern virClassPtr virAdmClientClass; } \ } while (0)
+# define virCheckDomainCheckpointReturn(obj, retval) \ + do { \ + virDomainCheckpointPtr _check = (obj); \ + if (!virObjectIsClass(_check, virDomainCheckpointClass) || \ + !virObjectIsClass(_check->domain, virDomainClass) || \ + !virObjectIsClass(_check->domain->conn, virConnectClass)) { \ + virReportErrorHelper(VIR_FROM_DOMAIN_CHECKPOINT, \ + VIR_ERR_INVALID_DOMAIN_CHECKPOINT, \ + __FILE__, __FUNCTION__, __LINE__, \ + __FUNCTION__); \ + virDispatchError(NULL); \ + return retval; \ + } \ + } while (0) +
/* Helper macros to implement VIR_DOMAIN_DEBUG using just C99. This * assumes you pass fewer than 15 arguments to VIR_DOMAIN_DEBUG, but @@ -667,6 +683,17 @@ struct _virStream { void *privateData; };
+/** + * _virDomainCheckpoint + * + * Internal structure associated with a domain checkpoint + */ +struct _virDomainCheckpoint { + virObject parent; + char *name; + virDomainPtr domain; +}; + /** * _virDomainSnapshot * @@ -743,6 +770,8 @@ virNWFilterPtr virGetNWFilter(virConnectPtr conn, virNWFilterBindingPtr virGetNWFilterBinding(virConnectPtr conn, const char *portdev, const char *filtername); +virDomainCheckpointPtr virGetDomainCheckpoint(virDomainPtr domain, + const char *name); virDomainSnapshotPtr virGetDomainSnapshot(virDomainPtr domain, const char *name);
diff --git a/src/datatypes.c b/src/datatypes.c index 09f63d9e15..68f7d86661 100644 --- a/src/datatypes.c +++ b/src/datatypes.c @@ -1,7 +1,7 @@ /* * datatypes.c: management of structs for public data types * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 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 @@ -36,6 +36,7 @@ VIR_LOG_INIT("datatypes"); virClassPtr virConnectClass; virClassPtr virConnectCloseCallbackDataClass; virClassPtr virDomainClass; +virClassPtr virDomainCheckpointClass; virClassPtr virDomainSnapshotClass; virClassPtr virInterfaceClass; virClassPtr virNetworkClass; @@ -50,6 +51,7 @@ virClassPtr virStoragePoolClass; static void virConnectDispose(void *obj); static void virConnectCloseCallbackDataDispose(void *obj); static void virDomainDispose(void *obj); +static void virDomainCheckpointDispose(void *obj); static void virDomainSnapshotDispose(void *obj); static void virInterfaceDispose(void *obj); static void virNetworkDispose(void *obj); @@ -86,6 +88,7 @@ virDataTypesOnceInit(void) DECLARE_CLASS_LOCKABLE(virConnect); DECLARE_CLASS_LOCKABLE(virConnectCloseCallbackData); DECLARE_CLASS(virDomain); + DECLARE_CLASS(virDomainCheckpoint); DECLARE_CLASS(virDomainSnapshot); DECLARE_CLASS(virInterface); DECLARE_CLASS(virNetwork); @@ -955,6 +958,63 @@ virDomainSnapshotDispose(void *obj) }
+/** + * virGetDomainCheckpoint: + * @domain: the domain to checkpoint + * @name: pointer to the domain checkpoint name + * + * Allocates a new domain checkpoint object. When the object is no longer needed, + * virObjectUnref() must be called in order to not leak data. + * + * Returns a pointer to the domain checkpoint object, or NULL on error. + */ +virDomainCheckpointPtr +virGetDomainCheckpoint(virDomainPtr domain, const char *name)
More recently we or I have been trying to enforce one line per argument for new code or changed methods.
+{ + virDomainCheckpointPtr ret = NULL; + + if (virDataTypesInitialize() < 0) + return NULL; + + virCheckDomainGoto(domain, error); + virCheckNonNullArgGoto(name, error); + + if (!(ret = virObjectNew(virDomainCheckpointClass))) + goto error;
Could return NULL too, but it is a copy of virGetDomainSnapshot Reviewed-by: John Ferlan <jferlan@redhat.com> John
+ if (VIR_STRDUP(ret->name, name) < 0) + goto error; + + ret->domain = virObjectRef(domain); + + return ret; + + error: + virObjectUnref(ret); + return NULL; +} + + +/** + * virDomainCheckpointDispose: + * @obj: the domain checkpoint to release + * + * Unconditionally release all memory associated with a checkpoint. + * The checkpoint object must not be used once this method returns. + * + * It will also unreference the associated connection object, + * which may also be released if its ref count hits zero. + */ +static void +virDomainCheckpointDispose(void *obj) +{ + virDomainCheckpointPtr checkpoint = obj; + VIR_DEBUG("release checkpoint %p %s", checkpoint, checkpoint->name); + + VIR_FREE(checkpoint->name); + virObjectUnref(checkpoint->domain); +} + + virAdmConnectPtr virAdmConnectNew(void) { diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 28a2ef707e..5e22acb059 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1219,10 +1219,12 @@ virConnectCloseCallbackDataClass; virConnectCloseCallbackDataGetCallback; virConnectCloseCallbackDataRegister; virConnectCloseCallbackDataUnregister; +virDomainCheckpointClass; virDomainClass; virDomainSnapshotClass; virGetConnect; virGetDomain; +virGetDomainCheckpoint; virGetDomainSnapshot; virGetInterface; virGetNetwork;

On 2/9/19 9:48 AM, John Ferlan wrote:
On 2/6/19 2:18 PM, Eric Blake wrote:
Prepare for introducing a bunch of new public APIs related to backup checkpoints by first introducing a new internal type and errors associated with that type. 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.
Signed-off-by: Eric Blake <eblake@redhat.com>
--- v2: fix copy-and-paste issue in virerror.c [John] --- include/libvirt/virterror.h | 7 +++-- src/util/virerror.c | 12 ++++++- include/libvirt/libvirt.h | 2 ++ src/datatypes.h | 31 ++++++++++++++++++- src/datatypes.c | 62 ++++++++++++++++++++++++++++++++++++- src/libvirt_private.syms | 2 ++ 6 files changed, 111 insertions(+), 5 deletions(-)
Naturally something changed recently in datatypes.h... so you will have a merge conflict here.
Yeah, a long-running issue. v5 will be rebased yet again.
diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h index 3c19ff5e2e..acbf03d0ea 100644 --- a/include/libvirt/virterror.h +++ b/include/libvirt/virterror.h @@ -4,7 +4,7 @@ * Description: Provides the interfaces of the libvirt library to handle * errors raised while using the library. * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2019 Red Hat, Inc.
I'll let someone else complain about your emacs macro ;-)
--- a/include/libvirt/libvirt.h +++ b/include/libvirt/libvirt.h @@ -34,6 +34,8 @@ extern "C" { # include <libvirt/libvirt-common.h> # include <libvirt/libvirt-host.h> # include <libvirt/libvirt-domain.h> +typedef struct _virDomainCheckpoint virDomainCheckpoint; +typedef virDomainCheckpoint *virDomainCheckpointPtr;
This is a weird construct for this file...
I think this should be in a libvirt-domain-checkpoint.h type file which I see is generated a few patches later and these lines moved. In the long run I guess as long as it's in a file it doesn't matter.
I'll add a comment here making it obvious that this is a temporary hack to avoid even more invasive changes in this patch, and that it gets cleaned up later.
+++ b/src/datatypes.h @@ -1,7 +1,7 @@ /* * datatypes.h: management of structs for public data types * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 Red Hat, Inc.
haha 2018 - I know when you last touched this ;-)
D'oh - my emacs macro may be nice, but it isn't consistent unless I retouch the file. :-)
+/** + * virGetDomainCheckpoint: + * @domain: the domain to checkpoint + * @name: pointer to the domain checkpoint name + * + * Allocates a new domain checkpoint object. When the object is no longer needed, + * virObjectUnref() must be called in order to not leak data. + * + * Returns a pointer to the domain checkpoint object, or NULL on error. + */ +virDomainCheckpointPtr +virGetDomainCheckpoint(virDomainPtr domain, const char *name)
More recently we or I have been trying to enforce one line per argument for new code or changed methods.
Can do. Old habits die hard, but I can make the effort to avoid them in favor of one-argument-per-line.
+{ + virDomainCheckpointPtr ret = NULL; + + if (virDataTypesInitialize() < 0) + return NULL; + + virCheckDomainGoto(domain, error); + virCheckNonNullArgGoto(name, error); + + if (!(ret = virObjectNew(virDomainCheckpointClass))) + goto error;
Could return NULL too, but it is a copy of virGetDomainSnapshot
Reviewed-by: John Ferlan <jferlan@redhat.com>
And as you've seen in my other recent patches, I've been cleaning up some snapshot oddities; any cleanups I do there will also need to be folded into v5 here. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

On 2/9/19 9:48 AM, John Ferlan wrote:
On 2/6/19 2:18 PM, Eric Blake wrote:
Prepare for introducing a bunch of new public APIs related to backup checkpoints by first introducing a new internal type and errors associated with that type. 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.
Signed-off-by: Eric Blake <eblake@redhat.com>
@@ -1214,6 +1215,15 @@ const virErrorMsgTuple virErrorMsgStrings[VIR_ERR_NUMBER_LAST] = { [VIR_ERR_NO_NWFILTER_BINDING] = { N_("Network filter binding not found"), N_("Network filter binding not found: %s") }, + [VIR_ERR_INVALID_DOMAIN_CHECKPOINT] = { + N_("Invalid checkpoint"), + N_("Invalid checkpoint: %s") },
Invalid domain checkpoint
Copy and paste from VIR_ERR_INVALID_DOMAIN_SNAPSHOT; will do separate patch to fix that wording. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

Prepare for new checkpoint and backup APIs by describing the XML that will represent a checkpoint and backup. The checkpoint XML is modeled heavily after virDomainSnapshotPtr, since both represent a point in time of the guest (however, a snapshot exists with the intent to roll back to that point, while a checkpoint exists to facilitate later incremental backups). Meanwhile, the backup XML has enough information to represent both push model (the hypervisor writes the backup file to a location of the user's choice) and the pull model (the hypervisor needs local temporary storage, and also creates an NBD server that the user can use to read the backup via a third-party client).. But while a snapshot exists with the intent of rolling back to that state, a checkpoint instead makes it possible to create an incremental backup at a later time. Add testsuite coverage for some minimal uses of both XML. Ultimately, I'd love for push model backups to target a network driver rather than just a local file or block device; but doing that got hairy (while <domain> uses <source> as the description of a host or network resource, I picked <target> as the description of a push model backup target [defaults to qcow2 but can also be raw or any other format], and <scratch> as the description of a pull model backup scratch space [must be qcow2]). The ideal refactoring would be a way to parameterize RNG to accept <disk type='FOO'>...</disk> so that the name of the subelement can be <source> for domain, or <target> or <scratch> as needed for backups. Future patches may improve this area of code. Signed-off-by: Eric Blake <eblake@redhat.com> --- v2: apply (some) wording changes from review --- docs/docs.html.in | 3 +- docs/domainstatecapture.html.in | 4 +- docs/format.html.in | 1 + docs/formatcheckpoint.html.in | 291 +++++++++++++++++++ docs/index.html.in | 3 +- docs/schemas/domainbackup.rng | 185 ++++++++++++ docs/schemas/domaincheckpoint.rng | 94 ++++++ libvirt.spec.in | 2 + mingw-libvirt.spec.in | 4 + tests/Makefile.am | 6 +- tests/domainbackupxml2xmlin/backup-pull.xml | 9 + tests/domainbackupxml2xmlin/backup-push.xml | 9 + tests/domainbackupxml2xmlin/empty.xml | 1 + tests/domainbackupxml2xmlout/backup-pull.xml | 9 + tests/domainbackupxml2xmlout/backup-push.xml | 9 + tests/domainbackupxml2xmlout/empty.xml | 7 + tests/domaincheckpointxml2xmlin/empty.xml | 1 + tests/domaincheckpointxml2xmlin/sample.xml | 7 + tests/domaincheckpointxml2xmlout/empty.xml | 10 + tests/domaincheckpointxml2xmlout/sample.xml | 16 + tests/virschematest.c | 4 + 21 files changed, 670 insertions(+), 5 deletions(-) create mode 100644 docs/formatcheckpoint.html.in create mode 100644 docs/schemas/domainbackup.rng create mode 100644 docs/schemas/domaincheckpoint.rng create mode 100644 tests/domainbackupxml2xmlin/backup-pull.xml create mode 100644 tests/domainbackupxml2xmlin/backup-push.xml create mode 100644 tests/domainbackupxml2xmlin/empty.xml create mode 100644 tests/domainbackupxml2xmlout/backup-pull.xml create mode 100644 tests/domainbackupxml2xmlout/backup-push.xml create mode 100644 tests/domainbackupxml2xmlout/empty.xml create mode 100644 tests/domaincheckpointxml2xmlin/empty.xml create mode 100644 tests/domaincheckpointxml2xmlin/sample.xml create mode 100644 tests/domaincheckpointxml2xmlout/empty.xml create mode 100644 tests/domaincheckpointxml2xmlout/sample.xml diff --git a/docs/docs.html.in b/docs/docs.html.in index 4c46b74980..4914e7dbed 100644 --- a/docs/docs.html.in +++ b/docs/docs.html.in @@ -79,7 +79,8 @@ <a href="formatdomaincaps.html">domain 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">backups and 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/domainstatecapture.html.in b/docs/domainstatecapture.html.in index f7f2fe0b98..9b890b4c0c 100644 --- a/docs/domainstatecapture.html.in +++ b/docs/domainstatecapture.html.in @@ -259,9 +259,9 @@ a checkpoint as a side-effect of starting a new incremental backup with <code>virDomainBackupBegin()</code>, since a second incremental backup is most useful when using the - checkpoint created during the first. <!--See also + checkpoint created during the first. See also the <a href="formatcheckpoint.html">XML details</a> used with - this command.--></dd> + this command.</dd> <dt>virDomainBackupBegin(), virDomainBackupEnd()</dt> <dd>This API wraps approaches for capturing the state of disks diff --git a/docs/format.html.in b/docs/format.html.in index 22b23e3fc7..8c4e15e079 100644 --- a/docs/format.html.in +++ b/docs/format.html.in @@ -24,6 +24,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">Backups and 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..6d66bd0511 --- /dev/null +++ b/docs/formatcheckpoint.html.in @@ -0,0 +1,291 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml"> + <body> + <h1>Checkpoint and Backup 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 + (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 + 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. Most disk + checkpoints are created in concert with a backup + via <code>virDomainBackupBegin()</code>; however, libvirt also + exposes enough support to create disk checkpoints independently + from a backup operation + via <code>virDomainCheckpointCreateXML()</code>. + </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. + </p> + <p> + Checkpoints are maintained in a hierarchy. A domain can have a + current checkpoint, which is the most recent checkpoint compared to + the current state of the domain (although a domain might have + checkpoints without a current checkpoint, if checkpoints have been + deleted in the meantime). Creating or reverting to a checkpoint + sets that checkpoint as current, and the prior current checkpoint is + the parent of the new checkpoint. Branches in the hierarchy can + be formed by reverting to a checkpoint with a child, then creating + another checkpoint. + </p> + <p> + The top-level <code>domaincheckpoint</code> element may contain + the following elements: + </p> + <dl> + <dt><code>name</code></dt> + <dd>The name for this checkpoint. If the name is specified when + initially creating the checkpoint, then the checkpoint will have + that particular name. If the name is omitted when initially + creating the checkpoint, then libvirt will make up a name for + the checkpoint, based on the time when it was created. + </dd> + <dt><code>description</code></dt> + <dd>A 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, but if individual disks were omitted from the + element, they will not be part of the checkpoint. On output, + this is fully populated to show the state of each disk in the + checkpoint. This element has a list of <code>disk</code> + sub-elements, describing anywhere from one to all of the disks + associated with the domain. + <dl> + <dt><code>disk</code></dt> + <dd>This sub-element describes the checkpoint properties of + a specific disk. The attribute <code>name</code> is + mandatory, and 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. The attribute <code>checkpoint</code> is + optional on input; 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, in + which case another attribute <code>bitmap</code> will be + the name of the tracking bitmap (defaulting to the + checkpoint name). On output, an additional + attribute <code>size</code> may be 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> + <dt><code>creationTime</code></dt> + <dd>The time this checkpoint was created. The time is specified + in seconds since the Epoch, UTC (i.e. Unix time). Readonly. + </dd> + <dt><code>parent</code></dt> + <dd>The parent of this checkpoint. If present, this element + contains exactly one child element, name. This specifies the + name of the parent checkpoint of this one, and is used to + represent trees of checkpoints. Readonly. + </dd> + <dt><code>domain</code></dt> + <dd>The inactive <a href="formatdomain.html">domain + configuration</a> at the time the checkpoint was created. + Readonly. + </dd> + </dl> + + <h2><a id="BackupAttributes">Backup XML</a></h2> + + <p> + Creating a backup, whether full or incremental, is done + via <code>virDomainBackupBegin()</code>, which takes an XML + description of the actions to perform. There are two general + modes for backups: a push mode (where the hypervisor writes out + the data to the destination file, which may be local or remote), + and a pull mode (where the hypervisor creates an NBD server that + a third-party client can then read as needed, and which requires + the use of temporary storage, typically local, until the backup + is complete). + </p> + <p> + The instructions for beginning a backup job are provided as + attributes and elements of the + top-level <code>domainbackup</code> element. This element + includes an optional attribute <code>mode</code> which can be + either "push" or "pull" (default push). Where elements are + optional on creation, <code>virDomainBackupGetXMLDesc()</code> + can be used to see the actual values selected (for example, + learning which port the NBD server is using in the pull model, + or what file names libvirt generated when none were supplied). + The following child elements are supported: + </p> + <dl> + <dt><code>incremental</code></dt> + <dd>Optional. If this element is present, it must name an + existing checkpoint of the domain, which will be used to make + this backup an incremental one (in the push model, only + changes since the checkpoint are written to the destination; + in the pull model, the NBD server uses the + NBD_OPT_SET_META_CONTEXT extension to advertise to the client + which portions of the export contain changes since the + checkpoint). If omitted, a full backup is performed. + </dd> + <dt><code>server</code></dt> + <dd>Present only for a pull mode backup. Contains the same + attributes as the <code>protocol</code> element of a disk + attached via NBD in the domain (such as transport, socket, + name, port, or tls), necessary to set up an NBD server that + exposes the content of each disk at the time the backup + started. + </dd> + <dt><code>disks</code></dt> + <dd>This is an optional listing of instructions for disks + participating in the backup (if omitted, all disks + participate, and libvirt attempts to generate filenames by + appending the current timestamp as a suffix). When provided on + input, disks omitted from the list do not participate in the + backup. On output, the list is present but contains only the + disks participating in the backup job. This element has a + list of <code>disk</code> sub-elements, describing anywhere + from one to all of the disks associated with the domain. + <dl> + <dt><code>disk</code></dt> + <dd>This sub-element describes the backup properties of + a specific disk. The attribute <code>name</code> is + mandatory, and 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. The optional attribute <code>type</code> can + be <code>file</code>, <code>block</code>, + or <code>network</code>, similar to a disk declaration for + a domain, controls what additional sub-elements are needed + to describe the destination (such as <code>protocol</code> + for a network destination). In push mode backups, the + primary sub-element is <code>target</code>; in pull mode, + the primary sub-element is <code>scratch</code>; but + either way, the primary sub-element describes the file + name to be used during the backup operation, similar to + the <code>source</code> sub-element of a domain disk. In + push mode, an optional sub-element <code>driver</code> can + also be used, with an attribute <code>type</code> to + specify a destination format different from + qcow2. Additionally, if a push backup is not + incremental, <code>target</code> may contain an optional + attribute <code>shallow="on"</code> so that the + destination file copies only the top-most source file in a + backing chain, rather than collapsing the entire chain + into the copy. + </dd> + </dl> + </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> + <creationTime>1525889631</creationTime> + <parent> + <name>1525111885</name> + </parent> + <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>. Now, we + can make a subsequent call + to <code>virDomainBackupBegin()</code> to perform an incremental + backup of just this data, using the following XML to start a + pull model NBD export of the vda disk: + </p> + <pre> +<domainbackup mode="pull"> + <incremental>1525889631</incremental> + <server transport="unix" socket="/path/to/server"/> + <disks/> + <disk name='vda' type='file'> + <scratch file='/path/to/file1.scratch'/> + </disk> + </disks/> +</domainbackup> + </pre> + </body> +</html> diff --git a/docs/index.html.in b/docs/index.html.in index 1f9f448399..6c5d3a6dc3 100644 --- a/docs/index.html.in +++ b/docs/index.html.in @@ -68,7 +68,8 @@ <a href="formatdomaincaps.html">domain 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">backups and 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/domainbackup.rng b/docs/schemas/domainbackup.rng new file mode 100644 index 0000000000..edc68a37cf --- /dev/null +++ b/docs/schemas/domainbackup.rng @@ -0,0 +1,185 @@ +<?xml version="1.0"?> +<!-- A Relax NG schema for the libvirt domain backup properties XML format --> +<grammar xmlns="http://relaxng.org/ns/structure/1.0"> + <start> + <ref name='domainbackup'/> + </start> + + <include href='domaincommon.rng'/> + + <define name='domainbackup'> + <element name='domainbackup'> + <optional> + <attribute name='id'> + <ref name="unsignedInt"/> + </attribute> + </optional> + <interleave> + <optional> + <element name='incremental'> + <text/> + </element> + </optional> + <choice> + <group> + <optional> + <attribute name='mode'> + <value>push</value> + </attribute> + </optional> + <ref name='backupDisksPush'/> + </group> + <group> + <attribute name='mode'> + <value>pull</value> + </attribute> + <interleave> + <element name='server'> + <choice> + <group> + <optional> + <attribute name='transport'> + <value>tcp</value> + </attribute> + </optional> + <attribute name='name'> + <choice> + <ref name='dnsName'/> + <ref name='ipAddr'/> + </choice> + </attribute> + <optional> + <attribute name='port'> + <ref name='unsignedInt'/> + </attribute> + </optional> + <!-- add tls? --> + </group> + <group> + <attribute name='transport'> + <value>unix</value> + </attribute> + <attribute name='socket'> + <ref name='absFilePath'/> + </attribute> + </group> + </choice> + </element> + <ref name='backupDisksPull'/> + </interleave> + </group> + </choice> + </interleave> + </element> + </define> + + <define name='backupPushDriver'> + <optional> + <element name='driver'> + <attribute name='type'> + <ref name='storageFormat'/> + </attribute> + </element> + </optional> + </define> + + <define name='backupDisksPush'> + <optional> + <element name='disks'> + <oneOrMore> + <element name='disk'> + <attribute name='name'> + <choice> + <ref name='diskTarget'/> + <ref name='absFilePath'/> + </choice> + </attribute> + <choice> + <!-- FIXME allow push to a network location, by + refactoring 'diskSource' to take element name by a + per-grammar ref --> + <group> + <optional> + <attribute name='type'> + <value>file</value> + </attribute> + </optional> + <interleave> + <optional> + <element name='target'> + <attribute name='file'> + <ref name='absFilePath'/> + </attribute> + </element> + </optional> + <ref name='backupPushDriver'/> + </interleave> + </group> + <group> + <attribute name='type'> + <value>disk</value> + </attribute> + <interleave> + <optional> + <element name='target'> + <attribute name='dev'> + <ref name='absFilePath'/> + </attribute> + </element> + </optional> + <ref name='backupPushDriver'/> + </interleave> + </group> + </choice> + </element> + </oneOrMore> + </element> + </optional> + </define> + + <define name='backupDisksPull'> + <optional> + <element name='disks'> + <oneOrMore> + <element name='disk'> + <attribute name='name'> + <choice> + <ref name='diskTarget'/> + <ref name='absFilePath'/> + </choice> + </attribute> + <choice> + <group> + <optional> + <attribute name='type'> + <value>file</value> + </attribute> + </optional> + <optional> + <element name='scratch'> + <attribute name='file'> + <ref name='absFilePath'/> + </attribute> + </element> + </optional> + </group> + <group> + <attribute name='type'> + <value>disk</value> + </attribute> + <optional> + <element name='scratch'> + <attribute name='dev'> + <ref name='absFilePath'/> + </attribute> + </element> + </optional> + </group> + </choice> + </element> + </oneOrMore> + </element> + </optional> + </define> + +</grammar> diff --git a/docs/schemas/domaincheckpoint.rng b/docs/schemas/domaincheckpoint.rng new file mode 100644 index 0000000000..d8dfda9f1c --- /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"> + <start> + <ref name='domaincheckpoint'/> + </start> + + <include href='domaincommon.rng'/> + + <define name='domaincheckpoint'> + <element name='domaincheckpoint'> + <interleave> + <optional> + <element name='name'> + <text/> + </element> + </optional> + <optional> + <element name='description'> + <text/> + </element> + </optional> + <optional> + <element name='creationTime'> + <text/> + </element> + </optional> + <optional> + <element name='disks'> + <oneOrMore> + <ref name='diskcheckpoint'/> + </oneOrMore> + </element> + </optional> + <optional> + <choice> + <element name='domain'> + <element name='uuid'> + <ref name="UUID"/> + </element> + </element> + <!-- 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> + </choice> + </optional> + <optional> + <element name='parent'> + <element name='name'> + <text/> + </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> + +</grammar> diff --git a/libvirt.spec.in b/libvirt.spec.in index c0e538d92d..2e9213510d 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1813,7 +1813,9 @@ exit 0 %{_datadir}/libvirt/schemas/capability.rng %{_datadir}/libvirt/schemas/cputypes.rng %{_datadir}/libvirt/schemas/domain.rng +%{_datadir}/libvirt/schemas/domainbackup.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 249abb8475..a7b697d7bd 100644 --- a/mingw-libvirt.spec.in +++ b/mingw-libvirt.spec.in @@ -239,7 +239,9 @@ rm -rf $RPM_BUILD_ROOT%{mingw64_libexecdir}/libvirt-guests.sh %{mingw32_datadir}/libvirt/schemas/capability.rng %{mingw32_datadir}/libvirt/schemas/cputypes.rng %{mingw32_datadir}/libvirt/schemas/domain.rng +%{mingw32_datadir}/libvirt/schemas/domainbackup.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 @@ -326,7 +328,9 @@ rm -rf $RPM_BUILD_ROOT%{mingw64_libexecdir}/libvirt-guests.sh %{mingw64_datadir}/libvirt/schemas/capability.rng %{mingw64_datadir}/libvirt/schemas/cputypes.rng %{mingw64_datadir}/libvirt/schemas/domain.rng +%{mingw64_datadir}/libvirt/schemas/domainbackup.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 bdf7154fd5..9b835fa369 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to produce Makefile.in -## Copyright (C) 2005-2015 Red Hat, Inc. +## Copyright (C) 2005-2018 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 @@ -92,7 +92,11 @@ EXTRA_DIST = \ capabilityschemadata \ commanddata \ cputestdata \ + domainbackupxml2xmlin \ + domainbackupxml2xmlout \ domaincapsschemadata \ + domaincheckpointxml2xmlin \ + domaincheckpointxml2xmlout \ domainconfdata \ domainschemadata \ domainsnapshotxml2xmlin \ diff --git a/tests/domainbackupxml2xmlin/backup-pull.xml b/tests/domainbackupxml2xmlin/backup-pull.xml new file mode 100644 index 0000000000..2ce5cd6711 --- /dev/null +++ b/tests/domainbackupxml2xmlin/backup-pull.xml @@ -0,0 +1,9 @@ +<domainbackup mode="pull"> + <incremental>1525889631</incremental> + <server transport='tcp' name='localhost' port='10809'/> + <disks> + <disk name='vda' type='file'> + <scratch file='/path/to/file'/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domainbackupxml2xmlin/backup-push.xml b/tests/domainbackupxml2xmlin/backup-push.xml new file mode 100644 index 0000000000..1b7d3061fd --- /dev/null +++ b/tests/domainbackupxml2xmlin/backup-push.xml @@ -0,0 +1,9 @@ +<domainbackup mode="push"> + <incremental>1525889631</incremental> + <disks> + <disk name='vda' type='file'> + <driver type='raw'/> + <target file='/path/to/file'/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domainbackupxml2xmlin/empty.xml b/tests/domainbackupxml2xmlin/empty.xml new file mode 100644 index 0000000000..7ed511f97b --- /dev/null +++ b/tests/domainbackupxml2xmlin/empty.xml @@ -0,0 +1 @@ +<domainbackup/> diff --git a/tests/domainbackupxml2xmlout/backup-pull.xml b/tests/domainbackupxml2xmlout/backup-pull.xml new file mode 100644 index 0000000000..2ce5cd6711 --- /dev/null +++ b/tests/domainbackupxml2xmlout/backup-pull.xml @@ -0,0 +1,9 @@ +<domainbackup mode="pull"> + <incremental>1525889631</incremental> + <server transport='tcp' name='localhost' port='10809'/> + <disks> + <disk name='vda' type='file'> + <scratch file='/path/to/file'/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domainbackupxml2xmlout/backup-push.xml b/tests/domainbackupxml2xmlout/backup-push.xml new file mode 100644 index 0000000000..1b7d3061fd --- /dev/null +++ b/tests/domainbackupxml2xmlout/backup-push.xml @@ -0,0 +1,9 @@ +<domainbackup mode="push"> + <incremental>1525889631</incremental> + <disks> + <disk name='vda' type='file'> + <driver type='raw'/> + <target file='/path/to/file'/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domainbackupxml2xmlout/empty.xml b/tests/domainbackupxml2xmlout/empty.xml new file mode 100644 index 0000000000..13600fbb1c --- /dev/null +++ b/tests/domainbackupxml2xmlout/empty.xml @@ -0,0 +1,7 @@ +<domainbackup mode="push"> + <disks> + <disk name="vda" type="file"> + <target file="/path/to/file1.copy"/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domaincheckpointxml2xmlin/empty.xml b/tests/domaincheckpointxml2xmlin/empty.xml new file mode 100644 index 0000000000..dc36449142 --- /dev/null +++ b/tests/domaincheckpointxml2xmlin/empty.xml @@ -0,0 +1 @@ +<domaincheckpoint/> diff --git a/tests/domaincheckpointxml2xmlin/sample.xml b/tests/domaincheckpointxml2xmlin/sample.xml new file mode 100644 index 0000000000..70ed964e1e --- /dev/null +++ b/tests/domaincheckpointxml2xmlin/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/domaincheckpointxml2xmlout/empty.xml b/tests/domaincheckpointxml2xmlout/empty.xml new file mode 100644 index 0000000000..a26c7caab0 --- /dev/null +++ b/tests/domaincheckpointxml2xmlout/empty.xml @@ -0,0 +1,10 @@ +<domaincheckpoint> + <name>1525889631</name> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + </disks> + <domain> + <uuid>9d37b878-a7cc-9f9a-b78f-49b3abad25a8</uuid> + </domain> +</domaincheckpoint> diff --git a/tests/domaincheckpointxml2xmlout/sample.xml b/tests/domaincheckpointxml2xmlout/sample.xml new file mode 100644 index 0000000000..559b29c8c1 --- /dev/null +++ b/tests/domaincheckpointxml2xmlout/sample.xml @@ -0,0 +1,16 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <creationTime>1525889631</creationTime> + <parent> + <name>1525111885</name> + </parent> + <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> + </domain> +</domaincheckpoint> diff --git a/tests/virschematest.c b/tests/virschematest.c index d1bcdeac9c..c3b41d5bbc 100644 --- a/tests/virschematest.c +++ b/tests/virschematest.c @@ -221,7 +221,11 @@ mymain(void) "lxcxml2xmloutdata", "bhyvexml2argvdata", "genericxml2xmlindata", "genericxml2xmloutdata", "xlconfigdata", "libxlxml2domconfigdata", "qemuhotplugtestdomains"); + DO_TEST_DIR("domainbackup.rng", "domainbackupxml2xmlin", + "domainbackupxml2xmlout"); DO_TEST_DIR("domaincaps.rng", "domaincapsschemadata"); + DO_TEST_DIR("domaincheckpoint.rng", "domaincheckpointxml2xmlin", + "domaincheckpointxml2xmlout"); DO_TEST_DIR("domainsnapshot.rng", "domainsnapshotxml2xmlin", "domainsnapshotxml2xmlout"); DO_TEST_DIR("interface.rng", "interfaceschemadata"); -- 2.20.1

On 2/6/19 2:18 PM, Eric Blake wrote:
Prepare for new checkpoint and backup APIs by describing the XML that will represent a checkpoint and backup. The checkpoint XML is modeled heavily after virDomainSnapshotPtr, since both represent a point in time of the guest (however, a snapshot exists with the intent to roll back to that point, while a checkpoint exists to facilitate later incremental backups). Meanwhile, the backup XML has enough information to represent both push model (the hypervisor writes the backup file to a location of the user's choice) and the pull model (the hypervisor needs local temporary storage, and also creates an NBD server that the user can use to read the backup via a third-party client).. But while a snapshot exists with the
s/../.
intent of rolling back to that state, a checkpoint instead makes it possible to create an incremental backup at a later time.
I think most of the description beyond the first sentence or so is covered in the docs and probably doesn't need to be in the commit. Perhaps just " Prepare for new checkpoint and backup APIs by describing the XML that will represent a checkpoint and backup. The checkpoint XML is modeled heavily after virDomainSnapshotPtr, while the backup XML is provides newer capabilities for both push and pull models.
Add testsuite coverage for some minimal uses of both XML.
Ultimately, I'd love for push model backups to target a network driver rather than just a local file or block device; but doing that got hairy (while <domain> uses <source> as the description of a host or network resource, I picked <target> as the description of a push model backup target [defaults to qcow2 but can also be raw or any other format], and <scratch> as the description of a pull model backup scratch space [must be qcow2]). The ideal refactoring would be a way to parameterize RNG to accept <disk type='FOO'>...</disk> so that the name of the subelement can be <source> for domain, or <target> or <scratch> as needed for backups. Future patches may improve this area of code.
Hmmm... Should this paragraph be in the commit message or under the ---? BTW: Only after reading through all this does this partially make sense.
Signed-off-by: Eric Blake <eblake@redhat.com>
--- v2: apply (some) wording changes from review --- docs/docs.html.in | 3 +- docs/domainstatecapture.html.in | 4 +- docs/format.html.in | 1 + docs/formatcheckpoint.html.in | 291 +++++++++++++++++++ docs/index.html.in | 3 +- docs/schemas/domainbackup.rng | 185 ++++++++++++ docs/schemas/domaincheckpoint.rng | 94 ++++++ libvirt.spec.in | 2 + mingw-libvirt.spec.in | 4 + tests/Makefile.am | 6 +- tests/domainbackupxml2xmlin/backup-pull.xml | 9 + tests/domainbackupxml2xmlin/backup-push.xml | 9 + tests/domainbackupxml2xmlin/empty.xml | 1 + tests/domainbackupxml2xmlout/backup-pull.xml | 9 + tests/domainbackupxml2xmlout/backup-push.xml | 9 + tests/domainbackupxml2xmlout/empty.xml | 7 + tests/domaincheckpointxml2xmlin/empty.xml | 1 + tests/domaincheckpointxml2xmlin/sample.xml | 7 + tests/domaincheckpointxml2xmlout/empty.xml | 10 + tests/domaincheckpointxml2xmlout/sample.xml | 16 + tests/virschematest.c | 4 + 21 files changed, 670 insertions(+), 5 deletions(-) create mode 100644 docs/formatcheckpoint.html.in create mode 100644 docs/schemas/domainbackup.rng create mode 100644 docs/schemas/domaincheckpoint.rng create mode 100644 tests/domainbackupxml2xmlin/backup-pull.xml create mode 100644 tests/domainbackupxml2xmlin/backup-push.xml create mode 100644 tests/domainbackupxml2xmlin/empty.xml create mode 100644 tests/domainbackupxml2xmlout/backup-pull.xml create mode 100644 tests/domainbackupxml2xmlout/backup-push.xml create mode 100644 tests/domainbackupxml2xmlout/empty.xml create mode 100644 tests/domaincheckpointxml2xmlin/empty.xml create mode 100644 tests/domaincheckpointxml2xmlin/sample.xml create mode 100644 tests/domaincheckpointxml2xmlout/empty.xml create mode 100644 tests/domaincheckpointxml2xmlout/sample.xml
Lots of detail and files... I know they're related, but if there's a way to split it may be nicer for review processing ;-)
diff --git a/docs/docs.html.in b/docs/docs.html.in index 4c46b74980..4914e7dbed 100644 --- a/docs/docs.html.in +++ b/docs/docs.html.in @@ -79,7 +79,8 @@ <a href="formatdomaincaps.html">domain 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">backups and 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/domainstatecapture.html.in b/docs/domainstatecapture.html.in index f7f2fe0b98..9b890b4c0c 100644 --- a/docs/domainstatecapture.html.in +++ b/docs/domainstatecapture.html.in @@ -259,9 +259,9 @@ a checkpoint as a side-effect of starting a new incremental backup with <code>virDomainBackupBegin()</code>, since a second incremental backup is most useful when using the - checkpoint created during the first. <!--See also + checkpoint created during the first. See also the <a href="formatcheckpoint.html">XML details</a> used with - this command.--></dd> + this command.</dd>
<dt>virDomainBackupBegin(), virDomainBackupEnd()</dt> <dd>This API wraps approaches for capturing the state of disks diff --git a/docs/format.html.in b/docs/format.html.in index 22b23e3fc7..8c4e15e079 100644 --- a/docs/format.html.in +++ b/docs/format.html.in @@ -24,6 +24,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">Backups and 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..6d66bd0511 --- /dev/null +++ b/docs/formatcheckpoint.html.in @@ -0,0 +1,291 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml"> + <body> + <h1>Checkpoint and Backup 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
QEMU
+ active layer; if other disk formats are in use, capturing disk + 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 + 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. Most disk + checkpoints are created in concert with a backup
s/concert/conjunction (although I understood concert, I think conjunction is more apropos)
+ via <code>virDomainBackupBegin()</code>; however, libvirt also + exposes enough support to create disk checkpoints independently + from a backup operation + via <code>virDomainCheckpointCreateXML()</code>. + </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. + </p> + <p> + Checkpoints are maintained in a hierarchy. A domain can have a + current checkpoint, which is the most recent checkpoint compared to + the current state of the domain (although a domain might have + checkpoints without a current checkpoint, if checkpoints have been + deleted in the meantime). Creating or reverting to a checkpoint + sets that checkpoint as current, and the prior current checkpoint is
s/,//
+ the parent of the new checkpoint. Branches in the hierarchy can + be formed by reverting to a checkpoint with a child, then creating + another checkpoint. + </p> + <p> + The top-level <code>domaincheckpoint</code> element may contain + the following elements: + </p> + <dl> + <dt><code>name</code></dt> + <dd>The name for this checkpoint. If the name is specified when
The optional name
+ initially creating the checkpoint, then the checkpoint will have + that particular name. If the name is omitted when initially + creating the checkpoint, then libvirt will make up a name for + the checkpoint, based on the time when it was created.
I would think the description of what happens when a name is specified would be obvious.
+ </dd> + <dt><code>description</code></dt> + <dd>A human-readable description of the checkpoint. If the
An optional human-...
+ 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
s/domain (/domain. In/ QEMU
+ disks, this element may be needed on input for excluding guest + disks that are not in qcow2 format); if the entire element was
s/); if/. If/
+ omitted on input, then all disks participate in the + checkpoint, but if individual disks were omitted from the + element, they will not be part of the checkpoint. On output, + this is fully populated to show the state of each disk in the
each disk participating in the checkpoint ? Or perhaps "On output, this is the state of the domain's list of disks relative to how the checkpoint is used for each." [ - or something like that]
+ checkpoint. This element has a list of <code>disk</code> + sub-elements, describing anywhere from one to all of the disks + associated with the domain. + <dl> + <dt><code>disk</code></dt> + <dd>This sub-element describes the checkpoint properties of + a specific disk. The attribute <code>name</code> is + mandatory, and 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. The attribute <code>checkpoint</code> is + optional on input; 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, in + which case another attribute <code>bitmap</code> will be + the name of the tracking bitmap (defaulting to the + checkpoint name). On output, an additional + attribute <code>size</code> may be 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.
This is tough to read as one fairly long paragraph especially when it comes to picking out the attributes, consider: <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>The attribute <code>name</code> is mandatory and 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>The attribute <code>checkpoint</code> is optional on input. 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>Optional attribute to define the name of the tracking bitmap (defaulting to the checkpoint name) when the disk is participating in the checkpoint processing.</dd> <dt><code>size</code></dt> <dd>On output, this attribute may be 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>The time this checkpoint was created. The time is specified
A readonly representation of the time this checkpoint was created.
+ in seconds since the Epoch, UTC (i.e. Unix time). Readonly. + </dd> + <dt><code>parent</code></dt> + <dd>The parent of this checkpoint. If present, this element
An optional readonly representation of the parent of this checkpoint.
+ contains exactly one child element, name. This specifies the
s/name/<code>name</code>
+ name of the parent checkpoint of this one, and is used to + represent trees of checkpoints. Readonly. + </dd> + <dt><code>domain</code></dt> + <dd>The inactive <a href="formatdomain.html">domain
A readonly representation of the inactive ...
+ configuration</a> at the time the checkpoint was created. + Readonly. + </dd>
And then just remove the Readonly single word.
+ </dl> + + <h2><a id="BackupAttributes">Backup XML</a></h2> + + <p> + Creating a backup, whether full or incremental, is done + via <code>virDomainBackupBegin()</code>, which takes an XML + description of the actions to perform. There are two general + modes for backups: a push mode (where the hypervisor writes out + the data to the destination file, which may be local or remote), + and a pull mode (where the hypervisor creates an NBD server that + a third-party client can then read as needed, and which requires + the use of temporary storage, typically local, until the backup + is complete). + </p> + <p> + The instructions for beginning a backup job are provided as + attributes and elements of the + top-level <code>domainbackup</code> element. This element + includes an optional attribute <code>mode</code> which can be + either "push" or "pull" (default push). Where elements are + optional on creation, <code>virDomainBackupGetXMLDesc()</code>
s/,/. The/
+ can be used to see the actual values selected (for example, + learning which port the NBD server is using in the pull model,
s/model,/model/
+ or what file names libvirt generated when none were supplied). + The following child elements are supported: + </p> + <dl> + <dt><code>incremental</code></dt> + <dd>Optional. If this element is present, it must name an
An optional element to describe the name of an existing checkpoint... [one would hope we wouldn't have to give a link to the above named checkpoints, but I suppose it would be possible - your call on that]
+ existing checkpoint of the domain, which will be used to make + this backup an incremental one (in the push model, only
s/one (in the/one. In the/
+ changes since the checkpoint are written to the destination;
s/;/./
+ in the pull model, the NBD server uses the
s/in/In/
+ NBD_OPT_SET_META_CONTEXT extension to advertise to the client + which portions of the export contain changes since the + checkpoint). If omitted, a full backup is performed.
s/)./.
+ </dd> + <dt><code>server</code></dt> + <dd>Present only for a pull mode backup. Contains the same + attributes as the <code>protocol</code> element of a disk
[could provide a link to the domain disk section if you feel it's appropriate or necessary]
+ attached via NBD in the domain (such as transport, socket, + name, port, or tls), necessary to set up an NBD server that + exposes the content of each disk at the time the backup + started.
s/started/is started/
+ </dd> + <dt><code>disks</code></dt> + <dd>This is an optional listing of instructions for disks
An optional listing
+ participating in the backup (if omitted, all disks + participate, and libvirt attempts to generate filenames by
s/, and/ and/
+ appending the current timestamp as a suffix). When provided on + input, disks omitted from the list do not participate in the + backup. On output, the list is present but contains only the + disks participating in the backup job. This element has a
s/ job././ [job is an implementation detail]
+ list of <code>disk</code> sub-elements, describing anywhere + from one to all of the disks associated with the domain. + <dl> + <dt><code>disk</code></dt> + <dd>This sub-element describes the backup properties of + a specific disk. The attribute <code>name</code> is + mandatory, and 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. The optional attribute <code>type</code> can + be <code>file</code>, <code>block</code>, + or <code>network</code>, similar to a disk declaration for + a domain, controls what additional sub-elements are needed + to describe the destination (such as <code>protocol</code> + for a network destination). In push mode backups, the + primary sub-element is <code>target</code>; in pull mode, + the primary sub-element is <code>scratch</code>; but + either way, the primary sub-element describes the file + name to be used during the backup operation, similar to + the <code>source</code> sub-element of a domain disk. In + push mode, an optional sub-element <code>driver</code> can + also be used, with an attribute <code>type</code> to + specify a destination format different from + qcow2. Additionally, if a push backup is not + incremental, <code>target</code> may contain an optional + attribute <code>shallow="on"</code> so that the + destination file copies only the top-most source file in a + backing chain, rather than collapsing the entire chain + into the copy.
This one's harder to read than the other one...Consider: <dt><code>disk</code></dt> <dd>This sub-element describes the backup properties of a specific disk with the following attributes: <dl> <dt><code>name</code></dt> <dd>A mandatory attribute and 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>type</code></dt> <dd>An optional attribute to describe the type of disk. Valid values can be <code>file</code>, <code>block</code>, or <code>network</code>. Similar to a disk declaration for a domain, controls what additional sub-elements are needed to describe the destination (such as <code>protocol</code> for a network destination).</dd> <dt><code>target</code></dt> <dd>For push mode backups, this is the primary sub-element to describe the file name to be used during the backup operation similar to the <code>source</code> sub-element of a domain disk. An optional sub-element <code>driver</code> can also be used, with an attribute <code>type</code> to specify a destination format different from qcow2. Additionally, if the push backup is not incremental, <code>target</code> may contain an optional attribute <code>shallow="on"</code> so that the destination file copies only the top-most source file in a backing chain, rather than collapsing the entire chain into the copy.</dd> <dt><code>scratch</code></dt> <dd>For pull mode backups, this is the primary sub-element to describe the file name to be used during the backup operation similar to the <code>source</code> sub-element of a domain disk.</dd> </dl> </dd>
+ </dd> + </dl> + </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> + <creationTime>1525889631</creationTime> + <parent> + <name>1525111885</name> + </parent> + <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>. Now, we + can make a subsequent call + to <code>virDomainBackupBegin()</code> to perform an incremental + backup of just this data, using the following XML to start a + pull model NBD export of the vda disk: + </p> + <pre> +<domainbackup mode="pull"> + <incremental>1525889631</incremental> + <server transport="unix" socket="/path/to/server"/> + <disks/> + <disk name='vda' type='file'> + <scratch file='/path/to/file1.scratch'/> + </disk> + </disks/> +</domainbackup> + </pre> + </body>
Perhaps an example of mode="push" would be useful as that <target> sub-element has a lot of "nuances" (kindly said ;-))
+</html> diff --git a/docs/index.html.in b/docs/index.html.in index 1f9f448399..6c5d3a6dc3 100644 --- a/docs/index.html.in +++ b/docs/index.html.in @@ -68,7 +68,8 @@ <a href="formatdomaincaps.html">domain 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">backups and 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/domainbackup.rng b/docs/schemas/domainbackup.rng new file mode 100644 index 0000000000..edc68a37cf --- /dev/null +++ b/docs/schemas/domainbackup.rng @@ -0,0 +1,185 @@ +<?xml version="1.0"?> +<!-- A Relax NG schema for the libvirt domain backup properties XML format --> +<grammar xmlns="http://relaxng.org/ns/structure/1.0"> + <start> + <ref name='domainbackup'/> + </start> + + <include href='domaincommon.rng'/> + + <define name='domainbackup'> + <element name='domainbackup'> + <optional> + <attribute name='id'> + <ref name="unsignedInt"/> + </attribute> + </optional>
The 'id' is not described in docs nor do I see it in any of your output. Remnant or future?
+ <interleave> + <optional> + <element name='incremental'> + <text/> + </element> + </optional> + <choice> + <group> + <optional> + <attribute name='mode'> + <value>push</value> + </attribute> + </optional> + <ref name='backupDisksPush'/> + </group> + <group> + <attribute name='mode'> + <value>pull</value> + </attribute> + <interleave> + <element name='server'> + <choice> + <group> + <optional> + <attribute name='transport'> + <value>tcp</value> + </attribute> + </optional> + <attribute name='name'> + <choice> + <ref name='dnsName'/> + <ref name='ipAddr'/> + </choice> + </attribute> + <optional> + <attribute name='port'> + <ref name='unsignedInt'/> + </attribute> + </optional> + <!-- add tls? -->
More work? leaving a golden egg like this keeps me interested ;-)
+ </group> + <group> + <attribute name='transport'> + <value>unix</value> + </attribute> + <attribute name='socket'> + <ref name='absFilePath'/> + </attribute> + </group> + </choice> + </element>
Could something be done to create some "common" include file that would allow for sharing w/ diskSourceNetworkHost so that when/if updating domaincommon someone doesn't forget or neglect to update the domainbackup? One difference I see would be rdma transport which would need to be handled specially in domainbackup XML processing since I assume it wouldn't be supported.
+ <ref name='backupDisksPull'/> + </interleave> + </group> + </choice> + </interleave> + </element> + </define> + + <define name='backupPushDriver'> + <optional> + <element name='driver'> + <attribute name='type'> + <ref name='storageFormat'/> + </attribute> + </element> + </optional> + </define> + + <define name='backupDisksPush'> + <optional> + <element name='disks'> + <oneOrMore> + <element name='disk'> + <attribute name='name'> + <choice> + <ref name='diskTarget'/> + <ref name='absFilePath'/> + </choice> + </attribute> + <choice> + <!-- FIXME allow push to a network location, by + refactoring 'diskSource' to take element name by a + per-grammar ref -->
Is there still work to be done here!
+ <group> + <optional> + <attribute name='type'> + <value>file</value> + </attribute> + </optional> + <interleave> + <optional> + <element name='target'> + <attribute name='file'> + <ref name='absFilePath'/> + </attribute> + </element> + </optional> + <ref name='backupPushDriver'/> + </interleave> + </group> + <group> + <attribute name='type'> + <value>disk</value>
So the docs describe attribute type having "file", "block", or "network" and this is different. Things should match I would think.
+ </attribute> + <interleave> + <optional> + <element name='target'> + <attribute name='dev'> + <ref name='absFilePath'/> + </attribute>
Missing optional attribute "shallow" w/ OnOff as describe in docs.
+ </element> + </optional> + <ref name='backupPushDriver'/> + </interleave> + </group> + </choice> + </element> + </oneOrMore> + </element> + </optional> + </define> + + <define name='backupDisksPull'> + <optional> + <element name='disks'> + <oneOrMore> + <element name='disk'> + <attribute name='name'> + <choice> + <ref name='diskTarget'/> + <ref name='absFilePath'/> + </choice> + </attribute> + <choice> + <group> + <optional> + <attribute name='type'> + <value>file</value> + </attribute> + </optional> + <optional> + <element name='scratch'> + <attribute name='file'> + <ref name='absFilePath'/> + </attribute> + </element> + </optional> + </group> + <group> + <attribute name='type'> + <value>disk</value>
Same w/r/t to valid 'type' values.
+ </attribute> + <optional> + <element name='scratch'> + <attribute name='dev'> + <ref name='absFilePath'/> + </attribute> + </element> + </optional> + </group> + </choice> + </element> + </oneOrMore> + </element> + </optional> + </define> + +</grammar> diff --git a/docs/schemas/domaincheckpoint.rng b/docs/schemas/domaincheckpoint.rng new file mode 100644 index 0000000000..d8dfda9f1c --- /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"> + <start> + <ref name='domaincheckpoint'/> + </start> + + <include href='domaincommon.rng'/> + + <define name='domaincheckpoint'> + <element name='domaincheckpoint'> + <interleave> + <optional> + <element name='name'> + <text/> + </element> + </optional> + <optional> + <element name='description'> + <text/> + </element> + </optional> + <optional> + <element name='creationTime'> + <text/> + </element> + </optional> + <optional> + <element name='disks'> + <oneOrMore> + <ref name='diskcheckpoint'/> + </oneOrMore> + </element> + </optional> + <optional> + <choice> + <element name='domain'> + <element name='uuid'> + <ref name="UUID"/> + </element> + </element> + <!-- 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> + </choice> + </optional> + <optional> + <element name='parent'> + <element name='name'> + <text/> + </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"/>
I haven't looked ahead to conf processing, but I assume this is the same type as whatever this gets read into. Whenever I see size in reference to disks I'm thinking ULL's.
+ </attribute> + </optional> + </group> + </choice> + </element> + </define> + +</grammar> diff --git a/libvirt.spec.in b/libvirt.spec.in index c0e538d92d..2e9213510d 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1813,7 +1813,9 @@ exit 0 %{_datadir}/libvirt/schemas/capability.rng %{_datadir}/libvirt/schemas/cputypes.rng %{_datadir}/libvirt/schemas/domain.rng +%{_datadir}/libvirt/schemas/domainbackup.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 249abb8475..a7b697d7bd 100644 --- a/mingw-libvirt.spec.in +++ b/mingw-libvirt.spec.in @@ -239,7 +239,9 @@ rm -rf $RPM_BUILD_ROOT%{mingw64_libexecdir}/libvirt-guests.sh %{mingw32_datadir}/libvirt/schemas/capability.rng %{mingw32_datadir}/libvirt/schemas/cputypes.rng %{mingw32_datadir}/libvirt/schemas/domain.rng +%{mingw32_datadir}/libvirt/schemas/domainbackup.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 @@ -326,7 +328,9 @@ rm -rf $RPM_BUILD_ROOT%{mingw64_libexecdir}/libvirt-guests.sh %{mingw64_datadir}/libvirt/schemas/capability.rng %{mingw64_datadir}/libvirt/schemas/cputypes.rng %{mingw64_datadir}/libvirt/schemas/domain.rng +%{mingw64_datadir}/libvirt/schemas/domainbackup.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 bdf7154fd5..9b835fa369 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to produce Makefile.in
-## Copyright (C) 2005-2015 Red Hat, Inc. +## Copyright (C) 2005-2018 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 @@ -92,7 +92,11 @@ EXTRA_DIST = \ capabilityschemadata \ commanddata \ cputestdata \ + domainbackupxml2xmlin \ + domainbackupxml2xmlout \ domaincapsschemadata \ + domaincheckpointxml2xmlin \ + domaincheckpointxml2xmlout \ domainconfdata \ domainschemadata \ domainsnapshotxml2xmlin \ diff --git a/tests/domainbackupxml2xmlin/backup-pull.xml b/tests/domainbackupxml2xmlin/backup-pull.xml new file mode 100644 index 0000000000..2ce5cd6711 --- /dev/null +++ b/tests/domainbackupxml2xmlin/backup-pull.xml @@ -0,0 +1,9 @@ +<domainbackup mode="pull"> + <incremental>1525889631</incremental> + <server transport='tcp' name='localhost' port='10809'/> + <disks> + <disk name='vda' type='file'> + <scratch file='/path/to/file'/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domainbackupxml2xmlin/backup-push.xml b/tests/domainbackupxml2xmlin/backup-push.xml new file mode 100644 index 0000000000..1b7d3061fd --- /dev/null +++ b/tests/domainbackupxml2xmlin/backup-push.xml @@ -0,0 +1,9 @@ +<domainbackup mode="push"> + <incremental>1525889631</incremental> + <disks> + <disk name='vda' type='file'> + <driver type='raw'/> + <target file='/path/to/file'/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domainbackupxml2xmlin/empty.xml b/tests/domainbackupxml2xmlin/empty.xml new file mode 100644 index 0000000000..7ed511f97b --- /dev/null +++ b/tests/domainbackupxml2xmlin/empty.xml @@ -0,0 +1 @@ +<domainbackup/>
Should add a 'block' and 'network' example too for completeness - of course that depends on the matching between docs and rng. Should add target w/ shallow=on for completeness. Yes, I recall the commit message indicating essentially minimal.
diff --git a/tests/domainbackupoml2xmlout/backup-pull.xml b/tests/domainbackupxml2xmlout/backup-pull.xml new file mode 100644 index 0000000000..2ce5cd6711 --- /dev/null +++ b/tests/domainbackupxml2xmlout/backup-pull.xml @@ -0,0 +1,9 @@ +<domainbackup mode="pull"> + <incremental>1525889631</incremental> + <server transport='tcp' name='localhost' port='10809'/> + <disks> + <disk name='vda' type='file'> + <scratch file='/path/to/file'/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domainbackupxml2xmlout/backup-push.xml b/tests/domainbackupxml2xmlout/backup-push.xml new file mode 100644 index 0000000000..1b7d3061fd --- /dev/null +++ b/tests/domainbackupxml2xmlout/backup-push.xml @@ -0,0 +1,9 @@ +<domainbackup mode="push"> + <incremental>1525889631</incremental> + <disks> + <disk name='vda' type='file'> + <driver type='raw'/> + <target file='/path/to/file'/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domainbackupxml2xmlout/empty.xml b/tests/domainbackupxml2xmlout/empty.xml new file mode 100644 index 0000000000..13600fbb1c --- /dev/null +++ b/tests/domainbackupxml2xmlout/empty.xml @@ -0,0 +1,7 @@ +<domainbackup mode="push"> + <disks> + <disk name="vda" type="file"> + <target file="/path/to/file1.copy"/> + </disk> + </disks> +</domainbackup> diff --git a/tests/domaincheckpointxml2xmlin/empty.xml b/tests/domaincheckpointxml2xmlin/empty.xml new file mode 100644 index 0000000000..dc36449142 --- /dev/null +++ b/tests/domaincheckpointxml2xmlin/empty.xml @@ -0,0 +1 @@ +<domaincheckpoint/> diff --git a/tests/domaincheckpointxml2xmlin/sample.xml b/tests/domaincheckpointxml2xmlin/sample.xml new file mode 100644 index 0000000000..70ed964e1e --- /dev/null +++ b/tests/domaincheckpointxml2xmlin/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/domaincheckpointxml2xmlout/empty.xml b/tests/domaincheckpointxml2xmlout/empty.xml new file mode 100644 index 0000000000..a26c7caab0 --- /dev/null +++ b/tests/domaincheckpointxml2xmlout/empty.xml @@ -0,0 +1,10 @@ +<domaincheckpoint> + <name>1525889631</name> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + </disks> + <domain> + <uuid>9d37b878-a7cc-9f9a-b78f-49b3abad25a8</uuid>
This looks strange without the name... and the domain type like the sample.xml output has
+ </domain> +</domaincheckpoint> diff --git a/tests/domaincheckpointxml2xmlout/sample.xml b/tests/domaincheckpointxml2xmlout/sample.xml new file mode 100644 index 0000000000..559b29c8c1 --- /dev/null +++ b/tests/domaincheckpointxml2xmlout/sample.xml @@ -0,0 +1,16 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <creationTime>1525889631</creationTime> + <parent> + <name>1525111885</name> + </parent> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/>
Maybe this one should show the size attribute too... I need to stop here for the day, but will continue... John
+ <disk name='vdb' checkpoint='no'/> + </disks> + <domain type='qemu'> + <name>fedora</name> + <uuid>93a5c045-6457-2c09-e56c-927cdf34e178</uuid> + </domain> +</domaincheckpoint> diff --git a/tests/virschematest.c b/tests/virschematest.c index d1bcdeac9c..c3b41d5bbc 100644 --- a/tests/virschematest.c +++ b/tests/virschematest.c @@ -221,7 +221,11 @@ mymain(void) "lxcxml2xmloutdata", "bhyvexml2argvdata", "genericxml2xmlindata", "genericxml2xmloutdata", "xlconfigdata", "libxlxml2domconfigdata", "qemuhotplugtestdomains"); + DO_TEST_DIR("domainbackup.rng", "domainbackupxml2xmlin", + "domainbackupxml2xmlout"); DO_TEST_DIR("domaincaps.rng", "domaincapsschemadata"); + DO_TEST_DIR("domaincheckpoint.rng", "domaincheckpointxml2xmlin", + "domaincheckpointxml2xmlout"); DO_TEST_DIR("domainsnapshot.rng", "domainsnapshotxml2xmlin", "domainsnapshotxml2xmlout"); DO_TEST_DIR("interface.rng", "interfaceschemadata");

On 2/9/19 9:56 AM, John Ferlan wrote:
+ <ref name="unsignedLong"/>
I haven't looked ahead to conf processing, but I assume this is the same type as whatever this gets read into. Whenever I see size in reference to disks I'm thinking ULL's.
It comes from docs/schemas/basictypes.rng. We don't document it very well, but in the RNG, 'int' is 32-bit, 'unsignedLong' is 64-bit, and we don't really do much to enforce things. Your question in later patches about using 'unsigned long long' C type to match 'unsignedLong' in the RNG is not unexpected, but this is not the first time we've had that sort of mismatch in naming (as the 'unsignedLong' as 64-bit type is a common idiom in the RNG).
+++ b/tests/domainbackupxml2xmlin/empty.xml @@ -0,0 +1 @@ +<domainbackup/>
Should add a 'block' and 'network' example too for completeness - of course that depends on the matching between docs and rng.
Should add target w/ shallow=on for completeness.
Yes, I recall the commit message indicating essentially minimal.
Yes, I need to beef up my examples, and make sure the testsuite passes.
+++ b/tests/domaincheckpointxml2xmlout/empty.xml @@ -0,0 +1,10 @@ +<domaincheckpoint> + <name>1525889631</name> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + </disks> + <domain> + <uuid>9d37b878-a7cc-9f9a-b78f-49b3abad25a8</uuid>
This looks strange without the name... and the domain type like the sample.xml output has
Here's part of my reason for still having 'wip' on later patches - the snapshot code USED to just take a single UUID, until we released a couple of releases later that you can't revert to a domain state unless you capture the entire <domain> at the point in time you plan to revert to (as hot-plug actions after that time must be rolled back for the reversion to be correct). So the snapshot code has a horrible hack of taking either a UUID or a full <domain> sub-element. I don't want to repeat that hack in <checkpoint> - I want a full <domain> element every time. But coming up with the absolute minimum <domain> that does not make the test balloon into a super-long demonstration while still passing the RNG as a valid XML was where I got stuck, especially since the user does NOT pass in a <domain> sublement except when using the _REDEFINE flag (the rest of the time, the <domain> sublement is computed at creation time from the virDomainPtr that is gaining a new checkpoint).
+++ b/tests/domaincheckpointxml2xmlout/sample.xml @@ -0,0 +1,16 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <creationTime>1525889631</creationTime> + <parent> + <name>1525111885</name> + </parent> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/>
Maybe this one should show the size attribute too...
No, I want size to be an output-only element, and one you have to pass in an additional flag to get (because it requires runtime computation, rather than a static output). Oh, and that reminds me of another long-standing yak hair - we introduced the ability to validate some of our XML against the RNG, but we have not finished wiring it up to all of the APIs that take XML. Domain snapshots don't have a validation flag, and hence my copy-and-paste code for checkpoints don't have one, but both need one. (Otherwise, the presence or absence of an ignored <size> element won't really matter whether the RNG permits or rejects it) -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

[...] I placed reviewing other changes higher over returning to this series to answer, agree with, provide feedback, etc. on anything that you responded to. Also, trying to avoid doing too many things at one time. So I'll just provide some extra thoughts and wait for v5. I think we're at a point where nickle and diming over minutiae is taking too much time away from getting something upstream as soon as possible.
+++ b/tests/domaincheckpointxml2xmlout/empty.xml @@ -0,0 +1,10 @@ +<domaincheckpoint> + <name>1525889631</name> + <creationTime>1525889631</creationTime> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/> + </disks> + <domain> + <uuid>9d37b878-a7cc-9f9a-b78f-49b3abad25a8</uuid>
This looks strange without the name... and the domain type like the sample.xml output has
Here's part of my reason for still having 'wip' on later patches - the snapshot code USED to just take a single UUID, until we released a couple of releases later that you can't revert to a domain state unless you capture the entire <domain> at the point in time you plan to revert to (as hot-plug actions after that time must be rolled back for the reversion to be correct). So the snapshot code has a horrible hack of taking either a UUID or a full <domain> sub-element. I don't want to repeat that hack in <checkpoint> - I want a full <domain> element every time. But coming up with the absolute minimum <domain> that does not make the test balloon into a super-long demonstration while still passing the RNG as a valid XML was where I got stuck, especially since the user does NOT pass in a <domain> sublement except when using the _REDEFINE flag (the rest of the time, the <domain> sublement is computed at creation time from the virDomainPtr that is gaining a new checkpoint).
OK - makes sense with the additional details. Whatever you're considering a minimum could just be added in.
+++ b/tests/domaincheckpointxml2xmlout/sample.xml @@ -0,0 +1,16 @@ +<domaincheckpoint> + <name>1525889631</name> + <description>Completion of updates after OS install</description> + <creationTime>1525889631</creationTime> + <parent> + <name>1525111885</name> + </parent> + <disks> + <disk name='vda' checkpoint='bitmap' bitmap='1525889631'/>
Maybe this one should show the size attribute too...
No, I want size to be an output-only element, and one you have to pass in an additional flag to get (because it requires runtime computation, rather than a static output).
Fair enough. It was mostly a well let's cover all the bases type note.
Oh, and that reminds me of another long-standing yak hair - we introduced the ability to validate some of our XML against the RNG, but we have not finished wiring it up to all of the APIs that take XML. Domain snapshots don't have a validation flag, and hence my copy-and-paste code for checkpoints don't have one, but both need one. (Otherwise, the presence or absence of an ignored <size> element won't really matter whether the RNG permits or rejects it)
So avoiding domain snapshot code isn't only something I've done then! John

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. The full list of new API: virDomainCheckpointCreateXML; virDomainCheckpointCurrent; virDomainCheckpointDelete; virDomainCheckpointFree; virDomainCheckpointGetConnect; virDomainCheckpointGetDomain; virDomainCheckpointGetParent; virDomainCheckpointGetXMLDesc; virDomainCheckpointHasMetadata; virDomainCheckpointIsCurrent; virDomainCheckpointListChildren; virDomainCheckpointLookupByName; virDomainCheckpointRef; virDomainHasCurrentCheckpoint; virDomainListCheckpoints; virDomainCheckpointGetName; Signed-off-by: Eric Blake <eblake@redhat.com> --- include/libvirt/libvirt-domain-checkpoint.h | 155 +++++ include/libvirt/libvirt.h | 5 +- src/driver-hypervisor.h | 60 +- 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 | 723 ++++++++++++++++++++ src/libvirt_public.syms | 20 + 12 files changed, 971 insertions(+), 4 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..9006a46c6e --- /dev/null +++ b/include/libvirt/libvirt-domain-checkpoint.h @@ -0,0 +1,155 @@ +/* + * 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_CURRENT = (1 << 1), /* With redefine, make + checkpoint current */ + VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA = (1 << 2), /* Make checkpoint without + remembering it */ + /* TODO: VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE */ +} 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 virDomainListCheckpoints() and + * virDomainCheckpointListChildren(). 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_LEAVES = (1 << 1), /* Filter by checkpoints + with no children */ + VIR_DOMAIN_CHECKPOINT_LIST_NO_LEAVES = (1 << 2), /* Filter by checkpoints + that have children */ + + VIR_DOMAIN_CHECKPOINT_LIST_METADATA = (1 << 3), /* Filter by checkpoints + which have metadata */ + VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA = (1 << 4), /* Filter by checkpoints + with no metadata */ +} virDomainCheckpointListFlags; + +/* Get all checkpoint objects for this domain */ +int virDomainListCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags); + +/* Get all checkpoint object children for this checkpoint */ +int virDomainCheckpointListChildren(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **children, + unsigned int flags); + +/* Get a handle to a named checkpoint */ +virDomainCheckpointPtr virDomainCheckpointLookupByName(virDomainPtr domain, + const char *name, + unsigned int flags); + +/* Check whether a domain has a checkpoint which is currently used */ +int virDomainHasCurrentCheckpoint(virDomainPtr domain, unsigned int flags); + +/* Get a handle to the current checkpoint */ +virDomainCheckpointPtr virDomainCheckpointCurrent(virDomainPtr domain, + unsigned int flags); + +/* Get a handle to the parent checkpoint, if one exists */ +virDomainCheckpointPtr virDomainCheckpointGetParent(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +/* Determine if a checkpoint is the current checkpoint of its domain. */ +int virDomainCheckpointIsCurrent(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +/* Determine if checkpoint has metadata that would prevent domain deletion. */ +int virDomainCheckpointHasMetadata(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.h b/include/libvirt/libvirt.h index b7238bd96e..5db12783be 100644 --- a/include/libvirt/libvirt.h +++ b/include/libvirt/libvirt.h @@ -4,7 +4,7 @@ * Description: Provides the interfaces of the libvirt library to handle * virtualized domains * - * Copyright (C) 2005-2006, 2010-2014 Red Hat, Inc. + * Copyright (C) 2005-2006, 2010-2018 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 @@ -34,8 +34,7 @@ extern "C" { # include <libvirt/libvirt-common.h> # include <libvirt/libvirt-host.h> # include <libvirt/libvirt-domain.h> -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/driver-hypervisor.h b/src/driver-hypervisor.h index 5315e33dde..14db8e49f3 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -1,7 +1,7 @@ /* * driver-hypervisor.h: entry points for hypervisor drivers * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 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 @@ -1328,6 +1328,53 @@ 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 +(*virDrvDomainListCheckpoints)(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointListChildren)(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **children, + unsigned int flags); + +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointLookupByName)(virDomainPtr domain, + const char *name, + unsigned int flags); + +typedef int +(*virDrvDomainHasCurrentCheckpoint)(virDomainPtr domain, + unsigned int flags); + +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointGetParent)(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointCurrent)(virDomainPtr domain, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointIsCurrent)(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointHasMetadata)(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointDelete)(virDomainCheckpointPtr checkpoint, + unsigned int flags); typedef struct _virHypervisorDriver virHypervisorDriver; typedef virHypervisorDriver *virHypervisorDriverPtr; @@ -1580,6 +1627,17 @@ struct _virHypervisorDriver { virDrvConnectBaselineHypervisorCPU connectBaselineHypervisorCPU; virDrvNodeGetSEVInfo nodeGetSEVInfo; virDrvDomainGetLaunchSecurityInfo domainGetLaunchSecurityInfo; + virDrvDomainCheckpointCreateXML domainCheckpointCreateXML; + virDrvDomainCheckpointGetXMLDesc domainCheckpointGetXMLDesc; + virDrvDomainListCheckpoints domainListCheckpoints; + virDrvDomainCheckpointListChildren domainCheckpointListChildren; + virDrvDomainCheckpointLookupByName domainCheckpointLookupByName; + virDrvDomainHasCurrentCheckpoint domainHasCurrentCheckpoint; + virDrvDomainCheckpointGetParent domainCheckpointGetParent; + virDrvDomainCheckpointCurrent domainCheckpointCurrent; + virDrvDomainCheckpointIsCurrent domainCheckpointIsCurrent; + virDrvDomainCheckpointHasMetadata domainCheckpointHasMetadata; + virDrvDomainCheckpointDelete domainCheckpointDelete; }; diff --git a/docs/Makefile.am b/docs/Makefile.am index bd7bc1a431..88c1e83275 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -25,6 +25,7 @@ apihtml = \ apihtml_generated = \ html/libvirt-libvirt-common.html \ html/libvirt-libvirt-domain.html \ + html/libvirt-libvirt-domain-checkpoint.html \ html/libvirt-libvirt-domain-snapshot.html \ html/libvirt-libvirt-event.html \ html/libvirt-libvirt-host.html \ @@ -316,6 +317,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 \ @@ -332,6 +334,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 4914e7dbed..596bc1613d 100644 --- a/docs/docs.html.in +++ b/docs/docs.html.in @@ -97,6 +97,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 2e9213510d..08c8a896e0 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1873,6 +1873,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 a7b697d7bd..be735bce48 100644 --- a/mingw-libvirt.spec.in +++ b/mingw-libvirt.spec.in @@ -271,6 +271,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 @@ -360,6 +361,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 9dd4ee7d99..88af551664 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 8c8dfe3dcf..c871e12d8f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -176,6 +176,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 \ @@ -727,6 +728,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..8a7b5b3c56 --- /dev/null +++ b/src/libvirt-domain-checkpoint.c @@ -0,0 +1,723 @@ +/* + * libvirt-domain-checkpoint.c: entry points for virDomainCheckpointPtr APIs + * + * Copyright (C) 2006-2014, 2018 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 on a running @domain. + * Typically, it is more common to create a new checkpoint as part of + * kicking off a backup job with virDomainBackupBegin(); however, it + * is also possible to start a checkpoint without a backup. + * + * 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 + * discarded, rather than creating a new checkpoint. When redefining + * checkpoint metadata, the current checkpoint will not be altered + * unless the VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT flag is also + * present. It is an error to request the + * VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT flag without + * VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE. + * + * If @flags includes VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA, then + * the domain's disk images are modified according to @xmlDesc, but + * then the just-created checkpoint has its metadata deleted. 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_REQUIRE_FLAG_GOTO(VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT, + VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, + error); + + VIR_EXCLUSIVE_FLAGS_GOTO(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, + VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA, + 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 snapshot 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; +} + + +/** + * virDomainListCheckpoints: + * @domain: a domain object + * @checkpoints: 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 virDomainCheckpoinListFlags + * + * Collect the list of domain checkpoints for the given domain, and allocate + * an array to store those objects. + * + * 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, where each group contains bits that describe mutually exclusive + * attributes of a checkpoint, and where all bits within a group describe + * all possible checkpoints. Some hypervisors might reject explicit bits + * from a group where the hypervisor cannot make a distinction. For a + * group supported by a given hypervisor, the behavior when no bits of a + * group are set is identical to the behavior when all bits in that group + * are set. When setting bits from more than one group, it is possible to + * select an impossible combination, in that case a 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). + * + * The next group of @flags is VIR_DOMAIN_CHECKPOINT_LIST_METADATA and + * VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA, for filtering checkpoints based on + * whether they have metadata that would prevent the removal of the last + * reference to a domain. + * + * 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 +virDomainListCheckpoints(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->domainListCheckpoints) { + int ret = conn->driver->domainListCheckpoints(domain, checkpoints, + flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointListChildren: + * @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. + * + * 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. Also, some filters are provided in + * groups, where each group contains bits that describe mutually exclusive + * attributes of a snapshot, and where all bits within a group describe + * all possible snapshots. Some hypervisors might reject explicit bits + * from a group where the hypervisor cannot make a distinction. For a + * group supported by a given hypervisor, the behavior when no bits of a + * group are set is identical to the behavior when all bits in that group + * are set. When setting bits from more than one group, it is possible to + * select an impossible combination, in that case a 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). + * + * The next group of @flags is VIR_DOMAIN_CHECKPOINT_LIST_METADATA and + * VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA, for filtering checkpoints based on + * whether they have metadata that would prevent the removal of the last + * reference to a domain. + * + * 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 +virDomainCheckpointListChildren(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->domainCheckpointListChildren) { + int ret = conn->driver->domainCheckpointListChildren(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 dom; + dom = conn->driver->domainCheckpointLookupByName(domain, name, flags); + if (!dom) + goto error; + return dom; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainHasCurrentCheckpoint: + * @domain: pointer to the domain object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Determine if the domain has a current checkpoint. + * + * Returns 1 if such checkpoint exists, 0 if it doesn't, -1 on error. + */ +int +virDomainHasCurrentCheckpoint(virDomainPtr domain, unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "flags=0x%x", flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + if (conn->driver->domainHasCurrentCheckpoint) { + int ret = conn->driver->domainHasCurrentCheckpoint(domain, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointCurrent: + * @domain: a domain object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Get the current checkpoint for a domain, 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 + * current domain checkpoint cannot be found, then the + * VIR_ERR_NO_DOMAIN_CHECKPOINT error is raised. + */ +virDomainCheckpointPtr +virDomainCheckpointCurrent(virDomainPtr domain, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "flags=0x%x", flags); + + virResetLastError(); + + virCheckDomainReturn(domain, NULL); + conn = domain->conn; + + if (conn->driver->domainCheckpointCurrent) { + virDomainCheckpointPtr snap; + snap = conn->driver->domainCheckpointCurrent(domain, flags); + if (!snap) + goto error; + return snap; + } + + 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 snap; + snap = conn->driver->domainCheckpointGetParent(checkpoint, flags); + if (!snap) + goto error; + return snap; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainCheckpointIsCurrent: + * @checkpoint: a checkpoint object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Determine if the given checkpoint is the domain's current checkpoint. See + * also virDomainHasCurrentCheckpoint(). + * + * Returns 1 if current, 0 if not current, or -1 on error. + */ +int +virDomainCheckpointIsCurrent(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("checkpoint=%p, flags=0x%x", checkpoint, flags); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, -1); + conn = checkpoint->domain->conn; + + if (conn->driver->domainCheckpointIsCurrent) { + int ret; + ret = conn->driver->domainCheckpointIsCurrent(checkpoint, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointHasMetadata: + * @checkpoint: a checkpoint object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Determine if the given checkpoint is associated with libvirt metadata + * that would prevent the deletion of the domain. + * + * Returns 1 if the checkpoint has metadata, 0 if the checkpoint exists without + * help from libvirt, or -1 on error. + */ +int +virDomainCheckpointHasMetadata(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("checkpoint=%p, flags=0x%x", checkpoint, flags); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, -1); + conn = checkpoint->domain->conn; + + if (conn->driver->domainCheckpointHasMetadata) { + int ret; + ret = conn->driver->domainCheckpointHasMetadata(checkpoint, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointDelete: + * @checkpoint: the checkpoint to remove + * @flags: not used yet, pass 0 + * @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. Likewise, if the + * checkpoint being deleted was the current checkpoint, the parent + * checkpoint becomes the new current checkpoint. + * + * 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_public.syms b/src/libvirt_public.syms index 042b4df043..1b18402e5e 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -814,4 +814,24 @@ LIBVIRT_4.10.0 { virDomainSetIOThreadParams; } LIBVIRT_4.5.0; +LIBVIRT_5.1.0 { + global: + virDomainCheckpointCreateXML; + virDomainCheckpointCurrent; + virDomainCheckpointDelete; + virDomainCheckpointFree; + virDomainCheckpointGetConnect; + virDomainCheckpointGetDomain; + virDomainCheckpointGetName; + virDomainCheckpointGetParent; + virDomainCheckpointGetXMLDesc; + virDomainCheckpointHasMetadata; + virDomainCheckpointIsCurrent; + virDomainCheckpointListChildren; + virDomainCheckpointLookupByName; + virDomainCheckpointRef; + virDomainHasCurrentCheckpoint; + virDomainListCheckpoints; +} LIBVIRT_4.10.0; + # .... define new API here using predicted next version number .... -- 2.20.1

On 2/6/19 2:18 PM, 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.
The full list of new API: virDomainCheckpointCreateXML; virDomainCheckpointCurrent; virDomainCheckpointDelete; virDomainCheckpointFree; virDomainCheckpointGetConnect; virDomainCheckpointGetDomain; virDomainCheckpointGetParent; virDomainCheckpointGetXMLDesc; virDomainCheckpointHasMetadata; virDomainCheckpointIsCurrent; virDomainCheckpointListChildren; virDomainCheckpointLookupByName; virDomainCheckpointRef; virDomainHasCurrentCheckpoint; virDomainListCheckpoints; virDomainCheckpointGetName;
Signed-off-by: Eric Blake <eblake@redhat.com> --- include/libvirt/libvirt-domain-checkpoint.h | 155 +++++ include/libvirt/libvirt.h | 5 +- src/driver-hypervisor.h | 60 +- 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 | 723 ++++++++++++++++++++ src/libvirt_public.syms | 20 + 12 files changed, 971 insertions(+), 4 deletions(-) create mode 100644 include/libvirt/libvirt-domain-checkpoint.h create mode 100644 src/libvirt-domain-checkpoint.c
After reading and commenting further I came back here and started wondering if we should just name this 'libvirt-domain-backup' since checkpoint is part of backup... Then of course adjust various comments accordingly to indicate checkpoint and backup. Of course there's also something to be said for separating out the domain-backup API's into their own module too. So while you may see comments later about splitting just keep this secondary thought in mind.
diff --git a/include/libvirt/libvirt-domain-checkpoint.h b/include/libvirt/libvirt-domain-checkpoint.h new file mode 100644 index 0000000000..9006a46c6e --- /dev/null +++ b/include/libvirt/libvirt-domain-checkpoint.h @@ -0,0 +1,155 @@ +/* + * 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.
New file right, so should this just be 2019? There's varying opinions to follow on this...
+ * + * 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_CURRENT = (1 << 1), /* With redefine, make + checkpoint current */ + VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA = (1 << 2), /* Make checkpoint without + remembering it */ + /* TODO: VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE */
Need to remove the TODO or add the flag of course...
+} 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 virDomainListCheckpoints() and + * virDomainCheckpointListChildren(). 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_LEAVES = (1 << 1), /* Filter by checkpoints + with no children */ + VIR_DOMAIN_CHECKPOINT_LIST_NO_LEAVES = (1 << 2), /* Filter by checkpoints + that have children */ + + VIR_DOMAIN_CHECKPOINT_LIST_METADATA = (1 << 3), /* Filter by checkpoints + which have metadata */ + VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA = (1 << 4), /* Filter by checkpoints + with no metadata */ +} virDomainCheckpointListFlags; + +/* Get all checkpoint objects for this domain */ +int virDomainListCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags); + +/* Get all checkpoint object children for this checkpoint */ +int virDomainCheckpointListChildren(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **children, + unsigned int flags); + +/* Get a handle to a named checkpoint */ +virDomainCheckpointPtr virDomainCheckpointLookupByName(virDomainPtr domain, + const char *name, + unsigned int flags); + +/* Check whether a domain has a checkpoint which is currently used */ +int virDomainHasCurrentCheckpoint(virDomainPtr domain, unsigned int flags); + +/* Get a handle to the current checkpoint */ +virDomainCheckpointPtr virDomainCheckpointCurrent(virDomainPtr domain, + unsigned int flags); + +/* Get a handle to the parent checkpoint, if one exists */ +virDomainCheckpointPtr virDomainCheckpointGetParent(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +/* Determine if a checkpoint is the current checkpoint of its domain. */ +int virDomainCheckpointIsCurrent(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +/* Determine if checkpoint has metadata that would prevent domain deletion. */ +int virDomainCheckpointHasMetadata(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.h b/include/libvirt/libvirt.h index b7238bd96e..5db12783be 100644 --- a/include/libvirt/libvirt.h +++ b/include/libvirt/libvirt.h @@ -4,7 +4,7 @@ * Description: Provides the interfaces of the libvirt library to handle * virtualized domains * - * Copyright (C) 2005-2006, 2010-2014 Red Hat, Inc. + * Copyright (C) 2005-2006, 2010-2018 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 @@ -34,8 +34,7 @@ extern "C" { # include <libvirt/libvirt-common.h> # include <libvirt/libvirt-host.h> # include <libvirt/libvirt-domain.h> -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/driver-hypervisor.h b/src/driver-hypervisor.h index 5315e33dde..14db8e49f3 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -1,7 +1,7 @@ /* * driver-hypervisor.h: entry points for hypervisor drivers * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 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 @@ -1328,6 +1328,53 @@ 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 +(*virDrvDomainListCheckpoints)(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointListChildren)(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **children, + unsigned int flags); + +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointLookupByName)(virDomainPtr domain, + const char *name, + unsigned int flags); + +typedef int +(*virDrvDomainHasCurrentCheckpoint)(virDomainPtr domain, + unsigned int flags); + +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointGetParent)(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +typedef virDomainCheckpointPtr +(*virDrvDomainCheckpointCurrent)(virDomainPtr domain, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointIsCurrent)(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointHasMetadata)(virDomainCheckpointPtr checkpoint, + unsigned int flags); + +typedef int +(*virDrvDomainCheckpointDelete)(virDomainCheckpointPtr checkpoint, + unsigned int flags);
typedef struct _virHypervisorDriver virHypervisorDriver; typedef virHypervisorDriver *virHypervisorDriverPtr; @@ -1580,6 +1627,17 @@ struct _virHypervisorDriver { virDrvConnectBaselineHypervisorCPU connectBaselineHypervisorCPU; virDrvNodeGetSEVInfo nodeGetSEVInfo; virDrvDomainGetLaunchSecurityInfo domainGetLaunchSecurityInfo; + virDrvDomainCheckpointCreateXML domainCheckpointCreateXML; + virDrvDomainCheckpointGetXMLDesc domainCheckpointGetXMLDesc; + virDrvDomainListCheckpoints domainListCheckpoints; + virDrvDomainCheckpointListChildren domainCheckpointListChildren; + virDrvDomainCheckpointLookupByName domainCheckpointLookupByName; + virDrvDomainHasCurrentCheckpoint domainHasCurrentCheckpoint; + virDrvDomainCheckpointGetParent domainCheckpointGetParent; + virDrvDomainCheckpointCurrent domainCheckpointCurrent; + virDrvDomainCheckpointIsCurrent domainCheckpointIsCurrent; + virDrvDomainCheckpointHasMetadata domainCheckpointHasMetadata; + virDrvDomainCheckpointDelete domainCheckpointDelete; };
Since we're adding new I think we should be consistent w/ naming virDrvDomainCheckpoint* and domainCheckpoint* DomainListCheckpoints and DomainHasCurrent break the mold, so to speak.
diff --git a/docs/Makefile.am b/docs/Makefile.am index bd7bc1a431..88c1e83275 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -25,6 +25,7 @@ apihtml = \ apihtml_generated = \ html/libvirt-libvirt-common.html \ html/libvirt-libvirt-domain.html \ + html/libvirt-libvirt-domain-checkpoint.html \ html/libvirt-libvirt-domain-snapshot.html \ html/libvirt-libvirt-event.html \ html/libvirt-libvirt-host.html \ @@ -316,6 +317,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 \ @@ -332,6 +334,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 4914e7dbed..596bc1613d 100644 --- a/docs/docs.html.in +++ b/docs/docs.html.in @@ -97,6 +97,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 2e9213510d..08c8a896e0 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1873,6 +1873,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 a7b697d7bd..be735bce48 100644 --- a/mingw-libvirt.spec.in +++ b/mingw-libvirt.spec.in @@ -271,6 +271,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 @@ -360,6 +361,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 9dd4ee7d99..88af551664 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 8c8dfe3dcf..c871e12d8f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -176,6 +176,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 \ @@ -727,6 +728,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..8a7b5b3c56 --- /dev/null +++ b/src/libvirt-domain-checkpoint.c @@ -0,0 +1,723 @@ +/* + * libvirt-domain-checkpoint.c: entry points for virDomainCheckpointPtr APIs + * + * Copyright (C) 2006-2014, 2018 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)
I see a copy of virDomainSnapshotGetConnect - part of me wonders why since this isn't the Checkpoint's @conn, why shouldn't the API writer use virDomainGetConnect once they have the @domain object. No big deal, just a while I'm reading type comment...
+{ + 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 on a running @domain. + * Typically, it is more common to create a new checkpoint as part of + * kicking off a backup job with virDomainBackupBegin(); however, it + * is also possible to start a checkpoint without a backup.
NIT, but no big deal... Patch order has virDomainBackupBegin in the next patch...
+ * + * 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 + * discarded, rather than creating a new checkpoint. When redefining + * checkpoint metadata, the current checkpoint will not be altered + * unless the VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT flag is also + * present. It is an error to request the + * VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT flag without + * VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE. + * + * If @flags includes VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA, then + * the domain's disk images are modified according to @xmlDesc, but + * then the just-created checkpoint has its metadata deleted. This
s/just-created/just created/
+ * flag is incompatible with VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE. + * + * Returns an (opaque) new virDomainCheckpointPtr on success, or NULL
s/, or/ or/
+ * 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_REQUIRE_FLAG_GOTO(VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT, + VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, + error); + + VIR_EXCLUSIVE_FLAGS_GOTO(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE, + VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA, + 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 snapshot 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.
s/, or/ or/
+ * the caller must free() the returned value.
s/the/The/ the indention is probably unnecessary too.
+ */ +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; +} + + +/** + * virDomainListCheckpoints: + * @domain: a domain object + * @checkpoints: pointer to variable to store the array containing checkpoint + * objects, or NULL if the list is not required (just returns
s/, or/ or/
+ * number of checkpoints) + * @flags: bitwise-OR of supported virDomainCheckpoinListFlags + * + * Collect the list of domain checkpoints for the given domain, and allocate
s/, and/ and/
+ * an array to store those objects. + * + * By default, this command covers all checkpoints; it is also possible to
s/; it/. It/
+ * limit things to just checkpoints with no parents, when @flags includes + * VIR_DOMAIN_CHECKPOINT_LIST_ROOTS. Additional filters are provided in + * groups, where each group contains bits that describe mutually exclusive + * attributes of a checkpoint, and where all bits within a group describe
s/, and/ and/
+ * all possible checkpoints. Some hypervisors might reject explicit bits + * from a group where the hypervisor cannot make a distinction. For a + * group supported by a given hypervisor, the behavior when no bits of a + * group are set is identical to the behavior when all bits in that group + * are set. When setting bits from more than one group, it is possible to + * select an impossible combination, in that case a hypervisor may return + * either 0 or an error.
Not clear what a group is within this context and how one defines that, uses that, and of course the CYA condition of impossible combination ;-). Of course groups start to be clearer the following description... Not that I completely understand them. Seems to add levels of complexity.
+ * + * 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). + * + * The next group of @flags is VIR_DOMAIN_CHECKPOINT_LIST_METADATA and + * VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA, for filtering checkpoints based on + * whether they have metadata that would prevent the removal of the last + * reference to a domain. + * + * 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 +virDomainListCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags)
Why not virDomainCheckpointListAll (beyond the difference w/ Snapshots)
+{ + 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->domainListCheckpoints) { + int ret = conn->driver->domainListCheckpoints(domain, checkpoints, + flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointListChildren: + * @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
s/, or/ or/
+ * 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.
s/, and/ and/
+ * + * 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. Also, some filters are provided in + * groups, where each group contains bits that describe mutually exclusive + * attributes of a snapshot, and where all bits within a group describe
s/, and/ and/
+ * all possible snapshots. Some hypervisors might reject explicit bits + * from a group where the hypervisor cannot make a distinction. For a + * group supported by a given hypervisor, the behavior when no bits of a + * group are set is identical to the behavior when all bits in that group + * are set. When setting bits from more than one group, it is possible to + * select an impossible combination, in that case a hypervisor may return + * either 0 or an error.
Can we just say similar to ListAll - that way we don't need to update both. If these really are similar API's then perhaps they could be combined such that if @checkpoint == NULL, then we're listing from root; otherwise, we're listing from @checkpoint. Assuming checkpoint == NULL is ListAll right? Could be easier to not have ListAll and ListChildren...
+ * + * 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). + * + * The next group of @flags is VIR_DOMAIN_CHECKPOINT_LIST_METADATA and + * VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA, for filtering checkpoints based on + * whether they have metadata that would prevent the removal of the last + * reference to a domain. + * + * 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 +virDomainCheckpointListChildren(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->domainCheckpointListChildren) { + int ret = conn->driver->domainCheckpointListChildren(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 dom; + dom = conn->driver->domainCheckpointLookupByName(domain, name, flags); + if (!dom) + goto error; + return dom; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainHasCurrentCheckpoint: + * @domain: pointer to the domain object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Determine if the domain has a current checkpoint. + * + * Returns 1 if such checkpoint exists, 0 if it doesn't, -1 on error. + */ +int +virDomainHasCurrentCheckpoint(virDomainPtr domain, unsigned int flags)
Use one line for each argument. Why not virDomainCheckpointHasCurrent to keep the virDomainCheckpoint* namespace consistent?
+{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "flags=0x%x", flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + if (conn->driver->domainHasCurrentCheckpoint) { + int ret = conn->driver->domainHasCurrentCheckpoint(domain, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointCurrent: + * @domain: a domain object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Get the current checkpoint for a domain, 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 + * current domain checkpoint cannot be found, then the + * VIR_ERR_NO_DOMAIN_CHECKPOINT error is raised. + */ +virDomainCheckpointPtr +virDomainCheckpointCurrent(virDomainPtr domain, + unsigned int flags)
Why isn't this virDomainCheckpointGetCurrent for consistency?
+{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "flags=0x%x", flags); + + virResetLastError(); + + virCheckDomainReturn(domain, NULL); + conn = domain->conn; + + if (conn->driver->domainCheckpointCurrent) { + virDomainCheckpointPtr snap; + snap = conn->driver->domainCheckpointCurrent(domain, flags); + if (!snap) + goto error; + return snap; + } + + 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 snap; + snap = conn->driver->domainCheckpointGetParent(checkpoint, flags); + if (!snap) + goto error; + return snap; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainCheckpointIsCurrent: + * @checkpoint: a checkpoint object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Determine if the given checkpoint is the domain's current checkpoint. See + * also virDomainHasCurrentCheckpoint(). + * + * Returns 1 if current, 0 if not current, or -1 on error. + */ +int +virDomainCheckpointIsCurrent(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("checkpoint=%p, flags=0x%x", checkpoint, flags); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, -1); + conn = checkpoint->domain->conn; + + if (conn->driver->domainCheckpointIsCurrent) { + int ret; + ret = conn->driver->domainCheckpointIsCurrent(checkpoint, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointHasMetadata: + * @checkpoint: a checkpoint object + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * Determine if the given checkpoint is associated with libvirt metadata + * that would prevent the deletion of the domain. + * + * Returns 1 if the checkpoint has metadata, 0 if the checkpoint exists without + * help from libvirt, or -1 on error. + */ +int +virDomainCheckpointHasMetadata(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DEBUG("checkpoint=%p, flags=0x%x", checkpoint, flags); + + virResetLastError(); + + virCheckDomainCheckpointReturn(checkpoint, -1); + conn = checkpoint->domain->conn; + + if (conn->driver->domainCheckpointHasMetadata) { + int ret; + ret = conn->driver->domainCheckpointHasMetadata(checkpoint, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainCheckpointDelete: + * @checkpoint: the checkpoint to remove + * @flags: not used yet, pass 0
Remove this one^^
+ * @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. Likewise, if the + * checkpoint being deleted was the current checkpoint, the parent + * checkpoint becomes the new current checkpoint. + * + * 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,
Two more flags that need to be documented above John
+ 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_public.syms b/src/libvirt_public.syms index 042b4df043..1b18402e5e 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -814,4 +814,24 @@ LIBVIRT_4.10.0 { virDomainSetIOThreadParams; } LIBVIRT_4.5.0;
+LIBVIRT_5.1.0 { + global: + virDomainCheckpointCreateXML; + virDomainCheckpointCurrent; + virDomainCheckpointDelete; + virDomainCheckpointFree; + virDomainCheckpointGetConnect; + virDomainCheckpointGetDomain; + virDomainCheckpointGetName; + virDomainCheckpointGetParent; + virDomainCheckpointGetXMLDesc; + virDomainCheckpointHasMetadata; + virDomainCheckpointIsCurrent; + virDomainCheckpointListChildren; + virDomainCheckpointLookupByName; + virDomainCheckpointRef; + virDomainHasCurrentCheckpoint; + virDomainListCheckpoints; +} LIBVIRT_4.10.0; + # .... define new API here using predicted next version number ....

On 2/11/19 3:19 PM, John Ferlan wrote:
On 2/6/19 2:18 PM, 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.
The full list of new API: virDomainCheckpointCreateXML; virDomainCheckpointCurrent; virDomainCheckpointDelete; virDomainCheckpointFree; virDomainCheckpointGetConnect; virDomainCheckpointGetDomain; virDomainCheckpointGetParent; virDomainCheckpointGetXMLDesc; virDomainCheckpointHasMetadata; virDomainCheckpointIsCurrent; virDomainCheckpointListChildren; virDomainCheckpointLookupByName; virDomainCheckpointRef; virDomainHasCurrentCheckpoint; virDomainListCheckpoints; virDomainCheckpointGetName;
After reading and commenting further I came back here and started wondering if we should just name this 'libvirt-domain-backup' since checkpoint is part of backup... Then of course adjust various comments accordingly to indicate checkpoint and backup.
Of course there's also something to be said for separating out the domain-backup API's into their own module too.
So while you may see comments later about splitting just keep this secondary thought in mind.
Long-term, I'd like to have both virDomainBackupBegin() and some form of virDomainSnapshotCreateXML() be able to kick off the creation of a checkpoint object. Object-wise, we have virDomainSnapshotPtr and virDomainCheckpointPtr (two distinct objects that are long-term XML entities present regardless of what else you do), but no virDomainBackupPtr (a backup job is just that - a job, that goes away when it is complete). I'm actually thinking that it is better to have virDomainBackupBegin() and friends live directly in libvirt-domain.h, alongside other job-like APIs (virDomainMigrate(), virDomainBlockCopy(), ...), especially if I fix snapshots to refer to checkpoints. So there's that to think about as well when considering where to place things.
diff --git a/include/libvirt/libvirt-domain-checkpoint.h b/include/libvirt/libvirt-domain-checkpoint.h new file mode 100644 index 0000000000..9006a46c6e --- /dev/null +++ b/include/libvirt/libvirt-domain-checkpoint.h @@ -0,0 +1,155 @@ +/* + * 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.
New file right, so should this just be 2019? There's varying opinions to follow on this...
Heavily copied from an existing file, so I copied the copyright as part of that.
+typedef enum { + VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE = (1 << 0), /* Restore or alter + metadata */ + VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT = (1 << 1), /* With redefine, make + checkpoint current */ + VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA = (1 << 2), /* Make checkpoint without + remembering it */ + /* TODO: VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE */
Need to remove the TODO or add the flag of course...
I'll see if I can implement it (it probably missed 5.1, but then again, this whole series needs a respin with freeze today; but should be easy enough to get by 5.2)
@@ -1580,6 +1627,17 @@ struct _virHypervisorDriver { virDrvConnectBaselineHypervisorCPU connectBaselineHypervisorCPU; virDrvNodeGetSEVInfo nodeGetSEVInfo; virDrvDomainGetLaunchSecurityInfo domainGetLaunchSecurityInfo; + virDrvDomainCheckpointCreateXML domainCheckpointCreateXML; + virDrvDomainCheckpointGetXMLDesc domainCheckpointGetXMLDesc; + virDrvDomainListCheckpoints domainListCheckpoints; + virDrvDomainCheckpointListChildren domainCheckpointListChildren; + virDrvDomainCheckpointLookupByName domainCheckpointLookupByName; + virDrvDomainHasCurrentCheckpoint domainHasCurrentCheckpoint; + virDrvDomainCheckpointGetParent domainCheckpointGetParent; + virDrvDomainCheckpointCurrent domainCheckpointCurrent; + virDrvDomainCheckpointIsCurrent domainCheckpointIsCurrent; + virDrvDomainCheckpointHasMetadata domainCheckpointHasMetadata; + virDrvDomainCheckpointDelete domainCheckpointDelete; };
Since we're adding new I think we should be consistent w/ naming
virDrvDomainCheckpoint* and domainCheckpoint*
DomainListCheckpoints and DomainHasCurrent break the mold, so to speak.
Here, I'm copying heavily from virDomainSnapshot. The APIs that deal directly with a virDomain object (namely, listing how many checkpoints belong to the domain, and whether the domain has a current checkpoint) are distinct from the APIs that deal with a virDomainCheckpointObject (creation, getting XML, looking up children). But you are also right that there are some other misleading names (virDomainCheckpointLookupByName and virDomainCheckpointCurrent are virDomain operations). Here's what snapshots have, and how I differ: virDomainSnapshotCreateXML; virDomainSnapshotGetXMLDesc; Both operate on snapshots. Counterparts virDomainCheckpointCreateXML, virDomainCheckpointGetXMLDesc. virDomainSnapshotNum; virDomainSnapshotListNames; No checkpoint counterpart for either (no need to do the racy API when ListAllSnapshots is better) virDomainSnapshotLookupByName; virDomainHasCurrentSnapshot; virDomainSnapshotCurrent; All 3 operate on domain. Counterparts virDomainCheckpointLookupByName, virDomainHasCurrentCheckpoint, virDomainCheckpointCurrent virDomainRevertToSnapshot; Operates on domain. No counterpart (you don't revert to a checkpoint, but instead use it as the basis to an incremental virDomainBackupBegin) virDomainSnapshotDelete; virDomainSnapshotFree; virDomainSnapshotGetConnect; virDomainSnapshotGetDomain; virDomainSnapshotGetName; virDomainSnapshotGetParent; virDomainSnapshotRef; All 7 operate on snapshot, counterparts are obvious (some don't require driver callbacks, because they obtained directly from the object). virDomainSnapshotListChildrenNames; virDomainSnapshotNumChildren; Both operate on snapshot, no counterpart (no need to do the racy API when ListAllChildren is better) virDomainSnapshotHasMetadata; virDomainSnapshotIsCurrent; Both operate on snapshot, counterparts virDomainSnapshotHasMetadata, virDomainSnapshotIsCurrent virDomainListAllSnapshots; virDomainSnapshotListAllChildren; One operates on domain, the other on a snapshot. For the checkpoint counterpoints, since I didn't copy the older racy names, I didn't see the point of keeping 'All' in the name, so I used virDomainListCheckpoints and virDomainCheckpointListChildren.
+/** + * virDomainCheckpointCreateXML: + * @domain: a domain object + * @xmlDesc: description of the checkpoint to create + * @flags: bitwise-OR of supported virDomainCheckpointCreateFlags + * + * Create a new checkpoint using @xmlDesc on a running @domain. + * Typically, it is more common to create a new checkpoint as part of + * kicking off a backup job with virDomainBackupBegin(); however, it + * is also possible to start a checkpoint without a backup.
NIT, but no big deal... Patch order has virDomainBackupBegin in the next patch...
Correct; I don't know how much churn to put to keep the docs consistent mid-series, or whether to just let it work out in the end.
+ * + * 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 + * discarded, rather than creating a new checkpoint. When redefining + * checkpoint metadata, the current checkpoint will not be altered + * unless the VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT flag is also + * present. It is an error to request the + * VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT flag without + * VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE. + * + * If @flags includes VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA, then + * the domain's disk images are modified according to @xmlDesc, but + * then the just-created checkpoint has its metadata deleted. This
s/just-created/just created/
copy-and-paste from snapshots. (I can touch those up as a separate trivial patch to match any grammar changes I make here based on your review).
+ * + * Returns a 0 terminated UTF-8 encoded XML instance, or NULL in case of error.
s/, or/ or/
+ * the caller must free() the returned value.
s/the/The/
the indention is probably unnecessary too.
Looks like an editor accident on my part.
+ * virDomainListCheckpoints:
+ * limit things to just checkpoints with no parents, when @flags includes + * VIR_DOMAIN_CHECKPOINT_LIST_ROOTS. Additional filters are provided in + * groups, where each group contains bits that describe mutually exclusive + * attributes of a checkpoint, and where all bits within a group describe
s/, and/ and/
+ * all possible checkpoints. Some hypervisors might reject explicit bits + * from a group where the hypervisor cannot make a distinction. For a + * group supported by a given hypervisor, the behavior when no bits of a + * group are set is identical to the behavior when all bits in that group + * are set. When setting bits from more than one group, it is possible to + * select an impossible combination, in that case a hypervisor may return + * either 0 or an error.
Not clear what a group is within this context and how one defines that, uses that, and of course the CYA condition of impossible combination ;-).
Of course groups start to be clearer the following description... Not that I completely understand them. Seems to add levels of complexity.
A lot of this text is copy-and-paste from the snapshot counterpart; except that snapshots have two additional filter groups (_INACTIVE/_ACTIVE, _DISK_ONLY/_INTERNAL/_EXTERNAL) that aren't applicable to filter groups for checkpoints. There may indeed be ways to clean this text up, but it stems from the fact that all of libvirt's objects that can be listed with various filter groups have to define which filter groups exist based on subsets of the overall flags.
+virDomainListCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags)
Why not virDomainCheckpointListAll (beyond the difference w/ Snapshots)
As mentioned above, the snapshot version was virDomainListAllSnapshots, but only because it was a newer API designed to replace the older virDomainSnapshotListNames that was inherently racy. Since the operation is on a virDomain object (and not a virDomainCheckpoint object), the naming makes sense to me, but I'm still open to suggestions if my explanation is not convincing, or if you like the name 'All' in the description to match all the other APIs, even if I don't want to match the non-modern listing interfaces.
+ * 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. Also, some filters are provided in + * groups, where each group contains bits that describe mutually exclusive + * attributes of a snapshot, and where all bits within a group describe
s/, and/ and/
+ * all possible snapshots. Some hypervisors might reject explicit bits + * from a group where the hypervisor cannot make a distinction. For a + * group supported by a given hypervisor, the behavior when no bits of a + * group are set is identical to the behavior when all bits in that group + * are set. When setting bits from more than one group, it is possible to + * select an impossible combination, in that case a hypervisor may return + * either 0 or an error.
Can we just say similar to ListAll - that way we don't need to update both.
If these really are similar API's then perhaps they could be combined such that if @checkpoint == NULL, then we're listing from root; otherwise, we're listing from @checkpoint. Assuming checkpoint == NULL is ListAll right?
Well, we could, except that one API takes a virDomainPtr, the other takes only a virDomainCheckpointPtr (where the associated virDomainPtr is grabbed implicitly from the checkpoint object), and you CAN'T pass a NULL virDomainCheckpointPtr (because you'd then be lacking a virDomainPtr object to use).
Could be easier to not have ListAll and ListChildren...
Snapshots have both (list a filtered set of all snapshots from a domain, and list a filtered set of descendent snapshots from a parent); having both was pretty easy to do just by copying concepts.
+int +virDomainHasCurrentCheckpoint(virDomainPtr domain, unsigned int flags)
Use one line for each argument.
Why not virDomainCheckpointHasCurrent to keep the virDomainCheckpoint* namespace consistent?
Mentioned above - because virDomainHasCurrentSnapshot() is likewise operating on a virDomainPtr.
+virDomainCheckpointPtr +virDomainCheckpointCurrent(virDomainPtr domain, + unsigned int flags)
Why isn't this virDomainCheckpointGetCurrent for consistency?
Again, because of existing practice with virDomainSnapshotCurrent. (I don't necessarily mind using smarter names, but it makes for that many more special cases in the language binding code, and we already have enough inconsistent oddities in the public API).
+ +/** + * virDomainCheckpointDelete: + * @checkpoint: the checkpoint to remove + * @flags: not used yet, pass 0
Remove this one^^
+ * @flags: bitwise-OR of supported virDomainCheckpointDeleteFlags
Oops - yeah. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

On 2/25/19 2:15 PM, Eric Blake wrote:
On 2/11/19 3:19 PM, John Ferlan wrote:
On 2/6/19 2:18 PM, 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.
The full list of new API: virDomainCheckpointCreateXML; virDomainCheckpointCurrent; virDomainCheckpointDelete; virDomainCheckpointFree; virDomainCheckpointGetConnect; virDomainCheckpointGetDomain; virDomainCheckpointGetParent; virDomainCheckpointGetXMLDesc; virDomainCheckpointHasMetadata; virDomainCheckpointIsCurrent; virDomainCheckpointListChildren; virDomainCheckpointLookupByName; virDomainCheckpointRef; virDomainHasCurrentCheckpoint; virDomainListCheckpoints; virDomainCheckpointGetName;
After reading and commenting further I came back here and started wondering if we should just name this 'libvirt-domain-backup' since checkpoint is part of backup... Then of course adjust various comments accordingly to indicate checkpoint and backup.
Of course there's also something to be said for separating out the domain-backup API's into their own module too.
So while you may see comments later about splitting just keep this secondary thought in mind.
Long-term, I'd like to have both virDomainBackupBegin() and some form of virDomainSnapshotCreateXML() be able to kick off the creation of a checkpoint object. Object-wise, we have virDomainSnapshotPtr and virDomainCheckpointPtr (two distinct objects that are long-term XML entities present regardless of what else you do), but no virDomainBackupPtr (a backup job is just that - a job, that goes away when it is complete). I'm actually thinking that it is better to have virDomainBackupBegin() and friends live directly in libvirt-domain.h, alongside other job-like APIs (virDomainMigrate(), virDomainBlockCopy(), ...), especially if I fix snapshots to refer to checkpoints.
So there's that to think about as well when considering where to place things.
Makes sense to me to have BackupBegin live in libvirt-domain.h Seems like merging Snapshot and Checkpoint code can be a "wishlist" type task. [...]
@@ -1580,6 +1627,17 @@ struct _virHypervisorDriver { virDrvConnectBaselineHypervisorCPU connectBaselineHypervisorCPU; virDrvNodeGetSEVInfo nodeGetSEVInfo; virDrvDomainGetLaunchSecurityInfo domainGetLaunchSecurityInfo; + virDrvDomainCheckpointCreateXML domainCheckpointCreateXML; + virDrvDomainCheckpointGetXMLDesc domainCheckpointGetXMLDesc; + virDrvDomainListCheckpoints domainListCheckpoints; + virDrvDomainCheckpointListChildren domainCheckpointListChildren; + virDrvDomainCheckpointLookupByName domainCheckpointLookupByName; + virDrvDomainHasCurrentCheckpoint domainHasCurrentCheckpoint; + virDrvDomainCheckpointGetParent domainCheckpointGetParent; + virDrvDomainCheckpointCurrent domainCheckpointCurrent; + virDrvDomainCheckpointIsCurrent domainCheckpointIsCurrent; + virDrvDomainCheckpointHasMetadata domainCheckpointHasMetadata; + virDrvDomainCheckpointDelete domainCheckpointDelete; };
Since we're adding new I think we should be consistent w/ naming
virDrvDomainCheckpoint* and domainCheckpoint*
DomainListCheckpoints and DomainHasCurrent break the mold, so to speak.
Here, I'm copying heavily from virDomainSnapshot. The APIs that deal directly with a virDomain object (namely, listing how many checkpoints belong to the domain, and whether the domain has a current checkpoint) are distinct from the APIs that deal with a virDomainCheckpointObject (creation, getting XML, looking up children). But you are also right that there are some other misleading names (virDomainCheckpointLookupByName and virDomainCheckpointCurrent are virDomain operations).
More recent changes to the hacking guide have some fairly specific guidelines about API naming. That's where my thoughts were since this is to a degree a new API set. But you made good points about consistency between Snapshots and Checkpoints as children of the Domain. In a way I see this as similar to how Volumes are a child of StoragePool objects. When in doubt I fall back to Andrea's epiphany "Naming is hard". Everyone seems to have an opinion or tweak to your chosen name. In the long run, for me it's about consistency.
Here's what snapshots have, and how I differ:
virDomainSnapshotCreateXML; virDomainSnapshotGetXMLDesc; Both operate on snapshots. Counterparts virDomainCheckpointCreateXML, virDomainCheckpointGetXMLDesc. virDomainSnapshotNum; virDomainSnapshotListNames; No checkpoint counterpart for either (no need to do the racy API when ListAllSnapshots is better) virDomainSnapshotLookupByName; virDomainHasCurrentSnapshot; virDomainSnapshotCurrent; All 3 operate on domain. Counterparts virDomainCheckpointLookupByName, virDomainHasCurrentCheckpoint, virDomainCheckpointCurrent
virDomainSnapshotCurrent probably should have been virDomainSnapshotGetCurrent... The virDomainHasCurrentSnapshot is more "magical" since it's something that was handled/saved more so internally and not exposed (prior to your other recent series). In any case, the naming does fit the current model it's just where the API is defined that throws me off. It should be in libvirt-domain.c at least "theoretically logically speaking" (how's that for wishy-washy!).
virDomainRevertToSnapshot; Operates on domain. No counterpart (you don't revert to a checkpoint, but instead use it as the basis to an incremental virDomainBackupBegin) virDomainSnapshotDelete; virDomainSnapshotFree; virDomainSnapshotGetConnect; virDomainSnapshotGetDomain; virDomainSnapshotGetName; virDomainSnapshotGetParent; virDomainSnapshotRef; All 7 operate on snapshot, counterparts are obvious (some don't require driver callbacks, because they obtained directly from the object). virDomainSnapshotListChildrenNames; virDomainSnapshotNumChildren; Both operate on snapshot, no counterpart (no need to do the racy API when ListAllChildren is better) virDomainSnapshotHasMetadata; virDomainSnapshotIsCurrent; Both operate on snapshot, counterparts virDomainSnapshotHasMetadata, virDomainSnapshotIsCurrent virDomainListAllSnapshots; virDomainSnapshotListAllChildren; One operates on domain, the other on a snapshot. For the checkpoint counterpoints, since I didn't copy the older racy names, I didn't see the point of keeping 'All' in the name, so I used virDomainListCheckpoints and virDomainCheckpointListChildren.
OK fair enough. I guess that's essentially consistent with how Volumes API's are named. The question then becomes does it belong in libvirt-domain.c or libvirt-domain-checkpoint.c... [...]
+ * all possible checkpoints. Some hypervisors might reject explicit bits + * from a group where the hypervisor cannot make a distinction. For a + * group supported by a given hypervisor, the behavior when no bits of a + * group are set is identical to the behavior when all bits in that group + * are set. When setting bits from more than one group, it is possible to + * select an impossible combination, in that case a hypervisor may return + * either 0 or an error.
Not clear what a group is within this context and how one defines that, uses that, and of course the CYA condition of impossible combination ;-).
Of course groups start to be clearer the following description... Not that I completely understand them. Seems to add levels of complexity.
A lot of this text is copy-and-paste from the snapshot counterpart; except that snapshots have two additional filter groups (_INACTIVE/_ACTIVE, _DISK_ONLY/_INTERNAL/_EXTERNAL) that aren't applicable to filter groups for checkpoints. There may indeed be ways to clean this text up, but it stems from the fact that all of libvirt's objects that can be listed with various filter groups have to define which filter groups exist based on subsets of the overall flags.
Suffice to say it's all very confusing with all the various options and gotcha. Simplicity is so much easier.
+virDomainListCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **checkpoints, + unsigned int flags)
Why not virDomainCheckpointListAll (beyond the difference w/ Snapshots)
As mentioned above, the snapshot version was virDomainListAllSnapshots, but only because it was a newer API designed to replace the older virDomainSnapshotListNames that was inherently racy. Since the operation is on a virDomain object (and not a virDomainCheckpoint object), the naming makes sense to me, but I'm still open to suggestions if my explanation is not convincing, or if you like the name 'All' in the description to match all the other APIs, even if I don't want to match the non-modern listing interfaces.
Your explanation above makes (more) sense now... Using virDomainListCheckpoints that is. John [...] The rest would be covered under the - I see more clearly now how you were thinking about this...

Introduce a few more new public APIs related to incremental backups. This builds on the previous notion of a checkpoint (without an existing checkpoint, the new API is a full backup, differing only from virDomainCopy in the point of time chosen); and also allows creation of a new checkpoint at the same time as starting the backup (after all, an incremental backup is only useful if it covers the state since the previous backup). It also enhances event reporting for signaling when a push model backup completes (where the hypervisor creates the backup); note that the pull model does not have an event (starting the backup lets a third party access the data, and only the third party knows when it is finished). The full list of new API: virDomainBackupBegin; virDomainBackupEnd; virDomainBackupGetXMLDesc; Signed-off-by: Eric Blake <eblake@redhat.com> --- include/libvirt/libvirt-domain-checkpoint.h | 21 ++ include/libvirt/libvirt-domain.h | 17 +- src/driver-hypervisor.h | 14 ++ src/qemu/qemu_blockjob.h | 1 + examples/object-events/event-test.c | 3 + src/conf/domain_conf.c | 2 +- src/libvirt-domain-checkpoint.c | 202 ++++++++++++++++++++ src/libvirt-domain.c | 8 +- src/libvirt_public.syms | 3 + tools/virsh-domain.c | 8 +- 10 files changed, 273 insertions(+), 6 deletions(-) diff --git a/include/libvirt/libvirt-domain-checkpoint.h b/include/libvirt/libvirt-domain-checkpoint.h index 9006a46c6e..6c3a85a1bf 100644 --- a/include/libvirt/libvirt-domain-checkpoint.h +++ b/include/libvirt/libvirt-domain-checkpoint.h @@ -152,4 +152,25 @@ int virDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, int virDomainCheckpointRef(virDomainCheckpointPtr checkpoint); int virDomainCheckpointFree(virDomainCheckpointPtr checkpoint); +typedef enum { + VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA = (1 << 0), /* Make checkpoint without + remembering it */ + /* TODO: VIR_DOMAIN_BACKUP_BEGIN_QUIESCE */ +} virDomainBackupBeginFlags; + +/* Begin an incremental backup job, possibly creating a checkpoint. */ +int virDomainBackupBegin(virDomainPtr domain, const char *diskXml, + const char *checkpointXml, unsigned int flags); + +/* Learn about an ongoing backup job. */ +char *virDomainBackupGetXMLDesc(virDomainPtr domain, int id, + unsigned int flags); + +typedef enum { + VIR_DOMAIN_BACKUP_END_ABORT = (1 << 0), /* Abandon a push model backup */ +} virDomainBackupEndFlags; + +/* Complete (or abort) an incremental backup job. */ +int virDomainBackupEnd(virDomainPtr domain, int id, unsigned int flags); + #endif /* LIBVIRT_DOMAIN_CHECKPOINT_H */ diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h index 0388911ded..cc23029c97 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -3,7 +3,7 @@ * Summary: APIs for management of domains * Description: Provides APIs for the management of domains * - * Copyright (C) 2006-2015 Red Hat, Inc. + * 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 @@ -2394,6 +2394,9 @@ typedef enum { * exists as long as sync is active */ VIR_DOMAIN_BLOCK_JOB_TYPE_ACTIVE_COMMIT = 4, + /* Backup (virDomainBackupBegin), job exists until virDomainBackupEnd */ + VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP = 5, + # ifdef VIR_ENUM_SENTINELS VIR_DOMAIN_BLOCK_JOB_TYPE_LAST # endif @@ -3215,6 +3218,7 @@ typedef enum { VIR_DOMAIN_JOB_OPERATION_SNAPSHOT = 6, VIR_DOMAIN_JOB_OPERATION_SNAPSHOT_REVERT = 7, VIR_DOMAIN_JOB_OPERATION_DUMP = 8, + VIR_DOMAIN_JOB_OPERATION_BACKUP = 9, # ifdef VIR_ENUM_SENTINELS VIR_DOMAIN_JOB_OPERATION_LAST @@ -3230,6 +3234,14 @@ typedef enum { */ # define VIR_DOMAIN_JOB_OPERATION "operation" +/** + * VIR_DOMAIN_JOB_ID: + * + * virDomainGetJobStats field: the id of the job (so far, only for jobs + * started by virDomainBackupBegin()), as VIR_TYPED_PARAM_INT. + */ +# define VIR_DOMAIN_JOB_ID "id" + /** * VIR_DOMAIN_JOB_TIME_ELAPSED: * @@ -4054,7 +4066,8 @@ typedef void (*virConnectDomainEventMigrationIterationCallback)(virConnectPtr co * @nparams: size of the params array * @opaque: application specific data * - * This callback occurs when a job (such as migration) running on the domain + * This callback occurs when a job (such as migration or push-model + * virDomainBackupBegin()) running on the domain * is completed. The params array will contain statistics of the just completed * job as virDomainGetJobStats would return. The callback must not free @params * (the array will be freed once the callback finishes). diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h index 14db8e49f3..4e65b8ad36 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -1376,6 +1376,17 @@ typedef int (*virDrvDomainCheckpointDelete)(virDomainCheckpointPtr checkpoint, unsigned int flags); +typedef int +(*virDrvDomainBackupBegin)(virDomainPtr domain, const char *diskXml, + const char *checkpointXml, unsigned int flags); + +typedef char * +(*virDrvDomainBackupGetXMLDesc)(virDomainPtr domain, int id, + unsigned int flags); + +typedef int +(*virDrvDomainBackupEnd)(virDomainPtr domain, int id, unsigned int flags); + typedef struct _virHypervisorDriver virHypervisorDriver; typedef virHypervisorDriver *virHypervisorDriverPtr; @@ -1638,6 +1649,9 @@ struct _virHypervisorDriver { virDrvDomainCheckpointIsCurrent domainCheckpointIsCurrent; virDrvDomainCheckpointHasMetadata domainCheckpointHasMetadata; virDrvDomainCheckpointDelete domainCheckpointDelete; + virDrvDomainBackupBegin domainBackupBegin; + virDrvDomainBackupGetXMLDesc domainBackupGetXMLDesc; + virDrvDomainBackupEnd domainBackupEnd; }; diff --git a/src/qemu/qemu_blockjob.h b/src/qemu/qemu_blockjob.h index c7325c6daf..95f53dde16 100644 --- a/src/qemu/qemu_blockjob.h +++ b/src/qemu/qemu_blockjob.h @@ -55,6 +55,7 @@ typedef enum { QEMU_BLOCKJOB_TYPE_COPY = VIR_DOMAIN_BLOCK_JOB_TYPE_COPY, QEMU_BLOCKJOB_TYPE_COMMIT = VIR_DOMAIN_BLOCK_JOB_TYPE_COMMIT, QEMU_BLOCKJOB_TYPE_ACTIVE_COMMIT = VIR_DOMAIN_BLOCK_JOB_TYPE_ACTIVE_COMMIT, + QEMU_BLOCKJOB_TYPE_BACKUP = VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP, /* Additional enum values local to qemu */ QEMU_BLOCKJOB_TYPE_INTERNAL, QEMU_BLOCKJOB_TYPE_LAST diff --git a/examples/object-events/event-test.c b/examples/object-events/event-test.c index fcf4492470..98337ad185 100644 --- a/examples/object-events/event-test.c +++ b/examples/object-events/event-test.c @@ -891,6 +891,9 @@ blockJobTypeToStr(int type) case VIR_DOMAIN_BLOCK_JOB_TYPE_ACTIVE_COMMIT: return "active layer block commit"; + + case VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP: + return "backup"; } return "unknown"; diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 0221eb0634..fa3db9266f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1030,7 +1030,7 @@ VIR_ENUM_IMPL(virDomainHPTResizing, * <mirror> XML (remaining types are not two-phase). */ VIR_ENUM_DECL(virDomainBlockJob); VIR_ENUM_IMPL(virDomainBlockJob, VIR_DOMAIN_BLOCK_JOB_TYPE_LAST, - "", "", "copy", "", "active-commit", + "", "", "copy", "", "active-commit", "", ); VIR_ENUM_IMPL(virDomainMemoryModel, diff --git a/src/libvirt-domain-checkpoint.c b/src/libvirt-domain-checkpoint.c index 8a7b5b3c56..29cefe41f1 100644 --- a/src/libvirt-domain-checkpoint.c +++ b/src/libvirt-domain-checkpoint.c @@ -721,3 +721,205 @@ virDomainCheckpointFree(virDomainCheckpointPtr checkpoint) virObjectUnref(checkpoint); return 0; } + + +/** + * virDomainBackupBegin: + * @domain: a domain object + * @diskXml: description of storage to utilize and expose during + * the backup, or NULL + * @checkpointXml: description of a checkpoint to create, or NULL + * @flags: bitwise-OR of supported virDomainBackupBeginFlags + * + * Start a point-in-time backup job for the specified disks of a + * running domain. + * + * A backup job is mutually exclusive with domain migration + * (particularly when the job sets up an NBD export, since it is not + * possible to tell any NBD clients about a server migrating between + * hosts). For now, backup jobs are also mutually exclusive with any + * other block job on the same device, although this restriction may + * be lifted in a future release. Progress of the backup job can be + * tracked via virDomainGetJobStats(). The job remains active until a + * subsequent call to virDomainBackupEnd(), even if it no longer has + * anything to copy. + * + * This API differs from virDomainBlockCopy() in that it can grab the + * state of more than one disk in parallel, and the state is captured + * as of the start of the job, rather than the end. + * + * There are two fundamental backup approaches. The first, called a + * push model, instructs the hypervisor to copy the state of the guest + * disk to the designated storage destination (which may be on the + * local file system or a network device); in this mode, the + * hypervisor writes the content of the guest disk to the destination, + * then emits VIR_DOMAIN_EVENT_ID_JOB_COMPLETED when the backup is + * either complete or failed (the backup image is invalid if the job + * is ended prior to the event being emitted). The second, called a + * pull model, instructs the hypervisor to expose the state of the + * guest disk over an NBD export; a third-party client can then + * connect to this export, and read whichever portions of the disk it + * desires. In this mode, there is no event; libvirt has to be + * informed when the third-party NBD client is done and the backup + * resources can be released. + * + * The @diskXml parameter is optional but usually provided, and + * contains details about the backup, including which backup mode to + * use, whether the backup is incremental from a previous checkpoint, + * which disks participate in the backup, the destination for a push + * model backup, and the temporary storage and NBD server details for + * a pull model backup. If omitted, the backup attempts to default to + * a push mode full backup of all disks, where libvirt generates a + * filename for each disk by appending a suffix of a timestamp in + * seconds since the Epoch. virDomainBackupGetXMLDesc() can be called + * to learn actual values selected. For more information, see + * formatcheckpoint.html#BackupAttributes. + * + * The @checkpointXml parameter is optional; if non-NULL, then libvirt + * behaves as if virDomainCheckpointCreateXML() were called with + * @checkpointXml and the flag VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA + * forwarded appropriately, atomically covering the same guest state + * that will be part of the backup. The creation of a new checkpoint + * allows for future incremental backups. Note that some hypervisors + * may require a particular disk format, such as qcow2, in order to + * take advantage of checkpoints, while allowing arbitrary formats + * if checkpoints are not involved. + * + * Returns a non-negative job id on success, or negative on failure. + * This operation returns quickly, such that a user can choose to + * start a backup job between virDomainFSFreeze() and + * virDomainFSThaw() in order to create the backup while guest I/O is + * quiesced. + */ +/* FIXME: Do we need a specific API for listing all current backup + * jobs (which, at the moment, is at most one job), or is it better to + * refactor other existing job APIs in libvirt-domain.c to have job-id + * counterparts along with a generic listing of all jobs (with flags + * for filtering to specific job types)? + */ +int +virDomainBackupBegin(virDomainPtr domain, const char *diskXml, + const char *checkpointXml, unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "diskXml=%s, checkpointXml=%s, flags=0x%x", + NULLSTR(diskXml), NULLSTR(checkpointXml), flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + virCheckReadOnlyGoto(conn->flags, error); + if (flags & VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA) + virCheckNonNullArgGoto(checkpointXml, error); + + if (conn->driver->domainBackupBegin) { + int ret; + ret = conn->driver->domainBackupBegin(domain, diskXml, checkpointXml, + flags); + if (!ret) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainBackupGetXMLDesc: + * @domain: a domain object + * @id: the id of an active backup job previously started with + * virDomainBackupBegin() + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * In some cases, a user can start a backup job without supplying all + * details, and rely on libvirt to fill in the rest (for example, + * selecting the port used for an NBD export). This API can then be + * used to learn what default values were chosen. + * + * Returns a NUL-terminated UTF-8 encoded XML instance, or NULL in + * case of error. The caller must free() the returned value. + */ +char * +virDomainBackupGetXMLDesc(virDomainPtr domain, int id, unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "id=%d, flags=0x%x", id, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, NULL); + conn = domain->conn; + + virCheckNonNegativeArgGoto(id, error); + + if (conn->driver->domainBackupGetXMLDesc) { + char *ret; + ret = conn->driver->domainBackupGetXMLDesc(domain, id, flags); + if (!ret) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainBackupEnd: + * @domain: a domain object + * @id: the id of an active backup job previously started with + * virDomainBackupBegin() + * @flags: bitwise-OR of supported virDomainBackupEndFlags + * + * Conclude a point-in-time backup job @id on the given domain. + * + * If the backup job uses the push model, but the event marking that + * all data has been copied has not yet been emitted, then the command + * fails unless @flags includes VIR_DOMAIN_BACKUP_END_ABORT. If the + * event has been issued, or if the backup uses the pull model, the + * flag has no effect. + * + * Returns 1 if the backup job completed successfully (the backup + * destination file in a push model is consistent), 0 if the job was + * aborted successfully (only when VIR_DOMAIN_BACKUP_END_ABORT is + * passed; the destination file is unusable), and -1 on failure. + */ +int +virDomainBackupEnd(virDomainPtr domain, int id, unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "id=%d, flags=0x%x", id, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + virCheckReadOnlyGoto(conn->flags, error); + virCheckNonNegativeArgGoto(id, error); + + if (conn->driver->domainBackupEnd) { + int ret; + ret = conn->driver->domainBackupEnd(domain, id, flags); + if (!ret) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index 58b4863c8f..a159b9d78c 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -1,7 +1,7 @@ /* * libvirt-domain.c: entry points for virDomainPtr APIs * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 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 @@ -10325,6 +10325,12 @@ virDomainBlockRebase(virDomainPtr dom, const char *disk, * over the destination format, the ability to copy to a destination that * is not a local file, and the possibility of additional tuning parameters. * + * The copy created by this API is not finalized until the job ends, + * and does not lend itself to incremental backups (beyond what + * VIR_DOMAIN_BLOCK_COPY_SHALLOW provides) nor to third-party control + * over the data being copied. For those features, use + * virDomainBackupBegin(). + * * Returns 0 if the operation has started, -1 on failure. */ int diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 1b18402e5e..96858388d1 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -816,6 +816,9 @@ LIBVIRT_4.10.0 { LIBVIRT_5.1.0 { global: + virDomainBackupBegin; + virDomainBackupEnd; + virDomainBackupGetXMLDesc; virDomainCheckpointCreateXML; virDomainCheckpointCurrent; virDomainCheckpointDelete; diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 275ac0c318..68d7dc6df7 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -2561,7 +2561,9 @@ VIR_ENUM_IMPL(virshDomainBlockJob, N_("Block Pull"), N_("Block Copy"), N_("Block Commit"), - N_("Active Block Commit")); + N_("Active Block Commit"), + N_("Backup"), +); static const char * virshDomainBlockJobToString(int type) @@ -6064,7 +6066,9 @@ VIR_ENUM_IMPL(virshDomainJobOperation, N_("Outgoing migration"), N_("Snapshot"), N_("Snapshot revert"), - N_("Dump")); + N_("Dump"), + N_("Backup"), +); static const char * virshDomainJobOperationToString(int op) -- 2.20.1

On 2/6/19 2:18 PM, Eric Blake wrote:
Introduce a few more new public APIs related to incremental backups. This builds on the previous notion of a checkpoint (without an existing checkpoint, the new API is a full backup, differing only from virDomainCopy in the point of time chosen); and also allows creation of a new checkpoint at the same time as starting the backup (after all, an incremental backup is only useful if it covers the state since the previous backup). It also enhances event reporting for signaling when a push model backup completes (where the hypervisor creates the backup); note that the pull model does not have an event (starting the backup lets a third party access the data, and only the third party knows when it is finished).
The full list of new API: virDomainBackupBegin; virDomainBackupEnd; virDomainBackupGetXMLDesc;
Signed-off-by: Eric Blake <eblake@redhat.com> --- include/libvirt/libvirt-domain-checkpoint.h | 21 ++ include/libvirt/libvirt-domain.h | 17 +- src/driver-hypervisor.h | 14 ++ src/qemu/qemu_blockjob.h | 1 + examples/object-events/event-test.c | 3 + src/conf/domain_conf.c | 2 +- src/libvirt-domain-checkpoint.c | 202 ++++++++++++++++++++ src/libvirt-domain.c | 8 +- src/libvirt_public.syms | 3 + tools/virsh-domain.c | 8 +- 10 files changed, 273 insertions(+), 6 deletions(-)
diff --git a/include/libvirt/libvirt-domain-checkpoint.h b/include/libvirt/libvirt-domain-checkpoint.h index 9006a46c6e..6c3a85a1bf 100644 --- a/include/libvirt/libvirt-domain-checkpoint.h +++ b/include/libvirt/libvirt-domain-checkpoint.h @@ -152,4 +152,25 @@ int virDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, int virDomainCheckpointRef(virDomainCheckpointPtr checkpoint); int virDomainCheckpointFree(virDomainCheckpointPtr checkpoint);
+typedef enum { + VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA = (1 << 0), /* Make checkpoint without + remembering it */ + /* TODO: VIR_DOMAIN_BACKUP_BEGIN_QUIESCE */
Implement or drop TODO:
+} virDomainBackupBeginFlags; + +/* Begin an incremental backup job, possibly creating a checkpoint. */ +int virDomainBackupBegin(virDomainPtr domain, const char *diskXml, + const char *checkpointXml, unsigned int flags); + +/* Learn about an ongoing backup job. */ +char *virDomainBackupGetXMLDesc(virDomainPtr domain, int id, + unsigned int flags); + +typedef enum { + VIR_DOMAIN_BACKUP_END_ABORT = (1 << 0), /* Abandon a push model backup */ +} virDomainBackupEndFlags; + +/* Complete (or abort) an incremental backup job. */ +int virDomainBackupEnd(virDomainPtr domain, int id, unsigned int flags); + #endif /* LIBVIRT_DOMAIN_CHECKPOINT_H */ diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h index 0388911ded..cc23029c97 100644 --- a/include/libvirt/libvirt-domain.h +++ b/include/libvirt/libvirt-domain.h @@ -3,7 +3,7 @@ * Summary: APIs for management of domains * Description: Provides APIs for the management of domains * - * Copyright (C) 2006-2015 Red Hat, Inc. + * 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 @@ -2394,6 +2394,9 @@ typedef enum { * exists as long as sync is active */ VIR_DOMAIN_BLOCK_JOB_TYPE_ACTIVE_COMMIT = 4,
+ /* Backup (virDomainBackupBegin), job exists until virDomainBackupEnd */ + VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP = 5, + # ifdef VIR_ENUM_SENTINELS VIR_DOMAIN_BLOCK_JOB_TYPE_LAST # endif @@ -3215,6 +3218,7 @@ typedef enum { VIR_DOMAIN_JOB_OPERATION_SNAPSHOT = 6, VIR_DOMAIN_JOB_OPERATION_SNAPSHOT_REVERT = 7, VIR_DOMAIN_JOB_OPERATION_DUMP = 8, + VIR_DOMAIN_JOB_OPERATION_BACKUP = 9,
# ifdef VIR_ENUM_SENTINELS VIR_DOMAIN_JOB_OPERATION_LAST @@ -3230,6 +3234,14 @@ typedef enum { */ # define VIR_DOMAIN_JOB_OPERATION "operation"
+/** + * VIR_DOMAIN_JOB_ID: + * + * virDomainGetJobStats field: the id of the job (so far, only for jobs + * started by virDomainBackupBegin()), as VIR_TYPED_PARAM_INT. + */ +# define VIR_DOMAIN_JOB_ID "id" + /** * VIR_DOMAIN_JOB_TIME_ELAPSED: * @@ -4054,7 +4066,8 @@ typedef void (*virConnectDomainEventMigrationIterationCallback)(virConnectPtr co * @nparams: size of the params array * @opaque: application specific data * - * This callback occurs when a job (such as migration) running on the domain + * This callback occurs when a job (such as migration or push-model + * virDomainBackupBegin()) running on the domain * is completed. The params array will contain statistics of the just completed * job as virDomainGetJobStats would return. The callback must not free @params * (the array will be freed once the callback finishes). diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h index 14db8e49f3..4e65b8ad36 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -1376,6 +1376,17 @@ typedef int (*virDrvDomainCheckpointDelete)(virDomainCheckpointPtr checkpoint, unsigned int flags);
+typedef int +(*virDrvDomainBackupBegin)(virDomainPtr domain, const char *diskXml, + const char *checkpointXml, unsigned int flags); + +typedef char * +(*virDrvDomainBackupGetXMLDesc)(virDomainPtr domain, int id, + unsigned int flags); + +typedef int +(*virDrvDomainBackupEnd)(virDomainPtr domain, int id, unsigned int flags); + typedef struct _virHypervisorDriver virHypervisorDriver; typedef virHypervisorDriver *virHypervisorDriverPtr;
@@ -1638,6 +1649,9 @@ struct _virHypervisorDriver { virDrvDomainCheckpointIsCurrent domainCheckpointIsCurrent; virDrvDomainCheckpointHasMetadata domainCheckpointHasMetadata; virDrvDomainCheckpointDelete domainCheckpointDelete; + virDrvDomainBackupBegin domainBackupBegin; + virDrvDomainBackupGetXMLDesc domainBackupGetXMLDesc; + virDrvDomainBackupEnd domainBackupEnd; };
diff --git a/src/qemu/qemu_blockjob.h b/src/qemu/qemu_blockjob.h index c7325c6daf..95f53dde16 100644 --- a/src/qemu/qemu_blockjob.h +++ b/src/qemu/qemu_blockjob.h @@ -55,6 +55,7 @@ typedef enum { QEMU_BLOCKJOB_TYPE_COPY = VIR_DOMAIN_BLOCK_JOB_TYPE_COPY, QEMU_BLOCKJOB_TYPE_COMMIT = VIR_DOMAIN_BLOCK_JOB_TYPE_COMMIT, QEMU_BLOCKJOB_TYPE_ACTIVE_COMMIT = VIR_DOMAIN_BLOCK_JOB_TYPE_ACTIVE_COMMIT, + QEMU_BLOCKJOB_TYPE_BACKUP = VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP, /* Additional enum values local to qemu */ QEMU_BLOCKJOB_TYPE_INTERNAL, QEMU_BLOCKJOB_TYPE_LAST diff --git a/examples/object-events/event-test.c b/examples/object-events/event-test.c index fcf4492470..98337ad185 100644 --- a/examples/object-events/event-test.c +++ b/examples/object-events/event-test.c @@ -891,6 +891,9 @@ blockJobTypeToStr(int type)
case VIR_DOMAIN_BLOCK_JOB_TYPE_ACTIVE_COMMIT: return "active layer block commit"; + + case VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP: + return "backup"; }
return "unknown"; diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 0221eb0634..fa3db9266f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1030,7 +1030,7 @@ VIR_ENUM_IMPL(virDomainHPTResizing, * <mirror> XML (remaining types are not two-phase). */ VIR_ENUM_DECL(virDomainBlockJob); VIR_ENUM_IMPL(virDomainBlockJob, VIR_DOMAIN_BLOCK_JOB_TYPE_LAST, - "", "", "copy", "", "active-commit", + "", "", "copy", "", "active-commit", "", );
VIR_ENUM_IMPL(virDomainMemoryModel, diff --git a/src/libvirt-domain-checkpoint.c b/src/libvirt-domain-checkpoint.c index 8a7b5b3c56..29cefe41f1 100644 --- a/src/libvirt-domain-checkpoint.c +++ b/src/libvirt-domain-checkpoint.c @@ -721,3 +721,205 @@ virDomainCheckpointFree(virDomainCheckpointPtr checkpoint) virObjectUnref(checkpoint); return 0; }
Dirtying the namespace of libvirt-domain-checkpoint w/ Backup. Why not a separate libvirt-domain-backup - beyond the obvious need to alter more stuff of course. If kept here would seem to need to alter line #2 way back up at the top to include virDomainBackupPtr API's too.
+ + +/** + * virDomainBackupBegin: + * @domain: a domain object + * @diskXml: description of storage to utilize and expose during + * the backup, or NULL + * @checkpointXml: description of a checkpoint to create, or NULL + * @flags: bitwise-OR of supported virDomainBackupBeginFlags + * + * Start a point-in-time backup job for the specified disks of a + * running domain. + * + * A backup job is mutually exclusive with domain migration + * (particularly when the job sets up an NBD export, since it is not + * possible to tell any NBD clients about a server migrating between + * hosts). For now, backup jobs are also mutually exclusive with any + * other block job on the same device, although this restriction may + * be lifted in a future release. Progress of the backup job can be + * tracked via virDomainGetJobStats(). The job remains active until a + * subsequent call to virDomainBackupEnd(), even if it no longer has + * anything to copy. + * + * This API differs from virDomainBlockCopy() in that it can grab the + * state of more than one disk in parallel, and the state is captured
s/, and/ and/
+ * as of the start of the job, rather than the end. + * + * There are two fundamental backup approaches. The first, called a + * push model, instructs the hypervisor to copy the state of the guest + * disk to the designated storage destination (which may be on the + * local file system or a network device); in this mode, the
s/; in/. In/
+ * hypervisor writes the content of the guest disk to the destination, + * then emits VIR_DOMAIN_EVENT_ID_JOB_COMPLETED when the backup is + * either complete or failed (the backup image is invalid if the job + * is ended prior to the event being emitted). The second, called a
s/is ended/ends/ (or fails ?)
+ * pull model, instructs the hypervisor to expose the state of the + * guest disk over an NBD export; a third-party client can then
s/; a/. A/
+ * connect to this export, and read whichever portions of the disk it
s/, and/ and/
+ * desires. In this mode, there is no event; libvirt has to be + * informed when the third-party NBD client is done and the backup + * resources can be released. + * + * The @diskXml parameter is optional but usually provided, and
s/, and/ and/
+ * contains details about the backup, including which backup mode to + * use, whether the backup is incremental from a previous checkpoint, + * which disks participate in the backup, the destination for a push + * model backup, and the temporary storage and NBD server details for + * a pull model backup. If omitted, the backup attempts to default to + * a push mode full backup of all disks, where libvirt generates a + * filename for each disk by appending a suffix of a timestamp in + * seconds since the Epoch. virDomainBackupGetXMLDesc() can be called + * to learn actual values selected. For more information, see + * formatcheckpoint.html#BackupAttributes. + * + * The @checkpointXml parameter is optional; if non-NULL, then libvirt + * behaves as if virDomainCheckpointCreateXML() were called with + * @checkpointXml and the flag VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA + * forwarded appropriately, atomically covering the same guest state
s/, / and /
+ * that will be part of the backup. The creation of a new checkpoint + * allows for future incremental backups. Note that some hypervisors + * may require a particular disk format, such as qcow2, in order to + * take advantage of checkpoints, while allowing arbitrary formats + * if checkpoints are not involved. + * + * Returns a non-negative job id on success, or negative on failure.
s/, or/ or/
+ * This operation returns quickly, such that a user can choose to + * start a backup job between virDomainFSFreeze() and + * virDomainFSThaw() in order to create the backup while guest I/O is + * quiesced. + */ +/* FIXME: Do we need a specific API for listing all current backup + * jobs (which, at the moment, is at most one job), or is it better to + * refactor other existing job APIs in libvirt-domain.c to have job-id + * counterparts along with a generic listing of all jobs (with flags + * for filtering to specific job types)? + */
Or do we wait until some consumer asks for this? Do you mean GetBlockJobInfo? GetJobStats? GetJobInfo? Is it required for initial implementation? Not sure I'm expert enough to answer that questions!
+int +virDomainBackupBegin(virDomainPtr domain, const char *diskXml, + const char *checkpointXml, unsigned int flags)
One line per argument
+{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "diskXml=%s, checkpointXml=%s, flags=0x%x", + NULLSTR(diskXml), NULLSTR(checkpointXml), flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + virCheckReadOnlyGoto(conn->flags, error); + if (flags & VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA) + virCheckNonNullArgGoto(checkpointXml, error); + + if (conn->driver->domainBackupBegin) { + int ret; + ret = conn->driver->domainBackupBegin(domain, diskXml, checkpointXml, + flags); + if (!ret) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} + + +/** + * virDomainBackupGetXMLDesc: + * @domain: a domain object + * @id: the id of an active backup job previously started with + * virDomainBackupBegin() + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * In some cases, a user can start a backup job without supplying all + * details, and rely on libvirt to fill in the rest (for example,
s/, and/ and/
+ * selecting the port used for an NBD export). This API can then be + * used to learn what default values were chosen. + * + * Returns a NUL-terminated UTF-8 encoded XML instance, or NULL in
s/, or/ or/
+ * case of error. The caller must free() the returned value. + */
Do we have the same security concerns as Checkpoint? IOW: One doesn't necessarily have to use a conn that was used for virDomainBackupBegin or do they? Should we just check for a non read-only connection (which I assume has implications later in remote.x defs).
+char * +virDomainBackupGetXMLDesc(virDomainPtr domain, int id, unsigned int flags)
One line per argument
+{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "id=%d, flags=0x%x", id, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, NULL); + conn = domain->conn; + + virCheckNonNegativeArgGoto(id, error); + + if (conn->driver->domainBackupGetXMLDesc) { + char *ret; + ret = conn->driver->domainBackupGetXMLDesc(domain, id, flags); + if (!ret) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return NULL; +} + + +/** + * virDomainBackupEnd: + * @domain: a domain object + * @id: the id of an active backup job previously started with + * virDomainBackupBegin() + * @flags: bitwise-OR of supported virDomainBackupEndFlags + * + * Conclude a point-in-time backup job @id on the given domain. + * + * If the backup job uses the push model, but the event marking that + * all data has been copied has not yet been emitted, then the command + * fails unless @flags includes VIR_DOMAIN_BACKUP_END_ABORT. If the + * event has been issued, or if the backup uses the pull model, the + * flag has no effect. + * + * Returns 1 if the backup job completed successfully (the backup + * destination file in a push model is consistent), 0 if the job was + * aborted successfully (only when VIR_DOMAIN_BACKUP_END_ABORT is + * passed; the destination file is unusable), and -1 on failure. + */
So 0 can only be returned if/when ABORT is used (just checking my reading...)
+int +virDomainBackupEnd(virDomainPtr domain, int id, unsigned int flags)
One line per argument John
+{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "id=%d, flags=0x%x", id, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + virCheckReadOnlyGoto(conn->flags, error); + virCheckNonNegativeArgGoto(id, error); + + if (conn->driver->domainBackupEnd) { + int ret; + ret = conn->driver->domainBackupEnd(domain, id, flags); + if (!ret) + goto error; + return ret; + } + + virReportUnsupportedError(); + error: + virDispatchError(conn); + return -1; +} diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c index 58b4863c8f..a159b9d78c 100644 --- a/src/libvirt-domain.c +++ b/src/libvirt-domain.c @@ -1,7 +1,7 @@ /* * libvirt-domain.c: entry points for virDomainPtr APIs * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 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 @@ -10325,6 +10325,12 @@ virDomainBlockRebase(virDomainPtr dom, const char *disk, * over the destination format, the ability to copy to a destination that * is not a local file, and the possibility of additional tuning parameters. * + * The copy created by this API is not finalized until the job ends, + * and does not lend itself to incremental backups (beyond what + * VIR_DOMAIN_BLOCK_COPY_SHALLOW provides) nor to third-party control + * over the data being copied. For those features, use + * virDomainBackupBegin(). + * * Returns 0 if the operation has started, -1 on failure. */ int diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms index 1b18402e5e..96858388d1 100644 --- a/src/libvirt_public.syms +++ b/src/libvirt_public.syms @@ -816,6 +816,9 @@ LIBVIRT_4.10.0 {
LIBVIRT_5.1.0 { global: + virDomainBackupBegin; + virDomainBackupEnd; + virDomainBackupGetXMLDesc; virDomainCheckpointCreateXML; virDomainCheckpointCurrent; virDomainCheckpointDelete; diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 275ac0c318..68d7dc6df7 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -2561,7 +2561,9 @@ VIR_ENUM_IMPL(virshDomainBlockJob, N_("Block Pull"), N_("Block Copy"), N_("Block Commit"), - N_("Active Block Commit")); + N_("Active Block Commit"), + N_("Backup"), +);
static const char * virshDomainBlockJobToString(int type) @@ -6064,7 +6066,9 @@ VIR_ENUM_IMPL(virshDomainJobOperation, N_("Outgoing migration"), N_("Snapshot"), N_("Snapshot revert"), - N_("Dump")); + N_("Dump"), + N_("Backup"), +);
static const char * virshDomainJobOperationToString(int op)

On 2/11/19 3:21 PM, John Ferlan wrote:
On 2/6/19 2:18 PM, Eric Blake wrote:
Introduce a few more new public APIs related to incremental backups. This builds on the previous notion of a checkpoint (without an existing checkpoint, the new API is a full backup, differing only from virDomainCopy in the point of time chosen); and also allows creation of a new checkpoint at the same time as starting the backup (after all, an incremental backup is only useful if it covers the state since the previous backup). It also enhances event reporting for signaling when a push model backup completes (where the hypervisor creates the backup); note that the pull model does not have an event (starting the backup lets a third party access the data, and only the third party knows when it is finished).
The full list of new API: virDomainBackupBegin; virDomainBackupEnd; virDomainBackupGetXMLDesc;
+typedef enum { + VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA = (1 << 0), /* Make checkpoint without + remembering it */ + /* TODO: VIR_DOMAIN_BACKUP_BEGIN_QUIESCE */
Implement or drop TODO:
Indeed. And if I get it working for virDomainCheckpointCreateXML, it's trivially supported here as well.
+++ b/src/libvirt-domain-checkpoint.c @@ -721,3 +721,205 @@ virDomainCheckpointFree(virDomainCheckpointPtr checkpoint) virObjectUnref(checkpoint); return 0; }
Dirtying the namespace of libvirt-domain-checkpoint w/ Backup. Why not a separate libvirt-domain-backup - beyond the obvious need to alter more stuff of course. If kept here would seem to need to alter line #2 way back up at the top to include virDomainBackupPtr API's too.
Or even place the backup APIs directly in libvirt-domain.c, as they are domain-based operations with no separate object being created, and are more similar to other job-based APIs like virDomainMigrate().
+ * This operation returns quickly, such that a user can choose to + * start a backup job between virDomainFSFreeze() and + * virDomainFSThaw() in order to create the backup while guest I/O is + * quiesced. + */ +/* FIXME: Do we need a specific API for listing all current backup + * jobs (which, at the moment, is at most one job), or is it better to + * refactor other existing job APIs in libvirt-domain.c to have job-id + * counterparts along with a generic listing of all jobs (with flags + * for filtering to specific job types)? + */
Or do we wait until some consumer asks for this? Do you mean GetBlockJobInfo? GetJobStats? GetJobInfo?
Is it required for initial implementation? Not sure I'm expert enough to answer that questions!
I've already had Nir asking for something; he also requested an ability to name jobs (he may use a UUID, but any name would work). My initial implementation was hard-coded to always create job id '1' (and what's the point of listing something, if the answer is either 'no job running' or 'job 1 is running'). But where things get tricky is that if we do NOT have an API for listing all jobs (even if there is just one possible job in the initial implementation), then it gets much harder to backport that API later. So yes, I _do_ need to get this implemented. I haven't got it up yet (my v5 posting will probably still have it lacking), but if I've missed 5.1, then I have enough time before 5.2 to definitely get that API tackled as part of everything else related to incremental backups.
+/** + * virDomainBackupGetXMLDesc: + * @domain: a domain object + * @id: the id of an active backup job previously started with + * virDomainBackupBegin() + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * In some cases, a user can start a backup job without supplying all + * details, and rely on libvirt to fill in the rest (for example,
s/, and/ and/
+ * selecting the port used for an NBD export). This API can then be + * used to learn what default values were chosen. + * + * Returns a NUL-terminated UTF-8 encoded XML instance, or NULL in
s/, or/ or/
+ * case of error. The caller must free() the returned value. + */
Do we have the same security concerns as Checkpoint? IOW: One doesn't necessarily have to use a conn that was used for virDomainBackupBegin or do they? Should we just check for a non read-only connection (which I assume has implications later in remote.x defs).
The security concern for CheckpointGetXMLDesc is that the checkpoint XML includes a <domain> sub-element, which must NOT expose any passwords in the <domain> except on a read-write connection where the _SECURE flag is passed. In the patches as posted so far, I separated the Checkpoint XML (with its <domain> subelement) to be completely distinct from the Backup XML (which is very minimal, and just describes the job and perhaps a <checkpoint> name but not full XML); thus, there is nothing security-related in the output. But I also asked the question in the cover letter if I should make <domaincheckpoint> be a subelement of <domainsnapshot>, in which case it should also be a subelement of a backup job (since both a backup job and a snapshot are logical places to atomically create a checkpoint at the same time). And depending on how we answer that, then yes, anything that might expose a <domain> sub-subelement (via a <domaincheckpoint> sub-element of the backup job) would need the same _SECURE flag.
+ * Returns 1 if the backup job completed successfully (the backup + * destination file in a push model is consistent), 0 if the job was + * aborted successfully (only when VIR_DOMAIN_BACKUP_END_ABORT is + * passed; the destination file is unusable), and -1 on failure. + */
So 0 can only be returned if/when ABORT is used (just checking my reading...)
Correct. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

[...]
+++ b/src/libvirt-domain-checkpoint.c @@ -721,3 +721,205 @@ virDomainCheckpointFree(virDomainCheckpointPtr checkpoint) virObjectUnref(checkpoint); return 0; }
Dirtying the namespace of libvirt-domain-checkpoint w/ Backup. Why not a separate libvirt-domain-backup - beyond the obvious need to alter more stuff of course. If kept here would seem to need to alter line #2 way back up at the top to include virDomainBackupPtr API's too.
Or even place the backup APIs directly in libvirt-domain.c, as they are domain-based operations with no separate object being created, and are more similar to other job-based APIs like virDomainMigrate().
Yes, makes more sense I think - operating on a domain or domain obj, then use libvirt-domain.c
+ * This operation returns quickly, such that a user can choose to + * start a backup job between virDomainFSFreeze() and + * virDomainFSThaw() in order to create the backup while guest I/O is + * quiesced. + */ +/* FIXME: Do we need a specific API for listing all current backup + * jobs (which, at the moment, is at most one job), or is it better to + * refactor other existing job APIs in libvirt-domain.c to have job-id + * counterparts along with a generic listing of all jobs (with flags + * for filtering to specific job types)? + */
Or do we wait until some consumer asks for this? Do you mean GetBlockJobInfo? GetJobStats? GetJobInfo?
Is it required for initial implementation? Not sure I'm expert enough to answer that questions!
I've already had Nir asking for something; he also requested an ability to name jobs (he may use a UUID, but any name would work). My initial implementation was hard-coded to always create job id '1' (and what's the point of listing something, if the answer is either 'no job running' or 'job 1 is running'). But where things get tricky is that if we do NOT have an API for listing all jobs (even if there is just one possible job in the initial implementation), then it gets much harder to backport that API later. So yes, I _do_ need to get this implemented. I haven't got it up yet (my v5 posting will probably still have it lacking), but if I've missed 5.1, then I have enough time before 5.2 to definitely get that API tackled as part of everything else related to incremental backups.
How much of that can be left for the future beyond 5.2. It seems more requirements keep getting added and we cannot at least get something going/pushed.
+/** + * virDomainBackupGetXMLDesc: + * @domain: a domain object + * @id: the id of an active backup job previously started with + * virDomainBackupBegin() + * @flags: extra flags; not used yet, so callers should always pass 0 + * + * In some cases, a user can start a backup job without supplying all + * details, and rely on libvirt to fill in the rest (for example,
s/, and/ and/
+ * selecting the port used for an NBD export). This API can then be + * used to learn what default values were chosen. + * + * Returns a NUL-terminated UTF-8 encoded XML instance, or NULL in
s/, or/ or/
+ * case of error. The caller must free() the returned value. + */
Do we have the same security concerns as Checkpoint? IOW: One doesn't necessarily have to use a conn that was used for virDomainBackupBegin or do they? Should we just check for a non read-only connection (which I assume has implications later in remote.x defs).
The security concern for CheckpointGetXMLDesc is that the checkpoint XML includes a <domain> sub-element, which must NOT expose any passwords in the <domain> except on a read-write connection where the _SECURE flag is passed.
In the patches as posted so far, I separated the Checkpoint XML (with its <domain> subelement) to be completely distinct from the Backup XML (which is very minimal, and just describes the job and perhaps a <checkpoint> name but not full XML); thus, there is nothing security-related in the output.
But I also asked the question in the cover letter if I should make <domaincheckpoint> be a subelement of <domainsnapshot>, in which case it should also be a subelement of a backup job (since both a backup job and a snapshot are logical places to atomically create a checkpoint at the same time). And depending on how we answer that, then yes, anything that might expose a <domain> sub-subelement (via a <domaincheckpoint> sub-element of the backup job) would need the same _SECURE flag.
OMG the cover letter - I wasn't even thinking about that at this point let alone remember what I may have read ;-) Not sure what the best answer would be. I can see overlap between the two, but I really do "fear" there's some pieces of heavy weight in snapshots that just would be so difficult to link directly as a parent to a checkpoint. As a lightweight piece, a checkpoint would be something that both backup and snapshot could conceivably use I suppose though. Of course you've thought about this much longer than I have. I'd be concerned over come cyclical dependency showing its ugly face as well as not knowing whether there is some depth to which it would not be supportable in XML to make checkpoint a subelement. Something about how "<backingStore>" ran into problems with depth comes to mind. John
+ * Returns 1 if the backup job completed successfully (the backup + * destination file in a push model is consistent), 0 if the job was + * aborted successfully (only when VIR_DOMAIN_BACKUP_END_ABORT is + * passed; the destination file is unusable), and -1 on failure. + */
So 0 can only be returned if/when ABORT is used (just checking my reading...)
Correct.

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> --- src/access/viraccessperm.h | 8 +++++++- src/access/viraccessperm.c | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/access/viraccessperm.h b/src/access/viraccessperm.h index ce3865b359..edf95d1f93 100644 --- a/src/access/viraccessperm.h +++ b/src/access/viraccessperm.h @@ -1,7 +1,7 @@ /* * viraccessperm.h: access control permissions * - * Copyright (C) 2012-2014 Red Hat, Inc. + * Copyright (C) 2012-2018 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 @@ -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 67f751ef9c..c1c40ac5bd 100644 --- a/src/access/viraccessperm.c +++ b/src/access/viraccessperm.c @@ -1,7 +1,7 @@ /* * viraccessperm.c: access control permissions * - * Copyright (C) 2012-2014 Red Hat, Inc. + * Copyright (C) 2012-2018 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 @@ -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

On 2/6/19 2:18 PM, Eric Blake wrote:
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> --- src/access/viraccessperm.h | 8 +++++++- src/access/viraccessperm.c | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-)
Seems reasonable, famous last words though ;-) Reviewed-by: John Ferlan <jferlan@redhat.com> John

The remote code generator had to be taught about the new virDomainCheckpointPtr type, at which point the remote driver code for backups can be generated. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/remote/remote_daemon_dispatch.c | 22 ++- src/remote/remote_driver.c | 33 +++- src/remote/remote_protocol.x | 238 +++++++++++++++++++++++++++- src/remote_protocol-structs | 129 +++++++++++++++ src/rpc/gendispatch.pl | 32 ++-- 5 files changed, 434 insertions(+), 20 deletions(-) diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index df28259042..5433378539 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -1,7 +1,7 @@ /* * remote_daemon_dispatch.c: handlers for RPC method calls * - * Copyright (C) 2007-2018 Red Hat, Inc. + * Copyright (C) 2007-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 @@ -89,6 +89,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; @@ -100,6 +101,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 @@ -7238,6 +7240,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) { @@ -7348,6 +7356,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_QUIET(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 058e4c926b..9feac856ef 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -2,7 +2,7 @@ * remote_driver.c: driver to provide access to libvirtd running * on a remote machine * - * Copyright (C) 2007-2015 Red Hat, Inc. + * Copyright (C) 2007-2018 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 @@ -145,6 +145,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); @@ -156,6 +157,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); /*----------------------------------------------------------------------*/ @@ -8212,6 +8214,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) { @@ -8286,6 +8294,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) { @@ -8535,7 +8550,21 @@ 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.1.0 */ + .domainCheckpointGetXMLDesc = remoteDomainCheckpointGetXMLDesc, /* 5.1.0 */ + .domainListCheckpoints = remoteDomainListCheckpoints, /* 5.1.0 */ + .domainCheckpointListChildren = remoteDomainCheckpointListChildren, /* 5.1.0 */ + .domainCheckpointLookupByName = remoteDomainCheckpointLookupByName, /* 5.1.0 */ + .domainHasCurrentCheckpoint = remoteDomainHasCurrentCheckpoint, /* 5.1.0 */ + .domainCheckpointGetParent = remoteDomainCheckpointGetParent, /* 5.1.0 */ + .domainCheckpointCurrent = remoteDomainCheckpointCurrent, /* 5.1.0 */ + .domainCheckpointIsCurrent = remoteDomainCheckpointIsCurrent, /* 5.1.0 */ + .domainCheckpointHasMetadata = remoteDomainCheckpointHasMetadata, /* 5.1.0 */ + .domainCheckpointDelete = remoteDomainCheckpointDelete, /* 5.1.0 */ + .domainBackupBegin = remoteDomainBackupBegin, /* 5.1.0 */ + .domainBackupGetXMLDesc = remoteDomainBackupGetXMLDesc, /* 5.1.0 */ + .domainBackupEnd = remoteDomainBackupEnd, /* 5.1.0 */ }; static virNetworkDriver network_driver = { diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x index b9d26b1849..0142dc2d3f 100644 --- a/src/remote/remote_protocol.x +++ b/src/remote/remote_protocol.x @@ -3,7 +3,7 @@ * remote_internal driver and libvirtd. This protocol is * internal and may change at any time. * - * Copyright (C) 2006-2015 Red Hat, Inc. + * Copyright (C) 2006-2018 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 @@ -137,6 +137,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; @@ -322,6 +325,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; @@ -3564,6 +3573,137 @@ struct remote_connect_list_all_nwfilter_bindings_ret { /* insert@1 */ remote_nonnull_nwfilter_binding bindings<REMOTE_NWFILTER_BINDING_LIST_MAX>; unsigned int ret; }; +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_checkpoints_args { + remote_nonnull_domain dom; + int need_results; + unsigned int flags; +}; + +struct remote_domain_list_checkpoints_ret { /* insert@1 */ + remote_nonnull_domain_checkpoint checkpoints<REMOTE_DOMAIN_CHECKPOINT_LIST_MAX>; + int ret; +}; + +struct remote_domain_checkpoint_list_children_args { + remote_nonnull_domain_checkpoint checkpoint; + int need_results; + unsigned int flags; +}; + +struct remote_domain_checkpoint_list_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_has_current_checkpoint_args { + remote_nonnull_domain dom; + unsigned int flags; +}; + +struct remote_domain_has_current_checkpoint_ret { + int result; +}; + +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_current_args { + remote_nonnull_domain dom; + unsigned int flags; +}; + +struct remote_domain_checkpoint_current_ret { + remote_nonnull_domain_checkpoint checkpoint; +}; + +struct remote_domain_checkpoint_is_current_args { + remote_nonnull_domain_checkpoint checkpoint; + unsigned int flags; +}; + +struct remote_domain_checkpoint_is_current_ret { + int current; +}; + +struct remote_domain_checkpoint_has_metadata_args { + remote_nonnull_domain_checkpoint checkpoint; + unsigned int flags; +}; + +struct remote_domain_checkpoint_has_metadata_ret { + int metadata; +}; + +struct remote_domain_checkpoint_delete_args { + remote_nonnull_domain_checkpoint checkpoint; + unsigned int flags; +}; + +struct remote_domain_backup_begin_args { + remote_nonnull_domain dom; + remote_string disk_xml; + remote_string checkpoint_xml; + unsigned int flags; +}; + +struct remote_domain_backup_begin_ret { + int result; +}; + +struct remote_domain_backup_get_xml_desc_args { + remote_nonnull_domain dom; + int id; + unsigned int flags; +}; + +struct remote_domain_backup_get_xml_desc_ret { + remote_nonnull_string xml; +}; + +struct remote_domain_backup_end_args { + remote_nonnull_domain dom; + int id; + unsigned int flags; +}; + +struct remote_domain_backup_end_ret { + int retcode; +}; /*----- Protocol. -----*/ @@ -6328,6 +6468,100 @@ enum remote_procedure { * @acl: domain:save:!VIR_DOMAIN_AFFECT_CONFIG|VIR_DOMAIN_AFFECT_LIVE * @acl: domain:save:VIR_DOMAIN_AFFECT_CONFIG */ - REMOTE_PROC_DOMAIN_SET_IOTHREAD_PARAMS = 402 + REMOTE_PROC_DOMAIN_SET_IOTHREAD_PARAMS = 402, + /** + * @generate: both + * @acl: domain:checkpoint + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_CREATE_XML = 403, + + /** + * @generate: both + * @acl: domain:read + * @acl: domain:read_secure:VIR_DOMAIN_CHECKPOINT_XML_SECURE + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_GET_XML_DESC = 404, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_LIST_CHECKPOINTS = 405, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_LIST_CHILDREN = 406, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_LOOKUP_BY_NAME = 407, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_HAS_CURRENT_CHECKPOINT = 408, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_CURRENT = 409, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_GET_PARENT = 410, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_IS_CURRENT = 411, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_HAS_METADATA = 412, + + /** + * @generate: both + * @acl: domain:checkpoint + */ + REMOTE_PROC_DOMAIN_CHECKPOINT_DELETE = 413, + + /** + * @generate: both + * @acl: domain:checkpoint + * @acl: domain:block_read + */ + REMOTE_PROC_DOMAIN_BACKUP_BEGIN = 414, + + /** + * @generate: both + * @priority: high + * @acl: domain:read + */ + REMOTE_PROC_DOMAIN_BACKUP_GET_XML_DESC = 415, + + /** + * @generate: both + * @acl: domain:checkpoint + */ + REMOTE_PROC_DOMAIN_BACKUP_END = 416 }; diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs index 7c27c63542..768fc16306 100644 --- a/src/remote_protocol-structs +++ b/src/remote_protocol-structs @@ -46,6 +46,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; @@ -2975,6 +2979,117 @@ struct remote_connect_list_all_nwfilter_bindings_ret { } bindings; u_int ret; }; +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_checkpoints_args { + remote_nonnull_domain dom; + int need_results; + u_int flags; +}; +struct remote_domain_list_checkpoints_ret { + struct { + u_int checkpoints_len; + remote_nonnull_domain_checkpoint * checkpoints_val; + } checkpoints; + int ret; +}; +struct remote_domain_checkpoint_list_children_args { + remote_nonnull_domain_checkpoint checkpoint; + int need_results; + u_int flags; +}; +struct remote_domain_checkpoint_list_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_has_current_checkpoint_args { + remote_nonnull_domain dom; + u_int flags; +}; +struct remote_domain_has_current_checkpoint_ret { + int result; +}; +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_current_args { + remote_nonnull_domain dom; + u_int flags; +}; +struct remote_domain_checkpoint_current_ret { + remote_nonnull_domain_checkpoint checkpoint; +}; +struct remote_domain_checkpoint_is_current_args { + remote_nonnull_domain_checkpoint checkpoint; + u_int flags; +}; +struct remote_domain_checkpoint_is_current_ret { + int current; +}; +struct remote_domain_checkpoint_has_metadata_args { + remote_nonnull_domain_checkpoint checkpoint; + u_int flags; +}; +struct remote_domain_checkpoint_has_metadata_ret { + int metadata; +}; +struct remote_domain_checkpoint_delete_args { + remote_nonnull_domain_checkpoint checkpoint; + u_int flags; +}; +struct remote_domain_backup_begin_args { + remote_nonnull_domain dom; + remote_string disk_xml; + remote_string checkpoint_xml; + u_int flags; +}; +struct remote_domain_backup_begin_ret { + int result; +}; +struct remote_domain_backup_get_xml_desc_args { + remote_nonnull_domain dom; + int id; + u_int flags; +}; +struct remote_domain_backup_get_xml_desc_ret { + remote_nonnull_string xml; +}; +struct remote_domain_backup_end_args { + remote_nonnull_domain dom; + int id; + u_int flags; +}; +struct remote_domain_backup_end_ret { + int retcode; +}; enum remote_procedure { REMOTE_PROC_CONNECT_OPEN = 1, REMOTE_PROC_CONNECT_CLOSE = 2, @@ -3378,4 +3493,18 @@ enum remote_procedure { REMOTE_PROC_NWFILTER_BINDING_DELETE = 400, REMOTE_PROC_CONNECT_LIST_ALL_NWFILTER_BINDINGS = 401, REMOTE_PROC_DOMAIN_SET_IOTHREAD_PARAMS = 402, + REMOTE_PROC_DOMAIN_CHECKPOINT_CREATE_XML = 403, + REMOTE_PROC_DOMAIN_CHECKPOINT_GET_XML_DESC = 404, + REMOTE_PROC_DOMAIN_LIST_CHECKPOINTS = 405, + REMOTE_PROC_DOMAIN_CHECKPOINT_LIST_CHILDREN = 406, + REMOTE_PROC_DOMAIN_CHECKPOINT_LOOKUP_BY_NAME = 407, + REMOTE_PROC_DOMAIN_HAS_CURRENT_CHECKPOINT = 408, + REMOTE_PROC_DOMAIN_CHECKPOINT_CURRENT = 409, + REMOTE_PROC_DOMAIN_CHECKPOINT_GET_PARENT = 410, + REMOTE_PROC_DOMAIN_CHECKPOINT_IS_CURRENT = 411, + REMOTE_PROC_DOMAIN_CHECKPOINT_HAS_METADATA = 412, + REMOTE_PROC_DOMAIN_CHECKPOINT_DELETE = 413, + REMOTE_PROC_DOMAIN_BACKUP_BEGIN = 414, + REMOTE_PROC_DOMAIN_BACKUP_GET_XML_DESC = 415, + REMOTE_PROC_DOMAIN_BACKUP_END = 416, }; diff --git a/src/rpc/gendispatch.pl b/src/rpc/gendispatch.pl index ce4db5d7b7..732308d1f9 100755 --- a/src/rpc/gendispatch.pl +++ b/src/rpc/gendispatch.pl @@ -1,6 +1,6 @@ #!/usr/bin/env perl # -# Copyright (C) 2010-2015 Red Hat, Inc. +# Copyright (C) 2010-2018 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 @@ -567,18 +567,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 +724,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|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|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 +782,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|storage_pool|storage_vol|interface|node_device|secret|nwfilter|nwfilter_binding|domain_snapshot) (\S+);/) { + } elsif ($ret_member =~ m/^remote_nonnull_(domain|network|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 +1330,13 @@ 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|storage_pool|storage_vol|interface|secret|nwfilter|nwfilter_binding|domain_snapshot) (\S+);/) { + } elsif ($args_member =~ m/^remote_nonnull_(domain|network|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 =~ m/^domain_.*/) { $priv_src = "$arg_name->domain->conn"; } else { $priv_src = "$arg_name->conn"; @@ -1521,7 +1523,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|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|storage_vol|storage_pool|domain_checkpoint|domain_snapshot|domain|server|client) (\S+)<(\S+)>;/) { my $proc_name = name_to_TypeName($1); if ($structprefix eq "admin") { @@ -1574,7 +1576,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|storage_pool|storage_vol|node_device|interface|secret|nwfilter|nwfilter_binding|domain_snapshot) (\S+);/) { + } elsif ($ret_member =~ m/^remote_nonnull_(domain|network|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); @@ -1588,7 +1590,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);"); @@ -1931,7 +1933,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

On 2/6/19 2:18 PM, Eric Blake wrote:
The remote code generator had to be taught about the new virDomainCheckpointPtr type, at which point the remote driver code for backups can be generated.
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/remote/remote_daemon_dispatch.c | 22 ++- src/remote/remote_driver.c | 33 +++- src/remote/remote_protocol.x | 238 +++++++++++++++++++++++++++- src/remote_protocol-structs | 129 +++++++++++++++ src/rpc/gendispatch.pl | 32 ++-- 5 files changed, 434 insertions(+), 20 deletions(-)
Some of this could change as a result of earlier review comments - vis-a-vis API names, but otherwise looks fine to my eyes. I'm by no means a perl expert - it looks like the right pieces were copied and/or extract. John [...]

Work in progress - the checkpoint code is not quite passing tests (part of that is figuring out the minimal XML that is still valid as a <domain> element, or just use --no-domain flag). Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 150 ++++ src/conf/domain_conf.h | 11 +- po/POTFILES | 1 + src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 1030 +++++++++++++++++++++++++++ src/conf/domain_conf.c | 7 +- src/libvirt_private.syms | 21 + tests/Makefile.am | 9 +- tests/domaincheckpointxml2xmltest.c | 231 ++++++ 9 files changed, 1458 insertions(+), 4 deletions(-) create mode 100644 src/conf/checkpoint_conf.h create mode 100644 src/conf/checkpoint_conf.c create mode 100644 tests/domaincheckpointxml2xmltest.c diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h new file mode 100644 index 0000000000..994a8bd083 --- /dev/null +++ b/src/conf/checkpoint_conf.h @@ -0,0 +1,150 @@ +/* + * checkpoint_conf.h: 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/>. + */ + +#ifndef LIBVIRT_CHECKPOINT_CONF_H +# define LIBVIRT_CHECKPOINT_CONF_H + +# include "internal.h" +# include "domain_conf.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 */ +typedef struct _virDomainCheckpointDef virDomainCheckpointDef; +typedef virDomainCheckpointDef *virDomainCheckpointDefPtr; +struct _virDomainCheckpointDef { + /* Public XML. */ + char *name; + char *description; + char *parent; + long long creationTime; /* in seconds */ + + size_t ndisks; /* should not exceed dom->ndisks */ + virDomainCheckpointDiskDef *disks; + + virDomainDefPtr dom; + + /* Internal use. */ + bool current; /* At most one checkpoint in the list should have this set */ +}; + +struct _virDomainCheckpointObj { + virDomainCheckpointDefPtr def; /* non-NULL except for metaroot */ + + virDomainCheckpointObjPtr parent; /* non-NULL except for metaroot, before + virDomainCheckpointUpdateRelations, or + after virDomainCheckpointDropParent */ + virDomainCheckpointObjPtr sibling; /* NULL if last child of parent */ + size_t nchildren; + virDomainCheckpointObjPtr first_child; /* NULL if no children */ +}; + +virDomainCheckpointObjListPtr virDomainCheckpointObjListNew(void); +void virDomainCheckpointObjListFree(virDomainCheckpointObjListPtr checkpoints); + +typedef enum { + VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE = 1 << 0, + VIR_DOMAIN_CHECKPOINT_PARSE_DISKS = 1 << 1, + VIR_DOMAIN_CHECKPOINT_PARSE_INTERNAL = 1 << 2, +} virDomainCheckpointParseFlags; + +virDomainCheckpointDefPtr virDomainCheckpointDefParseString(const char *xmlStr, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +virDomainCheckpointDefPtr virDomainCheckpointDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +void virDomainCheckpointDefFree(virDomainCheckpointDefPtr def); +char *virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags, + bool internal); +int virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr checkpoint); +virDomainCheckpointObjPtr virDomainCheckpointAssignDef(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def); + +virDomainCheckpointObjPtr virDomainCheckpointFindByName(virDomainCheckpointObjListPtr checkpoints, + const char *name); +void virDomainCheckpointObjListRemove(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr checkpoint); +int virDomainCheckpointForEach(virDomainCheckpointObjListPtr checkpoints, + virHashIterator iter, + void *data); +int virDomainCheckpointForEachChild(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data); +int virDomainCheckpointForEachDescendant(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data); +int virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints); +void virDomainCheckpointDropParent(virDomainCheckpointObjPtr checkpoint); + +# define VIR_DOMAIN_CHECKPOINT_FILTERS_METADATA \ + (VIR_DOMAIN_CHECKPOINT_LIST_METADATA | \ + VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA) + +# 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_METADATA | \ + VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) + +int virDomainListAllCheckpoints(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + virDomainPtr dom, + virDomainCheckpointPtr **objs, + unsigned int flags); + +int virDomainCheckpointRedefinePrep(virDomainPtr domain, + virDomainObjPtr vm, + virDomainCheckpointDefPtr *def, + virDomainCheckpointObjPtr *checkpoint, + virDomainXMLOptionPtr xmlopt, + bool *update_current); + +VIR_ENUM_DECL(virDomainCheckpoint); + +#endif /* LIBVIRT_CHECKPOINT_CONF_H */ diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 5db4396fd5..d31c45427e 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1,7 +1,7 @@ /* * domain_conf.h: domain XML processing * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2018 Red Hat, Inc. * Copyright (C) 2006-2008 Daniel P. Berrange * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany. * @@ -119,6 +119,12 @@ typedef virDomainMemballoonDef *virDomainMemballoonDefPtr; typedef struct _virDomainNVRAMDef virDomainNVRAMDef; typedef virDomainNVRAMDef *virDomainNVRAMDefPtr; +typedef struct _virDomainCheckpointObj virDomainCheckpointObj; +typedef virDomainCheckpointObj *virDomainCheckpointObjPtr; + +typedef struct _virDomainCheckpointObjList virDomainCheckpointObjList; +typedef virDomainCheckpointObjList *virDomainCheckpointObjListPtr; + typedef struct _virDomainSnapshotObj virDomainSnapshotObj; typedef virDomainSnapshotObj *virDomainSnapshotObjPtr; @@ -2631,6 +2637,9 @@ struct _virDomainObj { bool hasManagedSave; + virDomainCheckpointObjListPtr checkpoints; + virDomainCheckpointObjPtr current_checkpoint; + void *privateData; void (*privateDataFreeFunc)(void *); diff --git a/po/POTFILES b/po/POTFILES index 88af551664..57c55fb35f 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 219ff350d7..c425363bde 100644 --- a/src/conf/Makefile.inc.am +++ b/src/conf/Makefile.inc.am @@ -10,6 +10,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..c0840a96b2 --- /dev/null +++ b/src/conf/checkpoint_conf.c @@ -0,0 +1,1030 @@ +/* + * 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 <fcntl.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <unistd.h> + +#include "internal.h" +#include "virbitmap.h" +#include "virbuffer.h" +#include "count-one-bits.h" +#include "datatypes.h" +#include "domain_conf.h" +#include "virlog.h" +#include "viralloc.h" +#include "netdev_bandwidth_conf.h" +#include "netdev_vport_profile_conf.h" +#include "nwfilter_conf.h" +#include "secret_conf.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"); + +VIR_ENUM_IMPL(virDomainCheckpoint, VIR_DOMAIN_CHECKPOINT_TYPE_LAST, + "default", "no", "bitmap"); + +struct _virDomainCheckpointObjList { + /* name string -> virDomainCheckpointObj mapping + * for O(1), lockless lookup-by-name */ + virHashTable *objs; + + virDomainCheckpointObj metaroot; /* Special parent of all root checkpoints */ +}; + +/* Checkpoint Def functions */ +static void +virDomainCheckpointDiskDefClear(virDomainCheckpointDiskDefPtr disk) +{ + VIR_FREE(disk->name); + VIR_FREE(disk->bitmap); +} + +void virDomainCheckpointDefFree(virDomainCheckpointDefPtr def) +{ + size_t i; + + if (!def) + return; + + VIR_FREE(def->name); + VIR_FREE(def->description); + VIR_FREE(def->parent); + for (i = 0; i < def->ndisks; i++) + virDomainCheckpointDiskDefClear(&def->disks[i]); + VIR_FREE(def->disks); + virDomainDefFree(def->dom); + VIR_FREE(def); +} + +static int +virDomainCheckpointDiskDefParseXML(xmlNodePtr node, + xmlXPathContextPtr ctxt, + virDomainCheckpointDiskDefPtr def) +{ + int ret = -1; + char *checkpoint = NULL; + char *bitmap = NULL; + xmlNodePtr saved = ctxt->node; + + ctxt->node = node; + + def->name = virXMLPropString(node, "name"); + if (!def->name) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing name from disk checkpoint element")); + goto cleanup; + } + + checkpoint = virXMLPropString(node, "checkpoint"); + if (checkpoint) { + def->type = virDomainCheckpointTypeFromString(checkpoint); + if (def->type <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk checkpoint setting '%s'"), + checkpoint); + goto cleanup; + } + } else { + def->type = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + } + + bitmap = virXMLPropString(node, "bitmap"); + if (bitmap) { + if (def->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk checkpoint bitmap '%s' requires " + "type='bitmap'"), + bitmap); + goto cleanup; + } + VIR_STEAL_PTR(def->bitmap, bitmap); + } + + ret = 0; + cleanup: + ctxt->node = saved; + + VIR_FREE(checkpoint); + VIR_FREE(bitmap); + if (ret < 0) + virDomainCheckpointDiskDefClear(def); + return ret; +} + +/* flags is bitwise-or of virDomainCheckpointParseFlags. + * If flags does not include VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE, then + * caps are ignored. + */ +static virDomainCheckpointDefPtr +virDomainCheckpointDefParse(xmlXPathContextPtr ctxt, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainCheckpointDefPtr def = NULL; + virDomainCheckpointDefPtr ret = NULL; + xmlNodePtr *nodes = NULL; + size_t i; + int n; + char *creation = NULL; + struct timeval tv; + int active; + char *tmp; + + if (VIR_ALLOC(def) < 0) + goto cleanup; + + gettimeofday(&tv, NULL); + + def->name = virXPathString("string(./name)", ctxt); + if (def->name == NULL) { + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("a redefined checkpoint must have a name")); + goto cleanup; + } + if (virAsprintf(&def->name, "%lld", (long long)tv.tv_sec) < 0) + goto cleanup; + } + + def->description = virXPathString("string(./description)", ctxt); + + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE) { + if (virXPathLongLong("string(./creationTime)", ctxt, + &def->creationTime) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing creationTime from existing checkpoint")); + goto cleanup; + } + + def->parent = 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")); + goto cleanup; + } + def->dom = virDomainDefParseNode(ctxt->node->doc, domainNode, + caps, xmlopt, NULL, domainflags); + if (!def->dom) + goto cleanup; + } else { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint redefine")); + goto cleanup; + } + } else { + def->creationTime = tv.tv_sec; + } + + if ((n = virXPathNodeSet("./disks/*", ctxt, &nodes)) < 0) + goto cleanup; + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_DISKS) { + if (n && VIR_ALLOC_N(def->disks, n) < 0) + goto cleanup; + def->ndisks = n; + for (i = 0; i < def->ndisks; i++) { + if (virDomainCheckpointDiskDefParseXML(nodes[i], ctxt, + &def->disks[i]) < 0) + goto cleanup; + } + VIR_FREE(nodes); + } else if (n) { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", + _("unable to handle disk requests in checkpoint")); + goto cleanup; + } + + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_INTERNAL) { + if (virXPathInt("string(./active)", ctxt, &active) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not find 'active' element")); + goto cleanup; + } + def->current = active != 0; + } + + VIR_STEAL_PTR(ret, def); + + cleanup: + VIR_FREE(creation); + VIR_FREE(nodes); + virDomainCheckpointDefFree(def); + + return ret; +} + +virDomainCheckpointDefPtr +virDomainCheckpointDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + xmlXPathContextPtr ctxt = NULL; + virDomainCheckpointDefPtr def = NULL; + + if (!virXMLNodeNameEqual(root, "domaincheckpoint")) { + virReportError(VIR_ERR_XML_ERROR, "%s", _("domaincheckpoint")); + goto cleanup; + } + + 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->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->dom) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint")); + goto cleanup; + } + + if (def->ndisks > def->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->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 disks; otherwise, do nothing + * for omitted disks */ + if (!def->ndisks) + checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + + if (!(map = virBitmapNew(def->dom->ndisks))) + goto cleanup; + + /* Double check requested disks. */ + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &def->disks[i]; + int idx = virDomainDiskIndexByName(def->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; + } + ignore_value(virBitmapSetBit(map, idx)); + disk->idx = idx; + + if (STRNEQ(disk->name, def->dom->disks[idx]->dst)) { + VIR_FREE(disk->name); + if (VIR_STRDUP(disk->name, def->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->dom->ndisks - def->ndisks) < 0) + goto cleanup; + + for (i = 0; i < def->dom->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk; + + if (virBitmapIsBitSet(map, i)) + continue; + disk = &def->disks[ndisks++]; + if (VIR_STRDUP(disk->name, def->dom->disks[i]->dst) < 0) + goto cleanup; + disk->idx = i; + + /* Don't checkpoint empty drives */ + if (virStorageSourceIsEmpty(def->dom->disks[i]->src)) + 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; +} + +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_XML_SIZE) + virBufferAsprintf(buf, " size='%llu'", disk->size); + } + virBufferAddLit(buf, "/>\n"); + return 0; +} + + +char * +virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags, + bool internal) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + size_t i; + unsigned int domflags = VIR_DOMAIN_DEF_FORMAT_INACTIVE; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_XML_SECURE | + VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN | + VIR_DOMAIN_CHECKPOINT_XML_SIZE, NULL); + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SECURE) + domflags |= VIR_DOMAIN_DEF_FORMAT_SECURE; + + virBufferAddLit(&buf, "<domaincheckpoint>\n"); + virBufferAdjustIndent(&buf, 2); + + virBufferEscapeString(&buf, "<name>%s</name>\n", def->name); + if (def->description) + virBufferEscapeString(&buf, "<description>%s</description>\n", + def->description); + + if (def->parent) { + virBufferAddLit(&buf, "<parent>\n"); + virBufferAdjustIndent(&buf, 2); + virBufferEscapeString(&buf, "<name>%s</name>\n", def->parent); + virBufferAdjustIndent(&buf, -2); + virBufferAddLit(&buf, "</parent>\n"); + } + + virBufferAsprintf(&buf, "<creationTime>%lld</creationTime>\n", + def->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_XML_NO_DOMAIN) && + virDomainDefFormatInternal(def->dom, caps, domflags, &buf, xmlopt) < 0) + goto error; + + if (internal) + virBufferAsprintf(&buf, "<active>%d</active>\n", def->current); + + virBufferAdjustIndent(&buf, -2); + virBufferAddLit(&buf, "</domaincheckpoint>\n"); + + if (virBufferCheckError(&buf) < 0) + return NULL; + + return virBufferContentAndReset(&buf); + + error: + virBufferFreeAndReset(&buf); + return NULL; +} + +/* Checkpoint Obj functions */ +static virDomainCheckpointObjPtr virDomainCheckpointObjNew(void) +{ + virDomainCheckpointObjPtr checkpoint; + + if (VIR_ALLOC(checkpoint) < 0) + return NULL; + + VIR_DEBUG("obj=%p", checkpoint); + + return checkpoint; +} + +static void virDomainCheckpointObjFree(virDomainCheckpointObjPtr checkpoint) +{ + if (!checkpoint) + return; + + VIR_DEBUG("obj=%p", checkpoint); + + virDomainCheckpointDefFree(checkpoint->def); + VIR_FREE(checkpoint); +} + +virDomainCheckpointObjPtr virDomainCheckpointAssignDef(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def) +{ + virDomainCheckpointObjPtr chk; + + if (virHashLookup(checkpoints->objs, def->name) != NULL) { + virReportError(VIR_ERR_OPERATION_INVALID, + _("domain checkpoint %s already exists"), + def->name); + return NULL; + } + + if (!(chk = virDomainCheckpointObjNew())) + return NULL; + chk->def = def; + + if (virHashAddEntry(checkpoints->objs, chk->def->name, chk) < 0) { + VIR_FREE(chk); + return NULL; + } + + return chk; +} + +/* Checkpoint Obj List functions */ +static void +virDomainCheckpointObjListDataFree(void *payload, + const void *name ATTRIBUTE_UNUSED) +{ + virDomainCheckpointObjPtr obj = payload; + + virDomainCheckpointObjFree(obj); +} + +virDomainCheckpointObjListPtr +virDomainCheckpointObjListNew(void) +{ + virDomainCheckpointObjListPtr checkpoints; + if (VIR_ALLOC(checkpoints) < 0) + return NULL; + checkpoints->objs = virHashCreate(50, virDomainCheckpointObjListDataFree); + if (!checkpoints->objs) { + VIR_FREE(checkpoints); + return NULL; + } + return checkpoints; +} + +void +virDomainCheckpointObjListFree(virDomainCheckpointObjListPtr checkpoints) +{ + if (!checkpoints) + return; + virHashFree(checkpoints->objs); + VIR_FREE(checkpoints); +} + +struct virDomainCheckpointNameData { + char **const names; + int maxnames; + unsigned int flags; + int count; + bool error; +}; + +static int +virDomainCheckpointObjListCopyNames(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + virDomainCheckpointObjPtr obj = payload; + struct virDomainCheckpointNameData *data = opaque; + + if (data->error) + return 0; + /* Caller already sanitized flags. Filtering on DESCENDANTS was + * done by choice of iteration in the caller. */ + if ((data->flags & VIR_DOMAIN_CHECKPOINT_LIST_LEAVES) && obj->nchildren) + return 0; + if ((data->flags & VIR_DOMAIN_CHECKPOINT_LIST_NO_LEAVES) && !obj->nchildren) + return 0; + + if (data->names && data->count < data->maxnames && + VIR_STRDUP(data->names[data->count], obj->def->name) < 0) { + data->error = true; + return 0; + } + data->count++; + return 0; +} + +static int +virDomainCheckpointObjListGetNames(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + char **const names, int maxnames, + unsigned int flags) +{ + struct virDomainCheckpointNameData data = { names, maxnames, flags, 0, + false }; + size_t i; + + if (!from) { + /* LIST_ROOTS and LIST_DESCENDANTS have the same bit value, + * but opposite semantics. Toggle here to get the correct + * traversal on the metaroot. */ + flags ^= VIR_DOMAIN_CHECKPOINT_LIST_ROOTS; + from = &checkpoints->metaroot; + } + + /* We handle LIST_ROOT/LIST_DESCENDANTS directly, mask that bit + * out to determine when we must use the filter callback. */ + data.flags &= ~VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS; + + /* If this common code is being used, we assume that all checkpoints + * have metadata, and thus can handle METADATA up front as an + * all-or-none filter. XXX This might not always be true, if we + * add the ability to track qcow2 bitmaps without the + * use of metadata. */ + if ((data.flags & VIR_DOMAIN_CHECKPOINT_FILTERS_METADATA) == + VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA) + return 0; + data.flags &= ~VIR_DOMAIN_CHECKPOINT_FILTERS_METADATA; + + /* For ease of coding the visitor, it is easier to zero each group + * where all of the bits are set. */ + if ((data.flags & VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) == + VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) + data.flags &= ~VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES; + + if (flags & VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS) { + if (from->def) + virDomainCheckpointForEachDescendant(from, + virDomainCheckpointObjListCopyNames, + &data); + else if (names || data.flags) + virHashForEach(checkpoints->objs, + virDomainCheckpointObjListCopyNames, + &data); + else + data.count = virHashSize(checkpoints->objs); + } else if (names || data.flags) { + virDomainCheckpointForEachChild(from, + virDomainCheckpointObjListCopyNames, + &data); + } else { + data.count = from->nchildren; + } + + if (data.error) { + for (i = 0; i < data.count; i++) + VIR_FREE(names[i]); + return -1; + } + + return data.count; +} + +static int +virDomainCheckpointObjListNum(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + unsigned int flags) +{ + return virDomainCheckpointObjListGetNames(checkpoints, from, NULL, 0, + flags); +} + +virDomainCheckpointObjPtr +virDomainCheckpointFindByName(virDomainCheckpointObjListPtr checkpoints, + const char *name) +{ + return name ? virHashLookup(checkpoints->objs, name) : + &checkpoints->metaroot; +} + +void virDomainCheckpointObjListRemove(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr checkpoint) +{ + virHashRemoveEntry(checkpoints->objs, checkpoint->def->name); +} + +int +virDomainCheckpointForEach(virDomainCheckpointObjListPtr checkpoints, + virHashIterator iter, + void *data) +{ + return virHashForEach(checkpoints->objs, iter, data); +} + +/* Run iter(data) on all direct children of checkpoint, while ignoring all + * other entries in checkpoints. Return the number of children + * visited. No particular ordering is guaranteed. */ +int +virDomainCheckpointForEachChild(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data) +{ + virDomainCheckpointObjPtr child = checkpoint->first_child; + + while (child) { + virDomainCheckpointObjPtr next = child->sibling; + (iter)(child, child->def->name, data); + child = next; + } + + return checkpoint->nchildren; +} + +struct checkpoint_act_on_descendant { + int number; + virHashIterator iter; + void *data; +}; + +static int +virDomainCheckpointActOnDescendant(void *payload, + const void *name, + void *data) +{ + virDomainCheckpointObjPtr obj = payload; + struct checkpoint_act_on_descendant *curr = data; + + curr->number += 1 + virDomainCheckpointForEachDescendant(obj, + curr->iter, + curr->data); + (curr->iter)(payload, name, curr->data); + return 0; +} + +/* Run iter(data) on all descendants of checkpoint, while ignoring all + * other entries in checkpoints. Return the number of descendants + * visited. No particular ordering is guaranteed. */ +int +virDomainCheckpointForEachDescendant(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data) +{ + struct checkpoint_act_on_descendant act; + + act.number = 0; + act.iter = iter; + act.data = data; + virDomainCheckpointForEachChild(checkpoint, + virDomainCheckpointActOnDescendant, &act); + + return act.number; +} + +/* Struct and callback function used as a hash table callback; each call + * inspects the pre-existing checkpoint->def->parent field, and adjusts + * the checkpoint->parent field as well as the parent's child fields to + * wire up the hierarchical relations for the given checkpoint. The error + * indicator gets set if a parent is missing or a requested parent would + * cause a circular parent chain. */ +struct checkpoint_set_relation { + virDomainCheckpointObjListPtr checkpoints; + int err; +}; +static int +virDomainCheckpointSetRelations(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *data) +{ + virDomainCheckpointObjPtr obj = payload; + struct checkpoint_set_relation *curr = data; + virDomainCheckpointObjPtr tmp; + + obj->parent = virDomainCheckpointFindByName(curr->checkpoints, + obj->def->parent); + if (!obj->parent) { + curr->err = -1; + obj->parent = &curr->checkpoints->metaroot; + VIR_WARN("checkpoint %s lacks parent", obj->def->name); + } else { + tmp = obj->parent; + while (tmp && tmp->def) { + if (tmp == obj) { + curr->err = -1; + obj->parent = &curr->checkpoints->metaroot; + VIR_WARN("checkpoint %s in circular chain", obj->def->name); + break; + } + tmp = tmp->parent; + } + } + obj->parent->nchildren++; + obj->sibling = obj->parent->first_child; + obj->parent->first_child = obj; + return 0; +} + +/* Populate parent link and child count of all checkpoints, with all + * relations starting as 0/NULL. Return 0 on success, -1 if a parent + * is missing or if a circular relationship was requested. */ +int +virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints) +{ + struct checkpoint_set_relation act = { checkpoints, 0 }; + + virHashForEach(checkpoints->objs, virDomainCheckpointSetRelations, &act); + return act.err; +} + +/* Prepare to reparent or delete checkpoint, by removing it from its + * current listed parent. Note that when bulk removing all children + * of a parent, it is faster to just 0 the count rather than calling + * this function on each child. */ +void +virDomainCheckpointDropParent(virDomainCheckpointObjPtr checkpoint) +{ + virDomainCheckpointObjPtr prev = NULL; + virDomainCheckpointObjPtr curr = NULL; + + checkpoint->parent->nchildren--; + curr = checkpoint->parent->first_child; + while (curr != checkpoint) { + if (!curr) { + VIR_WARN("inconsistent checkpoint relations"); + return; + } + prev = curr; + curr = curr->sibling; + } + if (prev) + prev->sibling = checkpoint->sibling; + else + checkpoint->parent->first_child = checkpoint->sibling; + checkpoint->parent = NULL; + checkpoint->sibling = NULL; +} + +int +virDomainListAllCheckpoints(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + virDomainPtr dom, + virDomainCheckpointPtr **chks, + unsigned int flags) +{ + int count = virDomainCheckpointObjListNum(checkpoints, from, 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; +} + + +int +virDomainCheckpointRedefinePrep(virDomainPtr domain, + virDomainObjPtr vm, + virDomainCheckpointDefPtr *defptr, + virDomainCheckpointObjPtr *chk, + virDomainXMLOptionPtr xmlopt, + bool *update_current) +{ + virDomainCheckpointDefPtr def = *defptr; + int ret = -1; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + virDomainCheckpointObjPtr other; + + virUUIDFormat(domain->uuid, uuidstr); + + /* Prevent circular chains */ + if (def->parent) { + if (STREQ(def->name, def->parent)) { + virReportError(VIR_ERR_INVALID_ARG, + _("cannot set checkpoint %s as its own parent"), + def->name); + goto cleanup; + } + other = virDomainCheckpointFindByName(vm->checkpoints, def->parent); + if (!other) { + virReportError(VIR_ERR_INVALID_ARG, + _("parent %s for checkpoint %s not found"), + def->parent, def->name); + goto cleanup; + } + while (other->def->parent) { + if (STREQ(other->def->parent, def->name)) { + virReportError(VIR_ERR_INVALID_ARG, + _("parent %s would create cycle to %s"), + other->def->name, def->name); + goto cleanup; + } + other = virDomainCheckpointFindByName(vm->checkpoints, + other->def->parent); + if (!other) { + VIR_WARN("checkpoints are inconsistent for %s", + vm->def->name); + break; + } + } + } + + if (def->dom && + memcmp(def->dom->uuid, domain->uuid, VIR_UUID_BUFLEN)) { + virReportError(VIR_ERR_INVALID_ARG, + _("definition for checkpoint %s must use uuid %s"), + def->name, uuidstr); + goto cleanup; + } + + other = virDomainCheckpointFindByName(vm->checkpoints, def->name); + if (other) { + if (other->def->dom) { + if (def->dom) { + if (!virDomainDefCheckABIStability(other->def->dom, + def->dom, xmlopt)) + goto cleanup; + } else { + /* Transfer the domain def */ + def->dom = other->def->dom; + other->def->dom = NULL; + } + } + + if (def->dom) { + if (virDomainCheckpointAlignDisks(def) < 0) { + /* revert stealing of the checkpoint domain definition */ + if (def->dom && !other->def->dom) { + other->def->dom = def->dom; + def->dom = NULL; + } + goto cleanup; + } + } + + if (other == vm->current_checkpoint) { + *update_current = true; + vm->current_checkpoint = NULL; + } + + /* Drop and rebuild the parent relationship, but keep all + * child relations by reusing chk. */ + virDomainCheckpointDropParent(other); + virDomainCheckpointDefFree(other->def); + other->def = def; + *defptr = NULL; + *chk = other; + } else if (def->dom && virDomainCheckpointAlignDisks(def) < 0) { + goto cleanup; + } + + ret = 0; + cleanup: + return ret; +} diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index fa3db9266f..25fc4af450 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1,7 +1,7 @@ /* * domain_conf.c: domain XML processing * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2018 Red Hat, Inc. * Copyright (C) 2006-2008 Daniel P. Berrange * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany. * @@ -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" @@ -3313,6 +3314,7 @@ static void virDomainObjDispose(void *obj) (dom->privateDataFreeFunc)(dom->privateData); virDomainSnapshotObjListFree(dom->snapshots); + virDomainCheckpointObjListFree(dom->checkpoints); } virDomainObjPtr @@ -3342,6 +3344,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/libvirt_private.syms b/src/libvirt_private.syms index 5e22acb059..fbe7ba2d40 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -68,6 +68,27 @@ virCapabilitiesSetHostCPU; virCapabilitiesSetNetPrefix; +# conf/checkpoint_conf.h +virDomainCheckpointAlignDisks; +virDomainCheckpointAssignDef; +virDomainCheckpointDefFormat; +virDomainCheckpointDefFree; +virDomainCheckpointDefParseString; +virDomainCheckpointDropParent; +virDomainCheckpointFindByName; +virDomainCheckpointForEach; +virDomainCheckpointForEachChild; +virDomainCheckpointForEachDescendant; +virDomainCheckpointObjListFree; +virDomainCheckpointObjListNew; +virDomainCheckpointObjListRemove; +virDomainCheckpointRedefinePrep; +virDomainCheckpointTypeFromString; +virDomainCheckpointTypeToString; +virDomainCheckpointUpdateRelations; +virDomainListAllCheckpoints; + + # conf/cpu_conf.h virCPUCacheModeTypeFromString; virCPUCacheModeTypeToString; diff --git a/tests/Makefile.am b/tests/Makefile.am index 9b835fa369..87a4b3632b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -288,7 +288,7 @@ endif WITH_LIBXL if WITH_QEMU test_programs += qemuxml2argvtest qemuxml2xmltest \ - qemuargv2xmltest domainsnapshotxml2xmltest \ + qemuargv2xmltest domaincheckpointxml2xmltest domainsnapshotxml2xmltest \ qemumonitorjsontest qemuhotplugtest \ qemuagenttest qemucapabilitiestest qemucaps2xmltest \ qemumemlocktest \ @@ -673,6 +673,11 @@ qemublocktest_LDADD = \ $(LDADDS) \ $(NULL) +domaincheckpointxml2xmltest_SOURCES = \ + domaincheckpointxml2xmltest.c testutilsqemu.c testutilsqemu.h \ + testutils.c testutils.h +domaincheckpointxml2xmltest_LDADD = $(qemu_LDADDS) $(LDADDS) + domainsnapshotxml2xmltest_SOURCES = \ domainsnapshotxml2xmltest.c testutilsqemu.c testutilsqemu.h \ testutils.c testutils.h @@ -701,7 +706,7 @@ qemusecuritytest_LDADD = $(qemu_LDADDS) $(LDADDS) else ! WITH_QEMU EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuargv2xmltest.c \ - domainsnapshotxml2xmltest.c \ + domaincheckpointxml2xmltest.c domainsnapshotxml2xmltest.c \ testutilsqemu.c testutilsqemu.h \ testutilsqemuschema.c testutilsqemuschema.h \ qemumonitorjsontest.c qemuhotplugtest.c \ diff --git a/tests/domaincheckpointxml2xmltest.c b/tests/domaincheckpointxml2xmltest.c new file mode 100644 index 0000000000..5381b6352b --- /dev/null +++ b/tests/domaincheckpointxml2xmltest.c @@ -0,0 +1,231 @@ +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include <sys/types.h> +#include <fcntl.h> + +#include <regex.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; + +/* This regex will skip the following XML constructs in test files + * that are dynamically generated and thus problematic to test: + * <name>1234352345</name> if the checkpoint has no name, + * <creationTime>23523452345</creationTime>. + */ +static const char *testCheckpointXMLVariableLineRegexStr = + "<(name|creationTime)>[0-9]+</(name|creationTime)>"; + +regex_t *testCheckpointXMLVariableLineRegex = NULL; + +static char * +testFilterXML(char *xml) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + char **xmlLines = NULL; + char **xmlLine; + char *ret = NULL; + + if (!(xmlLines = virStringSplit(xml, "\n", 0))) { + VIR_FREE(xml); + goto cleanup; + } + VIR_FREE(xml); + + for (xmlLine = xmlLines; *xmlLine; xmlLine++) { + if (regexec(testCheckpointXMLVariableLineRegex, + *xmlLine, 0, NULL, 0) == 0) + continue; + + virBufferStrcat(&buf, *xmlLine, "\n", NULL); + } + + if (virBufferCheckError(&buf) < 0) + goto cleanup; + + ret = virBufferContentAndReset(&buf); + + cleanup: + virBufferFreeAndReset(&buf); + virStringListFree(xmlLines); + return ret; +} + +static int +testCompareXMLToXMLFiles(const char *inxml, + const char *outxml, + bool internal, + bool redefine) +{ + char *inXmlData = NULL; + char *outXmlData = NULL; + char *actual = NULL; + int ret = -1; + virDomainCheckpointDefPtr def = NULL; + unsigned int flags = VIR_DOMAIN_CHECKPOINT_PARSE_DISKS; + + if (internal) + flags |= VIR_DOMAIN_CHECKPOINT_PARSE_INTERNAL; + + if (redefine) + flags |= VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + + if (virTestLoadFile(inxml, &inXmlData) < 0) + goto cleanup; + + if (virTestLoadFile(outxml, &outXmlData) < 0) + goto cleanup; + + if (!(def = virDomainCheckpointDefParseString(inXmlData, driver.caps, + driver.xmlopt, + flags))) + goto cleanup; + + /* Parsing XML does not populate the domain definition, so add a + * canned bare-bones fallback */ + if (!def->dom) { + // HACK + ret = 77; + goto cleanup; + const char *def_dom = "" + "<domain type='qemu'>" + " <name>fedora</name>" + " <uuid>93a5c045-6457-2c09-e56c-927cdf34e178</uuid>" +/* arch='x86_64' machine='pc'*/ + " <os><type>hvm</type></os>" + "</domain>"; + int dom_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; + if (!(def->dom = virDomainDefParseString(def_dom, driver.caps, + driver.xmlopt, NULL, + dom_flags))) + goto cleanup; + } + + if (!(actual = virDomainCheckpointDefFormat(def, driver.caps, + driver.xmlopt, + VIR_DOMAIN_DEF_FORMAT_SECURE, + internal))) + goto cleanup; + + if (!redefine) { + if (!(actual = testFilterXML(actual))) + goto cleanup; + + if (!(outXmlData = testFilterXML(outXmlData))) + goto cleanup; + } + + if (STRNEQ(outXmlData, actual)) { + virTestDifferenceFull(stderr, outXmlData, outxml, actual, inxml); + goto cleanup; + } + + ret = 0; + + cleanup: + VIR_FREE(inXmlData); + VIR_FREE(outXmlData); + VIR_FREE(actual); + virDomainCheckpointDefFree(def); + return ret; +} + +struct testInfo { + const char *inxml; + const char *outxml; + bool internal; + bool redefine; +}; + + +static int +testCompareXMLToXMLHelper(const void *data) +{ + const struct testInfo *info = data; + + return testCompareXMLToXMLFiles(info->inxml, info->outxml, + info->internal, info->redefine); +} + + +static int +mymain(void) +{ + int ret = 0; + + if (qemuTestDriverInit(&driver) < 0) + return EXIT_FAILURE; + + if (VIR_ALLOC(testCheckpointXMLVariableLineRegex) < 0) + goto cleanup; + + if (regcomp(testCheckpointXMLVariableLineRegex, + testCheckpointXMLVariableLineRegexStr, + REG_EXTENDED | REG_NOSUB) != 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "failed to compile test regex"); + goto cleanup; + } + + +# define DO_TEST(prefix, name, inpath, outpath, internal, redefine) \ + do { \ + const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml", \ + abs_srcdir "/" outpath "/" name ".xml", \ + internal, redefine}; \ + if (virTestRun("CHECKPOINT XML-2-XML " prefix " " name, \ + testCompareXMLToXMLHelper, &info) < 0) \ + ret = -1; \ + } while (0) + +# define DO_TEST_INOUT(name, internal, redefine) \ + DO_TEST("in->out", name,\ + "domaincheckpointxml2xmlin",\ + "domaincheckpointxml2xmlout",\ + internal, redefine) + + /* 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); + + DO_TEST_INOUT("empty", false, false); + DO_TEST_INOUT("sample", false, false); + + cleanup: + if (testCheckpointXMLVariableLineRegex) + regfree(testCheckpointXMLVariableLineRegex); + VIR_FREE(testCheckpointXMLVariableLineRegex); + 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 2/6/19 2:18 PM, Eric Blake wrote:
Work in progress - the checkpoint code is not quite passing tests (part of that is figuring out the minimal XML that is still valid as a <domain> element, or just use --no-domain flag).
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 150 ++++ src/conf/domain_conf.h | 11 +- po/POTFILES | 1 + src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 1030 +++++++++++++++++++++++++++ src/conf/domain_conf.c | 7 +- src/libvirt_private.syms | 21 + tests/Makefile.am | 9 +- tests/domaincheckpointxml2xmltest.c | 231 ++++++ 9 files changed, 1458 insertions(+), 4 deletions(-) create mode 100644 src/conf/checkpoint_conf.h create mode 100644 src/conf/checkpoint_conf.c create mode 100644 tests/domaincheckpointxml2xmltest.c
Starting to lose some steam - seeing wip means I don't want to spend too much time on some algorithms for fear they'll change, but then again I know you're looking for feedback...
diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h new file mode 100644 index 0000000000..994a8bd083 --- /dev/null +++ b/src/conf/checkpoint_conf.h @@ -0,0 +1,150 @@ +/* + * checkpoint_conf.h: domain checkpoint XML processing + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange
This is new, right? Not a copy of...
+ * + * 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_CHECKPOINT_CONF_H +# define LIBVIRT_CHECKPOINT_CONF_H + +# include "internal.h" +# include "domain_conf.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 */
Recall earlier query in RNG file about unsigned long...
+}; + +/* Stores the complete checkpoint metadata */ +typedef struct _virDomainCheckpointDef virDomainCheckpointDef; +typedef virDomainCheckpointDef *virDomainCheckpointDefPtr; +struct _virDomainCheckpointDef { + /* Public XML. */ + char *name; + char *description; + char *parent; + long long creationTime; /* in seconds */ + + size_t ndisks; /* should not exceed dom->ndisks */ + virDomainCheckpointDiskDef *disks; + + virDomainDefPtr dom; + + /* Internal use. */ + bool current; /* At most one checkpoint in the list should have this set */
Typically these are then in the *Obj...
+}; + +struct _virDomainCheckpointObj { + virDomainCheckpointDefPtr def; /* non-NULL except for metaroot */ + + virDomainCheckpointObjPtr parent; /* non-NULL except for metaroot, before + virDomainCheckpointUpdateRelations, or + after virDomainCheckpointDropParent */ + virDomainCheckpointObjPtr sibling; /* NULL if last child of parent */ + size_t nchildren; + virDomainCheckpointObjPtr first_child; /* NULL if no children */
The whole relationship thing is I think overly complex and a bit difficult to follow. Having @sibling and @first_child seems to make the code even more complex. I would think you have a parent and maybe a child. If this is the "first" checkpoint, then there is no parent. I would think that means it's the root. There's just much more to be see when it comes to how these relationships play out as checkpoints are made and removed. I guess I just have a very linear model in mind, but reading the code seems to go beyond linearality. The use of hash tables just makes it easier to ensure no def->name is reused. Not having locks in the object means some parent lock is managing to make sure two checkpoint operations cannot occur at the same time from different threads (e.g. a domain object lock), but that is not 100% clear.
+};
When objectifying module to to avoid leaking *obj entries into other modules, this struct would go inside .c file w/ accessors to whatever is needed externally. I purposefully avoided domain & domain snapshot as there was just too much rework. Whether or not that can be done here - not sure yet, still reading, but figured I'd point it out at least ;-)
+ +virDomainCheckpointObjListPtr virDomainCheckpointObjListNew(void); +void virDomainCheckpointObjListFree(virDomainCheckpointObjListPtr checkpoints); + +typedef enum { + VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE = 1 << 0, + VIR_DOMAIN_CHECKPOINT_PARSE_DISKS = 1 << 1, + VIR_DOMAIN_CHECKPOINT_PARSE_INTERNAL = 1 << 2, +} virDomainCheckpointParseFlags; + +virDomainCheckpointDefPtr virDomainCheckpointDefParseString(const char *xmlStr, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +virDomainCheckpointDefPtr virDomainCheckpointDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +void virDomainCheckpointDefFree(virDomainCheckpointDefPtr def); +char *virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags, + bool internal); +int virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr checkpoint); +virDomainCheckpointObjPtr virDomainCheckpointAssignDef(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def); + +virDomainCheckpointObjPtr virDomainCheckpointFindByName(virDomainCheckpointObjListPtr checkpoints, + const char *name); +void virDomainCheckpointObjListRemove(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr checkpoint); +int virDomainCheckpointForEach(virDomainCheckpointObjListPtr checkpoints, + virHashIterator iter, + void *data); +int virDomainCheckpointForEachChild(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data); +int virDomainCheckpointForEachDescendant(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data); +int virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints); +void virDomainCheckpointDropParent(virDomainCheckpointObjPtr checkpoint);
More recently in the .h prototypes, been trying to follow the .c entry as well... where it's type function(args...); makes copy-pasta easier...
+ +# define VIR_DOMAIN_CHECKPOINT_FILTERS_METADATA \ + (VIR_DOMAIN_CHECKPOINT_LIST_METADATA | \ + VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA) + +# 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_METADATA | \ + VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) + +int virDomainListAllCheckpoints(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + virDomainPtr dom, + virDomainCheckpointPtr **objs, + unsigned int flags); + +int virDomainCheckpointRedefinePrep(virDomainPtr domain, + virDomainObjPtr vm, + virDomainCheckpointDefPtr *def, + virDomainCheckpointObjPtr *checkpoint, + virDomainXMLOptionPtr xmlopt, + bool *update_current); +
Add a: VIR_DEFINE_AUTOPTR_FUNC(virDomainCheckpointDef, virDomainCheckpointDefFree); details coming...
+VIR_ENUM_DECL(virDomainCheckpoint); + +#endif /* LIBVIRT_CHECKPOINT_CONF_H */ diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 5db4396fd5..d31c45427e 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1,7 +1,7 @@ /* * domain_conf.h: domain XML processing * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2018 Red Hat, Inc. * Copyright (C) 2006-2008 Daniel P. Berrange * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany. * @@ -119,6 +119,12 @@ typedef virDomainMemballoonDef *virDomainMemballoonDefPtr; typedef struct _virDomainNVRAMDef virDomainNVRAMDef; typedef virDomainNVRAMDef *virDomainNVRAMDefPtr;
+typedef struct _virDomainCheckpointObj virDomainCheckpointObj; +typedef virDomainCheckpointObj *virDomainCheckpointObjPtr; + +typedef struct _virDomainCheckpointObjList virDomainCheckpointObjList; +typedef virDomainCheckpointObjList *virDomainCheckpointObjListPtr; + typedef struct _virDomainSnapshotObj virDomainSnapshotObj; typedef virDomainSnapshotObj *virDomainSnapshotObjPtr;
@@ -2631,6 +2637,9 @@ struct _virDomainObj {
bool hasManagedSave;
+ virDomainCheckpointObjListPtr checkpoints; + virDomainCheckpointObjPtr current_checkpoint; +
Note to self or you since I could forget by the time I find this usage... wouldn't update to current_checkpoint need a lock? What's really not clear is whether the domain object lock is in place or not when we get here. I think the domainobj's are just plain write locks and not the domainobjlist fancy rwlocks.
void *privateData; void (*privateDataFreeFunc)(void *);
diff --git a/po/POTFILES b/po/POTFILES index 88af551664..57c55fb35f 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 219ff350d7..c425363bde 100644 --- a/src/conf/Makefile.inc.am +++ b/src/conf/Makefile.inc.am @@ -10,6 +10,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..c0840a96b2 --- /dev/null +++ b/src/conf/checkpoint_conf.c @@ -0,0 +1,1030 @@ +/* + * checkpoint_conf.c: domain checkpoint XML processing + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange
Similar w/r/t copyright
+ * + * 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 <fcntl.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <unistd.h> + +#include "internal.h" +#include "virbitmap.h" +#include "virbuffer.h" +#include "count-one-bits.h" +#include "datatypes.h" +#include "domain_conf.h" +#include "virlog.h" +#include "viralloc.h" +#include "netdev_bandwidth_conf.h" +#include "netdev_vport_profile_conf.h" +#include "nwfilter_conf.h" +#include "secret_conf.h" +#include "checkpoint_conf.h" +#include "virstoragefile.h" +#include "viruuid.h" +#include "virfile.h" +#include "virerror.h" +#include "virxml.h" +#include "virstring.h"
Are all of these really needed?
+ +#define VIR_FROM_THIS VIR_FROM_DOMAIN_CHECKPOINT + +VIR_LOG_INIT("conf.checkpoint_conf"); + +VIR_ENUM_IMPL(virDomainCheckpoint, VIR_DOMAIN_CHECKPOINT_TYPE_LAST, + "default", "no", "bitmap"); + +struct _virDomainCheckpointObjList { + /* name string -> virDomainCheckpointObj mapping + * for O(1), lockless lookup-by-name */ + virHashTable *objs; + + virDomainCheckpointObj metaroot; /* Special parent of all root checkpoints */'
So not a pointer, but copied object from somewhere?
+}; + +/* Checkpoint Def functions */ +static void +virDomainCheckpointDiskDefClear(virDomainCheckpointDiskDefPtr disk) +{ + VIR_FREE(disk->name); + VIR_FREE(disk->bitmap);
Should we clear idx, size, and type too? Depends on consumer usage which could clear and reuse, thus using something old.
+} +
... Two blank lines between functions (repeats)
+void virDomainCheckpointDefFree(virDomainCheckpointDefPtr def)
Two lines void virDomain*
+{ + size_t i; + + if (!def) + return; + + VIR_FREE(def->name); + VIR_FREE(def->description); + VIR_FREE(def->parent); + for (i = 0; i < def->ndisks; i++) + virDomainCheckpointDiskDefClear(&def->disks[i]); + VIR_FREE(def->disks); + virDomainDefFree(def->dom); + VIR_FREE(def); +} + +static int +virDomainCheckpointDiskDefParseXML(xmlNodePtr node, + xmlXPathContextPtr ctxt, + virDomainCheckpointDiskDefPtr def) +{ + int ret = -1; + char *checkpoint = NULL; + char *bitmap = NULL;
Use VIR_AUTOFREE(char *) for both
+ xmlNodePtr saved = ctxt->node; + + ctxt->node = node; + + def->name = virXMLPropString(node, "name"); + if (!def->name) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing name from disk checkpoint element")); + goto cleanup; + } + + checkpoint = virXMLPropString(node, "checkpoint"); + if (checkpoint) { + def->type = virDomainCheckpointTypeFromString(checkpoint); + if (def->type <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk checkpoint setting '%s'"), + checkpoint); + goto cleanup; + } + } else { + def->type = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + } + + bitmap = virXMLPropString(node, "bitmap"); + if (bitmap) { + if (def->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk checkpoint bitmap '%s' requires " + "type='bitmap'"), + bitmap); + goto cleanup; + } + VIR_STEAL_PTR(def->bitmap, bitmap); + }
Size is not parsed. Restore from restart will lose it.
+ + ret = 0; + cleanup: + ctxt->node = saved; + + VIR_FREE(checkpoint); + VIR_FREE(bitmap);
With VIR_AUTOFREE, these 2 are unnecessary
+ if (ret < 0) + virDomainCheckpointDiskDefClear(def);
The caller does this on error anyway so it's unnecessary (since ndisks == n from parse).
+ return ret; +} + +/* flags is bitwise-or of virDomainCheckpointParseFlags. + * If flags does not include VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE, then + * caps are ignored. + */ +static virDomainCheckpointDefPtr +virDomainCheckpointDefParse(xmlXPathContextPtr ctxt, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainCheckpointDefPtr def = NULL; + virDomainCheckpointDefPtr ret = NULL; + xmlNodePtr *nodes = NULL; + size_t i; + int n; + char *creation = NULL;
Never used.
+ struct timeval tv; + int active; + char *tmp;
VIR_AUTOPTR(virDomainCheckpointDef) def = NULL; VIR_AUTOFREE(xmlNodePtr *) nodes = NULL; VIR_AUTOFREE(char *) tmp = NULL; Erik likes 'em at the bottom too. With @ret autofree'd, the goto cleanup is unnecessary and replaced by return NULL Use of the AUTOFREE stuff really cleans up a lot.
+ + if (VIR_ALLOC(def) < 0) + goto cleanup; + + gettimeofday(&tv, NULL); + + def->name = virXPathString("string(./name)", ctxt); + if (def->name == NULL) { + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("a redefined checkpoint must have a name")); + goto cleanup; + } + if (virAsprintf(&def->name, "%lld", (long long)tv.tv_sec) < 0) + goto cleanup; + } + + def->description = virXPathString("string(./description)", ctxt); + + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE) { + if (virXPathLongLong("string(./creationTime)", ctxt, + &def->creationTime) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing creationTime from existing checkpoint")); + goto cleanup; + } + + def->parent = 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);
Unnecessary w/ autofree
+ if (!domainNode) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint")); + goto cleanup; + } + def->dom = virDomainDefParseNode(ctxt->node->doc, domainNode, + caps, xmlopt, NULL, domainflags); + if (!def->dom) + goto cleanup; + } else { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint redefine")); + goto cleanup; + } + } else { + def->creationTime = tv.tv_sec; + } + + if ((n = virXPathNodeSet("./disks/*", ctxt, &nodes)) < 0) + goto cleanup; + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_DISKS) { + if (n && VIR_ALLOC_N(def->disks, n) < 0) + goto cleanup; + def->ndisks = n; + for (i = 0; i < def->ndisks; i++) { + if (virDomainCheckpointDiskDefParseXML(nodes[i], ctxt, + &def->disks[i]) < 0) + goto cleanup; + } + VIR_FREE(nodes);
Unnecessary w/ autofree
+ } else if (n) { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, "%s", + _("unable to handle disk requests in checkpoint")); + goto cleanup; + } + + if (flags & VIR_DOMAIN_CHECKPOINT_PARSE_INTERNAL) { + if (virXPathInt("string(./active)", ctxt, &active) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not find 'active' element")); + goto cleanup; + } + def->current = active != 0; + } + + VIR_STEAL_PTR(ret, def); + + cleanup: + VIR_FREE(creation); + VIR_FREE(nodes); + virDomainCheckpointDefFree(def);
Cleanup unnecessary, just return ret;
+ + return ret; +} + +virDomainCheckpointDefPtr +virDomainCheckpointDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + xmlXPathContextPtr ctxt = NULL; + virDomainCheckpointDefPtr def = NULL; + + if (!virXMLNodeNameEqual(root, "domaincheckpoint")) { + virReportError(VIR_ERR_XML_ERROR, "%s", _("domaincheckpoint")); + goto cleanup;
Could just return NULL
+ } + + ctxt = xmlXPathNewContext(xml); + if (ctxt == NULL) { + virReportOOMError(); + goto cleanup;
return NULL;
+ } + + 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->name) < 0) + return -1; + } + + return 0; +} + + +static int +virDomainCheckpointCompareDiskIndex(const void *a, const void *b)
One line for each argument
+{ + const virDomainCheckpointDiskDef *diska = a; + const virDomainCheckpointDiskDef *diskb = b; + + /* Integer overflow shouldn't be a problem here. */
s/. /. / (one space before */)
+ 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. */
Things start to get more complex and confusing. What doesn't make sense here is why/how things/order could be different. If this is a checkpoint in time, then wouldn't the order saved (somewhere) be the same? So what causes something to get out of order? I guess this is part of the "problem" with posting code that's been in development for a long time. I'm sure this solves some issue, but what that is, I'm not sure. I'm beginning to think splitting things up to get the basic add objects, list objects, etc. and then adding in the more complex well we need to output a list in this manner or that manner or we have discovered an issue with order as a result of some operation and this fixes it would be more prudent. Means quite a bit of patch reordering to not expose the external API's, but I think will make things easier to review and I would think easier to debug and finish up the work.
+int +virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr def) +{ + int ret = -1; + virBitmapPtr map = NULL; + size_t i; + int ndisks; + int checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_NONE; +
I assume we'd enter here with sort of lock, right? to prevent some other unexpected action from undoing us.
+ if (!def->dom) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint")); + goto cleanup; + } + + if (def->ndisks > def->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->dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("domain must have at least one disk to perform " + "checkpoints")); + goto cleanup; + }
I'm still trying to come to grips with how def->ndisks != def->dom->ndisks.
+ + /* If <disks> omitted, do bitmap on all disks; otherwise, do nothing + * for omitted disks */ + if (!def->ndisks) + checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + + if (!(map = virBitmapNew(def->dom->ndisks))) + goto cleanup; + + /* Double check requested disks. */ + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &def->disks[i]; + int idx = virDomainDiskIndexByName(def->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; + } + ignore_value(virBitmapSetBit(map, idx)); + disk->idx = idx; + + if (STRNEQ(disk->name, def->dom->disks[idx]->dst)) { + VIR_FREE(disk->name); + if (VIR_STRDUP(disk->name, def->dom->disks[idx]->dst) < 0) + goto cleanup; + } + }
There's something about this remapping of disk->name that doesn't feel right or is ripe for something going wrong. I would think we'd want to correllate where we are/were in a previous list with now, but I guess I'm not 100% of how this is/will be used.
+ + /* Provide defaults for all remaining disks. */ + ndisks = def->ndisks; + if (VIR_EXPAND_N(def->disks, def->ndisks, + def->dom->ndisks - def->ndisks) < 0) + goto cleanup; + + for (i = 0; i < def->dom->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk; + + if (virBitmapIsBitSet(map, i)) + continue; + disk = &def->disks[ndisks++]; + if (VIR_STRDUP(disk->name, def->dom->disks[i]->dst) < 0) + goto cleanup; + disk->idx = i; + + /* Don't checkpoint empty drives */ + if (virStorageSourceIsEmpty(def->dom->disks[i]->src)) + 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; +} + +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_XML_SIZE) + virBufferAsprintf(buf, " size='%llu'", disk->size);
Recall my earlier point in RNG format w/ unsignedLong
+ } + virBufferAddLit(buf, "/>\n"); + return 0; +} + + +char * +virDomainCheckpointDefFormat(virDomainCheckpointDefPtr def, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags, + bool internal) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + size_t i; + unsigned int domflags = VIR_DOMAIN_DEF_FORMAT_INACTIVE; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_XML_SECURE | + VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN | + VIR_DOMAIN_CHECKPOINT_XML_SIZE, NULL); + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SECURE) + domflags |= VIR_DOMAIN_DEF_FORMAT_SECURE; + + virBufferAddLit(&buf, "<domaincheckpoint>\n"); + virBufferAdjustIndent(&buf, 2); + + virBufferEscapeString(&buf, "<name>%s</name>\n", def->name); + if (def->description) + virBufferEscapeString(&buf, "<description>%s</description>\n", + def->description); + + if (def->parent) { + virBufferAddLit(&buf, "<parent>\n"); + virBufferAdjustIndent(&buf, 2); + virBufferEscapeString(&buf, "<name>%s</name>\n", def->parent); + virBufferAdjustIndent(&buf, -2); + virBufferAddLit(&buf, "</parent>\n");
Interesting any reason why it's not <parent>%s</parent> - are you planning to add to parent. Guess I should have asked earlier, but it only dawns on me now seeing this.
+ } + + virBufferAsprintf(&buf, "<creationTime>%lld</creationTime>\n", + def->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_XML_NO_DOMAIN) && + virDomainDefFormatInternal(def->dom, caps, domflags, &buf, xmlopt) < 0) + goto error; + + if (internal) + virBufferAsprintf(&buf, "<active>%d</active>\n", def->current);
So this is only valid for ACTIVE guest and not INACTIVE parse/format? Perhaps no need for bool, but use @flags...
+ + virBufferAdjustIndent(&buf, -2); + virBufferAddLit(&buf, "</domaincheckpoint>\n"); + + if (virBufferCheckError(&buf) < 0) + return NULL; + + return virBufferContentAndReset(&buf); + + error: + virBufferFreeAndReset(&buf); + return NULL; +} +
Keeping the *Def logic separate from the *Obj logic is the new norm. The snapshot_conf was left alone by me when I went through the others... Anything object related should be virdomaincheckpointobj.{c,h}. That means stuff above when separated out could be added sooner in order to make sure review cycles shorter and the pile of code smaller. Then to start the object code should only need a subset of what's here. Adding in the more complex stuff later. There's probably a couple of patches worth of changes and functionality all being rolled into one here. I think it'll be easier overall to review and get accepted if a more logical (and slow) progression to the desired functionality is posted.
+/* Checkpoint Obj functions */ +static virDomainCheckpointObjPtr virDomainCheckpointObjNew(void)
Two lines
+{ + virDomainCheckpointObjPtr checkpoint; + + if (VIR_ALLOC(checkpoint) < 0) + return NULL; + + VIR_DEBUG("obj=%p", checkpoint); + + return checkpoint; +} + +static void virDomainCheckpointObjFree(virDomainCheckpointObjPtr checkpoint) +{ + if (!checkpoint) + return; + + VIR_DEBUG("obj=%p", checkpoint); + + virDomainCheckpointDefFree(checkpoint->def); + VIR_FREE(checkpoint); +}
Hmm, so not a real virObject... The domain and domain snapshot code was just too inter-related for me to attempt to modify back when I objectified other driver -> object -> def code. I think if you follow examples in virstorageobj, virsecretobj, virnodedevobj, virinterfaceobj, etc. you'll get a better mechanism than what exists in the domain and domain snapshot code for for how vir*Object can be used with hash tables. There's no comments here... most important perhaps is on success @def is consume in the object and thus cannot be free'd by the caller.
+ +virDomainCheckpointObjPtr virDomainCheckpointAssignDef(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def)
virDomainCheckpointObjPtr virDomainCheckpointAssignDef(...) I think "virDomainCheckpointObjListAdd" would be what's happening here.
+{ + virDomainCheckpointObjPtr chk; + + if (virHashLookup(checkpoints->objs, def->name) != NULL) {
s/ != NULL//
+ virReportError(VIR_ERR_OPERATION_INVALID, + _("domain checkpoint %s already exists"), + def->name); + return NULL; + } + + if (!(chk = virDomainCheckpointObjNew())) + return NULL; + chk->def = def; + + if (virHashAddEntry(checkpoints->objs, chk->def->name, chk) < 0) {
Hopefully we can never generate two checkpoints by name in the same clocktick ;-)
+ VIR_FREE(chk); + return NULL; + } + + return chk; +} + +/* Checkpoint Obj List functions */ +static void +virDomainCheckpointObjListDataFree(void *payload, + const void *name ATTRIBUTE_UNUSED) +{ + virDomainCheckpointObjPtr obj = payload; + + virDomainCheckpointObjFree(obj); +} + +virDomainCheckpointObjListPtr +virDomainCheckpointObjListNew(void) +{ + virDomainCheckpointObjListPtr checkpoints; + if (VIR_ALLOC(checkpoints) < 0) + return NULL; + checkpoints->objs = virHashCreate(50, virDomainCheckpointObjListDataFree); + if (!checkpoints->objs) { + VIR_FREE(checkpoints); + return NULL; + }
How is metaroot handled? At this point it's just an empty struct. Guess my first inclination was that it was going to be the first checkpoint added. That would mean some amount of management when it comes to that checkpoint being free'd on us.
+ return checkpoints; +} + +void +virDomainCheckpointObjListFree(virDomainCheckpointObjListPtr checkpoints) +{ + if (!checkpoints) + return; + virHashFree(checkpoints->objs); + VIR_FREE(checkpoints); +} + +struct virDomainCheckpointNameData { + char **const names; + int maxnames; + unsigned int flags; + int count; + bool error; +}; + +static int +virDomainCheckpointObjListCopyNames(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) +{ + virDomainCheckpointObjPtr obj = payload; + struct virDomainCheckpointNameData *data = opaque; + + if (data->error) + return 0; + /* Caller already sanitized flags. Filtering on DESCENDANTS was + * done by choice of iteration in the caller. */ + if ((data->flags & VIR_DOMAIN_CHECKPOINT_LIST_LEAVES) && obj->nchildren) + return 0; + if ((data->flags & VIR_DOMAIN_CHECKPOINT_LIST_NO_LEAVES) && !obj->nchildren) + return 0;
Filters in these callback routines have been ACL Filters (see src/conf/vir*obj*.c other than snapshot of course).
+ + if (data->names && data->count < data->maxnames && + VIR_STRDUP(data->names[data->count], obj->def->name) < 0) { + data->error = true; + return 0; + } + data->count++; + return 0; +} +
Maybe comments here would help answer questions below.
+static int +virDomainCheckpointObjListGetNames(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + char **const names, int maxnames,
args...
+ unsigned int flags) +{ + struct virDomainCheckpointNameData data = { names, maxnames, flags, 0, + false }; + size_t i; + + if (!from) { + /* LIST_ROOTS and LIST_DESCENDANTS have the same bit value, + * but opposite semantics. Toggle here to get the correct + * traversal on the metaroot. */ + flags ^= VIR_DOMAIN_CHECKPOINT_LIST_ROOTS; + from = &checkpoints->metaroot;
I don't see checkpoints->metaroot being set anywhere yet. I'd expect a Set/Get type API for external callers.
+ } + + /* We handle LIST_ROOT/LIST_DESCENDANTS directly, mask that bit + * out to determine when we must use the filter callback. */ + data.flags &= ~VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS; + + /* If this common code is being used, we assume that all checkpoints + * have metadata, and thus can handle METADATA up front as an + * all-or-none filter. XXX This might not always be true, if we ^^^ Need to address the XXX...
+ * add the ability to track qcow2 bitmaps without the + * use of metadata. */ + if ((data.flags & VIR_DOMAIN_CHECKPOINT_FILTERS_METADATA) == + VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA) + return 0; + data.flags &= ~VIR_DOMAIN_CHECKPOINT_FILTERS_METADATA; + + /* For ease of coding the visitor, it is easier to zero each group + * where all of the bits are set. */ + if ((data.flags & VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) == + VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) + data.flags &= ~VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES; +
oh, my head, my eyes, my favorite deity. It seems we're building a mostrously complex pile here. I have to wonder whether leaves and filters are really worth all this trouble and whether/how this will be used.
+ if (flags & VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS) { + if (from->def) + virDomainCheckpointForEachDescendant(from, + virDomainCheckpointObjListCopyNames, + &data); + else if (names || data.flags) + virHashForEach(checkpoints->objs, + virDomainCheckpointObjListCopyNames, + &data); + else + data.count = virHashSize(checkpoints->objs); + } else if (names || data.flags) { + virDomainCheckpointForEachChild(from, + virDomainCheckpointObjListCopyNames, + &data); + } else { + data.count = from->nchildren;
Well this seems to be some sort of magic...
+ }> + + if (data.error) { + for (i = 0; i < data.count; i++) + VIR_FREE(names[i]); + return -1; + } + + return data.count; +} + +static int +virDomainCheckpointObjListNum(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + unsigned int flags) +{
FWIW: virHashSize get's nb_elems in hash table. Do we even need this anymore? Functionality is above, abeit quite hidden in the pile of if - then - else of VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES
+ return virDomainCheckpointObjListGetNames(checkpoints, from, NULL, 0, + flags); +} + +virDomainCheckpointObjPtr +virDomainCheckpointFindByName(virDomainCheckpointObjListPtr checkpoints, + const char *name)
Usually have this closer to or above the *Add function and not in the midst of these functions that use Hash callbacks...
+{ + return name ? virHashLookup(checkpoints->objs, name) : + &checkpoints->metaroot;
Would a caller really expect this? That is obj->def == NULL?
+} + +void virDomainCheckpointObjListRemove(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr checkpoint) +{
I think when I first read things, it felt like metaroot was the first checkpoint ever created... If that does come to pass this is where the well we're about to remove metaroot would occur so we have to replace it with it's direct child/descendant.
+ virHashRemoveEntry(checkpoints->objs, checkpoint->def->name); +} + +int +virDomainCheckpointForEach(virDomainCheckpointObjListPtr checkpoints, + virHashIterator iter, + void *data) +{ + return virHashForEach(checkpoints->objs, iter, data); +} + +/* Run iter(data) on all direct children of checkpoint, while ignoring all + * other entries in checkpoints. Return the number of children + * visited. No particular ordering is guaranteed. */ +int +virDomainCheckpointForEachChild(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data) +{ + virDomainCheckpointObjPtr child = checkpoint->first_child; + + while (child) { + virDomainCheckpointObjPtr next = child->sibling; + (iter)(child, child->def->name, data); + child = next; + } + + return checkpoint->nchildren; +} + +struct checkpoint_act_on_descendant { + int number; + virHashIterator iter; + void *data; +}; + +static int +virDomainCheckpointActOnDescendant(void *payload, + const void *name, + void *data) +{ + virDomainCheckpointObjPtr obj = payload; + struct checkpoint_act_on_descendant *curr = data; + + curr->number += 1 + virDomainCheckpointForEachDescendant(obj, + curr->iter, + curr->data);
Does this double count with the 1 +. Having a really hard time understanding what's being done ... What's really the difference between a descendant and a child?
+ (curr->iter)(payload, name, curr->data); + return 0; +} + +/* Run iter(data) on all descendants of checkpoint, while ignoring all + * other entries in checkpoints. Return the number of descendants + * visited. No particular ordering is guaranteed. */ +int +virDomainCheckpointForEachDescendant(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data) +{ + struct checkpoint_act_on_descendant act; + + act.number = 0; + act.iter = iter; + act.data = data; + virDomainCheckpointForEachChild(checkpoint, + virDomainCheckpointActOnDescendant, &act);
So many levels of indirection in multiple directions. We call ForEachChild, which calls an @iter function to act on the descendant which calls ForEachDescendant (IOW, this function) again. ForEachDescendant and ForEachChild use ActOnDescendant, but have this relationship with each other which is hard to think about without some sort of picture. Maybe I'm thinking of a much simpler model where parent <- child1 <- child2 <- child3 [etc.] One takes a checkpoint and it is the parent. Then at some point in the future another checkpoint is taken. So it's now the child1 with it's parent. When the next checkpoint is taken, it's grandparent is parent and child1 is it's parent. Getting a desendant of some "child#" I thought was a linear operation. If it's more of a tree operation then the model used by the hash table to bucket and link same hashed values into a bucket would then be more useful.
+ + return act.number; +} + +/* Struct and callback function used as a hash table callback; each call + * inspects the pre-existing checkpoint->def->parent field, and adjusts + * the checkpoint->parent field as well as the parent's child fields to + * wire up the hierarchical relations for the given checkpoint. The error + * indicator gets set if a parent is missing or a requested parent would + * cause a circular parent chain. */ +struct checkpoint_set_relation { + virDomainCheckpointObjListPtr checkpoints; + int err; +};
Having a hard time picturing the use...
+static int +virDomainCheckpointSetRelations(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *data)> +{ + virDomainCheckpointObjPtr obj = payload; + struct checkpoint_set_relation *curr = data; + virDomainCheckpointObjPtr tmp; + + obj->parent = virDomainCheckpointFindByName(curr->checkpoints, + obj->def->parent); + if (!obj->parent) { + curr->err = -1; + obj->parent = &curr->checkpoints->metaroot; + VIR_WARN("checkpoint %s lacks parent", obj->def->name); + } else { + tmp = obj->parent; + while (tmp && tmp->def) { + if (tmp == obj) { + curr->err = -1; + obj->parent = &curr->checkpoints->metaroot; + VIR_WARN("checkpoint %s in circular chain", obj->def->name); + break; + } + tmp = tmp->parent; + } + } + obj->parent->nchildren++; + obj->sibling = obj->parent->first_child; + obj->parent->first_child = obj;
Ah so I see where nchildren comes from... Not sure I'm clear yet on the need for sibling/first_child. This would appear to be inserting something into the middle of a list.
+ return 0; +} + +/* Populate parent link and child count of all checkpoints, with all + * relations starting as 0/NULL. Return 0 on success, -1 if a parent + * is missing or if a circular relationship was requested. */ +int +virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints) +{ + struct checkpoint_set_relation act = { checkpoints, 0 }; +
Updating the relations for everyone? Having a hard time thinking about the usage model that would need this. If an objet is removed, the update is simple - alter the parent/child. If for some reason we're rebuilding the tree - what is that reason? What causes this to be necessary. I know, you've been at this for a long time.
+ virHashForEach(checkpoints->objs, virDomainCheckpointSetRelations, &act); + return act.err; +} + +/* Prepare to reparent or delete checkpoint, by removing it from its + * current listed parent. Note that when bulk removing all children + * of a parent, it is faster to just 0 the count rather than calling + * this function on each child. */ +void +virDomainCheckpointDropParent(virDomainCheckpointObjPtr checkpoint) +{ + virDomainCheckpointObjPtr prev = NULL; + virDomainCheckpointObjPtr curr = NULL; + + checkpoint->parent->nchildren--;
Decrementing the child count before actually dropping the child?
+ curr = checkpoint->parent->first_child; + while (curr != checkpoint) { + if (!curr) { + VIR_WARN("inconsistent checkpoint relations"); + return; + } + prev = curr; + curr = curr->sibling; + } + if (prev) + prev->sibling = checkpoint->sibling; + else + checkpoint->parent->first_child = checkpoint->sibling; + checkpoint->parent = NULL; + checkpoint->sibling = NULL; +} + +int +virDomainListAllCheckpoints(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + virDomainPtr dom, + virDomainCheckpointPtr **chks, + unsigned int flags) +{
There's better examples than what *snapshots do in order to build/export a list of virDomainCheckpointPtr's - usually in *Export type functions.
+ int count = virDomainCheckpointObjListNum(checkpoints, from, flags);
Go direct virHashSize
+ virDomainCheckpointPtr *list = NULL; + char **names; + int ret = -1; + size_t i; + + if (!chks || count < 0)
<= 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)
indent is off
+ goto cleanup; + for (i = 0; i < count; i++) + if ((list[i] = virGetDomainCheckpoint(dom, names[i])) == NULL)
Other examples will show that this is done in the callback function. I know you've found it easier to use the same callback function for several different kinds of operations. I tried that too when I did the common object changes and got told/reminded during review that one function should serve 3 or 4 purposes as it makes each callback function more unnecessarily complicated especially w/r/t knowing what each caller may expect. So take that advice as something to follow for the round.
+ 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; +} + +
This is like the vir*ObjList*Export* function from other code. Only a very sparse look/scan of anything below this point. John
+int> +virDomainCheckpointRedefinePrep(virDomainPtr domain, + virDomainObjPtr vm, + virDomainCheckpointDefPtr *defptr, + virDomainCheckpointObjPtr *chk, + virDomainXMLOptionPtr xmlopt, + bool *update_current) +{ + virDomainCheckpointDefPtr def = *defptr; + int ret = -1; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + virDomainCheckpointObjPtr other; + + virUUIDFormat(domain->uuid, uuidstr); + + /* Prevent circular chains */ + if (def->parent) { + if (STREQ(def->name, def->parent)) { + virReportError(VIR_ERR_INVALID_ARG, + _("cannot set checkpoint %s as its own parent"), + def->name); + goto cleanup; + } + other = virDomainCheckpointFindByName(vm->checkpoints, def->parent); + if (!other) { + virReportError(VIR_ERR_INVALID_ARG, + _("parent %s for checkpoint %s not found"), + def->parent, def->name); + goto cleanup; + } + while (other->def->parent) { + if (STREQ(other->def->parent, def->name)) { + virReportError(VIR_ERR_INVALID_ARG, + _("parent %s would create cycle to %s"), + other->def->name, def->name); + goto cleanup; + } + other = virDomainCheckpointFindByName(vm->checkpoints, + other->def->parent); + if (!other) { + VIR_WARN("checkpoints are inconsistent for %s", + vm->def->name); + break; + } + } + } + + if (def->dom && + memcmp(def->dom->uuid, domain->uuid, VIR_UUID_BUFLEN)) { + virReportError(VIR_ERR_INVALID_ARG, + _("definition for checkpoint %s must use uuid %s"), + def->name, uuidstr); + goto cleanup; + } + + other = virDomainCheckpointFindByName(vm->checkpoints, def->name); + if (other) { + if (other->def->dom) { + if (def->dom) { + if (!virDomainDefCheckABIStability(other->def->dom, + def->dom, xmlopt)) + goto cleanup; + } else { + /* Transfer the domain def */ + def->dom = other->def->dom; + other->def->dom = NULL; + } + } + + if (def->dom) { + if (virDomainCheckpointAlignDisks(def) < 0) { + /* revert stealing of the checkpoint domain definition */ + if (def->dom && !other->def->dom) { + other->def->dom = def->dom; + def->dom = NULL; + } + goto cleanup; + } + } + + if (other == vm->current_checkpoint) { + *update_current = true; + vm->current_checkpoint = NULL; + } + + /* Drop and rebuild the parent relationship, but keep all + * child relations by reusing chk. */ + virDomainCheckpointDropParent(other); + virDomainCheckpointDefFree(other->def); + other->def = def; + *defptr = NULL; + *chk = other; + } else if (def->dom && virDomainCheckpointAlignDisks(def) < 0) { + goto cleanup; + } + + ret = 0; + cleanup: + return ret; +} diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index fa3db9266f..25fc4af450 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1,7 +1,7 @@ /* * domain_conf.c: domain XML processing * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2018 Red Hat, Inc. * Copyright (C) 2006-2008 Daniel P. Berrange * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany. * @@ -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" @@ -3313,6 +3314,7 @@ static void virDomainObjDispose(void *obj) (dom->privateDataFreeFunc)(dom->privateData);
virDomainSnapshotObjListFree(dom->snapshots); + virDomainCheckpointObjListFree(dom->checkpoints); }
virDomainObjPtr @@ -3342,6 +3344,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/libvirt_private.syms b/src/libvirt_private.syms index 5e22acb059..fbe7ba2d40 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -68,6 +68,27 @@ virCapabilitiesSetHostCPU; virCapabilitiesSetNetPrefix;
+# conf/checkpoint_conf.h +virDomainCheckpointAlignDisks; +virDomainCheckpointAssignDef; +virDomainCheckpointDefFormat; +virDomainCheckpointDefFree; +virDomainCheckpointDefParseString; +virDomainCheckpointDropParent; +virDomainCheckpointFindByName; +virDomainCheckpointForEach; +virDomainCheckpointForEachChild; +virDomainCheckpointForEachDescendant; +virDomainCheckpointObjListFree; +virDomainCheckpointObjListNew; +virDomainCheckpointObjListRemove; +virDomainCheckpointRedefinePrep; +virDomainCheckpointTypeFromString; +virDomainCheckpointTypeToString; +virDomainCheckpointUpdateRelations; +virDomainListAllCheckpoints; + + # conf/cpu_conf.h virCPUCacheModeTypeFromString; virCPUCacheModeTypeToString; diff --git a/tests/Makefile.am b/tests/Makefile.am index 9b835fa369..87a4b3632b 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -288,7 +288,7 @@ endif WITH_LIBXL
if WITH_QEMU test_programs += qemuxml2argvtest qemuxml2xmltest \ - qemuargv2xmltest domainsnapshotxml2xmltest \ + qemuargv2xmltest domaincheckpointxml2xmltest domainsnapshotxml2xmltest \ qemumonitorjsontest qemuhotplugtest \ qemuagenttest qemucapabilitiestest qemucaps2xmltest \ qemumemlocktest \ @@ -673,6 +673,11 @@ qemublocktest_LDADD = \ $(LDADDS) \ $(NULL)
+domaincheckpointxml2xmltest_SOURCES = \ + domaincheckpointxml2xmltest.c testutilsqemu.c testutilsqemu.h \ + testutils.c testutils.h +domaincheckpointxml2xmltest_LDADD = $(qemu_LDADDS) $(LDADDS) + domainsnapshotxml2xmltest_SOURCES = \ domainsnapshotxml2xmltest.c testutilsqemu.c testutilsqemu.h \ testutils.c testutils.h @@ -701,7 +706,7 @@ qemusecuritytest_LDADD = $(qemu_LDADDS) $(LDADDS)
else ! WITH_QEMU EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuargv2xmltest.c \ - domainsnapshotxml2xmltest.c \ + domaincheckpointxml2xmltest.c domainsnapshotxml2xmltest.c \ testutilsqemu.c testutilsqemu.h \ testutilsqemuschema.c testutilsqemuschema.h \ qemumonitorjsontest.c qemuhotplugtest.c \ diff --git a/tests/domaincheckpointxml2xmltest.c b/tests/domaincheckpointxml2xmltest.c new file mode 100644 index 0000000000..5381b6352b --- /dev/null +++ b/tests/domaincheckpointxml2xmltest.c @@ -0,0 +1,231 @@ +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include <sys/types.h> +#include <fcntl.h> + +#include <regex.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; + +/* This regex will skip the following XML constructs in test files + * that are dynamically generated and thus problematic to test: + * <name>1234352345</name> if the checkpoint has no name, + * <creationTime>23523452345</creationTime>. + */ +static const char *testCheckpointXMLVariableLineRegexStr = + "<(name|creationTime)>[0-9]+</(name|creationTime)>"; + +regex_t *testCheckpointXMLVariableLineRegex = NULL; + +static char * +testFilterXML(char *xml) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + char **xmlLines = NULL; + char **xmlLine; + char *ret = NULL; + + if (!(xmlLines = virStringSplit(xml, "\n", 0))) { + VIR_FREE(xml); + goto cleanup; + } + VIR_FREE(xml); + + for (xmlLine = xmlLines; *xmlLine; xmlLine++) { + if (regexec(testCheckpointXMLVariableLineRegex, + *xmlLine, 0, NULL, 0) == 0) + continue; + + virBufferStrcat(&buf, *xmlLine, "\n", NULL); + } + + if (virBufferCheckError(&buf) < 0) + goto cleanup; + + ret = virBufferContentAndReset(&buf); + + cleanup: + virBufferFreeAndReset(&buf); + virStringListFree(xmlLines); + return ret; +} + +static int +testCompareXMLToXMLFiles(const char *inxml, + const char *outxml, + bool internal, + bool redefine) +{ + char *inXmlData = NULL; + char *outXmlData = NULL; + char *actual = NULL; + int ret = -1; + virDomainCheckpointDefPtr def = NULL; + unsigned int flags = VIR_DOMAIN_CHECKPOINT_PARSE_DISKS; + + if (internal) + flags |= VIR_DOMAIN_CHECKPOINT_PARSE_INTERNAL; + + if (redefine) + flags |= VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + + if (virTestLoadFile(inxml, &inXmlData) < 0) + goto cleanup; + + if (virTestLoadFile(outxml, &outXmlData) < 0) + goto cleanup; + + if (!(def = virDomainCheckpointDefParseString(inXmlData, driver.caps, + driver.xmlopt, + flags))) + goto cleanup; + + /* Parsing XML does not populate the domain definition, so add a + * canned bare-bones fallback */ + if (!def->dom) { + // HACK
yep.
+ ret = 77; + goto cleanup; + const char *def_dom = "" + "<domain type='qemu'>" + " <name>fedora</name>" + " <uuid>93a5c045-6457-2c09-e56c-927cdf34e178</uuid>" +/* arch='x86_64' machine='pc'*/ + " <os><type>hvm</type></os>" + "</domain>"; + int dom_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; + if (!(def->dom = virDomainDefParseString(def_dom, driver.caps, + driver.xmlopt, NULL, + dom_flags))) + goto cleanup; + } + + if (!(actual = virDomainCheckpointDefFormat(def, driver.caps, + driver.xmlopt, + VIR_DOMAIN_DEF_FORMAT_SECURE, + internal))) + goto cleanup; + + if (!redefine) { + if (!(actual = testFilterXML(actual))) + goto cleanup; + + if (!(outXmlData = testFilterXML(outXmlData))) + goto cleanup; + } + + if (STRNEQ(outXmlData, actual)) { + virTestDifferenceFull(stderr, outXmlData, outxml, actual, inxml); + goto cleanup; + } + + ret = 0; + + cleanup: + VIR_FREE(inXmlData); + VIR_FREE(outXmlData); + VIR_FREE(actual); + virDomainCheckpointDefFree(def); + return ret; +} + +struct testInfo { + const char *inxml; + const char *outxml; + bool internal; + bool redefine; +}; + + +static int +testCompareXMLToXMLHelper(const void *data) +{ + const struct testInfo *info = data; + + return testCompareXMLToXMLFiles(info->inxml, info->outxml, + info->internal, info->redefine); +} + + +static int +mymain(void) +{ + int ret = 0; + + if (qemuTestDriverInit(&driver) < 0) + return EXIT_FAILURE; + + if (VIR_ALLOC(testCheckpointXMLVariableLineRegex) < 0) + goto cleanup; + + if (regcomp(testCheckpointXMLVariableLineRegex, + testCheckpointXMLVariableLineRegexStr, + REG_EXTENDED | REG_NOSUB) != 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "failed to compile test regex"); + goto cleanup; + } + + +# define DO_TEST(prefix, name, inpath, outpath, internal, redefine) \ + do { \ + const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml", \ + abs_srcdir "/" outpath "/" name ".xml", \ + internal, redefine}; \ + if (virTestRun("CHECKPOINT XML-2-XML " prefix " " name, \ + testCompareXMLToXMLHelper, &info) < 0) \ + ret = -1; \ + } while (0) + +# define DO_TEST_INOUT(name, internal, redefine) \ + DO_TEST("in->out", name,\ + "domaincheckpointxml2xmlin",\ + "domaincheckpointxml2xmlout",\ + internal, redefine) + + /* 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); + + DO_TEST_INOUT("empty", false, false); + DO_TEST_INOUT("sample", false, false); + + cleanup: + if (testCheckpointXMLVariableLineRegex) + regfree(testCheckpointXMLVariableLineRegex); + VIR_FREE(testCheckpointXMLVariableLineRegex); + qemuTestDriverFree(&driver); + + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIR_TEST_MAIN(mymain) + +#else + +int +main(void) +{ + return EXIT_AM_SKIP; +} + +#endif /* WITH_QEMU */

On 2/12/19 11:23 AM, John Ferlan wrote:
On 2/6/19 2:18 PM, Eric Blake wrote:
Work in progress - the checkpoint code is not quite passing tests (part of that is figuring out the minimal XML that is still valid as a <domain> element, or just use --no-domain flag).
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 150 ++++ src/conf/domain_conf.h | 11 +- po/POTFILES | 1 + src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 1030 +++++++++++++++++++++++++++ src/conf/domain_conf.c | 7 +- src/libvirt_private.syms | 21 + tests/Makefile.am | 9 +- tests/domaincheckpointxml2xmltest.c | 231 ++++++ 9 files changed, 1458 insertions(+), 4 deletions(-) create mode 100644 src/conf/checkpoint_conf.h create mode 100644 src/conf/checkpoint_conf.c create mode 100644 tests/domaincheckpointxml2xmltest.c
Starting to lose some steam - seeing wip means I don't want to spend too much time on some algorithms for fear they'll change, but then again I know you're looking for feedback...
I understand. The 'wip' tags should be gone for v5.
diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h new file mode 100644 index 0000000000..994a8bd083 --- /dev/null +++ b/src/conf/checkpoint_conf.h @@ -0,0 +1,150 @@ +/* + * checkpoint_conf.h: domain checkpoint XML processing + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange
This is new, right? Not a copy of...
New file, but so heavily copied from snapshot_conf.h that I felt safer preserving all existing copyrights.
+struct _virDomainCheckpointDef { + /* Public XML. */ + char *name; + char *description; + char *parent; + long long creationTime; /* in seconds */ + + size_t ndisks; /* should not exceed dom->ndisks */ + virDomainCheckpointDiskDef *disks; + + virDomainDefPtr dom; + + /* Internal use. */ + bool current; /* At most one checkpoint in the list should have this set */
Typically these are then in the *Obj...
Copy-and-paste from virDomainSnapshotDef, but my recent work there has made me want to reconsider where the current object is tracked. The problem is that the current way we store things on disk is via a separate <domainsnapshot> XML per snapshot, with no other obvious place for the internal representation of a <domain> to track which snapshot is current. BUT, as I have just posted patches to add a bulk dump/redefine with VIR_DOMAIN_XML_SNAPSHOTS, we COULD just ditch our current separate-file storage of snapshots, and instead store the snapshots directly with the <domain> (for all new saves; when loading libvirtd, we'd still have to keep the code to load from older separate snapshot files for back-compat reasons, even if we no longer track separate files after the one-time conversion). And if I make snapshots cleaner like that, then checkpoints can just start life with the saner approach, rather than being stuck with copying the older one-file-per-checkpoint approach that I implemented using copy-and-paste.
+}; + +struct _virDomainCheckpointObj { + virDomainCheckpointDefPtr def; /* non-NULL except for metaroot */ + + virDomainCheckpointObjPtr parent; /* non-NULL except for metaroot, before + virDomainCheckpointUpdateRelations, or + after virDomainCheckpointDropParent */ + virDomainCheckpointObjPtr sibling; /* NULL if last child of parent */ + size_t nchildren; + virDomainCheckpointObjPtr first_child; /* NULL if no children */
The whole relationship thing is I think overly complex and a bit difficult to follow. Having @sibling and @first_child seems to make the code even more complex. I would think you have a parent and maybe a child. If this is the "first" checkpoint, then there is no parent. I would think that means it's the root.
There's just much more to be see when it comes to how these relationships play out as checkpoints are made and removed. I guess I just have a very linear model in mind, but reading the code seems to go beyond linearality.
The use of hash tables just makes it easier to ensure no def->name is reused. Not having locks in the object means some parent lock is managing to make sure two checkpoint operations cannot occur at the same time from different threads (e.g. a domain object lock), but that is not 100% clear.
Again, heavy copy-and-paste from snapshots, which DO lend themselves to non-linear DAGs of related snapshots. (If you create snapshot A, then run the guest, then create B, then revert to A, then run the guest and create C, then both B and C are children of A). I don't readily know if checkpoints lend themselves as easily to non-linear relations, or if my copy-and-paste is an instance of YAGNI. And it also makes me wonder if I would be better served by factoring out the snapshot non-linear relationships into some more reusable helper functions rather than just open-coding things in both files. The short answer is that there is a hash table (for O(1) name lookup) AND a linked-list relationship listing (for tracking DAG relationships, where one child is common, but more than one child is possible); and that the domain tracks a metaroot (a static instance with name==NULL) so that all other snapshots/checkpoints have nice properties of always having a parent (either another real snapshot as the parent, or the metaroot as the parent when the presentation to the user is that the snapshot is the root of a DAG with no user-created parent). If I improve anything here with better comments, I should also apply similar improvements to the snapshot code.
+};
When objectifying module to to avoid leaking *obj entries into other modules, this struct would go inside .c file w/ accessors to whatever is needed externally. I purposefully avoided domain & domain snapshot as there was just too much rework. Whether or not that can be done here - not sure yet, still reading, but figured I'd point it out at least ;-)
Indeed, since there is such heavy copy-and-paste between the two, first refactoring virDomainSnapshot to be Object-ified with better helper routines, and then making virDomainCheckpoint share a common parent class with virDomainSnapshot to reuse those helper routines, rather than open-coded copy-and-paste is probably worth the effort. (I didn't do it initially in order to get a working demo out the door, but I also admit that adds technical debt, and I should really be trying to reduce it rather than adding to it).
+int virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints); +void virDomainCheckpointDropParent(virDomainCheckpointObjPtr checkpoint);
More recently in the .h prototypes, been trying to follow the .c entry as well... where it's
type function(args...);
makes copy-pasta easier...
Yeah, I'll have to double-check what improvements have been made to snapshot in the meantime, and make sure I'm not regressing back to things the way they were before cleanups (since my copy-and-paste point is mostly from sometime around Sep-Oct 2018).
Add a:
VIR_DEFINE_AUTOPTR_FUNC(virDomainCheckpointDef, virDomainCheckpointDefFree);
details coming...
Yeah, I know it's coming. From what I've seen, I'll like it, too.
@@ -2631,6 +2637,9 @@ struct _virDomainObj {
bool hasManagedSave;
+ virDomainCheckpointObjListPtr checkpoints; + virDomainCheckpointObjPtr current_checkpoint; +
Note to self or you since I could forget by the time I find this usage... wouldn't update to current_checkpoint need a lock?
Same question for snapshots. So far, all modification of snapshots vs. current_snapshot have been under an appropriate API lock, but you do raise the point that I should be careful about it. In fact, I'm half-tempted to first fix virDomainSnapshotObjListPtr to subsume the current_snapshot designation rather than letting virDomainPtr track it independently, in which case this would need the same treatment.
What's really not clear is whether the domain object lock is in place or not when we get here. I think the domainobj's are just plain write locks and not the domainobjlist fancy rwlocks.
All the more reason to put the current object as part of the ObjListPtr, so that locking for adding/removing members from the list is guaranteed to be the same as locking for updating which member is current.
+#include <config.h> + +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <unistd.h> + +#include "internal.h" +#include "virbitmap.h" +#include "virbuffer.h" +#include "count-one-bits.h" +#include "datatypes.h" +#include "domain_conf.h" +#include "virlog.h" +#include "viralloc.h" +#include "netdev_bandwidth_conf.h" +#include "netdev_vport_profile_conf.h" +#include "nwfilter_conf.h" +#include "secret_conf.h" +#include "checkpoint_conf.h" +#include "virstoragefile.h" +#include "viruuid.h" +#include "virfile.h" +#include "virerror.h" +#include "virxml.h" +#include "virstring.h"
Are all of these really needed?
Possibly not. I'll play the trimming game.
+ +#define VIR_FROM_THIS VIR_FROM_DOMAIN_CHECKPOINT + +VIR_LOG_INIT("conf.checkpoint_conf"); + +VIR_ENUM_IMPL(virDomainCheckpoint, VIR_DOMAIN_CHECKPOINT_TYPE_LAST, + "default", "no", "bitmap"); + +struct _virDomainCheckpointObjList { + /* name string -> virDomainCheckpointObj mapping + * for O(1), lockless lookup-by-name */ + virHashTable *objs; + + virDomainCheckpointObj metaroot; /* Special parent of all root checkpoints */'
So not a pointer, but copied object from somewhere?
No, statically allocated right here. It's a trick that let's me convert a user's possible multi-root forest: A - B - C D - E into a single-root DAG: metarooot - A - B - C - D - E and various operations that need to quickly find all the user-visible roots can just traverse the children of the metaroot. Again, heavily copy-pasted from snapshots, and probably worth first refactoring it into a common parent-class object with better comments, so that both snapshots and checkpoints can reuse the same parent, rather than open-coding things twice.
+}; + +/* Checkpoint Def functions */ +static void +virDomainCheckpointDiskDefClear(virDomainCheckpointDiskDefPtr disk) +{ + VIR_FREE(disk->name); + VIR_FREE(disk->bitmap);
Should we clear idx, size, and type too? Depends on consumer usage which could clear and reuse, thus using something old.
I'll have to double-check if memset(,0) as a final step in clear would make any difference.
+ bitmap = virXMLPropString(node, "bitmap"); + if (bitmap) { + if (def->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk checkpoint bitmap '%s' requires " + "type='bitmap'"), + bitmap); + goto cleanup; + } + VIR_STEAL_PTR(def->bitmap, bitmap); + }
Size is not parsed. Restore from restart will lose it.
Intentional. Size is not persistent, but dynamically computed every time you pass the VIR_DOMAIN_CHECKPOINT_XML_SIZE flag (and potentially constantly changing, for a running domain). In other words, it is an output-only flag, and not tied to the checkpoint itself, so much as the checkpoint XML being the easiest way to query that particular piece of per-disk runtime information in bulk.
+static virDomainCheckpointDefPtr +virDomainCheckpointDefParse(xmlXPathContextPtr ctxt, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainCheckpointDefPtr def = NULL; + virDomainCheckpointDefPtr ret = NULL; + xmlNodePtr *nodes = NULL; + size_t i; + int n; + char *creation = NULL;
Never used.
+ struct timeval tv; + int active; + char *tmp;
VIR_AUTOPTR(virDomainCheckpointDef) def = NULL; VIR_AUTOFREE(xmlNodePtr *) nodes = NULL; VIR_AUTOFREE(char *) tmp = NULL;
Erik likes 'em at the bottom too.
With @ret autofree'd, the goto cleanup is unnecessary and replaced by return NULL
Use of the AUTOFREE stuff really cleans up a lot.
Yeah, snapshot should get it first, and then I should make sure my code here gets the same improvements.
+/* 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. */
Things start to get more complex and confusing. What doesn't make sense here is why/how things/order could be different. If this is a checkpoint in time, then wouldn't the order saved (somewhere) be the same? So what causes something to get out of order?
The user's XML for creating the checkpoint can do: <domaincheckpoint> <disks> <disk name='vdb'/> <disk name='vda'/> </disk> </domaincheckpoint> even though the corresponding <domain> at the same time declared things in the other order (vda before vdb); also, like snapshots, you may only want to take checkpoints on a subset of your disks (for example, if you don't care about changes to the OS disk because you can recreate the OS configuration, but DO care about changes to the user's database disk); if the <domaincheckpoint> lists only a subset of all disk names, this function exists to fill in the remaining array elements with sane defaults, so that the rest of the code can blindly process an entire array of disks (and just skip the disks that are not participating in the snapshot), rather than having to repeat checks for which disks are being manipulated.
I guess this is part of the "problem" with posting code that's been in development for a long time. I'm sure this solves some issue, but what that is, I'm not sure.
It's also a drawback of copying heavily from snapshots, which has the same sort of align function.
I'm beginning to think splitting things up to get the basic add objects, list objects, etc. and then adding in the more complex well we need to output a list in this manner or that manner or we have discovered an issue with order as a result of some operation and this fixes it would be more prudent. Means quite a bit of patch reordering to not expose the external API's, but I think will make things easier to review and I would think easier to debug and finish up the work.
So, back to that argument of having a common base class rather than open-coding duplicated efforts :)
+int +virDomainCheckpointAlignDisks(virDomainCheckpointDefPtr def) +{ + int ret = -1; + virBitmapPtr map = NULL; + size_t i; + int ndisks; + int checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_NONE; +
I assume we'd enter here with sort of lock, right? to prevent some other unexpected action from undoing us.
Yes (and same for snapshots), but a comment wouldn't hurt.
+ if (!def->dom) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing domain in checkpoint")); + goto cleanup; + } + + if (def->ndisks > def->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->dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("domain must have at least one disk to perform " + "checkpoints")); + goto cleanup; + }
I'm still trying to come to grips with how def->ndisks != def->dom->ndisks.
Users wanting a checkpoint on only a subset of their disks, or a user messing with XML output and then feeding it back in through the REDEFINE flag.
+ + /* If <disks> omitted, do bitmap on all disks; otherwise, do nothing + * for omitted disks */ + if (!def->ndisks) + checkpoint_default = VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP; + + if (!(map = virBitmapNew(def->dom->ndisks))) + goto cleanup; + + /* Double check requested disks. */ + for (i = 0; i < def->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk = &def->disks[i]; + int idx = virDomainDiskIndexByName(def->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; + } + ignore_value(virBitmapSetBit(map, idx)); + disk->idx = idx; + + if (STRNEQ(disk->name, def->dom->disks[idx]->dst)) { + VIR_FREE(disk->name); + if (VIR_STRDUP(disk->name, def->dom->disks[idx]->dst) < 0) + goto cleanup; + } + }
There's something about this remapping of disk->name that doesn't feel right or is ripe for something going wrong. I would think we'd want to correllate where we are/were in a previous list with now, but I guess I'm not 100% of how this is/will be used.
That's what the bitmap is supposed to track. The first pass sees which disk names the user passed, and figures out which index value those disks are in the corresponding domain definition (or errors out if the same disk is specified more than once or is not present in the domain); the second pass fills in array entries for all other disks that the user omitted.
+ + /* Provide defaults for all remaining disks. */ + ndisks = def->ndisks; + if (VIR_EXPAND_N(def->disks, def->ndisks, + def->dom->ndisks - def->ndisks) < 0) + goto cleanup; + + for (i = 0; i < def->dom->ndisks; i++) { + virDomainCheckpointDiskDefPtr disk; + + if (virBitmapIsBitSet(map, i)) + continue; + disk = &def->disks[ndisks++]; + if (VIR_STRDUP(disk->name, def->dom->disks[i]->dst) < 0) + goto cleanup; + disk->idx = i; + + /* Don't checkpoint empty drives */ + if (virStorageSourceIsEmpty(def->dom->disks[i]->src)) + disk->type = VIR_DOMAIN_CHECKPOINT_TYPE_NONE; + else + disk->type = checkpoint_default; + } + + qsort(&def->disks[0], def->ndisks, sizeof(def->disks[0]), + virDomainCheckpointCompareDiskIndex);
You are right that things might not be correlated until after the qsort, but after this point, iterating through def->disks[] should visit disks in the same order as iterating through def->dom->disks[].
+ + 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_XML_SIZE) + virBufferAsprintf(buf, " size='%llu'", disk->size);
Recall my earlier point in RNG format w/ unsignedLong
Which I hope I addressed.
+ virBufferEscapeString(&buf, "<name>%s</name>\n", def->name); + if (def->description) + virBufferEscapeString(&buf, "<description>%s</description>\n", + def->description); + + if (def->parent) { + virBufferAddLit(&buf, "<parent>\n"); + virBufferAdjustIndent(&buf, 2); + virBufferEscapeString(&buf, "<name>%s</name>\n", def->parent); + virBufferAdjustIndent(&buf, -2); + virBufferAddLit(&buf, "</parent>\n");
Interesting any reason why it's not <parent>%s</parent> - are you planning to add to parent. Guess I should have asked earlier, but it only dawns on me now seeing this.
Copy-paste from <domainsnapshot>. I don't know what else we might add to <parent>, but as snapshot left it open like that, it's easier to do the same here (and then things like virsh can reuse the same XPath queries for more code reuse when converting a listing from array form into tree form).
+ + if (!(flags & VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN) && + virDomainDefFormatInternal(def->dom, caps, domflags, &buf, xmlopt) < 0) + goto error; + + if (internal) + virBufferAsprintf(&buf, "<active>%d</active>\n", def->current);
So this is only valid for ACTIVE guest and not INACTIVE parse/format? Perhaps no need for bool, but use @flags...
Here, I've already improved the situation somewhat for snapshots, and so intend to flow those improvements into checkpoints for v5. But you've already got me wondering earlier in the series if we still want one file per checkpoint, or if I'm better off using VIR_DOMAIN_XML_CHECKPOINTS (where I don't need an internal <active> field after all) for how libvirt stores checkpoints persistently.
+ + virBufferAdjustIndent(&buf, -2); + virBufferAddLit(&buf, "</domaincheckpoint>\n"); + + if (virBufferCheckError(&buf) < 0) + return NULL; + + return virBufferContentAndReset(&buf); + + error: + virBufferFreeAndReset(&buf); + return NULL; +} +
Keeping the *Def logic separate from the *Obj logic is the new norm. The snapshot_conf was left alone by me when I went through the others...
Anything object related should be virdomaincheckpointobj.{c,h}. That means stuff above when separated out could be added sooner in order to make sure review cycles shorter and the pile of code smaller. Then to start the object code should only need a subset of what's here. Adding in the more complex stuff later. There's probably a couple of patches worth of changes and functionality all being rolled into one here. I think it'll be easier overall to review and get accepted if a more logical (and slow) progression to the desired functionality is posted.
Yeah, especially if I refactor the object code to make virDomainSnapshotObj and virDomainCheckpointObj have a common parent class, since the two are very similar (both are tied to a point-in-time <domain> capture, have a notion of one current object amid a set of more objects, and have a DAG relationship tracking parent/child pointers, with a possibility of more than one child depending on how reverts play with checkpoints in the future).
+/* Checkpoint Obj functions */ +static virDomainCheckpointObjPtr virDomainCheckpointObjNew(void)
Two lines
+{ + virDomainCheckpointObjPtr checkpoint; + + if (VIR_ALLOC(checkpoint) < 0) + return NULL; + + VIR_DEBUG("obj=%p", checkpoint); + + return checkpoint; +} + +static void virDomainCheckpointObjFree(virDomainCheckpointObjPtr checkpoint) +{ + if (!checkpoint) + return; + + VIR_DEBUG("obj=%p", checkpoint); + + virDomainCheckpointDefFree(checkpoint->def); + VIR_FREE(checkpoint); +}
Hmm, so not a real virObject... The domain and domain snapshot code was just too inter-related for me to attempt to modify back when I objectified other driver -> object -> def code.
I think if you follow examples in virstorageobj, virsecretobj, virnodedevobj, virinterfaceobj, etc. you'll get a better mechanism than what exists in the domain and domain snapshot code for for how vir*Object can be used with hash tables.
Indeed, improving virDomainSnapshot first may be a wiser use of my time, while still getting this checkpoint code ready for inclusion.
There's no comments here... most important perhaps is on success @def is consume in the object and thus cannot be free'd by the caller.
I blame the lack of comments in snapshot code. (Again, anything I fix here as a result of your comments, I plan to do to snapshots as well).
+ +virDomainCheckpointObjPtr virDomainCheckpointAssignDef(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointDefPtr def)
virDomainCheckpointObjPtr virDomainCheckpointAssignDef(...)
I think "virDomainCheckpointObjListAdd" would be what's happening here.
+{ + virDomainCheckpointObjPtr chk; + + if (virHashLookup(checkpoints->objs, def->name) != NULL) {
s/ != NULL//
+ virReportError(VIR_ERR_OPERATION_INVALID, + _("domain checkpoint %s already exists"), + def->name); + return NULL; + } + + if (!(chk = virDomainCheckpointObjNew())) + return NULL; + chk->def = def; + + if (virHashAddEntry(checkpoints->objs, chk->def->name, chk) < 0) {
Hopefully we can never generate two checkpoints by name in the same clocktick ;-)
Or two snapshots.
+virDomainCheckpointObjListPtr +virDomainCheckpointObjListNew(void) +{ + virDomainCheckpointObjListPtr checkpoints; + if (VIR_ALLOC(checkpoints) < 0) + return NULL; + checkpoints->objs = virHashCreate(50, virDomainCheckpointObjListDataFree); + if (!checkpoints->objs) { + VIR_FREE(checkpoints); + return NULL; + }
How is metaroot handled? At this point it's just an empty struct. Guess my first inclination was that it was going to be the first checkpoint added. That would mean some amount of management when it comes to that checkpoint being free'd on us.
The metaroot will ALWAYS be an empty placeholder object. It allows the rest of the code to have more than one independent user-visible root (depending on how reverting to an older snapshot ends up interplaying with checkpoints).
+static int +virDomainCheckpointObjListGetNames(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + char **const names, int maxnames,
args...
+ unsigned int flags) +{ + struct virDomainCheckpointNameData data = { names, maxnames, flags, 0, + false }; + size_t i; + + if (!from) { + /* LIST_ROOTS and LIST_DESCENDANTS have the same bit value, + * but opposite semantics. Toggle here to get the correct + * traversal on the metaroot. */ + flags ^= VIR_DOMAIN_CHECKPOINT_LIST_ROOTS; + from = &checkpoints->metaroot;
I don't see checkpoints->metaroot being set anywhere yet. I'd expect a Set/Get type API for external callers.
virDomainCheckpointLookupByName(objlist, NULL) returns the metaroot, for callers that directly manipulate its first_child/nchildren fields. But as you point out elsewhere, refactoring that into common helper code for both snapshots and checkpoints to reuse via accessor functions (rather than direct field access) may be even smarter.
+ } + + /* We handle LIST_ROOT/LIST_DESCENDANTS directly, mask that bit + * out to determine when we must use the filter callback. */ + data.flags &= ~VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS; + + /* If this common code is being used, we assume that all checkpoints + * have metadata, and thus can handle METADATA up front as an + * all-or-none filter. XXX This might not always be true, if we ^^^ Need to address the XXX...
+ * add the ability to track qcow2 bitmaps without the + * use of metadata. */
The same XXX exists for snapshots (if we were to ever reconstruct qcow2 snapshots without relying on libvirt metadata). I don't see it as a showstopper for the initial implementation.
+ if ((data.flags & VIR_DOMAIN_CHECKPOINT_FILTERS_METADATA) == + VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA) + return 0; + data.flags &= ~VIR_DOMAIN_CHECKPOINT_FILTERS_METADATA; + + /* For ease of coding the visitor, it is easier to zero each group + * where all of the bits are set. */ + if ((data.flags & VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) == + VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES) + data.flags &= ~VIR_DOMAIN_CHECKPOINT_FILTERS_LEAVES; +
oh, my head, my eyes, my favorite deity. It seems we're building a mostrously complex pile here. I have to wonder whether leaves and filters are really worth all this trouble and whether/how this will be used.
Sadly, the virsh 'snapshot-list --tree' code DOES use it, and therefore so does the new 'checkpoint-list --tree' code. (On the bright side, it also means that if I refactor things into a common base class for both snapshots and checkpoints to reuse, I won't be open-coding the stuff twice).
+ if (flags & VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS) { + if (from->def) + virDomainCheckpointForEachDescendant(from, + virDomainCheckpointObjListCopyNames, + &data); + else if (names || data.flags) + virHashForEach(checkpoints->objs, + virDomainCheckpointObjListCopyNames, + &data); + else + data.count = virHashSize(checkpoints->objs); + } else if (names || data.flags) { + virDomainCheckpointForEachChild(from, + virDomainCheckpointObjListCopyNames, + &data); + } else { + data.count = from->nchildren;
Well this seems to be some sort of magic...
Yes, all faithfully copied from snapshots. But some of it might not be necessary - while snapshots HAS to cater to the older public API that returns a count or list of names (rather than the list of objects directly), the checkpoint code does not have that older API. Then again, hiding the common code into a common parent class rather than re-coding it can't hurt, other than the time it takes for a good refactoring. (But less technical debt now may be worth it, compared to the pain of having to revisit this down the road).
+virDomainCheckpointObjPtr +virDomainCheckpointFindByName(virDomainCheckpointObjListPtr checkpoints, + const char *name)
Usually have this closer to or above the *Add function and not in the midst of these functions that use Hash callbacks...
+{ + return name ? virHashLookup(checkpoints->objs, name) : + &checkpoints->metaroot;
Would a caller really expect this? That is obj->def == NULL?
NULL is the special case for getting at the metaroot (idea copied from snapshots), for the external code that manipulates the metaroot's nchildren/first_child directly. (And as you've pointed out above, hiding that in saner accessor functions in a common base class would be a worthwhile refactoring).
+} + +void virDomainCheckpointObjListRemove(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr checkpoint) +{
I think when I first read things, it felt like metaroot was the first checkpoint ever created... If that does come to pass this is where the well we're about to remove metaroot would occur so we have to replace it with it's direct child/descendant.
No, metaroot is a placeholder which can never be created or deleted. Rather, the user's first checkpoint is a child of metaroot.
+ virHashRemoveEntry(checkpoints->objs, checkpoint->def->name); +} + +int +virDomainCheckpointForEach(virDomainCheckpointObjListPtr checkpoints, + virHashIterator iter, + void *data) +{ + return virHashForEach(checkpoints->objs, iter, data); +} + +/* Run iter(data) on all direct children of checkpoint, while ignoring all + * other entries in checkpoints. Return the number of children + * visited. No particular ordering is guaranteed. */ +int +virDomainCheckpointForEachChild(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data) +{ + virDomainCheckpointObjPtr child = checkpoint->first_child; + + while (child) { + virDomainCheckpointObjPtr next = child->sibling; + (iter)(child, child->def->name, data); + child = next; + } + + return checkpoint->nchildren; +} + +struct checkpoint_act_on_descendant { + int number; + virHashIterator iter; + void *data; +}; + +static int +virDomainCheckpointActOnDescendant(void *payload, + const void *name, + void *data) +{ + virDomainCheckpointObjPtr obj = payload; + struct checkpoint_act_on_descendant *curr = data; + + curr->number += 1 + virDomainCheckpointForEachDescendant(obj, + curr->iter, + curr->data);
Does this double count with the 1 +.
Having a really hard time understanding what's being done ... What's really the difference between a descendant and a child?
For the DAG: A - B - C - D The descendends of A are B, C, D; the children of A are just B, D. The function returns 1 + 3 (for a total of 4 nodes visited), because: ActOnDescendent(A) => 1 + ForEachDescendent(A) => 1 + ForEachChild(A, ActOnDescendent) => 1 + ActOnDescendent(B) + ActOnDescendent(D) => 1 + 1 + ForEachDescendent(B) + 1 + ForEachDescendent(D) => 1 + 1 + ForEachChild(B, ActOnDescendent) + 1 + ForEachChild(D, ActOnDescendent) => 1 + 1 + ActOnDescendent(C) + 1 => 1 + 1 + 1 + ForEachDescendent(C) + 1 => 1 + 1 + 1 + ForEachChild(C, ActOnDescendent) + 1 => 1 + 1 + 1 + 1 the ForEachDescendent Yeah, it's hairy, but it's a straight copy from snapshots, so I should really refactor that first into a common base class.
+ (curr->iter)(payload, name, curr->data); + return 0; +} + +/* Run iter(data) on all descendants of checkpoint, while ignoring all + * other entries in checkpoints. Return the number of descendants + * visited. No particular ordering is guaranteed. */ +int +virDomainCheckpointForEachDescendant(virDomainCheckpointObjPtr checkpoint, + virHashIterator iter, + void *data) +{ + struct checkpoint_act_on_descendant act; + + act.number = 0; + act.iter = iter; + act.data = data; + virDomainCheckpointForEachChild(checkpoint, + virDomainCheckpointActOnDescendant, &act);
So many levels of indirection in multiple directions. We call ForEachChild, which calls an @iter function to act on the descendant which calls ForEachDescendant (IOW, this function) again.
ForEachDescendant and ForEachChild use ActOnDescendant, but have this relationship with each other which is hard to think about without some sort of picture.
Maybe I'm thinking of a much simpler model where
parent <- child1 <- child2 <- child3 [etc.]
One takes a checkpoint and it is the parent. Then at some point in the future another checkpoint is taken. So it's now the child1 with it's parent. When the next checkpoint is taken, it's grandparent is parent and child1 is it's parent.
The simplest implementation is that you are indeed creating a linear link of checkpoints. But I don't know how snapshot reversion will play into the future (for qemu, or more importantly, for other domains that may track checkpoints differently via tracking generation ids on every cluster rather than a bitmap of which clusters were changed since a point in time). And since reverting to a snapshot IS a very likely action, I'd rather leave the door open to having a forest of DAGs for checkpoints just as we permit a forest of DAGs for snapshots (especially if I enhance virDomainSnapshotCreateXML to also capture a checkpoint at that time).
Getting a desendant of some "child#" I thought was a linear operation. If it's more of a tree operation then the model used by the hash table to bucket and link same hashed values into a bucket would then be more useful.
The snapshot operation is definitely tree-based, but the linked list approach of first_child/next_sibling was sufficient rather than having a full-on hash table of children.
+ + return act.number; +} + +/* Struct and callback function used as a hash table callback; each call + * inspects the pre-existing checkpoint->def->parent field, and adjusts + * the checkpoint->parent field as well as the parent's child fields to + * wire up the hierarchical relations for the given checkpoint. The error + * indicator gets set if a parent is missing or a requested parent would + * cause a circular parent chain. */ +struct checkpoint_set_relation { + virDomainCheckpointObjListPtr checkpoints; + int err; +};
Having a hard time picturing the use...
Easy - see my recent patch on VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE_LIST. When we read in multiple checkpoints at once (and not necessarily in topological order), we then do a post-parse pass over the list of checkpoints to reconstruct the parent/child relationship pointers.
+static int +virDomainCheckpointSetRelations(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *data)> +{ + virDomainCheckpointObjPtr obj = payload; + struct checkpoint_set_relation *curr = data; + virDomainCheckpointObjPtr tmp; + + obj->parent = virDomainCheckpointFindByName(curr->checkpoints, + obj->def->parent); + if (!obj->parent) { + curr->err = -1; + obj->parent = &curr->checkpoints->metaroot; + VIR_WARN("checkpoint %s lacks parent", obj->def->name); + } else { + tmp = obj->parent; + while (tmp && tmp->def) { + if (tmp == obj) { + curr->err = -1; + obj->parent = &curr->checkpoints->metaroot; + VIR_WARN("checkpoint %s in circular chain", obj->def->name); + break; + } + tmp = tmp->parent; + } + } + obj->parent->nchildren++; + obj->sibling = obj->parent->first_child; + obj->parent->first_child = obj;
Ah so I see where nchildren comes from... Not sure I'm clear yet on the need for sibling/first_child. This would appear to be inserting something into the middle of a list.
I track: A - B - C - D - E - F - G - H by having B->nchildren == 2, B->first_child = C, C->sibling = D, D->sibling = NULL, B->sibling = E, E->sibling = F, F->first_child = G, G->sibling = H, H->sibling = NULL, F->sibling = NULL Not so much inserting in the middle of the list, but at the front (any time I associate a child with its parent, the child's sibling pointer takes over the parent's original first_child, the parent's nchildren increments, and the parent's first_child becomes the newly inserted child). The lists are generally short (in fact, never more than one element long if you don't branch into having multiple children), so it isn't worth keeping a pointer to the tail of a list.
+ return 0; +} + +/* Populate parent link and child count of all checkpoints, with all + * relations starting as 0/NULL. Return 0 on success, -1 if a parent + * is missing or if a circular relationship was requested. */ +int +virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints) +{ + struct checkpoint_set_relation act = { checkpoints, 0 }; +
Updating the relations for everyone? Having a hard time thinking about the usage model that would need this. If an objet is removed, the update is simple - alter the parent/child. If for some reason we're rebuilding the tree - what is that reason? What causes this to be necessary. I know, you've been at this for a long time.
Rebuilding the tree is needed for when libvirtd is first started (reading multiple files of <domainsnapshot>/<domaincheckpoint>), as well as when doing REDEFINE_LIST (which has a precondition of no existing snapshots because it is about to read in multiple and then rebuild the relationships all at once).
+ virHashForEach(checkpoints->objs, virDomainCheckpointSetRelations, &act); + return act.err; +} + +/* Prepare to reparent or delete checkpoint, by removing it from its + * current listed parent. Note that when bulk removing all children + * of a parent, it is faster to just 0 the count rather than calling + * this function on each child. */ +void +virDomainCheckpointDropParent(virDomainCheckpointObjPtr checkpoint) +{ + virDomainCheckpointObjPtr prev = NULL; + virDomainCheckpointObjPtr curr = NULL; + + checkpoint->parent->nchildren--;
Decrementing the child count before actually dropping the child?
Copied from snapshots; if there's a latent bug there, it should be a separate fix. Meanwhile, refactoring to a common base class should get rid of the worst of understanding this code (it's easier to check if code motion is correct than it is to see if a duplicated open-code repeat is correct).
+int +virDomainListAllCheckpoints(virDomainCheckpointObjListPtr checkpoints, + virDomainCheckpointObjPtr from, + virDomainPtr dom, + virDomainCheckpointPtr **chks, + unsigned int flags) +{
There's better examples than what *snapshots do in order to build/export a list of virDomainCheckpointPtr's - usually in *Export type functions.
Except for the fact that the public APIs have nice filters for listing roots only vs. roots and all their descendents, for both the domain and for a subtree rooted in a particular checkpoint, and that virsh already takes advantage of those filters in implementing its --tree output.
+ int count = virDomainCheckpointObjListNum(checkpoints, from, flags);
Go direct virHashSize
Works if we aren't filtering to a particular subtree, but if any filtering is involved, then you don't want the return value to include the objects that were filtered out.
+ virDomainCheckpointPtr *list = NULL; + char **names; + int ret = -1; + size_t i; + + if (!chks || count < 0)
<= 0?
No, because when the answer is 0,...
+ return count; + if (VIR_ALLOC_N(names, count) < 0 || + VIR_ALLOC_N(list, count + 1) < 0) + goto cleanup;
...we still return a valid NULL-terminated list.
+ + if (virDomainCheckpointObjListGetNames(checkpoints, from, names, count, + flags) < 0)
indent is off
+ goto cleanup; + for (i = 0; i < count; i++) + if ((list[i] = virGetDomainCheckpoint(dom, names[i])) == NULL)
Other examples will show that this is done in the callback function. I know you've found it easier to use the same callback function for several different kinds of operations. I tried that too when I did the common object changes and got told/reminded during review that one function should serve 3 or 4 purposes as it makes each callback function more unnecessarily complicated especially w/r/t knowing what each caller may expect. So take that advice as something to follow for the round.
+ 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; +} + +
This is like the vir*ObjList*Export* function from other code.
Only a very sparse look/scan of anything below this point.
John
Thanks for the feedback so far. (And yes, I was totally expecting your point that refactoring the snapshot code to be cleaner, rather than just blindly copying its existing code into checkpoints, is a better reduction of overall technical debt, even if it will cost more time to get the patches polished).
+ /* Parsing XML does not populate the domain definition, so add a + * canned bare-bones fallback */ + if (!def->dom) { + // HACK
yep.
+ ret = 77; + goto cleanup;
Yeah, and now you see why I have 'wip' because testing is not finalized :) -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

On 2/25/19 4:06 PM, Eric Blake wrote:
On 2/12/19 11:23 AM, John Ferlan wrote:
On 2/6/19 2:18 PM, Eric Blake wrote:
Work in progress - the checkpoint code is not quite passing tests (part of that is figuring out the minimal XML that is still valid as a <domain> element, or just use --no-domain flag).
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 150 ++++ src/conf/domain_conf.h | 11 +- po/POTFILES | 1 + src/conf/Makefile.inc.am | 2 + src/conf/checkpoint_conf.c | 1030 +++++++++++++++++++++++++++ src/conf/domain_conf.c | 7 +- src/libvirt_private.syms | 21 + tests/Makefile.am | 9 +- tests/domaincheckpointxml2xmltest.c | 231 ++++++ 9 files changed, 1458 insertions(+), 4 deletions(-) create mode 100644 src/conf/checkpoint_conf.h create mode 100644 src/conf/checkpoint_conf.c create mode 100644 tests/domaincheckpointxml2xmltest.c
Starting to lose some steam - seeing wip means I don't want to spend too much time on some algorithms for fear they'll change, but then again I know you're looking for feedback...
I understand. The 'wip' tags should be gone for v5.
diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h new file mode 100644 index 0000000000..994a8bd083 --- /dev/null +++ b/src/conf/checkpoint_conf.h @@ -0,0 +1,150 @@ +/* + * checkpoint_conf.h: domain checkpoint XML processing + * + * Copyright (C) 2006-2019 Red Hat, Inc. + * Copyright (C) 2006-2008 Daniel P. Berrange
This is new, right? Not a copy of...
New file, but so heavily copied from snapshot_conf.h that I felt safer preserving all existing copyrights.
See src/conf/{vir*obj}.c for some examples, e.g.: * virinterfaceobj.c: interface object handling * (derived from interface_conf.c) that's what I ended up doing when splitting apart XML handling from Object handling code.
+struct _virDomainCheckpointDef { + /* Public XML. */ + char *name; + char *description; + char *parent; + long long creationTime; /* in seconds */ + + size_t ndisks; /* should not exceed dom->ndisks */ + virDomainCheckpointDiskDef *disks; + + virDomainDefPtr dom; + + /* Internal use. */ + bool current; /* At most one checkpoint in the list should have this set */
Typically these are then in the *Obj...
Copy-and-paste from virDomainSnapshotDef, but my recent work there has made me want to reconsider where the current object is tracked.
Yeah, understandable. Like I noted earlier - when I did the work to split out object code and make it more common, I skipped snapshots for various reasons, but mostly because I feared that code and wasn't sure if changing it would cause issues with any blockdev work Peter was doing. So I want the safer route. I do think snapshot_conf.c could be split up with virsnapshotobjlist.c being created, but that's future work. Maybe something you can consider for checkpoints though.
The problem is that the current way we store things on disk is via a separate <domainsnapshot> XML per snapshot, with no other obvious place for the internal representation of a <domain> to track which snapshot is current. BUT, as I have just posted patches to add a bulk dump/redefine with VIR_DOMAIN_XML_SNAPSHOTS, we COULD just ditch our current separate-file storage of snapshots, and instead store the snapshots directly with the <domain> (for all new saves; when loading libvirtd, we'd still have to keep the code to load from older separate snapshot files for back-compat reasons, even if we no longer track separate files after the one-time conversion). And if I make snapshots cleaner like that, then checkpoints can just start life with the saner approach, rather than being stuck with copying the older one-file-per-checkpoint approach that I implemented using copy-and-paste.
Hey, I recognize that explanation!
+}; + +struct _virDomainCheckpointObj { + virDomainCheckpointDefPtr def; /* non-NULL except for metaroot */ + + virDomainCheckpointObjPtr parent; /* non-NULL except for metaroot, before + virDomainCheckpointUpdateRelations, or + after virDomainCheckpointDropParent */ + virDomainCheckpointObjPtr sibling; /* NULL if last child of parent */ + size_t nchildren; + virDomainCheckpointObjPtr first_child; /* NULL if no children */
The whole relationship thing is I think overly complex and a bit difficult to follow. Having @sibling and @first_child seems to make the code even more complex. I would think you have a parent and maybe a child. If this is the "first" checkpoint, then there is no parent. I would think that means it's the root.
There's just much more to be see when it comes to how these relationships play out as checkpoints are made and removed. I guess I just have a very linear model in mind, but reading the code seems to go beyond linearality.
The use of hash tables just makes it easier to ensure no def->name is reused. Not having locks in the object means some parent lock is managing to make sure two checkpoint operations cannot occur at the same time from different threads (e.g. a domain object lock), but that is not 100% clear.
Again, heavy copy-and-paste from snapshots, which DO lend themselves to non-linear DAGs of related snapshots. (If you create snapshot A, then run the guest, then create B, then revert to A, then run the guest and create C, then both B and C are children of A). I don't readily know if checkpoints lend themselves as easily to non-linear relations, or if my copy-and-paste is an instance of YAGNI. And it also makes me wonder if I would be better served by factoring out the snapshot non-linear relationships into some more reusable helper functions rather than just open-coding things in both files.
The KISS principle comes to mind ;-).
The short answer is that there is a hash table (for O(1) name lookup) AND a linked-list relationship listing (for tracking DAG relationships, where one child is common, but more than one child is possible); and that the domain tracks a metaroot (a static instance with name==NULL) so that all other snapshots/checkpoints have nice properties of always having a parent (either another real snapshot as the parent, or the metaroot as the parent when the presentation to the user is that the snapshot is the root of a DAG with no user-created parent). If I improve anything here with better comments, I should also apply similar improvements to the snapshot code.
If the complexity is in revert, then is that required for what backup needs? If checkpoints are a "pseudo-child" of a snapshot or a backup, then snapshots can handle that awful complexity and backups can do something slightly different. Of course that goes back to a previous point about where the <domaincheckpoint...> should live <sigh>. My KISS sees checkpoints as points in time saving what's there allowing other parts of the system to build upon being able to handle differences in those points in time. Trying to merge/squeeze in snapshot logic or functionality into checkpoints is/was overwhelming... There's so much about snapshot processing that I avoided because unless you work with it often enough, it really don't necessarily make sense. A lot to do with terminology and thinking about the corresponding QEMU functionality you're trying to work with. Still checkpoints (and snapshots for that matter) could be theoretically implemented by a different hypervisor too, but all I have only partially understood is the QEMU logistics.
+};
When objectifying module to to avoid leaking *obj entries into other modules, this struct would go inside .c file w/ accessors to whatever is needed externally. I purposefully avoided domain & domain snapshot as there was just too much rework. Whether or not that can be done here - not sure yet, still reading, but figured I'd point it out at least ;-)
Indeed, since there is such heavy copy-and-paste between the two, first refactoring virDomainSnapshot to be Object-ified with better helper routines, and then making virDomainCheckpoint share a common parent class with virDomainSnapshot to reuse those helper routines, rather than open-coded copy-and-paste is probably worth the effort. (I didn't do it initially in order to get a working demo out the door, but I also admit that adds technical debt, and I should really be trying to reduce it rather than adding to it).
True - there's a lot going into many decisions made and to be made that is hard to quantify in the review. I know you've been working at this for a while and I wonder if there's a point where it is it better to get something in that perhaps someone else can refactor while you complete the rest of the work. Or know that you can come back to it. Since there is overlap from copypaste I'm sure someone can or will come up ways to converge logic. Still yet as you point out later in your response - it could be worth generating the common module/code for both checkpoints and snapshots to use. I think it would be too hard to unwind everything this late in the game!
+int virDomainCheckpointUpdateRelations(virDomainCheckpointObjListPtr checkpoints); +void virDomainCheckpointDropParent(virDomainCheckpointObjPtr checkpoint);
More recently in the .h prototypes, been trying to follow the .c entry as well... where it's
type function(args...);
makes copy-pasta easier...
Yeah, I'll have to double-check what improvements have been made to snapshot in the meantime, and make sure I'm not regressing back to things the way they were before cleanups (since my copy-and-paste point is mostly from sometime around Sep-Oct 2018).
Luckily a module that isn't in the often changed category... It's in the don't be the last to change it because you don't want to be the one to break it and thus own it ;-).
Add a:
VIR_DEFINE_AUTOPTR_FUNC(virDomainCheckpointDef, virDomainCheckpointDefFree);
details coming...
Yeah, I know it's coming. From what I've seen, I'll like it, too.
@@ -2631,6 +2637,9 @@ struct _virDomainObj {
bool hasManagedSave;
+ virDomainCheckpointObjListPtr checkpoints; + virDomainCheckpointObjPtr current_checkpoint; +
Note to self or you since I could forget by the time I find this usage... wouldn't update to current_checkpoint need a lock?
Same question for snapshots. So far, all modification of snapshots vs. current_snapshot have been under an appropriate API lock, but you do raise the point that I should be careful about it. In fact, I'm half-tempted to first fix virDomainSnapshotObjListPtr to subsume the current_snapshot designation rather than letting virDomainPtr track it independently, in which case this would need the same treatment.
I think I knew the answer was related to Snapshots and that snapshots may lack locking... Perhaps something else that caused me to avoid it when working through the common object changes. I do remember making some changes at some point in time in private branches, but the details escape me.
What's really not clear is whether the domain object lock is in place or not when we get here. I think the domainobj's are just plain write locks and not the domainobjlist fancy rwlocks.
All the more reason to put the current object as part of the ObjListPtr, so that locking for adding/removing members from the list is guaranteed to be the same as locking for updating which member is current.
If we come through the domain connect API's then we'd have domain lock - which perhaps would be the right thing here... Especially if there's this "relationship" between snapshots and checkpoints that we'd need to be really, really, careful to manage.
+#include <config.h> + +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <unistd.h> + +#include "internal.h" +#include "virbitmap.h" +#include "virbuffer.h" +#include "count-one-bits.h" +#include "datatypes.h" +#include "domain_conf.h" +#include "virlog.h" +#include "viralloc.h" +#include "netdev_bandwidth_conf.h" +#include "netdev_vport_profile_conf.h" +#include "nwfilter_conf.h" +#include "secret_conf.h" +#include "checkpoint_conf.h" +#include "virstoragefile.h" +#include "viruuid.h" +#include "virfile.h" +#include "virerror.h" +#include "virxml.h" +#include "virstring.h"
Are all of these really needed?
Possibly not. I'll play the trimming game.
+ +#define VIR_FROM_THIS VIR_FROM_DOMAIN_CHECKPOINT + +VIR_LOG_INIT("conf.checkpoint_conf"); + +VIR_ENUM_IMPL(virDomainCheckpoint, VIR_DOMAIN_CHECKPOINT_TYPE_LAST, + "default", "no", "bitmap"); + +struct _virDomainCheckpointObjList { + /* name string -> virDomainCheckpointObj mapping + * for O(1), lockless lookup-by-name */ + virHashTable *objs; + + virDomainCheckpointObj metaroot; /* Special parent of all root checkpoints */'
So not a pointer, but copied object from somewhere?
No, statically allocated right here.
It's a trick that let's me convert a user's possible multi-root forest:
A - B - C D - E
into a single-root DAG:
metarooot - A - B - C - D - E
and various operations that need to quickly find all the user-visible roots can just traverse the children of the metaroot.
Again, heavily copy-pasted from snapshots, and probably worth first refactoring it into a common parent-class object with better comments, so that both snapshots and checkpoints can reuse the same parent, rather than open-coding things twice.
Again back to the question of how much Checkpoints are going to be independent or children of snapshot/backup. Is it really desired to take all the snapshot logic?
+}; + +/* Checkpoint Def functions */ +static void +virDomainCheckpointDiskDefClear(virDomainCheckpointDiskDefPtr disk) +{ + VIR_FREE(disk->name); + VIR_FREE(disk->bitmap);
Should we clear idx, size, and type too? Depends on consumer usage which could clear and reuse, thus using something old.
I'll have to double-check if memset(,0) as a final step in clear would make any difference.
+ bitmap = virXMLPropString(node, "bitmap"); + if (bitmap) { + if (def->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk checkpoint bitmap '%s' requires " + "type='bitmap'"), + bitmap); + goto cleanup; + } + VIR_STEAL_PTR(def->bitmap, bitmap); + }
Size is not parsed. Restore from restart will lose it.
Intentional. Size is not persistent, but dynamically computed every time you pass the VIR_DOMAIN_CHECKPOINT_XML_SIZE flag (and potentially constantly changing, for a running domain). In other words, it is an output-only flag, and not tied to the checkpoint itself, so much as the checkpoint XML being the easiest way to query that particular piece of per-disk runtime information in bulk.
It's a calculated value - I was just covering all the bases.
+static virDomainCheckpointDefPtr +virDomainCheckpointDefParse(xmlXPathContextPtr ctxt, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainCheckpointDefPtr def = NULL; + virDomainCheckpointDefPtr ret = NULL; + xmlNodePtr *nodes = NULL; + size_t i; + int n; + char *creation = NULL;
Never used.
+ struct timeval tv; + int active; + char *tmp;
VIR_AUTOPTR(virDomainCheckpointDef) def = NULL; VIR_AUTOFREE(xmlNodePtr *) nodes = NULL; VIR_AUTOFREE(char *) tmp = NULL;
Erik likes 'em at the bottom too.
With @ret autofree'd, the goto cleanup is unnecessary and replaced by return NULL
Use of the AUTOFREE stuff really cleans up a lot.
Yeah, snapshot should get it first, and then I should make sure my code here gets the same improvements.
There's been a run on VIR_AUTO* patches lately. The original thought was that someone could use them to be a first time contributor. But no one has done anything since GSOC finished. As I started working through them and got varying opinions and feedback, I think it's less a first time contributor type activity than originally thought. There's quite a bit involved with not only making the physical change but dealing with refactorings and logic changes since cleanup/error labels become unnecessary. OK, so different soapbox topic, sorry ;-). John [...] I think the remainder of this was largely the copypaste from snapshots discussion. I'm not sure I have a "best option" answer, but I do like the concept of common code. There is a lot more information which I probably have to read a few more times to understand completely!

Accept XML describing a generic block job, and output it again as needed. At the moment, it has some qemu-specific hacks, such as storing internal XML for a node name, that might be cleaner once full-tree node-name support goes in. Still not done: decent tests Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 65 +++++ src/conf/domain_conf.h | 3 + src/conf/checkpoint_conf.c | 503 +++++++++++++++++++++++++++++++++++++ src/libvirt_private.syms | 8 +- 4 files changed, 578 insertions(+), 1 deletion(-) diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h index 994a8bd083..abaeaaad52 100644 --- a/src/conf/checkpoint_conf.h +++ b/src/conf/checkpoint_conf.h @@ -147,4 +147,69 @@ int virDomainCheckpointRedefinePrep(virDomainPtr domain, VIR_ENUM_DECL(virDomainCheckpoint); +/* Items related to incremental backup state */ + +typedef enum { + VIR_DOMAIN_BACKUP_TYPE_DEFAULT = 0, + VIR_DOMAIN_BACKUP_TYPE_PUSH, + VIR_DOMAIN_BACKUP_TYPE_PULL, + + VIR_DOMAIN_BACKUP_TYPE_LAST +} virDomainBackupType; + +typedef enum { + VIR_DOMAIN_BACKUP_DISK_STATE_DEFAULT = 0, /* Initial */ + VIR_DOMAIN_BACKUP_DISK_STATE_CREATED, /* File created */ + VIR_DOMAIN_BACKUP_DISK_STATE_LABEL, /* Security labels applied */ + VIR_DOMAIN_BACKUP_DISK_STATE_READY, /* Handed to guest */ + VIR_DOMAIN_BACKUP_DISK_STATE_BITMAP, /* Associated temp bitmap created */ + VIR_DOMAIN_BACKUP_DISK_STATE_EXPORT, /* NBD export created */ +} virDomainBackupDiskState; + +/* Stores disk-backup information */ +typedef struct _virDomainBackupDiskDef virDomainBackupDiskDef; +typedef virDomainBackupDiskDef *virDomainBackupDiskDefPtr; +struct _virDomainBackupDiskDef { + char *name; /* name matching the <target dev='...' of the domain */ + int idx; /* index within checkpoint->dom->disks that matches name */ + + /* details of target for push-mode, or of the scratch file for pull-mode */ + virStorageSourcePtr store; + int state; /* virDomainBackupDiskState, not stored in XML */ +}; + +/* Stores the complete backup metadata */ +typedef struct _virDomainBackupDef virDomainBackupDef; +typedef virDomainBackupDef *virDomainBackupDefPtr; +struct _virDomainBackupDef { + /* Public XML. */ + int type; /* virDomainBackupType */ + int id; + char *incremental; + virStorageNetHostDefPtr server; /* only when type == PULL */ + + size_t ndisks; /* should not exceed dom->ndisks */ + virDomainBackupDiskDef *disks; +}; + +VIR_ENUM_DECL(virDomainBackup); + +typedef enum { + VIR_DOMAIN_BACKUP_PARSE_INTERNAL = 1 << 0, +} virDomainBackupParseFlags; + +virDomainBackupDefPtr virDomainBackupDefParseString(const char *xmlStr, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +virDomainBackupDefPtr virDomainBackupDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +void virDomainBackupDefFree(virDomainBackupDefPtr def); +int virDomainBackupDefFormat(virBufferPtr buf, + virDomainBackupDefPtr def, + bool internal); +int virDomainBackupAlignDisks(virDomainBackupDefPtr backup, + virDomainDefPtr dom, const char *suffix); + #endif /* LIBVIRT_CHECKPOINT_CONF_H */ diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index d31c45427e..fe818a2b27 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -125,6 +125,9 @@ typedef virDomainCheckpointObj *virDomainCheckpointObjPtr; typedef struct _virDomainCheckpointObjList virDomainCheckpointObjList; typedef virDomainCheckpointObjList *virDomainCheckpointObjListPtr; +typedef struct _virDomainBackupDef virDomainBackupDef; +typedef virDomainBackupDef *virDomainBackupDefPtr; + typedef struct _virDomainSnapshotObj virDomainSnapshotObj; typedef virDomainSnapshotObj *virDomainSnapshotObjPtr; diff --git a/src/conf/checkpoint_conf.c b/src/conf/checkpoint_conf.c index c0840a96b2..4b148827c1 100644 --- a/src/conf/checkpoint_conf.c +++ b/src/conf/checkpoint_conf.c @@ -1028,3 +1028,506 @@ virDomainCheckpointRedefinePrep(virDomainPtr domain, cleanup: return ret; } + +/* Backup Def functions */ + +VIR_ENUM_IMPL(virDomainBackup, VIR_DOMAIN_BACKUP_TYPE_LAST, + "default", "push", "pull"); + +static void +virDomainBackupDiskDefClear(virDomainBackupDiskDefPtr disk) +{ + VIR_FREE(disk->name); + virStorageSourceClear(disk->store); + disk->store = NULL; +} + +void +virDomainBackupDefFree(virDomainBackupDefPtr def) +{ + size_t i; + + if (!def) + return; + + VIR_FREE(def->incremental); + VIR_FREE(def->server); // FIXME which struct + for (i = 0; i < def->ndisks; i++) + virDomainBackupDiskDefClear(&def->disks[i]); + VIR_FREE(def->disks); + VIR_FREE(def); +} + +static int +virDomainBackupDiskDefParseXML(xmlNodePtr node, + xmlXPathContextPtr ctxt, + virDomainBackupDiskDefPtr def, + bool push, bool internal, + virDomainXMLOptionPtr xmlopt) +{ + int ret = -1; + // char *backup = NULL; /* backup="yes|no"? */ + char *type = NULL; + char *driver = NULL; + xmlNodePtr cur; + xmlNodePtr saved = ctxt->node; + + ctxt->node = node; + + if (VIR_ALLOC(def->store) < 0) + goto cleanup; + + def->name = virXMLPropString(node, "name"); + if (!def->name) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing name from disk backup element")); + goto cleanup; + } + + /* Needed? A way for users to list a disk and explicitly mark it + * as not participating, and then output shows all disks rather + * than just active disks */ +#if 0 + backup = virXMLPropString(node, "backup"); + if (backup) { + def->type = virDomainCheckpointTypeFromString(checkpoint); + if (def->type <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk checkpoint setting '%s'"), + checkpoint); + goto cleanup; + } + } +#endif + + if ((type = virXMLPropString(node, "type"))) { + if ((def->store->type = virStorageTypeFromString(type)) <= 0 || + def->store->type == VIR_STORAGE_TYPE_VOLUME || + def->store->type == VIR_STORAGE_TYPE_DIR) { + virReportError(VIR_ERR_XML_ERROR, + _("unknown disk backup type '%s'"), type); + goto cleanup; + } + } else { + def->store->type = VIR_STORAGE_TYPE_FILE; + } + + if ((cur = virXPathNode(push ? "./target" : "./scratch", ctxt)) && + virDomainDiskSourceParse(cur, ctxt, def->store, 0, xmlopt) < 0) + goto cleanup; + + if (internal) { + int detected; + if (virXPathInt("string(./node/@detected)", ctxt, &detected) < 0) + goto cleanup; + def->store->detected = detected; + def->store->nodeformat = virXPathString("string(./node)", ctxt); + } + + if ((driver = virXPathString("string(./driver/@type)", ctxt))) { + def->store->format = virStorageFileFormatTypeFromString(driver); + if (def->store->format <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk backup driver '%s'"), driver); + goto cleanup; + } else if (!push && def->store->format != VIR_STORAGE_FILE_QCOW2) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("pull mode requires qcow2 driver, not '%s'"), + driver); + goto cleanup; + } + } + + /* validate that the passed path is absolute */ + if (virStorageSourceIsRelative(def->store)) { + virReportError(VIR_ERR_XML_ERROR, + _("disk backup image path '%s' must be absolute"), + def->store->path); + goto cleanup; + } + + ret = 0; + cleanup: + ctxt->node = saved; + + VIR_FREE(driver); +// VIR_FREE(backup); + VIR_FREE(type); + if (ret < 0) + virDomainBackupDiskDefClear(def); + return ret; +} + +static virDomainBackupDefPtr +virDomainBackupDefParse(xmlXPathContextPtr ctxt, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainBackupDefPtr def = NULL; + virDomainBackupDefPtr ret = NULL; + xmlNodePtr *nodes = NULL; + xmlNodePtr node = NULL; + char *mode = NULL; + bool push; + size_t i; + int n; + + if (VIR_ALLOC(def) < 0) + goto cleanup; + + mode = virXMLPropString(ctxt->node, "mode"); + if (mode) { + def->type = virDomainBackupTypeFromString(mode); + if (def->type <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown backup mode '%s'"), mode); + goto cleanup; + } + } else { + def->type = VIR_DOMAIN_BACKUP_TYPE_PUSH; + } + push = def->type == VIR_DOMAIN_BACKUP_TYPE_PUSH; + + if (flags & VIR_DOMAIN_BACKUP_PARSE_INTERNAL) { + char *tmp = virXMLPropString(ctxt->node, "id"); + if (tmp && virStrToLong_i(tmp, NULL, 10, &def->id) < 0) { + virReportError(VIR_ERR_XML_ERROR, + _("invalid 'id' value '%s'"), tmp); + VIR_FREE(tmp); + goto cleanup; + } + VIR_FREE(tmp); + } + + def->incremental = virXPathString("string(./incremental)", ctxt); + + node = virXPathNode("./server", ctxt); + if (node) { + if (def->type != VIR_DOMAIN_BACKUP_TYPE_PULL) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("use of <server> requires pull mode backup")); + goto cleanup; + } + if (VIR_ALLOC(def->server) < 0) + goto cleanup; + if (virDomainStorageNetworkParseHost(node, def->server) < 0) + goto cleanup; + if (def->server->transport == VIR_STORAGE_NET_HOST_TRANS_RDMA) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("transport rdma is not supported for <server>")); + goto cleanup; + } + } + + if ((n = virXPathNodeSet("./disks/*", ctxt, &nodes)) < 0) + goto cleanup; + if (n && VIR_ALLOC_N(def->disks, n) < 0) + goto cleanup; + def->ndisks = n; + for (i = 0; i < def->ndisks; i++) { + if (virDomainBackupDiskDefParseXML(nodes[i], ctxt, + &def->disks[i], push, + flags & VIR_DOMAIN_BACKUP_PARSE_INTERNAL, + xmlopt) < 0) + goto cleanup; + } + VIR_FREE(nodes); + + VIR_STEAL_PTR(ret, def); + + cleanup: + VIR_FREE(mode); + VIR_FREE(nodes); + virDomainBackupDefFree(def); + + return ret; +} + +virDomainBackupDefPtr +virDomainBackupDefParseString(const char *xmlStr, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainBackupDefPtr ret = NULL; + xmlDocPtr xml; + int keepBlanksDefault = xmlKeepBlanksDefault(0); + + if ((xml = virXMLParse(NULL, xmlStr, _("(domain_backup)")))) { + xmlKeepBlanksDefault(keepBlanksDefault); + ret = virDomainBackupDefParseNode(xml, xmlDocGetRootElement(xml), + xmlopt, flags); + xmlFreeDoc(xml); + } + xmlKeepBlanksDefault(keepBlanksDefault); + + return ret; +} + +virDomainBackupDefPtr +virDomainBackupDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + xmlXPathContextPtr ctxt = NULL; + virDomainBackupDefPtr def = NULL; + + if (!virXMLNodeNameEqual(root, "domainbackup")) { + virReportError(VIR_ERR_XML_ERROR, "%s", _("domainbackup")); + goto cleanup; + } + + ctxt = xmlXPathNewContext(xml); + if (ctxt == NULL) { + virReportOOMError(); + goto cleanup; + } + + ctxt->node = root; + def = virDomainBackupDefParse(ctxt, xmlopt, flags); + cleanup: + xmlXPathFreeContext(ctxt); + return def; +} + +static int +virDomainBackupDiskDefFormat(virBufferPtr buf, + virDomainBackupDiskDefPtr disk, + bool push, bool internal) +{ + int type = disk->store->type; + virBuffer attrBuf = VIR_BUFFER_INITIALIZER; + virBuffer childBuf = VIR_BUFFER_INITIALIZER; + int ret = -1; + + if (!disk->name) + return 0; + + virBufferEscapeString(buf, "<disk name='%s'", disk->name); + /* TODO: per-disk backup=off? */ + + virBufferAsprintf(buf, " type='%s'>\n", virStorageTypeToString(type)); + virBufferAdjustIndent(buf, 2); + + if (disk->store->format > 0) + virBufferEscapeString(buf, "<driver type='%s'/>\n", + virStorageFileFormatTypeToString(disk->store->format)); + /* TODO: should node names be part of storage file xml, rather + * than a one-off hack for qemu? */ + if (internal) { + virBufferEscapeString(buf, "<node detected='%s'", + disk->store->detected ? "1" : "0"); + virBufferEscapeString(buf, ">%s</node>\n", disk->store->nodeformat); + } + + virBufferSetChildIndent(&childBuf, buf); + if (virDomainStorageSourceFormat(&attrBuf, &childBuf, disk->store, 0, + false) < 0) + goto cleanup; + if (virXMLFormatElement(buf, push ? "target" : "scratch", + &attrBuf, &childBuf) < 0) + goto cleanup; + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</disk>\n"); + + ret = 0; + + cleanup: + virBufferFreeAndReset(&attrBuf); + virBufferFreeAndReset(&childBuf); + return ret; +} + +int +virDomainBackupDefFormat(virBufferPtr buf, virDomainBackupDefPtr def, + bool internal) +{ + size_t i; + + virBufferAsprintf(buf, "<domainbackup mode='%s'", + virDomainBackupTypeToString(def->type)); + if (def->id) + virBufferAsprintf(buf, " id='%d'", def->id); + virBufferAddLit(buf, ">\n"); + virBufferAdjustIndent(buf, 2); + + virBufferEscapeString(buf, "<incremental>%s</incremental>\n", + def->incremental); + if (def->server) { + virBufferAsprintf(buf, "<server transport='%s'", + virStorageNetHostTransportTypeToString(def->server->transport)); + virBufferEscapeString(buf, " name='%s'", def->server->name); + if (def->server->port) + virBufferAsprintf(buf, " port='%u'", def->server->port); + virBufferEscapeString(buf, " socket='%s'", def->server->socket); + virBufferAddLit(buf, "/>\n"); + } + + if (def->ndisks) { + virBufferAddLit(buf, "<disks>\n"); + virBufferAdjustIndent(buf, 2); + for (i = 0; i < def->ndisks; i++) { + if (!def->disks[i].store) + continue; + if (virDomainBackupDiskDefFormat(buf, &def->disks[i], + def->type == VIR_DOMAIN_BACKUP_TYPE_PUSH, + internal) < 0) + return -1; + } + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</disks>\n"); + } + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</domainbackup>\n"); + + return virBufferCheckError(buf); +} + + +static int +virDomainBackupCompareDiskIndex(const void *a, const void *b) +{ + const virDomainBackupDiskDef *diska = a; + const virDomainBackupDiskDef *diskb = b; + + /* Integer overflow shouldn't be a problem here. */ + return diska->idx - diskb->idx; +} + +static int +virDomainBackupDefAssignStore(virDomainBackupDiskDefPtr disk, + virStorageSourcePtr src, + const char *suffix) +{ + int ret = -1; + + if (virStorageSourceIsEmpty(src)) { + if (disk->store) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk '%s' has no media"), disk->name); + goto cleanup; + } + } else if (src->readonly && disk->store) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("backup of readonly disk '%s' makes no sense"), + disk->name); + goto cleanup; + } else if (!disk->store) { + if (virStorageSourceGetActualType(src) == VIR_STORAGE_TYPE_FILE) { + if (VIR_ALLOC(disk->store) < 0) + goto cleanup; + disk->store->type = VIR_STORAGE_TYPE_FILE; + if (virAsprintf(&disk->store->path, "%s.%s", src->path, + suffix) < 0) + goto cleanup; + disk->store->detected = true; + } else { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("refusing to generate file name for disk '%s'"), + disk->name); + goto cleanup; + } + } + ret = 0; + cleanup: + return ret; +} + +/* Align def->disks to domain. Sort the list of def->disks, + * generating storage names using suffix as needed. 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 +virDomainBackupAlignDisks(virDomainBackupDefPtr def, virDomainDefPtr dom, + const char *suffix) +{ + int ret = -1; + virBitmapPtr map = NULL; + size_t i; + int ndisks; + bool alloc_all = false; + + if (def->ndisks > dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("too many disk backup requests for domain")); + goto cleanup; + } + + /* Unlikely to have a guest without disks but technically possible. */ + if (!dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("domain must have at least one disk to perform " + "backups")); + goto cleanup; + } + + if (!(map = virBitmapNew(dom->ndisks))) + goto cleanup; + + /* Double check requested disks. */ + for (i = 0; i < def->ndisks; i++) { + virDomainBackupDiskDefPtr disk = &def->disks[i]; + int idx = virDomainDiskIndexByName(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; + } + ignore_value(virBitmapSetBit(map, idx)); + disk->idx = idx; + + if (STRNEQ(disk->name, dom->disks[idx]->dst)) { + VIR_FREE(disk->name); + if (VIR_STRDUP(disk->name, dom->disks[idx]->dst) < 0) + goto cleanup; + } + if (disk->store && !disk->store->path) { + virStorageSourceClear(disk->store); + disk->store = NULL; + } + if (virDomainBackupDefAssignStore(disk, dom->disks[i]->src, suffix) < 0) + goto cleanup; + } + + /* Provide fillers for all remaining disks, for easier iteration. */ + if (!def->ndisks) + alloc_all = true; + ndisks = def->ndisks; + if (VIR_EXPAND_N(def->disks, def->ndisks, + dom->ndisks - def->ndisks) < 0) + goto cleanup; + + for (i = 0; i < dom->ndisks; i++) { + virDomainBackupDiskDefPtr disk; + + if (virBitmapIsBitSet(map, i)) + continue; + disk = &def->disks[ndisks++]; + if (VIR_STRDUP(disk->name, dom->disks[i]->dst) < 0) + goto cleanup; + disk->idx = i; + if (alloc_all && + virDomainBackupDefAssignStore(disk, dom->disks[i]->src, suffix) < 0) + goto cleanup; + } + + qsort(&def->disks[0], def->ndisks, sizeof(def->disks[0]), + virDomainBackupCompareDiskIndex); + + ret = 0; + + cleanup: + virBitmapFree(map); + return ret; +} diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fbe7ba2d40..b9b7684494 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -69,6 +69,13 @@ virCapabilitiesSetNetPrefix; # conf/checkpoint_conf.h +virDomainBackupAlignDisks; +virDomainBackupDefFormat; +virDomainBackupDefFree; +virDomainBackupDefParseNode; +virDomainBackupDefParseString; +virDomainBackupTypeFromString; +virDomainBackupTypeToString; virDomainCheckpointAlignDisks; virDomainCheckpointAssignDef; virDomainCheckpointDefFormat; @@ -88,7 +95,6 @@ virDomainCheckpointTypeToString; virDomainCheckpointUpdateRelations; virDomainListAllCheckpoints; - # conf/cpu_conf.h virCPUCacheModeTypeFromString; virCPUCacheModeTypeToString; -- 2.20.1

On 2/6/19 2:18 PM, Eric Blake wrote:
Accept XML describing a generic block job, and output it again as needed. At the moment, it has some qemu-specific hacks, such as storing internal XML for a node name, that might be cleaner once full-tree node-name support goes in.
Still not done: decent tests
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 65 +++++ src/conf/domain_conf.h | 3 + src/conf/checkpoint_conf.c | 503 +++++++++++++++++++++++++++++++++++++ src/libvirt_private.syms | 8 +- 4 files changed, 578 insertions(+), 1 deletion(-)
I think this should be it's own module src/conf/backup_conf.{c,h}
diff --git a/src/conf/checkpoint_conf.h b/src/conf/checkpoint_conf.h index 994a8bd083..abaeaaad52 100644 --- a/src/conf/checkpoint_conf.h +++ b/src/conf/checkpoint_conf.h @@ -147,4 +147,69 @@ int virDomainCheckpointRedefinePrep(virDomainPtr domain,
VIR_ENUM_DECL(virDomainCheckpoint);
+/* Items related to incremental backup state */ + +typedef enum { + VIR_DOMAIN_BACKUP_TYPE_DEFAULT = 0, + VIR_DOMAIN_BACKUP_TYPE_PUSH, + VIR_DOMAIN_BACKUP_TYPE_PULL, + + VIR_DOMAIN_BACKUP_TYPE_LAST +} virDomainBackupType; + +typedef enum { + VIR_DOMAIN_BACKUP_DISK_STATE_DEFAULT = 0, /* Initial */ + VIR_DOMAIN_BACKUP_DISK_STATE_CREATED, /* File created */ + VIR_DOMAIN_BACKUP_DISK_STATE_LABEL, /* Security labels applied */ + VIR_DOMAIN_BACKUP_DISK_STATE_READY, /* Handed to guest */ + VIR_DOMAIN_BACKUP_DISK_STATE_BITMAP, /* Associated temp bitmap created */ + VIR_DOMAIN_BACKUP_DISK_STATE_EXPORT, /* NBD export created */ +} virDomainBackupDiskState; + +/* Stores disk-backup information */ +typedef struct _virDomainBackupDiskDef virDomainBackupDiskDef; +typedef virDomainBackupDiskDef *virDomainBackupDiskDefPtr; +struct _virDomainBackupDiskDef { + char *name; /* name matching the <target dev='...' of the domain */ + int idx; /* index within checkpoint->dom->disks that matches name */ + + /* details of target for push-mode, or of the scratch file for pull-mode */ + virStorageSourcePtr store; + int state; /* virDomainBackupDiskState, not stored in XML */ +}; + +/* Stores the complete backup metadata */ +typedef struct _virDomainBackupDef virDomainBackupDef; +typedef virDomainBackupDef *virDomainBackupDefPtr; +struct _virDomainBackupDef { + /* Public XML. */ + int type; /* virDomainBackupType */ + int id; + char *incremental; + virStorageNetHostDefPtr server; /* only when type == PULL */ + + size_t ndisks; /* should not exceed dom->ndisks */ + virDomainBackupDiskDef *disks; +};
Like checkpoint, the "struct _vir*" probably should go with in the .c files and accessors created.
+ +VIR_ENUM_DECL(virDomainBackup); + +typedef enum { + VIR_DOMAIN_BACKUP_PARSE_INTERNAL = 1 << 0, +} virDomainBackupParseFlags; + +virDomainBackupDefPtr virDomainBackupDefParseString(const char *xmlStr, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +virDomainBackupDefPtr virDomainBackupDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virDomainXMLOptionPtr xmlopt, + unsigned int flags); +void virDomainBackupDefFree(virDomainBackupDefPtr def); +int virDomainBackupDefFormat(virBufferPtr buf, + virDomainBackupDefPtr def, + bool internal); +int virDomainBackupAlignDisks(virDomainBackupDefPtr backup, + virDomainDefPtr dom, const char *suffix); +
Similar to previous patch. Could also have the VIR_DEFINE_AUTOPTR_FUNC(virDomainBackupDef, virDomainBackupDefFree);
#endif /* LIBVIRT_CHECKPOINT_CONF_H */ diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index d31c45427e..fe818a2b27 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -125,6 +125,9 @@ typedef virDomainCheckpointObj *virDomainCheckpointObjPtr; typedef struct _virDomainCheckpointObjList virDomainCheckpointObjList; typedef virDomainCheckpointObjList *virDomainCheckpointObjListPtr;
+typedef struct _virDomainBackupDef virDomainBackupDef; +typedef virDomainBackupDef *virDomainBackupDefPtr; + typedef struct _virDomainSnapshotObj virDomainSnapshotObj; typedef virDomainSnapshotObj *virDomainSnapshotObjPtr;
diff --git a/src/conf/checkpoint_conf.c b/src/conf/checkpoint_conf.c index c0840a96b2..4b148827c1 100644 --- a/src/conf/checkpoint_conf.c +++ b/src/conf/checkpoint_conf.c @@ -1028,3 +1028,506 @@ virDomainCheckpointRedefinePrep(virDomainPtr domain, cleanup: return ret; } + +/* Backup Def functions */ + +VIR_ENUM_IMPL(virDomainBackup, VIR_DOMAIN_BACKUP_TYPE_LAST, + "default", "push", "pull"); + +static void +virDomainBackupDiskDefClear(virDomainBackupDiskDefPtr disk) +{ + VIR_FREE(disk->name); + virStorageSourceClear(disk->store); + disk->store = NULL;
Initialize idx and state?
+}
Similar to previous w/ 2 blank lines.
+ +void +virDomainBackupDefFree(virDomainBackupDefPtr def) +{ + size_t i; + + if (!def) + return; + + VIR_FREE(def->incremental); + VIR_FREE(def->server); // FIXME which struct
True... You'll need some what to Free only what you've allocated or if there's a generic Server*Free function to use it.
+ for (i = 0; i < def->ndisks; i++) + virDomainBackupDiskDefClear(&def->disks[i]); + VIR_FREE(def->disks); + VIR_FREE(def); +} + +static int +virDomainBackupDiskDefParseXML(xmlNodePtr node, + xmlXPathContextPtr ctxt, + virDomainBackupDiskDefPtr def, + bool push, bool internal, + virDomainXMLOptionPtr xmlopt) +{ + int ret = -1; + // char *backup = NULL; /* backup="yes|no"? */ + char *type = NULL; + char *driver = NULL; + xmlNodePtr cur; + xmlNodePtr saved = ctxt->node;
VIR_AUTOFREE(char *) type = NULL; VIR_AUTOFREE(char *) driver = NULL;
+ + ctxt->node = node; + + if (VIR_ALLOC(def->store) < 0) + goto cleanup;
Rather than this you could have a @store which would stolen at the end before cleanup if everything has passed through parsing. I'm in the processing of adding a VIR_AUTOPTR(virStorageSource) too.
+ + def->name = virXMLPropString(node, "name"); + if (!def->name) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing name from disk backup element")); + goto cleanup; + } + + /* Needed? A way for users to list a disk and explicitly mark it + * as not participating, and then output shows all disks rather + * than just active disks */ +#if 0 + backup = virXMLPropString(node, "backup"); + if (backup) { + def->type = virDomainCheckpointTypeFromString(checkpoint); + if (def->type <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk checkpoint setting '%s'"), + checkpoint); + goto cleanup; + } + } +#endif
Still need to decide...
+ + if ((type = virXMLPropString(node, "type"))) { + if ((def->store->type = virStorageTypeFromString(type)) <= 0 || + def->store->type == VIR_STORAGE_TYPE_VOLUME || + def->store->type == VIR_STORAGE_TYPE_DIR) { + virReportError(VIR_ERR_XML_ERROR, + _("unknown disk backup type '%s'"), type); + goto cleanup; + } + } else { + def->store->type = VIR_STORAGE_TYPE_FILE; + } + + if ((cur = virXPathNode(push ? "./target" : "./scratch", ctxt)) && + virDomainDiskSourceParse(cur, ctxt, def->store, 0, xmlopt) < 0) + goto cleanup; + + if (internal) {
Is this another way of using FLAGS to determine the parse mode? IOW, is this only needed when parsing the status/running XML.
+ int detected; + if (virXPathInt("string(./node/@detected)", ctxt, &detected) < 0) + goto cleanup; + def->store->detected = detected; + def->store->nodeformat = virXPathString("string(./node)", ctxt); + } + + if ((driver = virXPathString("string(./driver/@type)", ctxt))) { + def->store->format = virStorageFileFormatTypeFromString(driver); + if (def->store->format <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk backup driver '%s'"), driver); + goto cleanup; + } else if (!push && def->store->format != VIR_STORAGE_FILE_QCOW2) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("pull mode requires qcow2 driver, not '%s'"), + driver); + goto cleanup; + } + } + + /* validate that the passed path is absolute */ + if (virStorageSourceIsRelative(def->store)) { + virReportError(VIR_ERR_XML_ERROR, + _("disk backup image path '%s' must be absolute"), + def->store->path); + goto cleanup; + } + + ret = 0; + cleanup: + ctxt->node = saved; + + VIR_FREE(driver); +// VIR_FREE(backup); + VIR_FREE(type);
VIR_FREE's won't be necessary w/ autofree stuff.
+ if (ret < 0) + virDomainBackupDiskDefClear(def);
Unnecessary since the caller will do this on error with virDomainBackupDefFree
+ return ret; +} + +static virDomainBackupDefPtr +virDomainBackupDefParse(xmlXPathContextPtr ctxt, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainBackupDefPtr def = NULL; + virDomainBackupDefPtr ret = NULL; + xmlNodePtr *nodes = NULL; + xmlNodePtr node = NULL; + char *mode = NULL; + bool push; + size_t i; + int n;
VIR_AUTOPTR(virDomainBackupDef) def = NULL; VIR_AUTOFREE(char *) mode = NULL; VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
+ + if (VIR_ALLOC(def) < 0) + goto cleanup; + + mode = virXMLPropString(ctxt->node, "mode"); + if (mode) { + def->type = virDomainBackupTypeFromString(mode); + if (def->type <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown backup mode '%s'"), mode); + goto cleanup; + } + } else { + def->type = VIR_DOMAIN_BACKUP_TYPE_PUSH; + } + push = def->type == VIR_DOMAIN_BACKUP_TYPE_PUSH; + + if (flags & VIR_DOMAIN_BACKUP_PARSE_INTERNAL) { + char *tmp = virXMLPropString(ctxt->node, "id");
You can use VIR_AUTOFREE(char *) tmp too... Ahh... so this is where it's referenced... I recall this from RNG...
+ if (tmp && virStrToLong_i(tmp, NULL, 10, &def->id) < 0) { + virReportError(VIR_ERR_XML_ERROR, + _("invalid 'id' value '%s'"), tmp); + VIR_FREE(tmp); + goto cleanup; + } + VIR_FREE(tmp); + } + + def->incremental = virXPathString("string(./incremental)", ctxt); + + node = virXPathNode("./server", ctxt); + if (node) { + if (def->type != VIR_DOMAIN_BACKUP_TYPE_PULL) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("use of <server> requires pull mode backup")); + goto cleanup; + } + if (VIR_ALLOC(def->server) < 0) + goto cleanup; + if (virDomainStorageNetworkParseHost(node, def->server) < 0) + goto cleanup; + if (def->server->transport == VIR_STORAGE_NET_HOST_TRANS_RDMA) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("transport rdma is not supported for <server>"));
If transport == FOO is ever added and not supported, then you're in trouble... Go with if (def->server->transport != VIR_STORAGE_NET_HOST_TRANS_...) {TCP|UNIX} and use the virStorageHostNetTransportTypeToString to translate what is not supported...
+ goto cleanup; + } + } + + if ((n = virXPathNodeSet("./disks/*", ctxt, &nodes)) < 0) + goto cleanup; + if (n && VIR_ALLOC_N(def->disks, n) < 0) + goto cleanup; + def->ndisks = n; + for (i = 0; i < def->ndisks; i++) { + if (virDomainBackupDiskDefParseXML(nodes[i], ctxt, + &def->disks[i], push, + flags & VIR_DOMAIN_BACKUP_PARSE_INTERNAL, + xmlopt) < 0) + goto cleanup; + } + VIR_FREE(nodes); + + VIR_STEAL_PTR(ret, def); + + cleanup: + VIR_FREE(mode); + VIR_FREE(nodes); + virDomainBackupDefFree(def);
Autofree removes the need for cleanup here, so we could return NULL directly on failure paths.
+ + return ret; +} + +virDomainBackupDefPtr +virDomainBackupDefParseString(const char *xmlStr, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + virDomainBackupDefPtr ret = NULL; + xmlDocPtr xml; + int keepBlanksDefault = xmlKeepBlanksDefault(0); + + if ((xml = virXMLParse(NULL, xmlStr, _("(domain_backup)")))) { + xmlKeepBlanksDefault(keepBlanksDefault); + ret = virDomainBackupDefParseNode(xml, xmlDocGetRootElement(xml), + xmlopt, flags); + xmlFreeDoc(xml); + } + xmlKeepBlanksDefault(keepBlanksDefault); + + return ret; +} + +virDomainBackupDefPtr +virDomainBackupDefParseNode(xmlDocPtr xml, + xmlNodePtr root, + virDomainXMLOptionPtr xmlopt, + unsigned int flags) +{ + xmlXPathContextPtr ctxt = NULL; + virDomainBackupDefPtr def = NULL; + + if (!virXMLNodeNameEqual(root, "domainbackup")) { + virReportError(VIR_ERR_XML_ERROR, "%s", _("domainbackup")); + goto cleanup;
return NULL;
+ } + + ctxt = xmlXPathNewContext(xml); + if (ctxt == NULL) { + virReportOOMError(); + goto cleanup;
return NULL;
+ } + + ctxt->node = root; + def = virDomainBackupDefParse(ctxt, xmlopt, flags); + cleanup: + xmlXPathFreeContext(ctxt); + return def; +} + +static int +virDomainBackupDiskDefFormat(virBufferPtr buf, + virDomainBackupDiskDefPtr disk, + bool push, bool internal)
one arg per line Should @internal make use of flags instead?
+{ + int type = disk->store->type; + virBuffer attrBuf = VIR_BUFFER_INITIALIZER; + virBuffer childBuf = VIR_BUFFER_INITIALIZER; + int ret = -1; + + if (!disk->name) + return 0; + + virBufferEscapeString(buf, "<disk name='%s'", disk->name); + /* TODO: per-disk backup=off? */
... I assume this is the <backup> noted earlier.
+ + virBufferAsprintf(buf, " type='%s'>\n", virStorageTypeToString(type)); + virBufferAdjustIndent(buf, 2); + + if (disk->store->format > 0) + virBufferEscapeString(buf, "<driver type='%s'/>\n", + virStorageFileFormatTypeToString(disk->store->format)); + /* TODO: should node names be part of storage file xml, rather + * than a one-off hack for qemu? */
Do we really want to store them in two places? I think it's been hard enough with just one. Hazards of a design where the checkpoint and/or backup are not subelements of the disk I suppose.
+ if (internal) { + virBufferEscapeString(buf, "<node detected='%s'", + disk->store->detected ? "1" : "0"); + virBufferEscapeString(buf, ">%s</node>\n", disk->store->nodeformat); + } + + virBufferSetChildIndent(&childBuf, buf); + if (virDomainStorageSourceFormat(&attrBuf, &childBuf, disk->store, 0, + false) < 0) + goto cleanup; + if (virXMLFormatElement(buf, push ? "target" : "scratch", + &attrBuf, &childBuf) < 0) + goto cleanup; + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</disk>\n"); + + ret = 0; + + cleanup: + virBufferFreeAndReset(&attrBuf); + virBufferFreeAndReset(&childBuf); + return ret; +} + +int +virDomainBackupDefFormat(virBufferPtr buf, virDomainBackupDefPtr def, + bool internal) +{ + size_t i; + + virBufferAsprintf(buf, "<domainbackup mode='%s'", + virDomainBackupTypeToString(def->type)); + if (def->id) + virBufferAsprintf(buf, " id='%d'", def->id);> + virBufferAddLit(buf, ">\n"); + virBufferAdjustIndent(buf, 2); + + virBufferEscapeString(buf, "<incremental>%s</incremental>\n", + def->incremental); + if (def->server) { + virBufferAsprintf(buf, "<server transport='%s'", + virStorageNetHostTransportTypeToString(def->server->transport)); + virBufferEscapeString(buf, " name='%s'", def->server->name); + if (def->server->port) + virBufferAsprintf(buf, " port='%u'", def->server->port); + virBufferEscapeString(buf, " socket='%s'", def->server->socket); + virBufferAddLit(buf, "/>\n"); + } + + if (def->ndisks) { + virBufferAddLit(buf, "<disks>\n"); + virBufferAdjustIndent(buf, 2); + for (i = 0; i < def->ndisks; i++) { + if (!def->disks[i].store) + continue; + if (virDomainBackupDiskDefFormat(buf, &def->disks[i], + def->type == VIR_DOMAIN_BACKUP_TYPE_PUSH, + internal) < 0) + return -1; + } + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</disks>\n"); + } + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "</domainbackup>\n"); + + return virBufferCheckError(buf); +} + + +static int +virDomainBackupCompareDiskIndex(const void *a, const void *b) +{ + const virDomainBackupDiskDef *diska = a; + const virDomainBackupDiskDef *diskb = b; + + /* Integer overflow shouldn't be a problem here. */ + return diska->idx - diskb->idx; +} + +static int +virDomainBackupDefAssignStore(virDomainBackupDiskDefPtr disk, + virStorageSourcePtr src, + const char *suffix) +{ + int ret = -1; + + if (virStorageSourceIsEmpty(src)) { + if (disk->store) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk '%s' has no media"), disk->name); + goto cleanup; + } + } else if (src->readonly && disk->store) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("backup of readonly disk '%s' makes no sense"), + disk->name); + goto cleanup; + } else if (!disk->store) { + if (virStorageSourceGetActualType(src) == VIR_STORAGE_TYPE_FILE) { + if (VIR_ALLOC(disk->store) < 0) + goto cleanup; + disk->store->type = VIR_STORAGE_TYPE_FILE; + if (virAsprintf(&disk->store->path, "%s.%s", src->path, + suffix) < 0) + goto cleanup; + disk->store->detected = true; + } else { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("refusing to generate file name for disk '%s'"), + disk->name); + goto cleanup; + } + }
does there need to be an else here ? e.g. can disk->store be set and @src not assigned there? The caller I assume knows to manage @src afterwards.
+ ret = 0; + cleanup: + return ret;
Since there's no cleanup to be done, we could just return directly.
+} + +/* Align def->disks to domain. Sort the list of def->disks, + * generating storage names using suffix as needed. 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. */
Similar locking concerns as previously - that is locking domain object. This is really familiar code too - perhaps some code sharability is possible.
+int +virDomainBackupAlignDisks(virDomainBackupDefPtr def, virDomainDefPtr dom, + const char *suffix) +{ + int ret = -1; + virBitmapPtr map = NULL; + size_t i; + int ndisks; + bool alloc_all = false;
VIR_AUTOPTR(virBitmap) map = NULL; (could be useful other times too, but I just noticed it).
+ + if (def->ndisks > dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("too many disk backup requests for domain")); + goto cleanup; + } + + /* Unlikely to have a guest without disks but technically possible. */ + if (!dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("domain must have at least one disk to perform " + "backups")); + goto cleanup; + } + + if (!(map = virBitmapNew(dom->ndisks))) + goto cleanup; + + /* Double check requested disks. */ + for (i = 0; i < def->ndisks; i++) { + virDomainBackupDiskDefPtr disk = &def->disks[i]; + int idx = virDomainDiskIndexByName(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; + } + ignore_value(virBitmapSetBit(map, idx)); + disk->idx = idx; + + if (STRNEQ(disk->name, dom->disks[idx]->dst)) {
Trying to picture how this happens... should this use STRNEQ_NULLABLE?
+ VIR_FREE(disk->name); + if (VIR_STRDUP(disk->name, dom->disks[idx]->dst) < 0) + goto cleanup; + } + if (disk->store && !disk->store->path) { + virStorageSourceClear(disk->store);
But no VIR_FREE(disk->source) how does that play with the following?
+ disk->store = NULL; + } + if (virDomainBackupDefAssignStore(disk, dom->disks[i]->src, suffix) < 0) + goto cleanup; + } + + /* Provide fillers for all remaining disks, for easier iteration. */
But is it "hooked up" with hotplug/hotunplug? Beginning to think/wonder why backup/checkpoint isn't a child of storage source.
+ if (!def->ndisks) + alloc_all = true; + ndisks = def->ndisks; + if (VIR_EXPAND_N(def->disks, def->ndisks, + dom->ndisks - def->ndisks) < 0) + goto cleanup; + + for (i = 0; i < dom->ndisks; i++) { + virDomainBackupDiskDefPtr disk; + + if (virBitmapIsBitSet(map, i)) + continue; + disk = &def->disks[ndisks++]; + if (VIR_STRDUP(disk->name, dom->disks[i]->dst) < 0) + goto cleanup; + disk->idx = i; + if (alloc_all && + virDomainBackupDefAssignStore(disk, dom->disks[i]->src, suffix) < 0) + goto cleanup; + } + + qsort(&def->disks[0], def->ndisks, sizeof(def->disks[0]), + virDomainBackupCompareDiskIndex); + + ret = 0; + + cleanup: + virBitmapFree(map);
Using autofree stuff means the cleanup is unnecessary and we return directly. John
+ return ret; +} diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fbe7ba2d40..b9b7684494 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -69,6 +69,13 @@ virCapabilitiesSetNetPrefix;
# conf/checkpoint_conf.h +virDomainBackupAlignDisks; +virDomainBackupDefFormat; +virDomainBackupDefFree; +virDomainBackupDefParseNode; +virDomainBackupDefParseString; +virDomainBackupTypeFromString; +virDomainBackupTypeToString; virDomainCheckpointAlignDisks; virDomainCheckpointAssignDef; virDomainCheckpointDefFormat; @@ -88,7 +95,6 @@ virDomainCheckpointTypeToString; virDomainCheckpointUpdateRelations; virDomainListAllCheckpoints;
- # conf/cpu_conf.h virCPUCacheModeTypeFromString; virCPUCacheModeTypeToString;

On 2/12/19 12:08 PM, John Ferlan wrote:
On 2/6/19 2:18 PM, Eric Blake wrote:
Accept XML describing a generic block job, and output it again as needed. At the moment, it has some qemu-specific hacks, such as storing internal XML for a node name, that might be cleaner once full-tree node-name support goes in.
Still not done: decent tests
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/checkpoint_conf.h | 65 +++++ src/conf/domain_conf.h | 3 + src/conf/checkpoint_conf.c | 503 +++++++++++++++++++++++++++++++++++++ src/libvirt_private.syms | 8 +- 4 files changed, 578 insertions(+), 1 deletion(-)
I think this should be it's own module src/conf/backup_conf.{c,h}
That one makes sense to me. Even if the public API lives directly in libvirt-domain.c (instead of my current placement in libvirt-domain-checkpoint.c), the backend for this XML is sufficient enough for its own file. Will split.
+ + /* Needed? A way for users to list a disk and explicitly mark it + * as not participating, and then output shows all disks rather + * than just active disks */ +#if 0 + backup = virXMLPropString(node, "backup"); + if (backup) { + def->type = virDomainCheckpointTypeFromString(checkpoint); + if (def->type <= 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("unknown disk checkpoint setting '%s'"), + checkpoint); + goto cleanup; + } + } +#endif
Still need to decide...
Yes, I do.
+ + if ((type = virXMLPropString(node, "type"))) { + if ((def->store->type = virStorageTypeFromString(type)) <= 0 || + def->store->type == VIR_STORAGE_TYPE_VOLUME || + def->store->type == VIR_STORAGE_TYPE_DIR) { + virReportError(VIR_ERR_XML_ERROR, + _("unknown disk backup type '%s'"), type); + goto cleanup; + } + } else { + def->store->type = VIR_STORAGE_TYPE_FILE; + } + + if ((cur = virXPathNode(push ? "./target" : "./scratch", ctxt)) && + virDomainDiskSourceParse(cur, ctxt, def->store, 0, xmlopt) < 0) + goto cleanup; + + if (internal) {
Is this another way of using FLAGS to determine the parse mode? IOW, is this only needed when parsing the status/running XML.
Yes, it is for internal-use XML, and I already know I need to fix things to use a single 'flags' argument with sane enum values (matching what I already cleaned up for snapshots).
+ + if (flags & VIR_DOMAIN_BACKUP_PARSE_INTERNAL) { + char *tmp = virXMLPropString(ctxt->node, "id");
You can use VIR_AUTOFREE(char *) tmp too...
Ahh... so this is where it's referenced... I recall this from RNG...
Which also raises the question of whether the RNG has to call it out, if the only thing using it is internal code, or if the user will ever see it. We have the interesting problem of outputting some things as user-visible which are output-only, but then having to validate them in the RNG on reparse even if we are going to ignore them, so that the user doesn't have to trim them out. I'll have to double-check my intent here, and will leave an appropriate comment (probably along the lines of "id is valid in output and hence part of the RNG, but ignored on input except when parsing internal XML for reconstructing job state across libvirtd restarts").
+ if (node) { + if (def->type != VIR_DOMAIN_BACKUP_TYPE_PULL) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("use of <server> requires pull mode backup")); + goto cleanup; + } + if (VIR_ALLOC(def->server) < 0) + goto cleanup; + if (virDomainStorageNetworkParseHost(node, def->server) < 0) + goto cleanup; + if (def->server->transport == VIR_STORAGE_NET_HOST_TRANS_RDMA) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("transport rdma is not supported for <server>"));
If transport == FOO is ever added and not supported, then you're in trouble... Go with
if (def->server->transport != VIR_STORAGE_NET_HOST_TRANS_...) {TCP|UNIX}
and use the virStorageHostNetTransportTypeToString to translate what is not supported...
Good call.
+static int +virDomainBackupDiskDefFormat(virBufferPtr buf, + virDomainBackupDiskDefPtr disk, + bool push, bool internal)
one arg per line
Should @internal make use of flags instead?
Yes, especially since I just fixed that in snapshots.
+{ + int type = disk->store->type; + virBuffer attrBuf = VIR_BUFFER_INITIALIZER; + virBuffer childBuf = VIR_BUFFER_INITIALIZER; + int ret = -1; + + if (!disk->name) + return 0; + + virBufferEscapeString(buf, "<disk name='%s'", disk->name); + /* TODO: per-disk backup=off? */
... I assume this is the <backup> noted earlier.
Yes, matches the earlier #if 0 code where I still need to make a decision.
+ + virBufferAsprintf(buf, " type='%s'>\n", virStorageTypeToString(type)); + virBufferAdjustIndent(buf, 2); + + if (disk->store->format > 0) + virBufferEscapeString(buf, "<driver type='%s'/>\n", + virStorageFileFormatTypeToString(disk->store->format)); + /* TODO: should node names be part of storage file xml, rather + * than a one-off hack for qemu? */
Do we really want to store them in two places? I think it's been hard enough with just one.
Hazards of a design where the checkpoint and/or backup are not subelements of the disk I suppose.
I've already had quite a bit of a battle making checkpoints work sanely across qemu restarts (let alone libvirtd restarts); v5 already has some different code here. Basically, libvirt has to recompute the node name any time it connects to a new qemu instance (well, new compared to that libvirtd process), but caching the node names internally is useful. This may clean up somewhat if I rebase on top of Peter's blockdev work, but I'm also trying to make my approaches work without being stuck waiting for his work to land. (Ideally, when blockdev DOES land, the API parts are all stable, and only the internals have to change - so I _really_ need to make sure that no XML locks us in to a particular node name being stored on disk, as that might be incompatible with blockdev refactoring down the road).
+static int +virDomainBackupDefAssignStore(virDomainBackupDiskDefPtr disk, + virStorageSourcePtr src, + const char *suffix) +{ + int ret = -1; + + if (virStorageSourceIsEmpty(src)) { + if (disk->store) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk '%s' has no media"), disk->name); + goto cleanup; + } + } else if (src->readonly && disk->store) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("backup of readonly disk '%s' makes no sense"), + disk->name); + goto cleanup; + } else if (!disk->store) { + if (virStorageSourceGetActualType(src) == VIR_STORAGE_TYPE_FILE) { + if (VIR_ALLOC(disk->store) < 0) + goto cleanup; + disk->store->type = VIR_STORAGE_TYPE_FILE; + if (virAsprintf(&disk->store->path, "%s.%s", src->path, + suffix) < 0) + goto cleanup; + disk->store->detected = true; + } else { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("refusing to generate file name for disk '%s'"), + disk->name); + goto cleanup; + } + }
does there need to be an else here ? e.g. can disk->store be set and @src not assigned there? The caller I assume knows to manage @src afterwards.
Remember the big #if 0? This is all a hack of using 'disk->store' as a boolean on whether to visit the disk at the same time as using it as a pointer to what disk storage to use when it IS being backed up. Splitting things into a separate flag field may make the code easier to reason about.
+ ret = 0; + cleanup: + return ret;
Since there's no cleanup to be done, we could just return directly.
+} + +/* Align def->disks to domain. Sort the list of def->disks, + * generating storage names using suffix as needed. 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. */
Similar locking concerns as previously - that is locking domain object.
This is really familiar code too - perhaps some code sharability is possible.
Yes, now all threee of snapshots, checkpoints, and backups have some form of a AlignDisks function. I'll see if I can come up with a good split (separating the sorting part that determines how the user's input maps to the domain, from the validation part that fills in defaults for anything the user didn't supply explicitly).
+int +virDomainBackupAlignDisks(virDomainBackupDefPtr def, virDomainDefPtr dom, + const char *suffix) +{ + int ret = -1; + virBitmapPtr map = NULL; + size_t i; + int ndisks; + bool alloc_all = false;
VIR_AUTOPTR(virBitmap) map = NULL;
(could be useful other times too, but I just noticed it).
+ + if (def->ndisks > dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("too many disk backup requests for domain")); + goto cleanup; + } + + /* Unlikely to have a guest without disks but technically possible. */ + if (!dom->ndisks) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("domain must have at least one disk to perform " + "backups")); + goto cleanup; + } + + if (!(map = virBitmapNew(dom->ndisks))) + goto cleanup; + + /* Double check requested disks. */ + for (i = 0; i < def->ndisks; i++) { + virDomainBackupDiskDefPtr disk = &def->disks[i]; + int idx = virDomainDiskIndexByName(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; + } + ignore_value(virBitmapSetBit(map, idx)); + disk->idx = idx; + + if (STRNEQ(disk->name, dom->disks[idx]->dst)) {
Trying to picture how this happens... should this use STRNEQ_NULLABLE?
No, both disk->name and dom->disks[idx]->dst are non-NULL as part of their XML parse.
+ VIR_FREE(disk->name); + if (VIR_STRDUP(disk->name, dom->disks[idx]->dst) < 0) + goto cleanup; + } + if (disk->store && !disk->store->path) { + virStorageSourceClear(disk->store);
But no VIR_FREE(disk->source) how does that play with the following?
+ disk->store = NULL; + } + if (virDomainBackupDefAssignStore(disk, dom->disks[i]->src, suffix) < 0) + goto cleanup; + } + + /* Provide fillers for all remaining disks, for easier iteration. */
But is it "hooked up" with hotplug/hotunplug?
Beginning to think/wonder why backup/checkpoint isn't a child of storage source.
Storage source visits only one disk, while snapshot/checkpoint visit all of the domain's disks at once. But you are also right that having storage sources be smarter about things may help.
+ if (!def->ndisks) + alloc_all = true; + ndisks = def->ndisks; + if (VIR_EXPAND_N(def->disks, def->ndisks, + dom->ndisks - def->ndisks) < 0) + goto cleanup; + + for (i = 0; i < dom->ndisks; i++) { + virDomainBackupDiskDefPtr disk; + + if (virBitmapIsBitSet(map, i)) + continue; + disk = &def->disks[ndisks++]; + if (VIR_STRDUP(disk->name, dom->disks[i]->dst) < 0) + goto cleanup; + disk->idx = i; + if (alloc_all && + virDomainBackupDefAssignStore(disk, dom->disks[i]->src, suffix) < 0) + goto cleanup; + } + + qsort(&def->disks[0], def->ndisks, sizeof(def->disks[0]), + virDomainBackupCompareDiskIndex); + + ret = 0; + + cleanup: + virBitmapFree(map);
Using autofree stuff means the cleanup is unnecessary and we return directly.
John
+ return ret; +} diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fbe7ba2d40..b9b7684494 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -69,6 +69,13 @@ virCapabilitiesSetNetPrefix;
# conf/checkpoint_conf.h +virDomainBackupAlignDisks; +virDomainBackupDefFormat; +virDomainBackupDefFree; +virDomainBackupDefParseNode; +virDomainBackupDefParseString; +virDomainBackupTypeFromString; +virDomainBackupTypeToString; virDomainCheckpointAlignDisks; virDomainCheckpointAssignDef; virDomainCheckpointDefFormat; @@ -88,7 +95,6 @@ virDomainCheckpointTypeToString; virDomainCheckpointUpdateRelations; virDomainListAllCheckpoints;
- # conf/cpu_conf.h virCPUCacheModeTypeFromString; virCPUCacheModeTypeToString;
-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

[...]
Ahh... so this is where it's referenced... I recall this from RNG...
Which also raises the question of whether the RNG has to call it out, if the only thing using it is internal code, or if the user will ever see it. We have the interesting problem of outputting some things as user-visible which are output-only, but then having to validate them in the RNG on reparse even if we are going to ignore them, so that the user doesn't have to trim them out. I'll have to double-check my intent here, and will leave an appropriate comment (probably along the lines of "id is valid in output and hence part of the RNG, but ignored on input except when parsing internal XML for reconstructing job state across libvirtd restarts").
There's quite a bit of stuff kept in private domain status files/output that's managed by qemuDomainObjPrivateXMLFormat that is meant to handle internal status that would be read on restarts that is important enough to save somewhere, but not something added to the formal RNG. [...]
+ + virBufferAsprintf(buf, " type='%s'>\n", virStorageTypeToString(type)); + virBufferAdjustIndent(buf, 2); + + if (disk->store->format > 0) + virBufferEscapeString(buf, "<driver type='%s'/>\n", + virStorageFileFormatTypeToString(disk->store->format)); + /* TODO: should node names be part of storage file xml, rather + * than a one-off hack for qemu? */
Do we really want to store them in two places? I think it's been hard enough with just one.
Hazards of a design where the checkpoint and/or backup are not subelements of the disk I suppose.
I've already had quite a bit of a battle making checkpoints work sanely across qemu restarts (let alone libvirtd restarts); v5 already has some different code here. Basically, libvirt has to recompute the node name any time it connects to a new qemu instance (well, new compared to that libvirtd process), but caching the node names internally is useful. This may clean up somewhat if I rebase on top of Peter's blockdev work, but I'm also trying to make my approaches work without being stuck waiting for his work to land. (Ideally, when blockdev DOES land, the API parts are all stable, and only the internals have to change - so I _really_ need to make sure that no XML locks us in to a particular node name being stored on disk, as that might be incompatible with blockdev refactoring down the road).
It's a complex maze of twisty passages, in the distance you hear a babbling engineer ... [based loosely on an old or shall I say original computer game of Colossal Cave Adventure] Exception processing is never easy... We should ask the OS to keep track of what was run before and knowing that some failure occurred be able to restart where things left off just before the failure and of course avoid the code that caused the failure ;-). The future of AI maybe. Nodenames certainly will make things complex... I guess I've thought about that as an under the covers thing and all that the XML really cares about is the target device (regardless of what's behind it) because that's someone else's problem. John [...]

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 (no need for checkpoint-revert, and checkpoint-list was a lot easier since we don't have to cater to older libvirt API). Signed-off-by: Eric Blake <eblake@redhat.com> --- tools/virsh-checkpoint.h | 29 + tools/virsh-completer.h | 4 + tools/virsh-util.h | 3 + tools/virsh.h | 1 + po/POTFILES | 1 + tools/Makefile.am | 3 +- tools/virsh-checkpoint.c | 1329 ++++++++++++++++++++++++++++++++++++++ tools/virsh-completer.c | 52 +- tools/virsh-util.c | 11 + tools/virsh.c | 2 + 10 files changed, 1433 insertions(+), 2 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..707defa1c5 --- /dev/null +++ b/tools/virsh-checkpoint.h @@ -0,0 +1,29 @@ +/* + * virsh-checkpoint.h: Commands to manage domain checkpoints + * + * Copyright (C) 2005-2018 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_VIRSH_CHECKPOINT_H +# define LIBVIRT_VIRSH_CHECKPOINT_H + +# include "virsh.h" + +extern const vshCmdDef checkpointCmds[]; + +#endif /* LIBVIRT_VIRSH_CHECKPOINT_H */ diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 4563fd76ac..59a8582f4a 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -71,6 +71,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 fb2ed277af..f814558144 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -43,6 +43,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 254ce3289e..da157d6caa 100644 --- a/tools/virsh.h +++ b/tools/virsh.h @@ -41,6 +41,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 57c55fb35f..70417cd01b 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -300,6 +300,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 613c9a77f0..1153385d2a 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -1,4 +1,4 @@ -## Copyright (C) 2005-2016 Red Hat, Inc. +## Copyright (C) 2005-2018 Red Hat, Inc. ## Copyright (C) 2013 Yuto KAWAMURA(kawamuray) <kawamuray.dadada@gmail.com> ## ## This library is free software; you can redistribute it and/or @@ -220,6 +220,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..cd08569813 --- /dev/null +++ b/tools/virsh-checkpoint.c @@ -0,0 +1,1329 @@ +/* + * virsh-checkpoint.c: Commands to manage domain checkpoints + * + * Copyright (C) 2005-2018 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" + +/* 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 snapshot 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") + }, + VIRSH_COMMON_OPT_CURRENT(N_("with redefine, set current checkpoint")), + {.name = "no-metadata", + .type = VSH_OT_BOOL, + .help = N_("create checkpoint but create no metadata") + }, + /* TODO - worth adding this flag? + {.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, "current")) + flags |= VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT; + if (vshCommandOptBool(cmd, "no-metadata")) + flags |= VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA; + /* TODO + if (vshCommandOptBool(cmd, "quiesce")) + flags |= VIR_DOMAIN_SNAPSHOT_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 = "no-metadata", + .type = VSH_OT_BOOL, + .help = N_("take checkpoint but create no metadata") + }, + /* TODO + {.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, "no-metadata")) { + if (vshCommandOptBool(cmd, "print-xml")) { + vshError(ctl, "%s", + _("--print-xml is incompatible with --no-metadata")); + return false; + } + flags |= VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA; + } + /* TODO + 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 {--current | --ARG name} into a checkpoint + * belonging to DOM. If EXCLUSIVE, fail if both --current and arg are + * present. 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, bool exclusive, virDomainPtr dom, + virDomainCheckpointPtr *chk, const char **name) +{ + bool current = vshCommandOptBool(cmd, "current"); + const char *chkname = NULL; + + if (vshCommandOptStringReq(ctl, cmd, arg, &chkname) < 0) + return -1; + + if (exclusive && current && chkname) { + vshError(ctl, _("--%s and --current are mutually exclusive"), arg); + return -1; + } + + if (chkname) { + *chk = virDomainCheckpointLookupByName(dom, chkname, 0); + } else if (current) { + *chk = virDomainCheckpointCurrent(dom, 0); + } else { + vshError(ctl, _("--%s or --current 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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("also set edited checkpoint as current")), + {.name = "rename", + .type = VSH_OT_BOOL, + .help = N_("allow renaming an existing checkpoint") + }, + {.name = "clone", + .type = VSH_OT_BOOL, + .help = N_("allow cloning to new name") + }, + {.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; + bool rename_okay = vshCommandOptBool(cmd, "rename"); + bool clone_okay = vshCommandOptBool(cmd, "clone"); + + VSH_EXCLUSIVE_OPTIONS_EXPR("rename", rename_okay, "clone", clone_okay) + + if (vshCommandOptBool(cmd, "current") && + vshCommandOptBool(cmd, "checkpointname")) + define_flags |= VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", false, dom, + &checkpoint, &name) < 0) + goto cleanup; + +#define EDIT_GET_XML \ + virDomainCheckpointGetXMLDesc(checkpoint, getxml_flags) +#define EDIT_NOT_CHANGED \ + do { \ + /* Depending on flags, we re-edit even if XML is unchanged. */ \ + if (!(define_flags & VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT)) { \ + 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 if (clone_okay) { + vshPrintExtra(ctl, _("Checkpoint %s cloned to %s.\n"), name, + edited_name); + } else { + unsigned int delete_flags; + + delete_flags = VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY; + if (virDomainCheckpointDelete(rename_okay ? checkpoint : edited, + delete_flags) < 0) { + vshReportError(ctl); + vshError(ctl, _("Failed to clean up %s"), + rename_okay ? name : edited_name); + goto cleanup; + } + if (!rename_okay) { + vshError(ctl, _("Must use --rename or --clone to change %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; +} + +/* + * "checkpoint-current" command + */ +static const vshCmdInfo info_checkpoint_current[] = { + {.name = "help", + .data = N_("Get or set the current checkpoint") + }, + {.name = "desc", + .data = N_("Get or set the current checkpoint") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_current[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "name", + .type = VSH_OT_BOOL, + .help = N_("list the name, rather than the full xml") + }, + {.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 = "checkpointname", + .type = VSH_OT_STRING, + .help = N_("name of existing checkpoint to make current"), + .completer = virshCheckpointNameCompleter, + }, + {.name = NULL} +}; + +static bool +cmdCheckpointCurrent(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + int current; + virDomainCheckpointPtr checkpoint = NULL; + char *xml = NULL; + const char *checkpointname = NULL; + unsigned int flags = 0; + const char *domname; + + 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; + + VSH_EXCLUSIVE_OPTIONS("name", "checkpointname"); + + if (!(dom = virshCommandOptDomain(ctl, cmd, &domname))) + return false; + + if (vshCommandOptStringReq(ctl, cmd, "checkpointname", &checkpointname) < 0) + goto cleanup; + + if (checkpointname) { + virDomainCheckpointPtr checkpoint2 = NULL; + flags = (VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE | + VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT); + + if (!(checkpoint = virDomainCheckpointLookupByName(dom, + checkpointname, 0))) + goto cleanup; + + xml = virDomainCheckpointGetXMLDesc(checkpoint, + VIR_DOMAIN_CHECKPOINT_XML_SECURE); + if (!xml) + goto cleanup; + + if (!(checkpoint2 = virDomainCheckpointCreateXML(dom, xml, flags))) + goto cleanup; + + virshDomainCheckpointFree(checkpoint2); + vshPrintExtra(ctl, _("Checkpoint %s set as current"), checkpointname); + ret = true; + goto cleanup; + } + + if ((current = virDomainHasCurrentCheckpoint(dom, 0)) < 0) + goto cleanup; + + if (!current) { + vshError(ctl, _("domain '%s' has no current checkpoint"), domname); + goto cleanup; + } else { + if (!(checkpoint = virDomainCheckpointCurrent(dom, 0))) + goto cleanup; + + if (vshCommandOptBool(cmd, "name")) { + const char *name; + if (!(name = virDomainCheckpointGetName(checkpoint))) + goto cleanup; + + vshPrint(ctl, "%s", name); + } else { + if (!(xml = virDomainCheckpointGetXMLDesc(checkpoint, flags))) + goto cleanup; + + vshPrint(ctl, "%s", xml); + } + } + + ret = true; + + cleanup: + if (!ret) + vshReportError(ctl); + VIR_FREE(xml); + 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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("info on current checkpoint")), + {.name = NULL} +}; + +static bool +cmdCheckpointInfo(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom; + virDomainCheckpointPtr checkpoint = NULL; + const char *name; + char *parent = NULL; + bool ret = false; + int count; + unsigned int flags; + int current; + int metadata; + + dom = virshCommandOptDomain(ctl, cmd, NULL); + if (dom == NULL) + return false; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", true, dom, + &checkpoint, &name) < 0) + goto cleanup; + + vshPrint(ctl, "%-15s %s\n", _("Name:"), name); + vshPrint(ctl, "%-15s %s\n", _("Domain:"), virDomainGetName(dom)); + + /* Determine if checkpoint is current. */ + current = virDomainCheckpointIsCurrent(checkpoint, 0); + if (current < 0) { + vshError(ctl, "%s", + _("unexpected problem querying checkpoint state")); + goto cleanup; + } + vshPrint(ctl, "%-15s %s\n", _("Current:"), + current > 0 ? _("yes") : _("no")); + + 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 = virDomainCheckpointListChildren(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 = virDomainCheckpointListChildren(checkpoint, NULL, flags); + if (count < 0) + goto cleanup; + vshPrint(ctl, "%-15s %d\n", _("Descendants:"), count); + + /* Metadata. */ + metadata = virDomainCheckpointHasMetadata(checkpoint, 0); + if (metadata >= 0) + vshPrint(ctl, "%-15s %s\n", _("Metadata:"), + metadata ? _("yes") : _("no")); + + ret = true; + + cleanup: + 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 chklist) +{ + size_t i; + + if (!chklist) + return; + if (chklist->chks) { + for (i = 0; i < chklist->nchks; i++) { + virshDomainCheckpointFree(chklist->chks[i].chk); + VIR_FREE(chklist->chks[i].parent); + } + VIR_FREE(chklist->chks); + } + VIR_FREE(chklist); +} + +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 chklist = vshMalloc(ctl, sizeof(*chklist)); + virshCheckpointListPtr ret = NULL; + unsigned int flags = orig_flags; + + if (from) + count = virDomainCheckpointListChildren(from, &chks, flags); + else + count = virDomainListCheckpoints(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. */ + chklist->chks = vshCalloc(ctl, count + (tree && from), + sizeof(*chklist->chks)); + chklist->nchks = count; + for (i = 0; i < count; i++) + chklist->chks[i].chk = chks[i]; + VIR_FREE(chks); + if (tree) { + for (i = 0; i < count; i++) { + if (virshGetCheckpointParent(ctl, chklist->chks[i].chk, + &chklist->chks[i].parent) < 0) + goto cleanup; + } + if (from) { + chklist->chks[chklist->nchks++].chk = from; + virDomainCheckpointRef(from); + } + } + + qsort(chklist->chks, chklist->nchks, sizeof(*chklist->chks), + virshChkSorter); + + ret = chklist; + chklist = NULL; + + cleanup: + virshCheckpointListFree(chklist); + 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 chklist = opaque; + if (parent) + return chklist->chks[id].parent; + return virDomainCheckpointGetName(chklist->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 = "metadata", + .type = VSH_OT_BOOL, + .help = N_("list only checkpoints that have metadata that would prevent undefine") + }, + {.name = "no-metadata", + .type = VSH_OT_BOOL, + .help = N_("list only checkpoints that have no metadata managed by libvirt") + }, + {.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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("limit list to children of current checkpoint")), + {.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 = 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"); + bool current = vshCommandOptBool(cmd, "current"); + const char *from_chk = NULL; + char *parent_chk = NULL; + virDomainCheckpointPtr start = NULL; + virshCheckpointListPtr chklist = 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); + VSH_EXCLUSIVE_OPTIONS_VAR(roots, current); + +#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 (roots) + flags |= VIR_DOMAIN_CHECKPOINT_LIST_ROOTS; + + if (vshCommandOptBool(cmd, "metadata")) + flags |= VIR_DOMAIN_CHECKPOINT_LIST_METADATA; + + if (vshCommandOptBool(cmd, "no-metadata")) + flags |= VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA; + + if (vshCommandOptBool(cmd, "descendants")) { + if (!from && !current) { + vshError(ctl, "%s", + _("--descendants requires either --from or --current")); + return false; + } + flags |= VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS; + } + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if ((from || current) && + virshLookupCheckpoint(ctl, cmd, "from", true, dom, &start, &from_chk) < 0) + goto cleanup; + + if (!(chklist = virshCheckpointListCollect(ctl, dom, start, flags, tree))) + goto cleanup; + + if (!tree && !name) { + if (parent) + vshPrintExtra(ctl, " %-20s %-25s %s", + _("Name"), _("Creation Time"), _("Parent")); + else + vshPrintExtra(ctl, " %-20s %-25s", + _("Name"), _("Creation Time")); + vshPrintExtra(ctl, "\n" + "------------------------------" + "--------------\n"); + } + + if (tree) { + for (i = 0; i < chklist->nchks; i++) { + if (!chklist->chks[i].parent && + vshTreePrint(ctl, virshCheckpointListLookup, chklist, + chklist->nchks, i) < 0) + goto cleanup; + } + ret = true; + goto cleanup; + } + + for (i = 0; i < chklist->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 = chklist->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) + vshPrint(ctl, " %-20s %-25s %s\n", + chk_name, timestr, parent_chk ?: "-"); + else + vshPrint(ctl, " %-20s %-25s\n", chk_name, timestr); + } + + ret = true; + + cleanup: + /* this frees up memory from the last iteration of the loop */ + virshCheckpointListFree(chklist); + VIR_FREE(parent_chk); + virshDomainCheckpointFree(start); + xmlXPathFreeContext(ctxt); + xmlFreeDoc(xml); + VIR_FREE(doc); + virshDomainFree(dom); + + 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_DATA, + .flags = VSH_OFLAG_REQ, + .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 (vshCommandOptStringReq(ctl, cmd, "checkpointname", &name) < 0) + return false; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (!(checkpoint = virDomainCheckpointLookupByName(dom, 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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("find parent of current checkpoint")), + {.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", true, 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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("delete current checkpoint")), + {.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", true, 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-current", + .handler = cmdCheckpointCurrent, + .opts = opts_checkpoint_current, + .info = info_checkpoint_current, + .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 e62226fc13..6918f215b4 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -1,7 +1,7 @@ /* * virsh-completer.c: virsh completer callbacks * - * Copyright (C) 2017 Red Hat, Inc. + * Copyright (C) 2017-2018 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 @@ -596,6 +596,56 @@ 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 = virDomainListCheckpoints(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-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}, -- 2.20.1

On 2/6/19 2:18 PM, 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 (no need for checkpoint-revert, and checkpoint-list was a lot easier since we don't have to cater to older libvirt API).
Signed-off-by: Eric Blake <eblake@redhat.com> --- tools/virsh-checkpoint.h | 29 + tools/virsh-completer.h | 4 + tools/virsh-util.h | 3 + tools/virsh.h | 1 + po/POTFILES | 1 + tools/Makefile.am | 3 +- tools/virsh-checkpoint.c | 1329 ++++++++++++++++++++++++++++++++++++++ tools/virsh-completer.c | 52 +- tools/virsh-util.c | 11 + tools/virsh.c | 2 + 10 files changed, 1433 insertions(+), 2 deletions(-) create mode 100644 tools/virsh-checkpoint.h create mode 100644 tools/virsh-checkpoint.c
virsh.pod would be useful in order to figure out what everything means. This is just too much being added here to really give this a proper review. This really is a case where too much has been built up. Start with the basics and build up from there. I have similar things as previously w/r/t 2 blank lines between functions and one argument per line per method - all newer norms than when -snapshot was created. Also may as well use the VIR_AUTOFREE type functions for (char *) VIR_FREE's. And VIR_AUTOPTR(virString) as approriate
diff --git a/tools/virsh-checkpoint.h b/tools/virsh-checkpoint.h new file mode 100644 index 0000000000..707defa1c5 --- /dev/null +++ b/tools/virsh-checkpoint.h @@ -0,0 +1,29 @@ +/* + * virsh-checkpoint.h: Commands to manage domain checkpoints + * + * Copyright (C) 2005-2018 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_VIRSH_CHECKPOINT_H +# define LIBVIRT_VIRSH_CHECKPOINT_H + +# include "virsh.h" + +extern const vshCmdDef checkpointCmds[]; + +#endif /* LIBVIRT_VIRSH_CHECKPOINT_H */ diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 4563fd76ac..59a8582f4a 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -71,6 +71,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 fb2ed277af..f814558144 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -43,6 +43,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 254ce3289e..da157d6caa 100644 --- a/tools/virsh.h +++ b/tools/virsh.h @@ -41,6 +41,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 57c55fb35f..70417cd01b 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -300,6 +300,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 613c9a77f0..1153385d2a 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -1,4 +1,4 @@ -## Copyright (C) 2005-2016 Red Hat, Inc. +## Copyright (C) 2005-2018 Red Hat, Inc. ## Copyright (C) 2013 Yuto KAWAMURA(kawamuray) <kawamuray.dadada@gmail.com> ## ## This library is free software; you can redistribute it and/or @@ -220,6 +220,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..cd08569813 --- /dev/null +++ b/tools/virsh-checkpoint.c @@ -0,0 +1,1329 @@ +/* + * virsh-checkpoint.c: Commands to manage domain checkpoints + * + * Copyright (C) 2005-2018 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" + +/* 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 snapshot name"));
Could not find domain checkpoint '%s' would be better
+ 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") + }, + VIRSH_COMMON_OPT_CURRENT(N_("with redefine, set current checkpoint")), + {.name = "no-metadata", + .type = VSH_OT_BOOL, + .help = N_("create checkpoint but create no metadata") + }, + /* TODO - worth adding this flag? + {.name = "quiesce", + .type = VSH_OT_BOOL, + .help = N_("quiesce guest's file systems") + }, + */
More to do
+ {.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, "current")) + flags |= VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT; + if (vshCommandOptBool(cmd, "no-metadata")) + flags |= VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA; + /* TODO + if (vshCommandOptBool(cmd, "quiesce")) + flags |= VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE; + */
Need to address the TODO
+ + 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 = "no-metadata", + .type = VSH_OT_BOOL, + .help = N_("take checkpoint but create no metadata") + }, + /* TODO + {.name = "quiesce", + .type = VSH_OT_BOOL, + .help = N_("quiesce guest's file systems") + }, + */
More todo
+ {.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, "no-metadata")) { + if (vshCommandOptBool(cmd, "print-xml")) { + vshError(ctl, "%s", + _("--print-xml is incompatible with --no-metadata")); + return false; + } + flags |= VIR_DOMAIN_SNAPSHOT_CREATE_NO_METADATA; + } + /* TODO + if (vshCommandOptBool(cmd, "quiesce")) + flags |= VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE; + */
More things to do
+ + 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 {--current | --ARG name} into a checkpoint + * belonging to DOM. If EXCLUSIVE, fail if both --current and arg are + * present. 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, bool exclusive, virDomainPtr dom, + virDomainCheckpointPtr *chk, const char **name) +{ + bool current = vshCommandOptBool(cmd, "current"); + const char *chkname = NULL; + + if (vshCommandOptStringReq(ctl, cmd, arg, &chkname) < 0) + return -1; + + if (exclusive && current && chkname) { + vshError(ctl, _("--%s and --current are mutually exclusive"), arg); + return -1; + } + + if (chkname) { + *chk = virDomainCheckpointLookupByName(dom, chkname, 0); + } else if (current) { + *chk = virDomainCheckpointCurrent(dom, 0); + } else { + vshError(ctl, _("--%s or --current is required"), arg); + return -1; + } + if (!*chk) { + vshReportError(ctl); + return -1; + } + + *name = virDomainCheckpointGetName(*chk); + return 0; +} + +/* + * "checkpoint-edit" command + */
Oh and this is *really* a scary thing to allow! Especially since IIRC the Assign function would just create a new one if a name didn't exist... There seems to be a few things that could happen without locks that could really mess things up.
+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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("also set edited checkpoint as current")), + {.name = "rename", + .type = VSH_OT_BOOL, + .help = N_("allow renaming an existing checkpoint") + }, + {.name = "clone", + .type = VSH_OT_BOOL, + .help = N_("allow cloning to new name") + }, + {.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; + bool rename_okay = vshCommandOptBool(cmd, "rename"); + bool clone_okay = vshCommandOptBool(cmd, "clone"); + + VSH_EXCLUSIVE_OPTIONS_EXPR("rename", rename_okay, "clone", clone_okay) + + if (vshCommandOptBool(cmd, "current") && + vshCommandOptBool(cmd, "checkpointname")) + define_flags |= VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", false, dom, + &checkpoint, &name) < 0) + goto cleanup; + +#define EDIT_GET_XML \ + virDomainCheckpointGetXMLDesc(checkpoint, getxml_flags) +#define EDIT_NOT_CHANGED \ + do { \ + /* Depending on flags, we re-edit even if XML is unchanged. */ \ + if (!(define_flags & VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT)) { \ + 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 if (clone_okay) { + vshPrintExtra(ctl, _("Checkpoint %s cloned to %s.\n"), name, + edited_name); + } else { + unsigned int delete_flags; + + delete_flags = VIR_DOMAIN_CHECKPOINT_DELETE_METADATA_ONLY; + if (virDomainCheckpointDelete(rename_okay ? checkpoint : edited, + delete_flags) < 0) { + vshReportError(ctl); + vshError(ctl, _("Failed to clean up %s"), + rename_okay ? name : edited_name); + goto cleanup; + } + if (!rename_okay) { + vshError(ctl, _("Must use --rename or --clone to change %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; +} + +/* + * "checkpoint-current" command + */ +static const vshCmdInfo info_checkpoint_current[] = { + {.name = "help", + .data = N_("Get or set the current checkpoint") + }, + {.name = "desc", + .data = N_("Get or set the current checkpoint") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_checkpoint_current[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "name", + .type = VSH_OT_BOOL, + .help = N_("list the name, rather than the full xml") + }, + {.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 = "checkpointname", + .type = VSH_OT_STRING, + .help = N_("name of existing checkpoint to make current"), + .completer = virshCheckpointNameCompleter, + }, + {.name = NULL} +}; + +static bool +cmdCheckpointCurrent(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + int current; + virDomainCheckpointPtr checkpoint = NULL; + char *xml = NULL; + const char *checkpointname = NULL; + unsigned int flags = 0; + const char *domname; + + 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; + + VSH_EXCLUSIVE_OPTIONS("name", "checkpointname"); + + if (!(dom = virshCommandOptDomain(ctl, cmd, &domname))) + return false; + + if (vshCommandOptStringReq(ctl, cmd, "checkpointname", &checkpointname) < 0) + goto cleanup; + + if (checkpointname) { + virDomainCheckpointPtr checkpoint2 = NULL; + flags = (VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE | + VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT); + + if (!(checkpoint = virDomainCheckpointLookupByName(dom, + checkpointname, 0))) + goto cleanup; + + xml = virDomainCheckpointGetXMLDesc(checkpoint, + VIR_DOMAIN_CHECKPOINT_XML_SECURE); + if (!xml) + goto cleanup; + + if (!(checkpoint2 = virDomainCheckpointCreateXML(dom, xml, flags))) + goto cleanup;
I need a better map. When creating a checkpoint a couple patches ago, the AssignDef would fail if the name already existed. This code seems to take existing XML with a name that would already exist.
+ + virshDomainCheckpointFree(checkpoint2); + vshPrintExtra(ctl, _("Checkpoint %s set as current"), checkpointname); + ret = true; + goto cleanup; + } + + if ((current = virDomainHasCurrentCheckpoint(dom, 0)) < 0) + goto cleanup; + + if (!current) { + vshError(ctl, _("domain '%s' has no current checkpoint"), domname);
True, so if I don't have one, but want to create one, then I cannot use this?
+ goto cleanup; + } else { + if (!(checkpoint = virDomainCheckpointCurrent(dom, 0))) + goto cleanup; + + if (vshCommandOptBool(cmd, "name")) { + const char *name; + if (!(name = virDomainCheckpointGetName(checkpoint))) + goto cleanup; + + vshPrint(ctl, "%s", name); + } else { + if (!(xml = virDomainCheckpointGetXMLDesc(checkpoint, flags))) + goto cleanup; + + vshPrint(ctl, "%s", xml); + } + } + + ret = true; + + cleanup: + if (!ret) + vshReportError(ctl); + VIR_FREE(xml); + 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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("info on current checkpoint")), + {.name = NULL} +}; + +static bool +cmdCheckpointInfo(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom; + virDomainCheckpointPtr checkpoint = NULL; + const char *name; + char *parent = NULL; + bool ret = false; + int count; + unsigned int flags; + int current; + int metadata; + + dom = virshCommandOptDomain(ctl, cmd, NULL); + if (dom == NULL) + return false; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", true, dom, + &checkpoint, &name) < 0) + goto cleanup; + + vshPrint(ctl, "%-15s %s\n", _("Name:"), name); + vshPrint(ctl, "%-15s %s\n", _("Domain:"), virDomainGetName(dom));
Meaning we could have partial printing if the subequent fails
+ + /* Determine if checkpoint is current. */ + current = virDomainCheckpointIsCurrent(checkpoint, 0); + if (current < 0) { + vshError(ctl, "%s", + _("unexpected problem querying checkpoint state")); + goto cleanup; + } + vshPrint(ctl, "%-15s %s\n", _("Current:"), + current > 0 ? _("yes") : _("no")); + + 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 = virDomainCheckpointListChildren(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 = virDomainCheckpointListChildren(checkpoint, NULL, flags); + if (count < 0) + goto cleanup; + vshPrint(ctl, "%-15s %d\n", _("Descendants:"), count);
Is delineating children and desendants necessary?
+ + /* Metadata. */ + metadata = virDomainCheckpointHasMetadata(checkpoint, 0); + if (metadata >= 0) + vshPrint(ctl, "%-15s %s\n", _("Metadata:"), + metadata ? _("yes") : _("no"));
Does the lack of children or failures therein mean we shouldn't print this. Ordering I suppose.
+ + ret = true; + + cleanup: + 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 chklist) +{ + size_t i; + + if (!chklist) + return; + if (chklist->chks) { + for (i = 0; i < chklist->nchks; i++) { + virshDomainCheckpointFree(chklist->chks[i].chk); + VIR_FREE(chklist->chks[i].parent); + } + VIR_FREE(chklist->chks); + } + VIR_FREE(chklist); +} + +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 chklist = vshMalloc(ctl, sizeof(*chklist)); + virshCheckpointListPtr ret = NULL; + unsigned int flags = orig_flags; + + if (from) + count = virDomainCheckpointListChildren(from, &chks, flags); + else + count = virDomainListCheckpoints(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. */
Still mumbling about too many options.
+ chklist->chks = vshCalloc(ctl, count + (tree && from), + sizeof(*chklist->chks)); + chklist->nchks = count; + for (i = 0; i < count; i++) + chklist->chks[i].chk = chks[i]; + VIR_FREE(chks); + if (tree) { + for (i = 0; i < count; i++) { + if (virshGetCheckpointParent(ctl, chklist->chks[i].chk, + &chklist->chks[i].parent) < 0) + goto cleanup; + } + if (from) { + chklist->chks[chklist->nchks++].chk = from; + virDomainCheckpointRef(from); + } + } + + qsort(chklist->chks, chklist->nchks, sizeof(*chklist->chks), + virshChkSorter); + + ret = chklist; + chklist = NULL; + + cleanup: + virshCheckpointListFree(chklist); + 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 chklist = opaque; + if (parent) + return chklist->chks[id].parent; + return virDomainCheckpointGetName(chklist->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 = "metadata", + .type = VSH_OT_BOOL, + .help = N_("list only checkpoints that have metadata that would prevent undefine") + }, + {.name = "no-metadata", + .type = VSH_OT_BOOL, + .help = N_("list only checkpoints that have no metadata managed by libvirt") + }, + {.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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("limit list to children of current checkpoint")), + {.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 = NULL} +}; +
That's a lot of different options... I'm confused over multiple checkpoints without a parent. I would figure a checkpoint without a parent is the root or first checkpoint and children are essentially linear. But this tree concept just complicates things so much.
+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"); + bool current = vshCommandOptBool(cmd, "current"); + const char *from_chk = NULL; + char *parent_chk = NULL; + virDomainCheckpointPtr start = NULL; + virshCheckpointListPtr chklist = 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); + VSH_EXCLUSIVE_OPTIONS_VAR(roots, current); + +#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 (roots) + flags |= VIR_DOMAIN_CHECKPOINT_LIST_ROOTS; + + if (vshCommandOptBool(cmd, "metadata")) + flags |= VIR_DOMAIN_CHECKPOINT_LIST_METADATA; + + if (vshCommandOptBool(cmd, "no-metadata")) + flags |= VIR_DOMAIN_CHECKPOINT_LIST_NO_METADATA; + + if (vshCommandOptBool(cmd, "descendants")) { + if (!from && !current) { + vshError(ctl, "%s", + _("--descendants requires either --from or --current")); + return false; + } + flags |= VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS; + } + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if ((from || current) && + virshLookupCheckpoint(ctl, cmd, "from", true, dom, &start, &from_chk) < 0) + goto cleanup; + + if (!(chklist = virshCheckpointListCollect(ctl, dom, start, flags, tree))) + goto cleanup; + + if (!tree && !name) { + if (parent) + vshPrintExtra(ctl, " %-20s %-25s %s", + _("Name"), _("Creation Time"), _("Parent")); + else + vshPrintExtra(ctl, " %-20s %-25s", + _("Name"), _("Creation Time")); + vshPrintExtra(ctl, "\n" + "------------------------------" + "--------------\n"); + } + + if (tree) { + for (i = 0; i < chklist->nchks; i++) { + if (!chklist->chks[i].parent && + vshTreePrint(ctl, virshCheckpointListLookup, chklist, + chklist->nchks, i) < 0) + goto cleanup; + } + ret = true; + goto cleanup; + } + + for (i = 0; i < chklist->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 = chklist->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) + vshPrint(ctl, " %-20s %-25s %s\n", + chk_name, timestr, parent_chk ?: "-"); + else + vshPrint(ctl, " %-20s %-25s\n", chk_name, timestr); + } + + ret = true; + + cleanup: + /* this frees up memory from the last iteration of the loop */ + virshCheckpointListFree(chklist); + VIR_FREE(parent_chk); + virshDomainCheckpointFree(start); + xmlXPathFreeContext(ctxt); + xmlFreeDoc(xml); + VIR_FREE(doc); + virshDomainFree(dom); + + 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_DATA, + .flags = VSH_OFLAG_REQ, + .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 (vshCommandOptStringReq(ctl, cmd, "checkpointname", &name) < 0) + return false; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (!(checkpoint = virDomainCheckpointLookupByName(dom, 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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("find parent of current checkpoint")), + {.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", true, dom, + &checkpoint, &name) < 0) + goto cleanup; + + if (virshGetCheckpointParent(ctl, checkpoint, &parent) < 0) + goto cleanup; + if (!parent) { + vshError(ctl, _("checkpoint '%s' has no parent"), name);
Similar thoughts here w/r/t parent being root or first checkpoint.
+ 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, + }, + VIRSH_COMMON_OPT_CURRENT(N_("delete current checkpoint")), + {.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", true, 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-current", + .handler = cmdCheckpointCurrent, + .opts = opts_checkpoint_current, + .info = info_checkpoint_current, + .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} +};
Like I noted above - my eyes and brain are in absolute overloaded mode. Hard to perform a solid/good review with just so much data. John
diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index e62226fc13..6918f215b4 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -1,7 +1,7 @@ /* * virsh-completer.c: virsh completer callbacks * - * Copyright (C) 2017 Red Hat, Inc. + * Copyright (C) 2017-2018 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 @@ -596,6 +596,56 @@ 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 = virDomainListCheckpoints(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-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},

On 2/12/19 1:15 PM, John Ferlan wrote:
On 2/6/19 2:18 PM, 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 (no need for checkpoint-revert, and checkpoint-list was a lot easier since we don't have to cater to older libvirt API).
Signed-off-by: Eric Blake <eblake@redhat.com> --- tools/virsh-checkpoint.h | 29 + tools/virsh-completer.h | 4 + tools/virsh-util.h | 3 + tools/virsh.h | 1 + po/POTFILES | 1 + tools/Makefile.am | 3 +- tools/virsh-checkpoint.c | 1329 ++++++++++++++++++++++++++++++++++++++ tools/virsh-completer.c | 52 +- tools/virsh-util.c | 11 + tools/virsh.c | 2 + 10 files changed, 1433 insertions(+), 2 deletions(-) create mode 100644 tools/virsh-checkpoint.h create mode 100644 tools/virsh-checkpoint.c
virsh.pod would be useful in order to figure out what everything means.
Gah. I completely forgot to copy-and-paste that portion of the snapshot code. Will rectify.
This is just too much being added here to really give this a proper review. This really is a case where too much has been built up. Start with the basics and build up from there.
I have similar things as previously w/r/t 2 blank lines between functions and one argument per line per method - all newer norms than when -snapshot was created.
Also may as well use the VIR_AUTOFREE type functions for (char *) VIR_FREE's. And VIR_AUTOPTR(virString) as approriate
Yep, on my list of things to clean up where I spot them, to avoid regressing to the older state-of-the-art when I first copied this code from snapshots.
+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 snapshot name"));
Could not find domain checkpoint '%s'
would be better
Particularly since it's not a snapshot. (Too much copy-and-paste...)
+ /* TODO - worth adding this flag? + {.name = "quiesce", + .type = VSH_OT_BOOL, + .help = N_("quiesce guest's file systems") + }, + */
More to do
At least adding it here is easy. Adding it in qemu is harder.
+/* + * "checkpoint-edit" command + */
Oh and this is *really* a scary thing to allow! Especially since IIRC the Assign function would just create a new one if a name didn't exist... There seems to be a few things that could happen without locks that could really mess things up.
Modeled after "snapshot-edit" - but yes, it is a scary-enough command that splitting it to a separate commit doesn't hurt my feelings (nor hold up the initial API).
+static bool +cmdCheckpointCurrent(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + int current; + virDomainCheckpointPtr checkpoint = NULL; + char *xml = NULL; + const char *checkpointname = NULL; + unsigned int flags = 0; + const char *domname; + + 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; + + VSH_EXCLUSIVE_OPTIONS("name", "checkpointname"); + + if (!(dom = virshCommandOptDomain(ctl, cmd, &domname))) + return false; + + if (vshCommandOptStringReq(ctl, cmd, "checkpointname", &checkpointname) < 0) + goto cleanup; + + if (checkpointname) { + virDomainCheckpointPtr checkpoint2 = NULL; + flags = (VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE | + VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT); + + if (!(checkpoint = virDomainCheckpointLookupByName(dom, + checkpointname, 0))) + goto cleanup; + + xml = virDomainCheckpointGetXMLDesc(checkpoint, + VIR_DOMAIN_CHECKPOINT_XML_SECURE); + if (!xml) + goto cleanup; + + if (!(checkpoint2 = virDomainCheckpointCreateXML(dom, xml, flags))) + goto cleanup;
I need a better map. When creating a checkpoint a couple patches ago, the AssignDef would fail if the name already existed. This code seems to take existing XML with a name that would already exist.
Modeled after the existing 'snapshot-current', which can be used to redefine WHICH snapshot is marked current (but oddly enough lacks a way to mark NO snapshot as current). It uses the _REDEFINE flag to do an in-place edit to the virDomainCheckpointObjList on which checkpoint is marked current (and based on the use of the _CURRENT flag to state which checkpoint is the new current one).
+ + dom = virshCommandOptDomain(ctl, cmd, NULL); + if (dom == NULL) + return false; + + if (virshLookupCheckpoint(ctl, cmd, "checkpointname", true, dom, + &checkpoint, &name) < 0) + goto cleanup; + + vshPrint(ctl, "%-15s %s\n", _("Name:"), name); + vshPrint(ctl, "%-15s %s\n", _("Domain:"), virDomainGetName(dom));
Meaning we could have partial printing if the subequent fails
Yeah. And isn't the new norm to use the new table-printer that formats columns nicely according to content width? I'll have to see if snapshot code has improved here in the meantime.
+ /* Children, Descendants. */ + flags = 0; + count = virDomainCheckpointListChildren(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 = virDomainCheckpointListChildren(checkpoint, NULL, flags); + if (count < 0) + goto cleanup; + vshPrint(ctl, "%-15s %d\n", _("Descendants:"), count);
Is delineating children and desendants necessary?
Knowing that you have 1 child but 3 descendents (and therefore, 2 of the 3 descendents are grandchildren or below) is interesting, if only to prove that the filtering API flags work :)
+ /* When mixing --from and --tree, we also want a copy of from + * in the list, but with no parent for that one entry. */
Still mumbling about too many options.
And that snapshot-list has just as many options.
That's a lot of different options... I'm confused over multiple checkpoints without a parent. I would figure a checkpoint without a parent is the root or first checkpoint and children are essentially linear. But this tree concept just complicates things so much.
Snapshots can definitely be more than linear. I'm not sure if checkpoints can easily become more than linear, but I don't want to rule it out (after all, reverting to prior states can do weird things). -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

--- tools/virsh-checkpoint.c | 249 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) diff --git a/tools/virsh-checkpoint.c b/tools/virsh-checkpoint.c index cd08569813..367a424c7b 100644 --- a/tools/virsh-checkpoint.c +++ b/tools/virsh-checkpoint.c @@ -1270,6 +1270,237 @@ cmdCheckpointDelete(vshControl *ctl, const vshCmd *cmd) return ret; } +/* + * "backup-begin" command + */ +static const vshCmdInfo info_backup_begin[] = { + {.name = "help", + .data = N_("Start a disk backup of a live domain") + }, + {.name = "desc", + .data = N_("Use XML to start a full or incremental disk backup of a live " + "domain, optionally creating a checkpoint") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_backup_begin[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "xmlfile", + .type = VSH_OT_STRING, + .help = N_("domain backup XML") + }, + {.name = "checkpointxml", + .type = VSH_OT_STRING, + .help = N_("domain checkpoint XML") + }, + {.name = "no-metadata", + .type = VSH_OT_BOOL, + .help = N_("create checkpoint but don't track metadata") + }, + /* TODO - worth adding this flag? + {.name = "quiesce", + .type = VSH_OT_BOOL, + .help = N_("quiesce guest's file systems") + }, + */ + {.name = NULL} +}; + +static bool +cmdBackupBegin(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + const char *backup_from = NULL; + char *backup_buffer = NULL; + const char *check_from = NULL; + char *check_buffer = NULL; + unsigned int flags = 0; + int id; + + if (vshCommandOptBool(cmd, "no-metadata")) + flags |= VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA; + /* TODO + if (vshCommandOptBool(cmd, "quiesce")) + flags |= VIR_DOMAIN_BACKUP_BEGIN_QUIESCE; + */ + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + goto cleanup; + + if (vshCommandOptStringReq(ctl, cmd, "xmlfile", &backup_from) < 0) + goto cleanup; + if (!backup_from) { + backup_buffer = vshStrdup(ctl, "<domainbackup/>"); + } else { + if (virFileReadAll(backup_from, VSH_MAX_XML_FILE, &backup_buffer) < 0) { + vshSaveLibvirtError(); + goto cleanup; + } + } + + if (vshCommandOptStringReq(ctl, cmd, "checkpointxml", &check_from) < 0) + goto cleanup; + if (check_from) { + if (virFileReadAll(check_from, VSH_MAX_XML_FILE, &check_buffer) < 0) { + vshSaveLibvirtError(); + goto cleanup; + } + } + + id = virDomainBackupBegin(dom, backup_buffer, check_buffer, flags); + + if (id < 0) + goto cleanup; + + vshPrint(ctl, _("Backup id %d started\n"), id); + if (backup_from) + vshPrintExtra(ctl, _("backup used description from '%s'\n"), + backup_from); + if (check_buffer) + vshPrintExtra(ctl, _("checkpoint created from '%s'\n"), check_from); + + ret = true; + + cleanup: + VIR_FREE(backup_buffer); + VIR_FREE(check_buffer); + virshDomainFree(dom); + + return ret; +} + +/* TODO: backup-begin-as? */ + +/* + * "backup-dumpxml" command + */ +static const vshCmdInfo info_backup_dumpxml[] = { + {.name = "help", + .data = N_("Dump XML for an ongoing domain block backup job") + }, + {.name = "desc", + .data = N_("Backup Dump XML") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_backup_dumpxml[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "id", + .type = VSH_OT_INT, + .flags = VSH_OFLAG_REQ, + .help = N_("backup job id"), + /* TODO: Add API for listing active jobs, then adding a completer? */ + }, + {.name = "security-info", + .type = VSH_OT_BOOL, + .help = N_("include security sensitive information in XML dump") + }, + /* TODO - worth adding this flag? + {.name = "checkpoint", + .type = VSH_OT_BOOL, + .help = N_("if the backup created a checkpoint, also dump that XML") + }, + */ + {.name = NULL} +}; + +static bool +cmdBackupDumpXML(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + char *xml = NULL; + unsigned int flags = 0; + int id; + + if (vshCommandOptBool(cmd, "security-info")) + flags |= VIR_DOMAIN_XML_SECURE; + + if (vshCommandOptInt(ctl, cmd, "id", &id) < 0) + return false; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + return false; + + if (!(xml = virDomainBackupGetXMLDesc(dom, id, flags))) + goto cleanup; + + vshPrint(ctl, "%s", xml); + ret = true; + + cleanup: + VIR_FREE(xml); + virshDomainFree(dom); + + return ret; +} + +/* + * "backup-end" command + */ +static const vshCmdInfo info_backup_end[] = { + {.name = "help", + .data = N_("Conclude a disk backup of a live domain") + }, + {.name = "desc", + .data = N_("End a domain block backup job") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_backup_end[] = { + VIRSH_COMMON_OPT_DOMAIN_FULL(0), + {.name = "id", + .type = VSH_OT_INT, + .flags = VSH_OFLAG_REQ, + .help = N_("backup job id"), + /* TODO: Add API for listing active jobs, then adding a completer? */ + }, + {.name = "abort", + .type = VSH_OT_BOOL, + .help = N_("abandon a push model backup that has not yet completed") + }, + {.name = NULL} +}; + +static bool +cmdBackupEnd(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + unsigned int flags = 0; + int id; + int rc; + + if (vshCommandOptBool(cmd, "abort")) + flags |= VIR_DOMAIN_BACKUP_END_ABORT; + + if (vshCommandOptInt(ctl, cmd, "id", &id) < 0) + return false; + + if (!(dom = virshCommandOptDomain(ctl, cmd, NULL))) + goto cleanup; + + rc = virDomainBackupEnd(dom, id, flags); + + if (rc < 0) + goto cleanup; + if (rc == 0) + vshPrint(ctl, _("Backup id %d aborted"), id); + else + vshPrint(ctl, _("Backup id %d completed"), id); + + ret = true; + + cleanup: + virshDomainFree(dom); + + return ret; +} + const vshCmdDef checkpointCmds[] = { {.name = "checkpoint-create", .handler = cmdCheckpointCreate, @@ -1325,5 +1556,23 @@ const vshCmdDef checkpointCmds[] = { .info = info_checkpoint_parent, .flags = 0 }, + {.name = "backup-begin", + .handler = cmdBackupBegin, + .opts = opts_backup_begin, + .info = info_backup_begin, + .flags = 0 + }, + {.name = "backup-dumpxml", + .handler = cmdBackupDumpXML, + .opts = opts_backup_dumpxml, + .info = info_backup_dumpxml, + .flags = 0 + }, + {.name = "backup-end", + .handler = cmdBackupEnd, + .opts = opts_backup_end, + .info = info_backup_end, + .flags = 0 + }, {.name = NULL} }; -- 2.20.1

Add some monitor commands to be used during backup/checkpoint operations: - another facet to query-block for learning bitmap size - the new bitmap parameter to nbd-server-add-bitmap - new block-dirty-bitmap-{add,enable,disable,merge} functions Also add two capabilities for testing that they are supported; using block-dirty-bitmap-merge as the generic witness of checkpoint support (since all of the functionalities were added in the same qemu 4.0 release), and the bitmap parameter to nbd-server-add for pull-mode backup support. Even though both capabilities are likely to be present or absent together (that is, it is unlikely to encounter a qemu that backports only one of the two), it still makes sense to keep two capabilities as the two uses are orthogonal (full backups don't require checkpoints, push mode backups don't require NBD bitmap support, and checkpoints can be used for more than just incremental backups). Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_capabilities.h | 2 + src/qemu/qemu_monitor.h | 20 +- src/qemu/qemu_monitor_json.h | 18 +- src/qemu/qemu_capabilities.c | 4 + src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_monitor.c | 64 +++++- src/qemu/qemu_monitor_json.c | 195 +++++++++++++++++- .../caps_4.0.0.riscv32.xml | 2 + .../caps_4.0.0.riscv64.xml | 2 + tests/qemumonitorjsontest.c | 2 +- 10 files changed, 303 insertions(+), 8 deletions(-) diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 6d5ed8a3cc..3953e3ad3c 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -504,6 +504,8 @@ typedef enum { /* virQEMUCapsFlags grouping marker for syntax-check */ /* 325 */ QEMU_CAPS_OBJECT_MEMORY_FILE_PMEM, /* -object memory-backend-file,pmem= */ QEMU_CAPS_DEVICE_NVDIMM_UNARMED, /* -device nvdimm,unarmed= */ + QEMU_CAPS_BITMAP_MERGE, /* block-dirty-bitmap-merge */ + QEMU_CAPS_NBD_BITMAP, /* nbd-server-add supports bitmap */ QEMU_CAPS_LAST /* this must always be the last item */ } virQEMUCapsFlags; diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 55acd60380..7eb0d067b9 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -25,6 +25,7 @@ # include "internal.h" # include "domain_conf.h" +# include "checkpoint_conf.h" # include "virbitmap.h" # include "virhash.h" # include "virjson.h" @@ -629,6 +630,9 @@ int qemuMonitorBlockStatsUpdateCapacity(qemuMonitorPtr mon, int qemuMonitorBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon, virHashTablePtr stats) ATTRIBUTE_NONNULL(2); +int qemuMonitorUpdateCheckpointSize(qemuMonitorPtr mon, + virDomainCheckpointDefPtr chk) + ATTRIBUTE_NONNULL(2); int qemuMonitorBlockResize(qemuMonitorPtr mon, const char *device, @@ -647,6 +651,19 @@ int qemuMonitorSetBalloon(qemuMonitorPtr mon, unsigned long long newmem); int qemuMonitorSetCPU(qemuMonitorPtr mon, int cpu, bool online); +int qemuMonitorAddBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap, bool persistent) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); +int qemuMonitorEnableBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); +int qemuMonitorMergeBitmaps(qemuMonitorPtr mon, const char *node, + const char *dst, virJSONValuePtr *src) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4); +int qemuMonitorDeleteBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); + /* XXX should we pass the virDomainDiskDefPtr instead * and hide dev_name details inside monitor. Reconsider @@ -1100,7 +1117,8 @@ int qemuMonitorNBDServerStart(qemuMonitorPtr mon, int qemuMonitorNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, const char *export, - bool writable); + bool writable, + const char *bitmap); int qemuMonitorNBDServerStop(qemuMonitorPtr); int qemuMonitorGetTPMModels(qemuMonitorPtr mon, char ***tpmmodels); diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index b105964ed6..076f7b6dde 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -98,6 +98,9 @@ int qemuMonitorJSONBlockResize(qemuMonitorPtr mon, const char *nodename, unsigned long long size); +int qemuMonitorJSONUpdateCheckpointSize(qemuMonitorPtr mon, + virDomainCheckpointDefPtr chk); + int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon, const char *password); int qemuMonitorJSONSetPassword(qemuMonitorPtr mon, @@ -466,7 +469,8 @@ int qemuMonitorJSONNBDServerStart(qemuMonitorPtr mon, int qemuMonitorJSONNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, const char *export, - bool writable); + bool writable, + const char *bitmap); int qemuMonitorJSONNBDServerStop(qemuMonitorPtr mon); int qemuMonitorJSONGetTPMModels(qemuMonitorPtr mon, char ***tpmmodels) @@ -579,4 +583,16 @@ int qemuMonitorJSONGetPRManagerInfo(qemuMonitorPtr mon, virHashTablePtr info) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); +int qemuMonitorJSONAddBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap, bool persistent); + +int qemuMonitorJSONEnableBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap); + +int qemuMonitorJSONMergeBitmaps(qemuMonitorPtr mon, const char *node, + const char *dst, virJSONValuePtr *src); + +int qemuMonitorJSONDeleteBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap); + #endif /* LIBVIRT_QEMU_MONITOR_JSON_H */ diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 81ef0357e7..cc4f197835 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -524,6 +524,8 @@ VIR_ENUM_IMPL(virQEMUCaps, QEMU_CAPS_LAST, /* 325 */ "memory-backend-file.pmem", "nvdimm.unarmed", + "bitmap-merge", + "nbd-bitmap", ); @@ -979,6 +981,7 @@ struct virQEMUCapsStringFlags virQEMUCapsCommands[] = { { "query-cpus-fast", QEMU_CAPS_QUERY_CPUS_FAST }, { "qom-list-properties", QEMU_CAPS_QOM_LIST_PROPERTIES }, { "blockdev-del", QEMU_CAPS_BLOCKDEV_DEL }, + { "block-dirty-bitmap-merge", QEMU_CAPS_BITMAP_MERGE }, }; struct virQEMUCapsStringFlags virQEMUCapsMigration[] = { @@ -1257,6 +1260,7 @@ static struct virQEMUCapsStringFlags virQEMUCapsQMPSchemaQueries[] = { { "block-commit/arg-type/*top", QEMU_CAPS_ACTIVE_COMMIT }, { "query-iothreads/ret-type/poll-max-ns", QEMU_CAPS_IOTHREAD_POLLING }, { "query-display-options/ret-type/+egl-headless/rendernode", QEMU_CAPS_EGL_HEADLESS_RENDERNODE }, + { "nbd-server-add/arg-type/bitmap", QEMU_CAPS_NBD_BITMAP }, }; typedef struct _virQEMUCapsObjectTypeProps virQEMUCapsObjectTypeProps; diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index 39a228d977..1aa89809a3 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -417,7 +417,7 @@ qemuMigrationDstStartNBDServer(virQEMUDriverPtr driver, goto exit_monitor; } - if (qemuMonitorNBDServerAdd(priv->mon, diskAlias, NULL, true) < 0) + if (qemuMonitorNBDServerAdd(priv->mon, diskAlias, NULL, true, NULL) < 0) goto exit_monitor; if (qemuDomainObjExitMonitor(driver, vm) < 0) goto cleanup; diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 296563ce1c..906ed50466 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -2342,6 +2342,17 @@ qemuMonitorBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon, return qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(mon, stats); } +/* Updates "chk" to fill in size of the associated bitmap */ +int qemuMonitorUpdateCheckpointSize(qemuMonitorPtr mon, + virDomainCheckpointDefPtr chk) +{ + VIR_DEBUG("chk=%p", chk); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONUpdateCheckpointSize(mon, chk); +} + int qemuMonitorBlockResize(qemuMonitorPtr mon, const char *device, @@ -3946,13 +3957,16 @@ int qemuMonitorNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, const char *export, - bool writable) + bool writable, + const char *bitmap) { - VIR_DEBUG("deviceID=%s, export=%s", deviceID, NULLSTR(export)); + VIR_DEBUG("deviceID=%s, export=%s, bitmap=%s", deviceID, NULLSTR(export), + NULLSTR(bitmap)); QEMU_CHECK_MONITOR(mon); - return qemuMonitorJSONNBDServerAdd(mon, deviceID, export, writable); + return qemuMonitorJSONNBDServerAdd(mon, deviceID, export, writable, + bitmap); } @@ -4479,3 +4493,47 @@ qemuMonitorGetPRManagerInfo(qemuMonitorPtr mon, virHashFree(info); return ret; } + +int +qemuMonitorAddBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap, bool persistent) +{ + VIR_DEBUG("node=%s bitmap=%s persistent=%d", node, bitmap, persistent); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONAddBitmap(mon, node, bitmap, persistent); +} + +int +qemuMonitorEnableBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) +{ + VIR_DEBUG("node=%s bitmap=%s", node, bitmap); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONEnableBitmap(mon, node, bitmap); +} + +int +qemuMonitorMergeBitmaps(qemuMonitorPtr mon, const char *node, + const char *dst, virJSONValuePtr *src) +{ + VIR_DEBUG("node=%s dst=%s src=%p", node, dst, *src); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONMergeBitmaps(mon, node, dst, src); +} + +int +qemuMonitorDeleteBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) +{ + VIR_DEBUG("node=%s bitmap=%s", node, bitmap); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONDeleteBitmap(mon, node, bitmap); +} diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 37626b64ea..06c3945670 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -1011,6 +1011,8 @@ qemuMonitorJSONHandleBlockJobImpl(qemuMonitorPtr mon, type = VIR_DOMAIN_BLOCK_JOB_TYPE_COMMIT; else if (STREQ(type_str, "mirror")) type = VIR_DOMAIN_BLOCK_JOB_TYPE_COPY; + else if (STREQ(type_str, "backup")) + type = VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP; switch ((virConnectDomainEventBlockJobStatus) event) { case VIR_DOMAIN_BLOCK_JOB_COMPLETED: @@ -2755,6 +2757,82 @@ int qemuMonitorJSONBlockResize(qemuMonitorPtr mon, return ret; } +int qemuMonitorJSONUpdateCheckpointSize(qemuMonitorPtr mon, + virDomainCheckpointDefPtr chk) +{ + int ret = -1; + size_t i, j; + virJSONValuePtr devices; + + if (!(devices = qemuMonitorJSONQueryBlock(mon))) + return -1; + + for (i = 0; i < virJSONValueArraySize(devices); i++) { + virJSONValuePtr dev = virJSONValueArrayGet(devices, i); + virJSONValuePtr inserted; + virJSONValuePtr bitmaps = NULL; + const char *node; + virDomainCheckpointDiskDefPtr disk; + + if (!(dev = qemuMonitorJSONGetBlockDev(devices, i))) + goto cleanup; + + if (!(inserted = virJSONValueObjectGetObject(dev, "inserted"))) + continue; + if (!(node = virJSONValueObjectGetString(inserted, "node-name"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-block device entry was not in expected format")); + goto cleanup; + } + + for (j = 0; j < chk->ndisks; j++) { + disk = &chk->disks[j]; + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + if (STREQ(chk->dom->disks[disk->idx]->src->nodeformat, node)) + break; + } + if (j == chk->ndisks) { + VIR_DEBUG("query-block did not find node %s", node); + continue; + } + if (!(bitmaps = virJSONValueObjectGetArray(dev, "dirty-bitmaps"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("disk %s dirty bitmaps missing"), disk->name); + goto cleanup; + } + for (j = 0; j < virJSONValueArraySize(bitmaps); j++) { + virJSONValuePtr map = virJSONValueArrayGet(bitmaps, j); + const char *name; + + if (!(name = virJSONValueObjectGetString(map, "name"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("dirty bitmaps entry was not in expected format")); + goto cleanup; + } + if (STRNEQ(name, disk->bitmap)) + continue; + if (virJSONValueObjectGetNumberUlong(map, "count", &disk->size) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("invalid bitmap count")); + goto cleanup; + } + break; + } + if (j == virJSONValueArraySize(bitmaps)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("disk %s dirty bitmap info missing"), disk->name); + goto cleanup; + } + } + + ret = 0; + + cleanup: + virJSONValueFree(devices); + return ret; +} + int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon, const char *password) { @@ -6768,16 +6846,19 @@ int qemuMonitorJSONNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, const char *export, - bool writable) + bool writable, + const char *bitmap) { int ret = -1; virJSONValuePtr cmd; virJSONValuePtr reply = NULL; + /* Note: bitmap must be NULL if QEMU_CAPS_NBD_BITMAP is lacking */ if (!(cmd = qemuMonitorJSONMakeCommand("nbd-server-add", "s:device", deviceID, "S:name", export, "b:writable", writable, + "S:bitmap", bitmap, NULL))) return ret; @@ -8486,3 +8567,115 @@ qemuMonitorJSONGetPRManagerInfo(qemuMonitorPtr mon, return ret; } + +int +qemuMonitorJSONAddBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap, bool persistent) +{ + int ret = -1; + virJSONValuePtr cmd; + virJSONValuePtr reply = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("block-dirty-bitmap-add", + "s:node", node, + "s:name", bitmap, + "b:persistent", persistent, + NULL))) + return -1; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + ret = 0; + cleanup: + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} + +int +qemuMonitorJSONEnableBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) +{ + int ret = -1; + virJSONValuePtr cmd; + virJSONValuePtr reply = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("block-dirty-bitmap-enable", + "s:node", node, + "s:name", bitmap, + NULL))) + return -1; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + ret = 0; + cleanup: + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} + +int +qemuMonitorJSONMergeBitmaps(qemuMonitorPtr mon, const char *node, + const char *dst, virJSONValuePtr *src) +{ + int ret = -1; + virJSONValuePtr cmd; + virJSONValuePtr reply = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("block-dirty-bitmap-merge", + "s:node", node, + "s:target", dst, + "a:bitmaps", src, + NULL))) + goto cleanup; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + ret = 0; + cleanup: + virJSONValueFree(*src); + *src = NULL; + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} + +int +qemuMonitorJSONDeleteBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) +{ + int ret = -1; + virJSONValuePtr cmd; + virJSONValuePtr reply = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("block-dirty-bitmap-remove", + "s:node", node, + "s:name", bitmap, + NULL))) + return -1; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + ret = 0; + cleanup: + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} diff --git a/tests/qemucapabilitiesdata/caps_4.0.0.riscv32.xml b/tests/qemucapabilitiesdata/caps_4.0.0.riscv32.xml index 15e447742d..195b9132c6 100644 --- a/tests/qemucapabilitiesdata/caps_4.0.0.riscv32.xml +++ b/tests/qemucapabilitiesdata/caps_4.0.0.riscv32.xml @@ -167,6 +167,8 @@ <flag name='egl-headless.rendernode'/> <flag name='memory-backend-file.align'/> <flag name='memory-backend-file.pmem'/> + <flag name='bitmap-merge'/> + <flag name='nbd-bitmap'/> <version>3001050</version> <kvmVersion>0</kvmVersion> <microcodeVersion>0</microcodeVersion> diff --git a/tests/qemucapabilitiesdata/caps_4.0.0.riscv64.xml b/tests/qemucapabilitiesdata/caps_4.0.0.riscv64.xml index 066c892eaa..4263edbb55 100644 --- a/tests/qemucapabilitiesdata/caps_4.0.0.riscv64.xml +++ b/tests/qemucapabilitiesdata/caps_4.0.0.riscv64.xml @@ -167,6 +167,8 @@ <flag name='egl-headless.rendernode'/> <flag name='memory-backend-file.align'/> <flag name='memory-backend-file.pmem'/> + <flag name='bitmap-merge'/> + <flag name='nbd-bitmap'/> <version>3001050</version> <kvmVersion>0</kvmVersion> <microcodeVersion>0</microcodeVersion> diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index 1bdef11d15..c5eac06cc1 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -1354,7 +1354,7 @@ GEN_TEST_FUNC(qemuMonitorJSONDrivePivot, "vdb") GEN_TEST_FUNC(qemuMonitorJSONScreendump, "devicename", 1, "/foo/bar") GEN_TEST_FUNC(qemuMonitorJSONOpenGraphics, "spice", "spicefd", false) GEN_TEST_FUNC(qemuMonitorJSONNBDServerStart, "localhost", 12345, "test-alias") -GEN_TEST_FUNC(qemuMonitorJSONNBDServerAdd, "vda", NULL, true) +GEN_TEST_FUNC(qemuMonitorJSONNBDServerAdd, "vda", NULL, true, NULL) GEN_TEST_FUNC(qemuMonitorJSONDetachCharDev, "serial1") GEN_TEST_FUNC(qemuMonitorJSONBlockdevTrayOpen, "foodev", true) GEN_TEST_FUNC(qemuMonitorJSONBlockdevTrayClose, "foodev") -- 2.20.1

On 2/6/19 2:18 PM, Eric Blake wrote:
Add some monitor commands to be used during backup/checkpoint operations: - another facet to query-block for learning bitmap size - the new bitmap parameter to nbd-server-add-bitmap - new block-dirty-bitmap-{add,enable,disable,merge} functions
Also add two capabilities for testing that they are supported; using block-dirty-bitmap-merge as the generic witness of checkpoint support (since all of the functionalities were added in the same qemu 4.0 release), and the bitmap parameter to nbd-server-add for pull-mode backup support. Even though both capabilities are likely to be present or absent together (that is, it is unlikely to encounter a qemu that backports only one of the two), it still makes sense to keep two capabilities as the two uses are orthogonal (full backups don't require checkpoints, push mode backups don't require NBD bitmap support, and checkpoints can be used for more than just incremental backups).
Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_capabilities.h | 2 + src/qemu/qemu_monitor.h | 20 +- src/qemu/qemu_monitor_json.h | 18 +- src/qemu/qemu_capabilities.c | 4 + src/qemu/qemu_migration.c | 2 +- src/qemu/qemu_monitor.c | 64 +++++- src/qemu/qemu_monitor_json.c | 195 +++++++++++++++++- .../caps_4.0.0.riscv32.xml | 2 + .../caps_4.0.0.riscv64.xml | 2 + tests/qemumonitorjsontest.c | 2 +- 10 files changed, 303 insertions(+), 8 deletions(-)
The series starts to fail to apply for me here. Please just separate the capabilities for the future. That is all the qemu_capabilities in one patch. That way if top of tree is updated before applying the patch for review - it's really easy to regenerate and allow subsequent git am's to also work... My suggestions so far involve a fair amount of effort to extract stuff out, but I think we have to figure out a way to get some of this stuff pushed. I know it hasn't been the norm to push partial support, but I think as long as we don't declare support in new features we should be able add some stuff that may still undergo change, but as long as it's not "baked into" an API or able to be used in some way, then I would think we'd be OK. The longer you hold onto it, the more things change while you're working that cause you to have to go back. I am stopping here. I did not review patch 14. John
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 6d5ed8a3cc..3953e3ad3c 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -504,6 +504,8 @@ typedef enum { /* virQEMUCapsFlags grouping marker for syntax-check */ /* 325 */ QEMU_CAPS_OBJECT_MEMORY_FILE_PMEM, /* -object memory-backend-file,pmem= */ QEMU_CAPS_DEVICE_NVDIMM_UNARMED, /* -device nvdimm,unarmed= */ + QEMU_CAPS_BITMAP_MERGE, /* block-dirty-bitmap-merge */ + QEMU_CAPS_NBD_BITMAP, /* nbd-server-add supports bitmap */
QEMU_CAPS_LAST /* this must always be the last item */ } virQEMUCapsFlags; diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 55acd60380..7eb0d067b9 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -25,6 +25,7 @@ # include "internal.h"
# include "domain_conf.h" +# include "checkpoint_conf.h" # include "virbitmap.h" # include "virhash.h" # include "virjson.h" @@ -629,6 +630,9 @@ int qemuMonitorBlockStatsUpdateCapacity(qemuMonitorPtr mon, int qemuMonitorBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon, virHashTablePtr stats) ATTRIBUTE_NONNULL(2); +int qemuMonitorUpdateCheckpointSize(qemuMonitorPtr mon, + virDomainCheckpointDefPtr chk) + ATTRIBUTE_NONNULL(2);
int qemuMonitorBlockResize(qemuMonitorPtr mon, const char *device, @@ -647,6 +651,19 @@ int qemuMonitorSetBalloon(qemuMonitorPtr mon, unsigned long long newmem); int qemuMonitorSetCPU(qemuMonitorPtr mon, int cpu, bool online);
+int qemuMonitorAddBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap, bool persistent) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); +int qemuMonitorEnableBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); +int qemuMonitorMergeBitmaps(qemuMonitorPtr mon, const char *node, + const char *dst, virJSONValuePtr *src) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4); +int qemuMonitorDeleteBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) + ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3); +
/* XXX should we pass the virDomainDiskDefPtr instead * and hide dev_name details inside monitor. Reconsider @@ -1100,7 +1117,8 @@ int qemuMonitorNBDServerStart(qemuMonitorPtr mon, int qemuMonitorNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, const char *export, - bool writable); + bool writable, + const char *bitmap); int qemuMonitorNBDServerStop(qemuMonitorPtr); int qemuMonitorGetTPMModels(qemuMonitorPtr mon, char ***tpmmodels); diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index b105964ed6..076f7b6dde 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -98,6 +98,9 @@ int qemuMonitorJSONBlockResize(qemuMonitorPtr mon, const char *nodename, unsigned long long size);
+int qemuMonitorJSONUpdateCheckpointSize(qemuMonitorPtr mon, + virDomainCheckpointDefPtr chk); + int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon, const char *password); int qemuMonitorJSONSetPassword(qemuMonitorPtr mon, @@ -466,7 +469,8 @@ int qemuMonitorJSONNBDServerStart(qemuMonitorPtr mon, int qemuMonitorJSONNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, const char *export, - bool writable); + bool writable, + const char *bitmap); int qemuMonitorJSONNBDServerStop(qemuMonitorPtr mon); int qemuMonitorJSONGetTPMModels(qemuMonitorPtr mon, char ***tpmmodels) @@ -579,4 +583,16 @@ int qemuMonitorJSONGetPRManagerInfo(qemuMonitorPtr mon, virHashTablePtr info) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+int qemuMonitorJSONAddBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap, bool persistent); + +int qemuMonitorJSONEnableBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap); + +int qemuMonitorJSONMergeBitmaps(qemuMonitorPtr mon, const char *node, + const char *dst, virJSONValuePtr *src); + +int qemuMonitorJSONDeleteBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap); + #endif /* LIBVIRT_QEMU_MONITOR_JSON_H */ diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 81ef0357e7..cc4f197835 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -524,6 +524,8 @@ VIR_ENUM_IMPL(virQEMUCaps, QEMU_CAPS_LAST, /* 325 */ "memory-backend-file.pmem", "nvdimm.unarmed", + "bitmap-merge", + "nbd-bitmap", );
@@ -979,6 +981,7 @@ struct virQEMUCapsStringFlags virQEMUCapsCommands[] = { { "query-cpus-fast", QEMU_CAPS_QUERY_CPUS_FAST }, { "qom-list-properties", QEMU_CAPS_QOM_LIST_PROPERTIES }, { "blockdev-del", QEMU_CAPS_BLOCKDEV_DEL }, + { "block-dirty-bitmap-merge", QEMU_CAPS_BITMAP_MERGE }, };
struct virQEMUCapsStringFlags virQEMUCapsMigration[] = { @@ -1257,6 +1260,7 @@ static struct virQEMUCapsStringFlags virQEMUCapsQMPSchemaQueries[] = { { "block-commit/arg-type/*top", QEMU_CAPS_ACTIVE_COMMIT }, { "query-iothreads/ret-type/poll-max-ns", QEMU_CAPS_IOTHREAD_POLLING }, { "query-display-options/ret-type/+egl-headless/rendernode", QEMU_CAPS_EGL_HEADLESS_RENDERNODE }, + { "nbd-server-add/arg-type/bitmap", QEMU_CAPS_NBD_BITMAP }, };
typedef struct _virQEMUCapsObjectTypeProps virQEMUCapsObjectTypeProps; diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index 39a228d977..1aa89809a3 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -417,7 +417,7 @@ qemuMigrationDstStartNBDServer(virQEMUDriverPtr driver, goto exit_monitor; }
- if (qemuMonitorNBDServerAdd(priv->mon, diskAlias, NULL, true) < 0) + if (qemuMonitorNBDServerAdd(priv->mon, diskAlias, NULL, true, NULL) < 0) goto exit_monitor; if (qemuDomainObjExitMonitor(driver, vm) < 0) goto cleanup; diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 296563ce1c..906ed50466 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -2342,6 +2342,17 @@ qemuMonitorBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon, return qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(mon, stats); }
+/* Updates "chk" to fill in size of the associated bitmap */ +int qemuMonitorUpdateCheckpointSize(qemuMonitorPtr mon, + virDomainCheckpointDefPtr chk) +{ + VIR_DEBUG("chk=%p", chk); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONUpdateCheckpointSize(mon, chk); +} + int qemuMonitorBlockResize(qemuMonitorPtr mon, const char *device, @@ -3946,13 +3957,16 @@ int qemuMonitorNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, const char *export, - bool writable) + bool writable, + const char *bitmap) { - VIR_DEBUG("deviceID=%s, export=%s", deviceID, NULLSTR(export)); + VIR_DEBUG("deviceID=%s, export=%s, bitmap=%s", deviceID, NULLSTR(export), + NULLSTR(bitmap));
QEMU_CHECK_MONITOR(mon);
- return qemuMonitorJSONNBDServerAdd(mon, deviceID, export, writable); + return qemuMonitorJSONNBDServerAdd(mon, deviceID, export, writable, + bitmap); }
@@ -4479,3 +4493,47 @@ qemuMonitorGetPRManagerInfo(qemuMonitorPtr mon, virHashFree(info); return ret; } + +int +qemuMonitorAddBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap, bool persistent) +{ + VIR_DEBUG("node=%s bitmap=%s persistent=%d", node, bitmap, persistent); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONAddBitmap(mon, node, bitmap, persistent); +} + +int +qemuMonitorEnableBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) +{ + VIR_DEBUG("node=%s bitmap=%s", node, bitmap); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONEnableBitmap(mon, node, bitmap); +} + +int +qemuMonitorMergeBitmaps(qemuMonitorPtr mon, const char *node, + const char *dst, virJSONValuePtr *src) +{ + VIR_DEBUG("node=%s dst=%s src=%p", node, dst, *src); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONMergeBitmaps(mon, node, dst, src); +} + +int +qemuMonitorDeleteBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) +{ + VIR_DEBUG("node=%s bitmap=%s", node, bitmap); + + QEMU_CHECK_MONITOR(mon); + + return qemuMonitorJSONDeleteBitmap(mon, node, bitmap); +} diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 37626b64ea..06c3945670 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -1011,6 +1011,8 @@ qemuMonitorJSONHandleBlockJobImpl(qemuMonitorPtr mon, type = VIR_DOMAIN_BLOCK_JOB_TYPE_COMMIT; else if (STREQ(type_str, "mirror")) type = VIR_DOMAIN_BLOCK_JOB_TYPE_COPY; + else if (STREQ(type_str, "backup")) + type = VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP;
switch ((virConnectDomainEventBlockJobStatus) event) { case VIR_DOMAIN_BLOCK_JOB_COMPLETED: @@ -2755,6 +2757,82 @@ int qemuMonitorJSONBlockResize(qemuMonitorPtr mon, return ret; }
+int qemuMonitorJSONUpdateCheckpointSize(qemuMonitorPtr mon, + virDomainCheckpointDefPtr chk) +{ + int ret = -1; + size_t i, j; + virJSONValuePtr devices; + + if (!(devices = qemuMonitorJSONQueryBlock(mon))) + return -1; + + for (i = 0; i < virJSONValueArraySize(devices); i++) { + virJSONValuePtr dev = virJSONValueArrayGet(devices, i); + virJSONValuePtr inserted; + virJSONValuePtr bitmaps = NULL; + const char *node; + virDomainCheckpointDiskDefPtr disk; + + if (!(dev = qemuMonitorJSONGetBlockDev(devices, i))) + goto cleanup; + + if (!(inserted = virJSONValueObjectGetObject(dev, "inserted"))) + continue; + if (!(node = virJSONValueObjectGetString(inserted, "node-name"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("query-block device entry was not in expected format")); + goto cleanup; + } + + for (j = 0; j < chk->ndisks; j++) { + disk = &chk->disks[j]; + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + if (STREQ(chk->dom->disks[disk->idx]->src->nodeformat, node)) + break; + } + if (j == chk->ndisks) { + VIR_DEBUG("query-block did not find node %s", node); + continue; + } + if (!(bitmaps = virJSONValueObjectGetArray(dev, "dirty-bitmaps"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("disk %s dirty bitmaps missing"), disk->name); + goto cleanup; + } + for (j = 0; j < virJSONValueArraySize(bitmaps); j++) { + virJSONValuePtr map = virJSONValueArrayGet(bitmaps, j); + const char *name; + + if (!(name = virJSONValueObjectGetString(map, "name"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("dirty bitmaps entry was not in expected format")); + goto cleanup; + } + if (STRNEQ(name, disk->bitmap)) + continue; + if (virJSONValueObjectGetNumberUlong(map, "count", &disk->size) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("invalid bitmap count")); + goto cleanup; + } + break; + } + if (j == virJSONValueArraySize(bitmaps)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("disk %s dirty bitmap info missing"), disk->name); + goto cleanup; + } + } + + ret = 0; + + cleanup: + virJSONValueFree(devices); + return ret; +} + int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon, const char *password) { @@ -6768,16 +6846,19 @@ int qemuMonitorJSONNBDServerAdd(qemuMonitorPtr mon, const char *deviceID, const char *export, - bool writable) + bool writable, + const char *bitmap) { int ret = -1; virJSONValuePtr cmd; virJSONValuePtr reply = NULL;
+ /* Note: bitmap must be NULL if QEMU_CAPS_NBD_BITMAP is lacking */ if (!(cmd = qemuMonitorJSONMakeCommand("nbd-server-add", "s:device", deviceID, "S:name", export, "b:writable", writable, + "S:bitmap", bitmap, NULL))) return ret;
@@ -8486,3 +8567,115 @@ qemuMonitorJSONGetPRManagerInfo(qemuMonitorPtr mon, return ret;
} + +int +qemuMonitorJSONAddBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap, bool persistent) +{ + int ret = -1; + virJSONValuePtr cmd; + virJSONValuePtr reply = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("block-dirty-bitmap-add", + "s:node", node, + "s:name", bitmap, + "b:persistent", persistent, + NULL))) + return -1; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + ret = 0; + cleanup: + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} + +int +qemuMonitorJSONEnableBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) +{ + int ret = -1; + virJSONValuePtr cmd; + virJSONValuePtr reply = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("block-dirty-bitmap-enable", + "s:node", node, + "s:name", bitmap, + NULL))) + return -1; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + ret = 0; + cleanup: + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} + +int +qemuMonitorJSONMergeBitmaps(qemuMonitorPtr mon, const char *node, + const char *dst, virJSONValuePtr *src) +{ + int ret = -1; + virJSONValuePtr cmd; + virJSONValuePtr reply = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("block-dirty-bitmap-merge", + "s:node", node, + "s:target", dst, + "a:bitmaps", src, + NULL))) + goto cleanup; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + ret = 0; + cleanup: + virJSONValueFree(*src); + *src = NULL; + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} + +int +qemuMonitorJSONDeleteBitmap(qemuMonitorPtr mon, const char *node, + const char *bitmap) +{ + int ret = -1; + virJSONValuePtr cmd; + virJSONValuePtr reply = NULL; + + if (!(cmd = qemuMonitorJSONMakeCommand("block-dirty-bitmap-remove", + "s:node", node, + "s:name", bitmap, + NULL))) + return -1; + + if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + goto cleanup; + + if (qemuMonitorJSONCheckError(cmd, reply) < 0) + goto cleanup; + + ret = 0; + cleanup: + virJSONValueFree(cmd); + virJSONValueFree(reply); + return ret; +} diff --git a/tests/qemucapabilitiesdata/caps_4.0.0.riscv32.xml b/tests/qemucapabilitiesdata/caps_4.0.0.riscv32.xml index 15e447742d..195b9132c6 100644 --- a/tests/qemucapabilitiesdata/caps_4.0.0.riscv32.xml +++ b/tests/qemucapabilitiesdata/caps_4.0.0.riscv32.xml @@ -167,6 +167,8 @@ <flag name='egl-headless.rendernode'/> <flag name='memory-backend-file.align'/> <flag name='memory-backend-file.pmem'/> + <flag name='bitmap-merge'/> + <flag name='nbd-bitmap'/> <version>3001050</version> <kvmVersion>0</kvmVersion> <microcodeVersion>0</microcodeVersion> diff --git a/tests/qemucapabilitiesdata/caps_4.0.0.riscv64.xml b/tests/qemucapabilitiesdata/caps_4.0.0.riscv64.xml index 066c892eaa..4263edbb55 100644 --- a/tests/qemucapabilitiesdata/caps_4.0.0.riscv64.xml +++ b/tests/qemucapabilitiesdata/caps_4.0.0.riscv64.xml @@ -167,6 +167,8 @@ <flag name='egl-headless.rendernode'/> <flag name='memory-backend-file.align'/> <flag name='memory-backend-file.pmem'/> + <flag name='bitmap-merge'/> + <flag name='nbd-bitmap'/> <version>3001050</version> <kvmVersion>0</kvmVersion> <microcodeVersion>0</microcodeVersion> diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index 1bdef11d15..c5eac06cc1 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -1354,7 +1354,7 @@ GEN_TEST_FUNC(qemuMonitorJSONDrivePivot, "vdb") GEN_TEST_FUNC(qemuMonitorJSONScreendump, "devicename", 1, "/foo/bar") GEN_TEST_FUNC(qemuMonitorJSONOpenGraphics, "spice", "spicefd", false) GEN_TEST_FUNC(qemuMonitorJSONNBDServerStart, "localhost", 12345, "test-alias") -GEN_TEST_FUNC(qemuMonitorJSONNBDServerAdd, "vda", NULL, true) +GEN_TEST_FUNC(qemuMonitorJSONNBDServerAdd, "vda", NULL, true, NULL) GEN_TEST_FUNC(qemuMonitorJSONDetachCharDev, "serial1") GEN_TEST_FUNC(qemuMonitorJSONBlockdevTrayOpen, "foodev", true) GEN_TEST_FUNC(qemuMonitorJSONBlockdevTrayClose, "foodev")

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_block.h | 3 + src/qemu/qemu_conf.h | 2 + src/qemu/qemu_domain.h | 25 +- src/qemu/qemu_block.c | 12 + src/qemu/qemu_conf.c | 5 + src/qemu/qemu_domain.c | 132 +++++- src/qemu/qemu_driver.c | 881 ++++++++++++++++++++++++++++++++++++++++- 7 files changed, 1051 insertions(+), 9 deletions(-) diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h index 9401ab4e12..fbcd019d5c 100644 --- a/src/qemu/qemu_block.h +++ b/src/qemu/qemu_block.h @@ -125,4 +125,7 @@ qemuBlockSnapshotAddLegacy(virJSONValuePtr actions, virStorageSourcePtr newsrc, bool reuse); +const char * +qemuBlockNodeLookup(virDomainObjPtr vm, const char *disk); + #endif /* LIBVIRT_QEMU_BLOCK_H */ diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index 14c9d15a72..a94c229526 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -29,6 +29,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" @@ -110,6 +111,7 @@ struct _virQEMUDriverConfig { char *cacheDir; char *saveDir; char *snapshotDir; + char *checkpointDir; char *channelTargetDir; char *nvramDir; char *swtpmStorageDir; diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index fe474170dc..fed271d22d 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -1,7 +1,7 @@ /* * qemu_domain.h: QEMU domain private state * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2018 Red Hat, Inc. * Copyright (C) 2006 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -697,9 +697,10 @@ int qemuDomainSnapshotDiscard(virQEMUDriverPtr driver, bool update_current, bool metadata_only); -typedef struct _virQEMUSnapRemove virQEMUSnapRemove; -typedef virQEMUSnapRemove *virQEMUSnapRemovePtr; -struct _virQEMUSnapRemove { +/* Used for both snapshots and checkpoints */ +typedef struct _virQEMUDependentRemove virQEMUDependentRemove; +typedef virQEMUDependentRemove *virQEMUDependentRemovePtr; +struct _virQEMUDependentRemove { virQEMUDriverPtr driver; virDomainObjPtr vm; int err; @@ -714,6 +715,22 @@ int qemuDomainSnapshotDiscardAll(void *payload, int qemuDomainSnapshotDiscardAllMetadata(virQEMUDriverPtr driver, virDomainObjPtr vm); +int qemuDomainCheckpointWriteMetadata(virDomainObjPtr vm, + virDomainCheckpointObjPtr checkpoint, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + char *checkpointDir); + +int qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainCheckpointObjPtr chk, + bool update_current, + bool metadata_only); + +int qemuDomainCheckpointDiscardAll(void *payload, + const void *name, + void *data); + void qemuDomainRemoveInactive(virQEMUDriverPtr driver, virDomainObjPtr vm); diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index cbf0aa4189..df04abeb07 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -1767,3 +1767,15 @@ qemuBlockStorageGetCopyOnReadProps(virDomainDiskDefPtr disk) return ret; } + +const char * +qemuBlockNodeLookup(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; +} diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 2f5ef8d0c4..cf1c9c9daf 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -179,6 +179,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, @@ -242,6 +244,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, @@ -354,6 +358,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_domain.c b/src/qemu/qemu_domain.c index b6c1a0e4e5..6a6266a05b 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -1,7 +1,7 @@ /* * qemu_domain.c: QEMU domain private state * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2018 Red Hat, Inc. * Copyright (C) 2006 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -8424,6 +8424,45 @@ qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm, return ret; } +int +qemuDomainCheckpointWriteMetadata(virDomainObjPtr vm, + virDomainCheckpointObjPtr checkpoint, + virCapsPtr caps, + virDomainXMLOptionPtr xmlopt, + char *checkpointDir) +{ + char *newxml = NULL; + int ret = -1; + char *chkDir = NULL; + char *chkFile = NULL; + + newxml = virDomainCheckpointDefFormat( + checkpoint->def, caps, xmlopt, + virDomainDefFormatConvertXMLFlags(QEMU_DOMAIN_FORMAT_LIVE_FLAGS), + 1); + if (newxml == NULL) + return -1; + + if (virAsprintf(&chkDir, "%s/%s", checkpointDir, vm->def->name) < 0) + goto cleanup; + if (virFileMakePath(chkDir) < 0) { + virReportSystemError(errno, _("cannot create checkpoint directory '%s'"), + chkDir); + goto cleanup; + } + + if (virAsprintf(&chkFile, "%s/%s.xml", chkDir, checkpoint->def->name) < 0) + goto cleanup; + + ret = virXMLSaveFile(chkFile, NULL, "checkpoint-edit", newxml); + + cleanup: + VIR_FREE(chkFile); + VIR_FREE(chkDir); + VIR_FREE(newxml); + return ret; +} + /* 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 @@ -8591,7 +8630,7 @@ int qemuDomainSnapshotDiscardAll(void *payload, void *data) { virDomainSnapshotObjPtr snap = payload; - virQEMUSnapRemovePtr curr = data; + virQEMUDependentRemovePtr curr = data; int err; if (snap->def->current) @@ -8607,7 +8646,7 @@ int qemuDomainSnapshotDiscardAllMetadata(virQEMUDriverPtr driver, virDomainObjPtr vm) { - virQEMUSnapRemove rem; + virQEMUDependentRemove rem; rem.driver = driver; rem.vm = vm; @@ -8620,6 +8659,93 @@ qemuDomainSnapshotDiscardAllMetadata(virQEMUDriverPtr driver, } +/* Discard one checkpoint (or its metadata), without reparenting any children. */ +int +qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainCheckpointObjPtr chk, + bool update_parent, + bool metadata_only) +{ + char *chkFile = NULL; + int ret = -1; + virDomainCheckpointObjPtr 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 == vm->current_checkpoint) { + if (update_parent && chk->def->parent) { + parentchk = virDomainCheckpointFindByName(vm->checkpoints, + chk->def->parent); + if (!parentchk) { + VIR_WARN("missing parent checkpoint matching name '%s'", + chk->def->parent); + } else { + parentchk->def->current = true; + if (qemuDomainCheckpointWriteMetadata(vm, parentchk, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) { + VIR_WARN("failed to set parent checkpoint '%s' as current", + chk->def->parent); + parentchk->def->current = false; + parentchk = NULL; + } + } + } + vm->current_checkpoint = parentchk; + } + + if (unlink(chkFile) < 0) + VIR_WARN("Failed to unlink %s", chkFile); + if (update_parent) + virDomainCheckpointDropParent(chk); + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + + ret = 0; + + cleanup: + VIR_FREE(chkFile); + virObjectUnref(cfg); + return ret; +} + +/* Hash iterator callback to discard multiple checkpoints. */ +int qemuDomainCheckpointDiscardAll(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *data) +{ + virDomainCheckpointObjPtr chk = payload; + virQEMUDependentRemovePtr curr = data; + int err; + + if (chk->def->current) + curr->current = true; + err = qemuDomainCheckpointDiscard(curr->driver, curr->vm, chk, false, + curr->metadata_only); + if (err && !curr->err) + curr->err = err; + return 0; +} + static void qemuDomainRemoveInactiveCommon(virQEMUDriverPtr driver, virDomainObjPtr vm) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 2d39e9de96..bc5ced5ab2 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -1,7 +1,7 @@ /* * qemu_driver.c: core driver methods for managing qemu guests * - * Copyright (C) 2006-2016 Red Hat, Inc. + * Copyright (C) 2006-2019 Red Hat, Inc. * Copyright (C) 2006 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -219,6 +219,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 virDomainCheckpointObjPtr +qemuCheckObjFromName(virDomainObjPtr vm, + const char *name) +{ + virDomainCheckpointObjPtr 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 virDomainCheckpointObjPtr +qemuCheckObjFromCheckpoint(virDomainObjPtr vm, + virDomainCheckpointPtr checkpoint) +{ + return qemuCheckObjFromName(vm, checkpoint->name); +} + static int qemuAutostartDomain(virDomainObjPtr vm, void *opaque) @@ -526,6 +560,127 @@ 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; + virDomainCheckpointObjPtr chk = NULL; + virDomainCheckpointObjPtr current = NULL; + unsigned int flags = (VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE | + VIR_DOMAIN_CHECKPOINT_PARSE_DISKS | + VIR_DOMAIN_CHECKPOINT_PARSE_INTERNAL); + 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); + continue; + } + + chk = virDomainCheckpointAssignDef(vm->checkpoints, def); + if (chk == NULL) { + virDomainCheckpointDefFree(def); + } else if (chk->def->current) { + current = chk; + if (!vm->current_checkpoint) + vm->current_checkpoint = chk; + } + + VIR_FREE(fullpath); + VIR_FREE(xmlStr); + } + if (direrr < 0) + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to fully read directory %s"), + chkDir); + + if (vm->current_checkpoint != current) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Too many checkpoints claiming to be current for domain %s"), + vm->def->name); + vm->current_checkpoint = NULL; + } + + if (virDomainCheckpointUpdateRelations(vm->checkpoints) < 0) + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Checkpoints have inconsistent relations for domain %s"), + vm->def->name); + + /* FIXME: qemu keeps internal track of bitmaps, which form the + * basis for checkpoints; it would be nice if we could update our + * internal state to reflect that information automatically. But + * qemu 3.0 did not have access to this via qemu-img for offline + * images (you have to use QMP commands on a running guest), and + * it also does not track <parent> relations which we find + * important in our metadata. + */ + + 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) @@ -656,6 +811,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); @@ -763,6 +923,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"), @@ -901,6 +1068,10 @@ qemuStateInitialize(bool privileged, qemuDomainSnapshotLoad, cfg->snapshotDir); + virDomainObjListForEach(qemu_driver->domains, + qemuDomainCheckpointLoad, + cfg->checkpointDir); + virDomainObjListForEach(qemu_driver->domains, qemuDomainManagedSaveLoad, qemu_driver); @@ -7804,6 +7975,7 @@ qemuDomainUndefineFlags(virDomainPtr dom, if (qemuDomainSnapshotDiscardAllMetadata(driver, vm) < 0) goto endjob; } + /* TODO: Restrict deletion if checkpoints exist? */ name = qemuDomainManagedSavePath(driver, vm); if (name == NULL) @@ -16726,7 +16898,7 @@ qemuDomainSnapshotDelete(virDomainSnapshotPtr snapshot, virDomainObjPtr vm = NULL; int ret = -1; virDomainSnapshotObjPtr snap = NULL; - virQEMUSnapRemove rem; + virQEMUDependentRemove rem; virQEMUSnapReparent rep; bool metadata_only = !!(flags & VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY); int external = 0; @@ -16830,6 +17002,693 @@ qemuDomainSnapshotDelete(virDomainSnapshotPtr snapshot, return ret; } + +/* Called prior to job lock */ +static virDomainCheckpointDefPtr +qemuDomainCheckpointDefParseString(virQEMUDriverPtr driver, virCapsPtr caps, + const char *xmlDesc, unsigned int flags) +{ + virDomainCheckpointDefPtr def = NULL; + virDomainCheckpointDefPtr ret = NULL; + unsigned int parse_flags = VIR_DOMAIN_CHECKPOINT_PARSE_DISKS; + + if (flags & VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE) + parse_flags |= VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; + if (!(def = virDomainCheckpointDefParseString(xmlDesc, caps, driver->xmlopt, + parse_flags))) + goto cleanup; + + /* reject checkpoint names containing slashes or starting with dot as + * checkpoint definitions are saved in files named by the checkpoint name */ + if (!(flags & VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA)) { + if (strchr(def->name, '/')) { + virReportError(VIR_ERR_XML_DETAIL, + _("invalid checkpoint name '%s': " + "name can't contain '/'"), + def->name); + goto cleanup; + } + + if (def->name[0] == '.') { + virReportError(VIR_ERR_XML_DETAIL, + _("invalid checkpoint name '%s': " + "name can't start with '.'"), + def->name); + goto cleanup; + } + } + + VIR_STEAL_PTR(ret, def); + + cleanup: + virDomainCheckpointDefFree(def); + 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->dom = virDomainDefParseString(xml, caps, driver->xmlopt, NULL, + VIR_DOMAIN_DEF_PARSE_INACTIVE | + VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE))) + goto cleanup; + + if (virDomainCheckpointAlignDisks(def) < 0 || + qemuBlockNodeNamesDetect(driver, vm, QEMU_ASYNC_JOB_NONE) < 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 > 0 && + 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; + virDomainCheckpointObjPtr chk = NULL; + virDomainCheckpointPtr checkpoint = NULL; + virDomainCheckpointDefPtr def = NULL; + bool update_current = true; + bool redefine = flags & VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE; + virDomainCheckpointObjPtr other = NULL; + virQEMUDriverConfigPtr cfg = NULL; + virCapsPtr caps = NULL; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE | + VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT | + VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA, NULL); + /* TODO: VIR_DOMAIN_CHECKPOINT_CREATE_QUIESCE */ + + if ((redefine && !(flags & VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT)) || + (flags & VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA)) + update_current = false; + + if (!(vm = qemuDomObjFromDomain(domain))) + goto cleanup; + + cfg = virQEMUDriverGetConfig(driver); + + if (virDomainCheckpointCreateXMLEnsureACL(domain->conn, vm->def) < 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 = qemuDomainCheckpointDefParseString(driver, caps, xmlDesc, + flags))) + goto cleanup; + + /* 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; + } + + if (update_current) + chk->def->current = true; + if (vm->current_checkpoint) { + if (!redefine && + VIR_STRDUP(chk->def->parent, vm->current_checkpoint->def->name) < 0) + goto endjob; + if (update_current) { + vm->current_checkpoint->def->current = false; + if (qemuDomainCheckpointWriteMetadata(vm, vm->current_checkpoint, + driver->caps, driver->xmlopt, + cfg->checkpointDir) < 0) + goto endjob; + vm->current_checkpoint = NULL; + } + } + + /* 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 && !(flags & VIR_DOMAIN_CHECKPOINT_CREATE_NO_METADATA)) { + 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 { + if (update_current) + vm->current_checkpoint = chk; + other = virDomainCheckpointFindByName(vm->checkpoints, + chk->def->parent); + chk->parent = other; + other->nchildren++; + chk->sibling = other->first_child; + other->first_child = chk; + } + } else if (chk) { + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + } + + qemuDomainObjEndJob(driver, vm); + + cleanup: + virDomainObjEndAPI(&vm); + virDomainCheckpointDefFree(def); + VIR_FREE(xml); + virObjectUnref(caps); + virObjectUnref(cfg); + return checkpoint; +} + + +static int +qemuDomainListCheckpoints(virDomainPtr domain, + virDomainCheckpointPtr **chks, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + int n = -1; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_LIST_ROOTS | + VIR_DOMAIN_CHECKPOINT_FILTERS_ALL, -1); + + if (!(vm = qemuDomObjFromDomain(domain))) + return -1; + + if (virDomainListCheckpointsEnsureACL(domain->conn, vm->def) < 0) + goto cleanup; + + n = virDomainListAllCheckpoints(vm->checkpoints, NULL, domain, chks, flags); + + cleanup: + virDomainObjEndAPI(&vm); + return n; +} + + +static int +qemuDomainCheckpointListChildren(virDomainCheckpointPtr checkpoint, + virDomainCheckpointPtr **chks, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + virDomainCheckpointObjPtr chk = NULL; + int n = -1; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_LIST_DESCENDANTS | + VIR_DOMAIN_CHECKPOINT_FILTERS_ALL, -1); + + if (!(vm = qemuDomObjFromCheckpoint(checkpoint))) + return -1; + + if (virDomainCheckpointListChildrenEnsureACL(checkpoint->domain->conn, vm->def) < 0) + goto cleanup; + + if (!(chk = qemuCheckObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + n = virDomainListAllCheckpoints(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; + virDomainCheckpointObjPtr 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 = qemuCheckObjFromName(vm, name))) + goto cleanup; + + checkpoint = virGetDomainCheckpoint(domain, chk->def->name); + + cleanup: + virDomainObjEndAPI(&vm); + return checkpoint; +} + + +static int +qemuDomainHasCurrentCheckpoint(virDomainPtr domain, + unsigned int flags) +{ + virDomainObjPtr vm; + int ret = -1; + + virCheckFlags(0, -1); + + if (!(vm = qemuDomObjFromDomain(domain))) + return -1; + + if (virDomainHasCurrentCheckpointEnsureACL(domain->conn, vm->def) < 0) + goto cleanup; + + ret = (vm->current_checkpoint != NULL); + + cleanup: + virDomainObjEndAPI(&vm); + return ret; +} + + +static virDomainCheckpointPtr +qemuDomainCheckpointGetParent(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virDomainObjPtr vm; + virDomainCheckpointObjPtr 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 = qemuCheckObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + if (!chk->def->parent) { + 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); + + cleanup: + virDomainObjEndAPI(&vm); + return parent; +} + + +static virDomainCheckpointPtr +qemuDomainCheckpointCurrent(virDomainPtr domain, + unsigned int flags) +{ + virDomainObjPtr vm; + virDomainCheckpointPtr checkpoint = NULL; + + virCheckFlags(0, NULL); + + if (!(vm = qemuDomObjFromDomain(domain))) + return NULL; + + if (virDomainCheckpointCurrentEnsureACL(domain->conn, vm->def) < 0) + goto cleanup; + + if (!vm->current_checkpoint) { + virReportError(VIR_ERR_NO_DOMAIN_CHECKPOINT, "%s", + _("the domain does not have a current checkpoint")); + goto cleanup; + } + + checkpoint = virGetDomainCheckpoint(domain, vm->current_checkpoint->def->name); + + cleanup: + virDomainObjEndAPI(&vm); + return checkpoint; +} + + +static char * +qemuDomainCheckpointGetXMLDesc(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virQEMUDriverPtr driver = checkpoint->domain->conn->privateData; + virDomainObjPtr vm = NULL; + char *xml = NULL; + virDomainCheckpointObjPtr chk = NULL; + qemuDomainObjPrivatePtr priv; + int rc; + size_t i; + + virCheckFlags(VIR_DOMAIN_CHECKPOINT_XML_SECURE | + VIR_DOMAIN_CHECKPOINT_XML_NO_DOMAIN | + VIR_DOMAIN_CHECKPOINT_XML_SIZE, NULL); + + if (!(vm = qemuDomObjFromCheckpoint(checkpoint))) + return NULL; + + if (virDomainCheckpointGetXMLDescEnsureACL(checkpoint->domain->conn, vm->def, flags) < 0) + goto cleanup; + + if (!(chk = qemuCheckObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SIZE) { + /* TODO: for non-current checkpoint, this requires a QMP sequence per + disk, since the stat of one bitmap in isolation is too low, + and merely adding bitmap sizes may be too high: + block-dirty-bitmap-create tmp + for each bitmap from checkpoint to current: + add bitmap to src_list + block-dirty-bitmap-merge dst=tmp src_list + query-block and read tmp size + block-dirty-bitmap-remove tmp + So for now, go with simpler query-blocks only for current. + */ + if (!vm->current_checkpoint || + STRNEQ(checkpoint->name, vm->current_checkpoint->def->name)) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("cannot compute size for non-current checkpoint '%s'"), + checkpoint->name); + goto cleanup; + } + + if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_QUERY) < 0) + goto cleanup; + + if (virDomainObjCheckActive(vm) < 0) + goto endjob; + + if (qemuBlockNodeNamesDetect(driver, vm, QEMU_ASYNC_JOB_NONE) < 0) + goto endjob; + + /* TODO: Shouldn't need to recompute node names. */ + for (i = 0; i < chk->def->ndisks; i++) { + virDomainCheckpointDiskDef *disk = &chk->def->disks[i]; + + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + VIR_FREE(chk->def->dom->disks[disk->idx]->src->nodeformat); + if (VIR_STRDUP(chk->def->dom->disks[disk->idx]->src->nodeformat, + qemuBlockNodeLookup(vm, disk->name)) < 0) + goto endjob; + } + + priv = vm->privateData; + qemuDomainObjEnterMonitor(driver, vm); + rc = qemuMonitorUpdateCheckpointSize(priv->mon, chk->def); + if (qemuDomainObjExitMonitor(driver, vm) < 0) + goto endjob; + if (rc < 0) + goto endjob; + } + + xml = virDomainCheckpointDefFormat(chk->def, driver->caps, driver->xmlopt, + flags, 0); + + endjob: + if (flags & VIR_DOMAIN_CHECKPOINT_XML_SIZE) + qemuDomainObjEndJob(driver, vm); + + cleanup: + virDomainObjEndAPI(&vm); + return xml; +} + + +static int +qemuDomainCheckpointIsCurrent(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + int ret = -1; + virDomainCheckpointObjPtr chk = NULL; + + virCheckFlags(0, -1); + + if (!(vm = qemuDomObjFromCheckpoint(checkpoint))) + return -1; + + if (virDomainCheckpointIsCurrentEnsureACL(checkpoint->domain->conn, vm->def) < 0) + goto cleanup; + + if (!(chk = qemuCheckObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + ret = (vm->current_checkpoint && + STREQ(checkpoint->name, vm->current_checkpoint->def->name)); + + cleanup: + virDomainObjEndAPI(&vm); + return ret; +} + + +static int +qemuDomainCheckpointHasMetadata(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + int ret = -1; + virDomainCheckpointObjPtr chk = NULL; + + virCheckFlags(0, -1); + + if (!(vm = qemuDomObjFromCheckpoint(checkpoint))) + return -1; + + if (virDomainCheckpointHasMetadataEnsureACL(checkpoint->domain->conn, vm->def) < 0) + goto cleanup; + + if (!(chk = qemuCheckObjFromCheckpoint(vm, checkpoint))) + goto cleanup; + + /* XXX Someday, we should recognize internal bitmaps in qcow2 + * images that are not tied to a libvirt checkpoint; if we ever do + * that, then we would have a reason to return 0 here. */ + ret = 1; + + cleanup: + virDomainObjEndAPI(&vm); + return ret; +} + + +typedef struct _virQEMUCheckReparent virQEMUCheckReparent; +typedef virQEMUCheckReparent *virQEMUCheckReparentPtr; +struct _virQEMUCheckReparent { + virQEMUDriverConfigPtr cfg; + virDomainCheckpointObjPtr parent; + virDomainObjPtr vm; + virCapsPtr caps; + virDomainXMLOptionPtr xmlopt; + int err; + virDomainCheckpointObjPtr last; +}; + + +static int +qemuDomainCheckpointReparentChildren(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *data) +{ + virDomainCheckpointObjPtr chk = payload; + virQEMUCheckReparentPtr rep = data; + + if (rep->err < 0) + return 0; + + VIR_FREE(chk->def->parent); + chk->parent = rep->parent; + + if (rep->parent->def && + VIR_STRDUP(chk->def->parent, rep->parent->def->name) < 0) { + rep->err = -1; + return 0; + } + + if (!chk->sibling) + rep->last = chk; + + rep->err = qemuDomainCheckpointWriteMetadata(rep->vm, chk, + rep->caps, rep->xmlopt, + rep->cfg->checkpointDir); + return 0; +} + + +static int +qemuDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, + unsigned int flags) +{ + virQEMUDriverPtr driver = checkpoint->domain->conn->privateData; + virDomainObjPtr vm = NULL; + int ret = -1; + virDomainCheckpointObjPtr chk = NULL; + virQEMUDependentRemove rem; + virQEMUCheckReparent 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 = qemuCheckObjFromCheckpoint(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 = false; + virDomainCheckpointForEachDescendant(chk, + qemuDomainCheckpointDiscardAll, + &rem); + if (rem.err < 0) + goto endjob; + if (rem.current) { + if (flags & VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY) { + chk->def->current = true; + 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); + chk->def->current = false; + goto endjob; + } + } + vm->current_checkpoint = chk; + } + } else if (chk->nchildren) { + rep.cfg = cfg; + rep.parent = chk->parent; + rep.vm = vm; + rep.err = 0; + rep.last = NULL; + rep.caps = driver->caps; + rep.xmlopt = driver->xmlopt; + virDomainCheckpointForEachChild(chk, + qemuDomainCheckpointReparentChildren, + &rep); + if (rep.err < 0) + goto endjob; + /* Can't modify siblings during ForEachChild, so do it now. */ + chk->parent->nchildren += chk->nchildren; + rep.last->sibling = chk->parent->first_child; + chk->parent->first_child = chk->first_child; + } + + if (flags & VIR_DOMAIN_CHECKPOINT_DELETE_CHILDREN_ONLY) { + chk->nchildren = 0; + chk->first_child = NULL; + 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) { @@ -21770,6 +22629,12 @@ static int qemuDomainRename(virDomainPtr dom, goto endjob; } + if (virDomainListAllCheckpoints(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; @@ -22590,6 +23455,18 @@ static virHypervisorDriver qemuHypervisorDriver = { .connectBaselineHypervisorCPU = qemuConnectBaselineHypervisorCPU, /* 4.4.0 */ .nodeGetSEVInfo = qemuNodeGetSEVInfo, /* 4.5.0 */ .domainGetLaunchSecurityInfo = qemuDomainGetLaunchSecurityInfo, /* 4.5.0 */ + .domainCheckpointCreateXML = qemuDomainCheckpointCreateXML, /* 5.1.0 */ + .domainCheckpointGetXMLDesc = qemuDomainCheckpointGetXMLDesc, /* 5.1.0 */ + + .domainListCheckpoints = qemuDomainListCheckpoints, /* 5.1.0 */ + .domainCheckpointListChildren = qemuDomainCheckpointListChildren, /* 5.1.0 */ + .domainCheckpointLookupByName = qemuDomainCheckpointLookupByName, /* 5.1.0 */ + .domainHasCurrentCheckpoint = qemuDomainHasCurrentCheckpoint, /* 5.1.0 */ + .domainCheckpointGetParent = qemuDomainCheckpointGetParent, /* 5.1.0 */ + .domainCheckpointCurrent = qemuDomainCheckpointCurrent, /* 5.1.0 */ + .domainCheckpointIsCurrent = qemuDomainCheckpointIsCurrent, /* 5.1.0 */ + .domainCheckpointHasMetadata = qemuDomainCheckpointHasMetadata, /* 5.1.0 */ + .domainCheckpointDelete = qemuDomainCheckpointDelete, /* 5.1.0 */ }; -- 2.20.1

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 parent checkpoint (if any). For deletion, we need multiple calls: if the checkpoint to be deleted is active, we must enable the parent; then we must merge the existing checkpoint into the parent, and finally we can delete the checkpoint. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.c | 106 +++++++++++++++++++++++++++++------------ src/qemu/qemu_driver.c | 84 +++++++++++++++++++++++++++++++- 2 files changed, 159 insertions(+), 31 deletions(-) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 6a6266a05b..43b6675341 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -1,7 +1,7 @@ /* * qemu_domain.c: QEMU domain private state * - * Copyright (C) 2006-2018 Red Hat, Inc. + * Copyright (C) 2006-2019 Red Hat, Inc. * Copyright (C) 2006 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -8671,44 +8671,89 @@ qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, int ret = -1; virDomainCheckpointObjPtr parentchk = NULL; virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver); + size_t i, j; + virJSONValuePtr arr = 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")); + goto cleanup; } if (virAsprintf(&chkFile, "%s/%s/%s.xml", cfg->checkpointDir, vm->def->name, chk->def->name) < 0) goto cleanup; + if (chk->def->parent) { + parentchk = virDomainCheckpointFindByName(vm->checkpoints, + chk->def->parent); + if (!parentchk) { + VIR_WARN("missing parent checkpoint matching name '%s'", + chk->def->parent); + } + } + + if (!metadata_only) { + qemuDomainObjPrivatePtr priv = vm->privateData; + bool success = true; + + if (qemuBlockNodeNamesDetect(driver, vm, QEMU_ASYNC_JOB_NONE) < 0) + goto cleanup; + qemuDomainObjEnterMonitor(driver, vm); + for (i = 0; i < chk->def->ndisks; i++) { + virDomainCheckpointDiskDef *disk = &chk->def->disks[i]; + const char *node; + + if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) + continue; + + node = qemuBlockNodeLookup(vm, disk->name); + if (parentchk) { + arr = virJSONValueNewArray(); + if (!arr || + virJSONValueArrayAppendString(arr, disk->bitmap) < 0) { + success = false; + break; + } + + for (j = 0; j < parentchk->def->ndisks; j++) { + virDomainCheckpointDiskDef *disk2; + + disk2 = &parentchk->def->disks[j]; + if (STRNEQ(disk->name, disk2->name)) + continue; + if (chk == vm->current_checkpoint && + 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) + goto cleanup; + } + if (chk == vm->current_checkpoint) { - if (update_parent && chk->def->parent) { - parentchk = virDomainCheckpointFindByName(vm->checkpoints, - chk->def->parent); - if (!parentchk) { - VIR_WARN("missing parent checkpoint matching name '%s'", + if (update_parent && parentchk) { + parentchk->def->current = true; + if (qemuDomainCheckpointWriteMetadata(vm, parentchk, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) { + VIR_WARN("failed to set parent checkpoint '%s' as current", chk->def->parent); - } else { - parentchk->def->current = true; - if (qemuDomainCheckpointWriteMetadata(vm, parentchk, driver->caps, - driver->xmlopt, - cfg->checkpointDir) < 0) { - VIR_WARN("failed to set parent checkpoint '%s' as current", - chk->def->parent); - parentchk->def->current = false; - parentchk = NULL; - } + parentchk->def->current = false; + parentchk = NULL; } } vm->current_checkpoint = parentchk; @@ -8725,6 +8770,7 @@ qemuDomainCheckpointDiscard(virQEMUDriverPtr driver, cleanup: VIR_FREE(chkFile); virObjectUnref(cfg); + virJSONValueFree(arr); return ret; } diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index bc5ced5ab2..e37c5442dd 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -48,6 +48,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" @@ -17095,6 +17096,51 @@ qemuDomainCheckpointPrepare(virQEMUDriverPtr driver, virCapsPtr caps, return ret; } +static int +qemuDomainCheckpointAddActions(virDomainObjPtr vm, + virJSONValuePtr actions, + virDomainCheckpointObjPtr old_current, + virDomainCheckpointDefPtr def) +{ + size_t i, j; + int ret = -1; + + 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 = qemuBlockNodeLookup(vm, disk->name); + if (qemuMonitorJSONTransactionAdd(actions, + "block-dirty-bitmap-add", + "s:node", node, + "s:name", disk->bitmap, + "b:persistent", true, + NULL) < 0) + goto cleanup; + if (old_current) { + for (j = 0; j < old_current->def->ndisks; j++) { + virDomainCheckpointDiskDef *disk2; + + disk2 = &old_current->def->disks[j]; + if (STRNEQ(disk->name, disk2->name)) + continue; + if (disk2->type == VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP && + qemuMonitorJSONTransactionAdd(actions, + "block-dirty-bitmap-disable", + "s:node", node, + "s:name", disk2->bitmap, + NULL) < 0) + goto cleanup; + } + } + } + ret = 0; + + cleanup: + return ret; +} static virDomainCheckpointPtr qemuDomainCheckpointCreateXML(virDomainPtr domain, @@ -17112,6 +17158,9 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, virDomainCheckpointObjPtr other = NULL; virQEMUDriverConfigPtr cfg = NULL; virCapsPtr caps = NULL; + qemuDomainObjPrivatePtr priv; + virJSONValuePtr actions = NULL; + int ret; virCheckFlags(VIR_DOMAIN_CHECKPOINT_CREATE_REDEFINE | VIR_DOMAIN_CHECKPOINT_CREATE_CURRENT | @@ -17125,11 +17174,18 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, if (!(vm = qemuDomObjFromDomain(domain))) goto cleanup; + priv = vm->privateData; cfg = virQEMUDriverGetConfig(driver); if (virDomainCheckpointCreateXMLEnsureACL(domain->conn, vm->def) < 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; @@ -17171,6 +17227,7 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, if (update_current) chk->def->current = true; if (vm->current_checkpoint) { + other = vm->current_checkpoint; if (!redefine && VIR_STRDUP(chk->def->parent, vm->current_checkpoint->def->name) < 0) goto endjob; @@ -17190,7 +17247,14 @@ 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, chk->def) < 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; @@ -17229,6 +17293,7 @@ qemuDomainCheckpointCreateXML(virDomainPtr domain, qemuDomainObjEndJob(driver, vm); cleanup: + virJSONValueFree(actions); virDomainObjEndAPI(&vm); virDomainCheckpointDefFree(def); VIR_FREE(xml); @@ -17601,6 +17666,7 @@ qemuDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, { virQEMUDriverPtr driver = checkpoint->domain->conn->privateData; virDomainObjPtr vm = NULL; + qemuDomainObjPrivatePtr priv; int ret = -1; virDomainCheckpointObjPtr chk = NULL; virQEMUDependentRemove rem; @@ -17623,6 +17689,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 = qemuCheckObjFromCheckpoint(vm, checkpoint))) goto endjob; -- 2.20.1

Still needs to actually kick off the right QMP commands, but at least allows validation of backup XML, including the fact that a backup job can survive a libvirtd restart. Atomically creating a checkpoint alongside the backup still needs implementing. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.h | 4 + src/qemu/qemu_domain.c | 31 ++++++- src/qemu/qemu_driver.c | 184 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+), 4 deletions(-) diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index fed271d22d..c9cb62c1e1 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -374,6 +374,10 @@ struct _qemuDomainObjPrivate { /* true if global -mem-prealloc appears on cmd line */ bool memPrealloc; + + /* Any currently running backup job. + * FIXME: allow jobs in parallel. For now, at most one job, always id 1. */ + virDomainBackupDefPtr backup; }; # define QEMU_DOMAIN_PRIVATE(vm) \ diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 43b6675341..1385df962d 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2312,13 +2312,25 @@ static int qemuDomainObjPrivateXMLFormatBlockjobs(virBufferPtr buf, virDomainObjPtr vm) { + qemuDomainObjPrivatePtr priv = vm->privateData; virBuffer attrBuf = VIR_BUFFER_INITIALIZER; bool bj = qemuDomainHasBlockjob(vm, false); + int ret = -1; virBufferAsprintf(&attrBuf, " active='%s'", virTristateBoolTypeToString(virTristateBoolFromBool(bj))); - return virXMLFormatElement(buf, "blockjobs", &attrBuf, NULL); + if (virXMLFormatElement(buf, "blockjobs", &attrBuf, NULL) < 0) + goto cleanup; + + /* TODO: merge other blockjobs and backups into uniform space? */ + if (priv->backup && virDomainBackupDefFormat(buf, priv->backup, true) < 0) + goto cleanup; + + ret = 0; + cleanup: + virBufferFreeAndReset(&attrBuf); + return ret; } @@ -2675,18 +2687,29 @@ qemuDomainObjPrivateXMLParseAutomaticPlacement(xmlXPathContextPtr ctxt, static int -qemuDomainObjPrivateXMLParseBlockjobs(qemuDomainObjPrivatePtr priv, +qemuDomainObjPrivateXMLParseBlockjobs(virQEMUDriverPtr driver, + qemuDomainObjPrivatePtr priv, xmlXPathContextPtr ctxt) { + xmlNodePtr node; char *active; int tmp; + int ret = -1; if ((active = virXPathString("string(./blockjobs/@active)", ctxt)) && (tmp = virTristateBoolTypeFromString(active)) > 0) priv->reconnectBlockjobs = tmp; + if ((node = virXPathNode("./domainbackup", ctxt)) && + !(priv->backup = virDomainBackupDefParseNode(ctxt->doc, node, + driver->xmlopt, + VIR_DOMAIN_BACKUP_PARSE_INTERNAL))) + goto cleanup; + + ret = 0; + cleanup: VIR_FREE(active); - return 0; + return ret; } @@ -3065,7 +3088,7 @@ qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt, qemuDomainObjPrivateXMLParsePR(ctxt, &priv->prDaemonRunning); - if (qemuDomainObjPrivateXMLParseBlockjobs(priv, ctxt) < 0) + if (qemuDomainObjPrivateXMLParseBlockjobs(driver, priv, ctxt) < 0) goto error; qemuDomainStorageIdReset(priv); diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index e37c5442dd..4e9273ef9c 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -17771,6 +17771,187 @@ qemuDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, return ret; } +static int qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, + const char *checkpointXml, unsigned int flags) +{ + virQEMUDriverPtr driver = domain->conn->privateData; + virDomainObjPtr vm = NULL; + virDomainBackupDefPtr def = NULL; + virQEMUDriverConfigPtr cfg = NULL; + virCapsPtr caps = NULL; + qemuDomainObjPrivatePtr priv; + int ret = -1; + struct timeval tv; + char *suffix = NULL; + + virCheckFlags(VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA, -1); + /* TODO: VIR_DOMAIN_BACKUP_BEGIN_QUIESCE */ + + // FIXME: Support non-null checkpointXML for incremental - what + // code can be shared with CheckpointCreateXML, then add to transaction + // to create new checkpoint at same time as starting blockdev-backup + if (checkpointXml) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create incremental backups yet")); + return -1; + } + // if (chk) VIR_STRDUP(suffix, chk->name); + gettimeofday(&tv, NULL); + if (virAsprintf(&suffix, "%lld", (long long)tv.tv_sec) < 0) + goto cleanup; + + if (!(vm = qemuDomObjFromDomain(domain))) + goto cleanup; + + cfg = virQEMUDriverGetConfig(driver); + + if (virDomainBackupBeginEnsureACL(domain->conn, vm->def) < 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 perform disk backup for inactive domain")); + goto cleanup; + } + if (!(def = virDomainBackupDefParseString(diskXml, driver->xmlopt, 0))) + goto cleanup; + + /* We are going to modify the domain below. */ + if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) + goto cleanup; + + priv = vm->privateData; + if (priv->backup) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", _("another backup job is already running")); + goto endjob; + } + + if (virDomainBackupAlignDisks(def, vm->def, suffix) < 0) + goto endjob; + + /* actually start the checkpoint. 2x2 array of push/pull, full/incr, + plus additional tweak if checkpoint requested */ + /* TODO: issue QMP commands: + - pull: nbd-server-start with <server> from user (or autogenerate server) + - push/pull: blockdev-add per <disk> + - incr: bitmap-add of tmp, bitmap-merge per <disk> + - transaction, containing: + - push+full: blockdev-backup sync:full + - push+incr: blockdev-backup sync:incremental bitmap:tmp + - pull+full: blockdev-backup sync:none + - pull+incr: blockdev-backup sync:none, bitmap-disable of tmp + - if checkpoint: bitmap-disable of old, bitmap-add of new + - pull: nbd-server-add per <disk>, including bitmap for incr + */ + + VIR_STEAL_PTR(priv->backup, def); + ret = priv->backup->id = 1; /* Hard-coded job id for now */ + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, + driver->caps) < 0) + VIR_WARN("Unable to save status on vm %s after backup job", + vm->def->name); + + endjob: + qemuDomainObjEndJob(driver, vm); + + cleanup: + VIR_FREE(suffix); + virDomainObjEndAPI(&vm); + virDomainBackupDefFree(def); + virObjectUnref(caps); + virObjectUnref(cfg); + return ret; +} + +static char *qemuDomainBackupGetXMLDesc(virDomainPtr domain, int id, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + char *xml = NULL; + qemuDomainObjPrivatePtr priv; + virBuffer buf = VIR_BUFFER_INITIALIZER; + + virCheckFlags(0, NULL); + + if (!(vm = qemuDomObjFromDomain(domain))) + return NULL; + + if (virDomainBackupGetXMLDescEnsureACL(domain->conn, vm->def) < 0) + goto cleanup; + + /* TODO: Allow more than one hard-coded job id */ + priv = vm->privateData; + if (id != 1 || !priv->backup) { + virReportError(VIR_ERR_NO_DOMAIN_CHECKPOINT, + _("no domain backup job with id '%d'"), id); + goto cleanup; + } + + if (virDomainBackupDefFormat(&buf, priv->backup, false) < 0) + goto cleanup; + xml = virBufferContentAndReset(&buf); + + cleanup: + virDomainObjEndAPI(&vm); + return xml; +} + +static int qemuDomainBackupEnd(virDomainPtr domain, int id, unsigned int flags) +{ + virQEMUDriverPtr driver = domain->conn->privateData; + virQEMUDriverConfigPtr cfg = NULL; + virDomainObjPtr vm = NULL; + int ret = -1; + virDomainBackupDefPtr backup = NULL; + qemuDomainObjPrivatePtr priv; + bool want_abort = flags & VIR_DOMAIN_BACKUP_END_ABORT; + + virCheckFlags(VIR_DOMAIN_BACKUP_END_ABORT, -1); + + if (!(vm = qemuDomObjFromDomain(domain))) + return -1; + + cfg = virQEMUDriverGetConfig(driver); + if (virDomainBackupEndEnsureACL(domain->conn, vm->def) < 0) + goto cleanup; + + /* TODO: Allow more than one hard-coded job id */ + priv = vm->privateData; + if (id != 1 || !priv->backup) { + virReportError(VIR_ERR_NO_DOMAIN_CHECKPOINT, + _("no domain backup job with id '%d'"), id); + goto cleanup; + } + + if (priv->backup->type != VIR_DOMAIN_BACKUP_TYPE_PUSH) + want_abort = false; + + /* TODO: QMP commands to actually cancel the pending job, and on + * pull, also tear down the NBD server */ + VIR_STEAL_PTR(backup, priv->backup); + if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, + driver->caps) < 0) + VIR_WARN("Unable to save status on vm %s after backup job", + vm->def->name); + + ret = want_abort ? 0 : 1; + + cleanup: + virDomainBackupDefFree(backup); + virDomainObjEndAPI(&vm); + return ret; +} + static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, char **result, unsigned int flags) { @@ -23549,6 +23730,9 @@ static virHypervisorDriver qemuHypervisorDriver = { .domainCheckpointIsCurrent = qemuDomainCheckpointIsCurrent, /* 5.1.0 */ .domainCheckpointHasMetadata = qemuDomainCheckpointHasMetadata, /* 5.1.0 */ .domainCheckpointDelete = qemuDomainCheckpointDelete, /* 5.1.0 */ + .domainBackupBegin = qemuDomainBackupBegin, /* 5.1.0 */ + .domainBackupGetXMLDesc = qemuDomainBackupGetXMLDesc, /* 5.1.0 */ + .domainBackupEnd = qemuDomainBackupEnd, /* 5.1.0 */ }; -- 2.20.1

Time to actually issue the QMP transactions that start and stop backup commands (for now, just pull mode, not push). Starting a job has to kick off several pre-req steps, then a transaction, and additionally spawn an NBD server for pull mode; ending a job as well as failing partway through beginning a job has to unwind the earlier steps. Implementing incremental pull and checkpoint creation is deferred to the next patch. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_domain.c | 18 ++- src/qemu/qemu_driver.c | 282 ++++++++++++++++++++++++++++++++++++++-- src/qemu/qemu_process.c | 7 + 3 files changed, 293 insertions(+), 14 deletions(-) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 1385df962d..6e684782fa 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2695,16 +2695,24 @@ qemuDomainObjPrivateXMLParseBlockjobs(virQEMUDriverPtr driver, char *active; int tmp; int ret = -1; + size_t i; if ((active = virXPathString("string(./blockjobs/@active)", ctxt)) && (tmp = virTristateBoolTypeFromString(active)) > 0) priv->reconnectBlockjobs = tmp; - if ((node = virXPathNode("./domainbackup", ctxt)) && - !(priv->backup = virDomainBackupDefParseNode(ctxt->doc, node, - driver->xmlopt, - VIR_DOMAIN_BACKUP_PARSE_INTERNAL))) - goto cleanup; + if ((node = virXPathNode("./domainbackup", ctxt))) { + if (!(priv->backup = virDomainBackupDefParseNode(ctxt->doc, node, + driver->xmlopt, + VIR_DOMAIN_BACKUP_PARSE_INTERNAL))) + goto cleanup; + /* The backup job is only stored in XML if backupBegin + * succeeded at exporting the disk, so no need to store disk + * state when we can just force-reset it to a known-good + * value. */ + for (i = 0; i < priv->backup->ndisks; i++) + priv->backup->disks[i].state = VIR_DOMAIN_BACKUP_DISK_STATE_EXPORT; + } ret = 0; cleanup: diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 4e9273ef9c..267b6e5c4d 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -17771,8 +17771,80 @@ qemuDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, return ret; } -static int qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, - const char *checkpointXml, unsigned int flags) +static int +qemuDomainBackupPrepare(virQEMUDriverPtr driver, virDomainObjPtr vm, + virDomainBackupDefPtr def) +{ + int ret = -1; + size_t i; + + if (qemuBlockNodeNamesDetect(driver, vm, QEMU_ASYNC_JOB_NONE) < 0) + goto cleanup; + for (i = 0; i < def->ndisks; i++) { + virDomainBackupDiskDef *disk = &def->disks[i]; + virStorageSourcePtr src = vm->def->disks[disk->idx]->src; + + if (!disk->store) + continue; + if (virAsprintf(&disk->store->nodeformat, "tmp-%s", disk->name) < 0) + goto cleanup; + if (!disk->store->format) + disk->store->format = VIR_STORAGE_FILE_QCOW2; + if (def->incremental) { + if (src->format != VIR_STORAGE_FILE_QCOW2) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("incremental backup of %s requires qcow2"), + disk->name); + goto cleanup; + } + } + } + ret = 0; + cleanup: + return ret; +} + +/* Called while monitor lock is held. Best-effort cleanup. */ +static int +qemuDomainBackupDiskCleanup(virQEMUDriverPtr driver, virDomainObjPtr vm, + virDomainBackupDiskDef *disk, bool incremental) +{ + qemuDomainObjPrivatePtr priv = vm->privateData; + const char *node = vm->def->disks[disk->idx]->src->nodeformat; + int ret = 0; + + if (!disk->store) + return 0; + if (disk->state >= VIR_DOMAIN_BACKUP_DISK_STATE_EXPORT) { + /* No real need to use nbd-server-remove, since we will + * shortly be calling nbd-server-stop. */ + } + if (incremental && disk->state >= VIR_DOMAIN_BACKUP_DISK_STATE_BITMAP && + qemuMonitorDeleteBitmap(priv->mon, node, disk->store->nodeformat) < 0) { + VIR_WARN("Unable to remove temp bitmap for disk %s after backup", + disk->name); + ret = -1; + } + if (disk->state >= VIR_DOMAIN_BACKUP_DISK_STATE_READY && + qemuMonitorBlockdevDel(priv->mon, disk->store->nodeformat) < 0) { + VIR_WARN("Unable to remove temp disk %s after backup", + disk->name); + ret = -1; + } + if (disk->state >= VIR_DOMAIN_BACKUP_DISK_STATE_LABEL) + qemuDomainDiskChainElementRevoke(driver, vm, disk->store); + if (disk->state >= VIR_DOMAIN_BACKUP_DISK_STATE_CREATED && + disk->store->detected && unlink(disk->store->path) < 0) { + VIR_WARN("Unable to unlink temp disk %s after backup", + disk->store->path); + ret = -1; + } + return ret; +} + +static int +qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, + const char *checkpointXml, unsigned int flags) { virQEMUDriverPtr driver = domain->conn->privateData; virDomainObjPtr vm = NULL; @@ -17781,8 +17853,14 @@ static int qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, virCapsPtr caps = NULL; qemuDomainObjPrivatePtr priv; int ret = -1; + virJSONValuePtr json = NULL; + bool job_started = false; + bool nbd_running = false; + size_t i; struct timeval tv; char *suffix = NULL; + virCommandPtr cmd = NULL; + const char *qemuImgPath; virCheckFlags(VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA, -1); /* TODO: VIR_DOMAIN_BACKUP_BEGIN_QUIESCE */ @@ -17803,6 +17881,7 @@ static int qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, if (!(vm = qemuDomObjFromDomain(domain))) goto cleanup; + priv = vm->privateData; cfg = virQEMUDriverGetConfig(driver); if (virDomainBackupBeginEnsureACL(domain->conn, vm->def) < 0) @@ -17825,25 +17904,115 @@ static int qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, if (!(def = virDomainBackupDefParseString(diskXml, driver->xmlopt, 0))) goto cleanup; + if (def->type == VIR_DOMAIN_BACKUP_TYPE_PULL) { + if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_NBD_BITMAP)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("qemu binary lacks pull-mode backup support")); + goto cleanup; + } + if (!def->server || + def->server->transport != VIR_STORAGE_NET_HOST_TRANS_TCP) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("<domainbackup> must specify TCP server for now")); + goto cleanup; + } + } else { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("push mode backups not supported yet")); + goto cleanup; + } + if (def->incremental) { + if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BITMAP_MERGE)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("qemu binary lacks persistent bitmaps support")); + goto cleanup; + } + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("cannot create incremental backups yet")); + goto cleanup; + } + + if (!(qemuImgPath = qemuFindQemuImgBinary(driver))) + goto cleanup; + /* We are going to modify the domain below. */ if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) goto cleanup; - priv = vm->privateData; if (priv->backup) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("another backup job is already running")); goto endjob; } - if (virDomainBackupAlignDisks(def, vm->def, suffix) < 0) + if (virDomainBackupAlignDisks(def, vm->def, suffix) < 0 || + qemuDomainBackupPrepare(driver, vm, def) < 0) goto endjob; /* actually start the checkpoint. 2x2 array of push/pull, full/incr, plus additional tweak if checkpoint requested */ - /* TODO: issue QMP commands: - - pull: nbd-server-start with <server> from user (or autogenerate server) - - push/pull: blockdev-add per <disk> + qemuDomainObjEnterMonitor(driver, vm); + /* - push/pull: blockdev-add per <disk> */ + for (i = 0; i < def->ndisks; i++) { + virDomainBackupDiskDef *disk = &def->disks[i]; + virJSONValuePtr file; + virStorageSourcePtr src = vm->def->disks[disk->idx]->src; + const char *node = src->nodeformat; + + if (!disk->store) + continue; + if (qemuDomainStorageFileInit(driver, vm, disk->store, src) < 0) + goto endmon; + if (disk->store->detected) { + if (virStorageFileCreate(disk->store) < 0) { + virReportSystemError(errno, + _("failed to create image file '%s'"), + NULLSTR(disk->store->path)); + goto endmon; + } + disk->state = VIR_DOMAIN_BACKUP_DISK_STATE_CREATED; + } + if (qemuDomainDiskChainElementPrepare(driver, vm, disk->store, false, + true) < 0) + goto endmon; + disk->state = VIR_DOMAIN_BACKUP_DISK_STATE_LABEL; + /* Force initialization of scratch file to new qcow2 */ + if (!(cmd = virCommandNewArgList(qemuImgPath, + "create", + "-f", + virStorageFileFormatTypeToString(disk->store->format), + "-o", + NULL))) + goto endmon; + virCommandAddArgFormat(cmd, "backing_file=%s,backing_fmt=%s", + src->path, + virStorageFileFormatTypeToString(src->format)); + virCommandAddArg(cmd, disk->store->path); + if (virCommandRun(cmd, NULL) < 0) + goto endmon; + virCommandFree(cmd); + cmd = NULL; + + if (virJSONValueObjectCreate(&file, + "s:driver", "file", + "s:filename", disk->store->path, + NULL) < 0) + goto endmon; + if (virJSONValueObjectCreate(&json, + "s:driver", virStorageFileFormatTypeToString(disk->store->format), + "s:node-name", disk->store->nodeformat, + "a:file", &file, + "s:backing", node, NULL) < 0) { + virJSONValueFree(file); + goto endmon; + } + if (qemuMonitorBlockdevAdd(priv->mon, json) < 0) + goto endmon; + json = NULL; + disk->state = VIR_DOMAIN_BACKUP_DISK_STATE_READY; + } + + /* TODO: - incr: bitmap-add of tmp, bitmap-merge per <disk> - transaction, containing: - push+full: blockdev-backup sync:full @@ -17851,8 +18020,78 @@ static int qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, - pull+full: blockdev-backup sync:none - pull+incr: blockdev-backup sync:none, bitmap-disable of tmp - if checkpoint: bitmap-disable of old, bitmap-add of new + */ + if (!(json = virJSONValueNewArray())) + goto endmon; + for (i = 0; i < def->ndisks; i++) { + virDomainBackupDiskDef *disk = &def->disks[i]; + virStorageSourcePtr src = vm->def->disks[disk->idx]->src; + + if (!disk->store) + continue; + if (qemuMonitorJSONTransactionAdd(json, + "blockdev-backup", + "s:device", src->nodeformat, + "s:target", disk->store->nodeformat, + "s:sync", "none", + "s:job-id", disk->store->nodeformat, + NULL) < 0) + goto endmon; + } + if (qemuMonitorTransaction(priv->mon, &json) < 0) + goto endmon; + job_started = true; + + /* + - pull: nbd-server-start with <server> from user (or autogenerate server) - pull: nbd-server-add per <disk>, including bitmap for incr */ + if (def->type == VIR_DOMAIN_BACKUP_TYPE_PULL) { + if (qemuMonitorNBDServerStart(priv->mon, def->server->name, + def->server->port, NULL) < 0) + goto endmon; + nbd_running = true; + for (i = 0; i < def->ndisks; i++) { + virDomainBackupDiskDef *disk = &def->disks[i]; + + if (!disk->store) + continue; + if (qemuMonitorNBDServerAdd(priv->mon, disk->store->nodeformat, + disk->name, false, + def->incremental ? disk->name : + NULL) < 0) + goto endmon; + disk->state = VIR_DOMAIN_BACKUP_DISK_STATE_EXPORT; + } + } + + ret = 0; + endmon: + /* Best effort cleanup if we fail partway through */ + if (ret < 0) { + virErrorPtr save_err = virSaveLastError(); + + if (nbd_running && + qemuMonitorNBDServerStop(priv->mon) < 0) + VIR_WARN("Unable to stop NBD server on vm %s after backup job", + vm->def->name); + for (i = 0; i < def->ndisks; i++) { + virDomainBackupDiskDef *disk = &def->disks[i]; + + if (job_started && + qemuMonitorBlockJobCancel(priv->mon, + disk->store->nodeformat) < 0) + VIR_WARN("Unable to stop backup job %s on vm %s after failure", + disk->store->nodeformat, vm->def->name); + qemuDomainBackupDiskCleanup(driver, vm, disk, !!def->incremental); + } + virSetError(save_err); + virFreeError(save_err); + } + if (qemuDomainObjExitMonitor(driver, vm) < 0) + ret = -1; + if (ret < 0) + goto endjob; VIR_STEAL_PTR(priv->backup, def); ret = priv->backup->id = 1; /* Hard-coded job id for now */ @@ -17865,7 +18104,9 @@ static int qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, qemuDomainObjEndJob(driver, vm); cleanup: + virCommandFree(cmd); VIR_FREE(suffix); + virJSONValueFree(json); virDomainObjEndAPI(&vm); virDomainBackupDefFree(def); virObjectUnref(caps); @@ -17915,6 +18156,8 @@ static int qemuDomainBackupEnd(virDomainPtr domain, int id, unsigned int flags) virDomainBackupDefPtr backup = NULL; qemuDomainObjPrivatePtr priv; bool want_abort = flags & VIR_DOMAIN_BACKUP_END_ABORT; + virDomainBackupDefPtr def; + size_t i; virCheckFlags(VIR_DOMAIN_BACKUP_END_ABORT, -1); @@ -17935,9 +18178,27 @@ static int qemuDomainBackupEnd(virDomainPtr domain, int id, unsigned int flags) if (priv->backup->type != VIR_DOMAIN_BACKUP_TYPE_PUSH) want_abort = false; + def = priv->backup; + + /* We are going to modify the domain below. */ + if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0) + goto cleanup; + + qemuDomainObjEnterMonitor(driver, vm); + if (def->type == VIR_DOMAIN_BACKUP_TYPE_PULL) + ret = qemuMonitorNBDServerStop(priv->mon); + for (i = 0; i < def->ndisks; i++) { + if (qemuMonitorBlockJobCancel(priv->mon, + def->disks[i].store->nodeformat) < 0 || + qemuDomainBackupDiskCleanup(driver, vm, &def->disks[i], + !!def->incremental) < 0) + ret = -1; + } + if (qemuDomainObjExitMonitor(driver, vm) < 0 || ret < 0) { + ret = -1; + goto endjob; + } - /* TODO: QMP commands to actually cancel the pending job, and on - * pull, also tear down the NBD server */ VIR_STEAL_PTR(backup, priv->backup); if (virDomainSaveStatus(driver->xmlopt, cfg->stateDir, vm, driver->caps) < 0) @@ -17946,6 +18207,9 @@ static int qemuDomainBackupEnd(virDomainPtr domain, int id, unsigned int flags) ret = want_abort ? 0 : 1; + endjob: + qemuDomainObjEndJob(driver, vm); + cleanup: virDomainBackupDefFree(backup); virDomainObjEndAPI(&vm); diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 0583eb03f2..4f6061ed93 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -925,6 +925,7 @@ qemuProcessHandleBlockJob(qemuMonitorPtr mon ATTRIBUTE_UNUSED, void *opaque) { virQEMUDriverPtr driver = opaque; + qemuDomainObjPrivatePtr priv; struct qemuProcessEvent *processEvent = NULL; virDomainDiskDefPtr disk; qemuBlockJobDataPtr job = NULL; @@ -935,6 +936,12 @@ qemuProcessHandleBlockJob(qemuMonitorPtr mon ATTRIBUTE_UNUSED, VIR_DEBUG("Block job for device %s (domain: %p,%s) type %d status %d", diskAlias, vm, vm->def->name, type, status); + priv = vm->privateData; + if (type == VIR_DOMAIN_BLOCK_JOB_TYPE_BACKUP && priv->backup) { + /* Event for canceling a pull-mode backup is side-effect that + * should not be forwarded on to user */ + goto cleanup; + } if (!(disk = qemuProcessFindDomainDiskByAliasOrQOM(vm, diskAlias, NULL))) goto cleanup; -- 2.20.1

Complete wiring up incremental backup, by adding in support for creating a checkpoint at the same time as a backup (make the transaction have a few more steps) as well as exposing the dirty bitmap for a prior backup over NBD (requires creating a temporary bitmap, merging all appropriate bitmaps in, then exposing that bitmap over NBD). Signed-off-by: Eric Blake <eblake@redhat.com> --- src/qemu/qemu_driver.c | 189 ++++++++++++++++++++++++++++++++++------- 1 file changed, 159 insertions(+), 30 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 267b6e5c4d..0564bab52f 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -17077,6 +17077,24 @@ qemuDomainCheckpointPrepare(virQEMUDriverPtr driver, virCapsPtr caps, if (disk->type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) continue; + /* We want to name temporary bitmap after disk name during + * incremental backup, which is not possible if that is a + * persistent bitmap name. We can also make life easier by + * enforcing bitmap names match checkpoint name, although this + * is not technically necessary. */ + if (STREQ(disk->name, disk->bitmap)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("checkpoint for disk %s must have distinct bitmap name"), + disk->name); + goto cleanup; + } + if (STRNEQ(disk->bitmap, def->name)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("disk %s bitmap should match checkpoint name %s"), + disk->name, def->name); + goto cleanup; + } + if (vm->def->disks[i]->src->format > 0 && vm->def->disks[i]->src->format != VIR_STORAGE_FILE_QCOW2) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, @@ -17773,19 +17791,42 @@ qemuDomainCheckpointDelete(virDomainCheckpointPtr checkpoint, static int qemuDomainBackupPrepare(virQEMUDriverPtr driver, virDomainObjPtr vm, - virDomainBackupDefPtr def) + virDomainBackupDefPtr def, + virDomainCheckpointObjPtr chk) { int ret = -1; size_t i; + if (chk && def->ndisks != chk->def->ndisks) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("inconsistency between backup and checkpoint disks")); + goto cleanup; + } if (qemuBlockNodeNamesDetect(driver, vm, QEMU_ASYNC_JOB_NONE) < 0) goto cleanup; for (i = 0; i < def->ndisks; i++) { virDomainBackupDiskDef *disk = &def->disks[i]; virStorageSourcePtr src = vm->def->disks[disk->idx]->src; - if (!disk->store) + /* For now, insist that atomic checkpoint affect same disks as + * those being backed up. */ + if (!disk->store) { + if (chk && + chk->def->disks[i].type != VIR_DOMAIN_CHECKPOINT_TYPE_NONE) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("disk %s requested checkpoint without backup"), + disk->name); + goto cleanup; + } continue; + } + if (chk && + chk->def->disks[i].type != VIR_DOMAIN_CHECKPOINT_TYPE_BITMAP) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("disk %s requested backup without checkpoint"), + disk->name); + goto cleanup; + } if (virAsprintf(&disk->store->nodeformat, "tmp-%s", disk->name) < 0) goto cleanup; if (!disk->store->format) @@ -17820,7 +17861,7 @@ qemuDomainBackupDiskCleanup(virQEMUDriverPtr driver, virDomainObjPtr vm, * shortly be calling nbd-server-stop. */ } if (incremental && disk->state >= VIR_DOMAIN_BACKUP_DISK_STATE_BITMAP && - qemuMonitorDeleteBitmap(priv->mon, node, disk->store->nodeformat) < 0) { + qemuMonitorDeleteBitmap(priv->mon, node, disk->name) < 0) { VIR_WARN("Unable to remove temp bitmap for disk %s after backup", disk->name); ret = -1; @@ -17861,23 +17902,15 @@ qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, char *suffix = NULL; virCommandPtr cmd = NULL; const char *qemuImgPath; + virDomainCheckpointDefPtr chkdef = NULL; + virDomainCheckpointObjPtr chk = NULL; + virDomainCheckpointObjPtr other = NULL; + virDomainCheckpointObjPtr parent = NULL; + virJSONValuePtr arr = NULL; virCheckFlags(VIR_DOMAIN_BACKUP_BEGIN_NO_METADATA, -1); /* TODO: VIR_DOMAIN_BACKUP_BEGIN_QUIESCE */ - // FIXME: Support non-null checkpointXML for incremental - what - // code can be shared with CheckpointCreateXML, then add to transaction - // to create new checkpoint at same time as starting blockdev-backup - if (checkpointXml) { - virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", - _("cannot create incremental backups yet")); - return -1; - } - // if (chk) VIR_STRDUP(suffix, chk->name); - gettimeofday(&tv, NULL); - if (virAsprintf(&suffix, "%lld", (long long)tv.tv_sec) < 0) - goto cleanup; - if (!(vm = qemuDomObjFromDomain(domain))) goto cleanup; @@ -17904,6 +17937,17 @@ qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, if (!(def = virDomainBackupDefParseString(diskXml, driver->xmlopt, 0))) goto cleanup; + if (checkpointXml) { + if (!(chkdef = qemuDomainCheckpointDefParseString(driver, caps, + checkpointXml, 0)) || + VIR_STRDUP(suffix, chkdef->name) < 0) + goto cleanup; + } else { + gettimeofday(&tv, NULL); + if (virAsprintf(&suffix, "%lld", (long long)tv.tv_sec) < 0) + goto cleanup; + } + if (def->type == VIR_DOMAIN_BACKUP_TYPE_PULL) { if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_NBD_BITMAP)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", @@ -17927,9 +17971,18 @@ qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, _("qemu binary lacks persistent bitmaps support")); goto cleanup; } - virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", - _("cannot create incremental backups yet")); - goto cleanup; + for (other = vm->current_checkpoint; other; + other = other->def->parent ? + virDomainCheckpointFindByName(vm->checkpoints, + other->def->parent) : NULL) + if (STREQ(other->def->name, def->incremental)) + break; + if (!other) { + virReportError(VIR_ERR_OPERATION_INVALID, + _("could not locate checkpoint '%s' for incremental backup"), + def->incremental); + goto cleanup; + } } if (!(qemuImgPath = qemuFindQemuImgBinary(driver))) @@ -17945,14 +17998,40 @@ qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, goto endjob; } + if (chkdef) { + if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BITMAP_MERGE)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("qemu binary lacks persistent bitmaps support")); + goto endjob; + } + + if (qemuDomainCheckpointPrepare(driver, caps, vm, chkdef) < 0) + goto endjob; + if (!(chk = virDomainCheckpointAssignDef(vm->checkpoints, chkdef))) + goto endjob; + chkdef = NULL; + chk->def->current = true; + if (vm->current_checkpoint) { + parent = vm->current_checkpoint; + if (VIR_STRDUP(chk->def->parent, parent->def->name) < 0) + goto endjob; + if (qemuDomainCheckpointWriteMetadata(vm, parent, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) + goto endjob; + vm->current_checkpoint = NULL; + } + } + if (virDomainBackupAlignDisks(def, vm->def, suffix) < 0 || - qemuDomainBackupPrepare(driver, vm, def) < 0) + qemuDomainBackupPrepare(driver, vm, def, chk) < 0) goto endjob; /* actually start the checkpoint. 2x2 array of push/pull, full/incr, plus additional tweak if checkpoint requested */ qemuDomainObjEnterMonitor(driver, vm); - /* - push/pull: blockdev-add per <disk> */ + /* - push/pull: blockdev-add per <disk> + - incr: bitmap-add of tmp, bitmap-merge per <disk> */ for (i = 0; i < def->ndisks; i++) { virDomainBackupDiskDef *disk = &def->disks[i]; virJSONValuePtr file; @@ -18010,11 +18089,32 @@ qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, goto endmon; json = NULL; disk->state = VIR_DOMAIN_BACKUP_DISK_STATE_READY; + + if (def->incremental) { + if (!(arr = virJSONValueNewArray())) + goto endmon; + if (qemuMonitorAddBitmap(priv->mon, node, disk->name, false) < 0) { + virJSONValueFree(arr); + goto endmon; + } + disk->state = VIR_DOMAIN_BACKUP_DISK_STATE_BITMAP; + for (other = parent ? parent : vm->current_checkpoint; other; + other = other->def->parent ? + virDomainCheckpointFindByName(vm->checkpoints, + other->def->parent) : NULL) { + if (virJSONValueArrayAppendString(arr, other->def->name) < 0) { + virJSONValueFree(arr); + goto endmon; + } + if (STREQ(other->def->name, def->incremental)) + break; + } + if (qemuMonitorMergeBitmaps(priv->mon, node, disk->name, &arr) < 0) + goto endmon; + } } - /* TODO: - - incr: bitmap-add of tmp, bitmap-merge per <disk> - - transaction, containing: + /* - transaction, containing: - push+full: blockdev-backup sync:full - push+incr: blockdev-backup sync:incremental bitmap:tmp - pull+full: blockdev-backup sync:none @@ -18025,22 +18125,50 @@ qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, goto endmon; for (i = 0; i < def->ndisks; i++) { virDomainBackupDiskDef *disk = &def->disks[i]; - virStorageSourcePtr src = vm->def->disks[disk->idx]->src; + const char *node; if (!disk->store) continue; + node = qemuBlockNodeLookup(vm, disk->name); if (qemuMonitorJSONTransactionAdd(json, "blockdev-backup", - "s:device", src->nodeformat, + "s:device", node, "s:target", disk->store->nodeformat, "s:sync", "none", - "s:job-id", disk->store->nodeformat, + "s:job-id", disk->name, + NULL) < 0) + goto endmon; + if (def->incremental && def->type == VIR_DOMAIN_BACKUP_TYPE_PULL && + qemuMonitorJSONTransactionAdd(json, + "block-dirty-bitmap-disable", + "s:node", node, + "s:name", disk->name, NULL) < 0) goto endmon; } + if (chk && qemuDomainCheckpointAddActions(vm, json, parent, chk->def) < 0) + goto endmon; if (qemuMonitorTransaction(priv->mon, &json) < 0) goto endmon; job_started = true; + if (chk) { + if (qemuDomainCheckpointWriteMetadata(vm, chk, driver->caps, + driver->xmlopt, + cfg->checkpointDir) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unable to save metadata for checkpoint %s"), + chk->def->name); + virDomainCheckpointObjListRemove(vm->checkpoints, chk); + goto endmon; + } + vm->current_checkpoint = chk; + other = virDomainCheckpointFindByName(vm->checkpoints, + chk->def->parent); + chk->parent = other; + other->nchildren++; + chk->sibling = other->first_child; + other->first_child = chk; + } /* - pull: nbd-server-start with <server> from user (or autogenerate server) @@ -18079,10 +18207,9 @@ qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, virDomainBackupDiskDef *disk = &def->disks[i]; if (job_started && - qemuMonitorBlockJobCancel(priv->mon, - disk->store->nodeformat) < 0) + qemuMonitorBlockJobCancel(priv->mon, disk->name) < 0) VIR_WARN("Unable to stop backup job %s on vm %s after failure", - disk->store->nodeformat, vm->def->name); + disk->name, vm->def->name); qemuDomainBackupDiskCleanup(driver, vm, disk, !!def->incremental); } virSetError(save_err); @@ -18104,6 +18231,8 @@ qemuDomainBackupBegin(virDomainPtr domain, const char *diskXml, qemuDomainObjEndJob(driver, vm); cleanup: + virJSONValueFree(arr); + virDomainCheckpointDefFree(chkdef); virCommandFree(cmd); VIR_FREE(suffix); virJSONValueFree(json); -- 2.20.1

On Wed, Feb 06, 2019 at 01:17:58PM -0600, Eric Blake wrote:
The following is the latest version of my API proposal for incremental backups, and follows along from the demo I presented as part of my KVM Forum 2018 talk: https://kvmforum2018.sched.com/event/FzuB/facilitating-incremental-backup-er...
The patches are also available via https://repo.or.cz/libvirt/ericb.git at the tag backup-v4.
Integration with external snapshots is still not included in these patches, although I have been playing more with that locally, and I still haven't gotten a working demonstration of a push-mode incremental backup.
At this point, I'm posting mainly because there have been enough changes in qemu (the backup APIs are now stable! and in qemu.git in time for 4.0) and libvirt (several releases and code cleanups in between, such that the v3 no longer applies easily), while I continue to work locally on more features, addressing the review comments I got on v3, and remove the 'wip' tag on several of the later patches.
Among other things, I still haven't determined how we want to integrate checkpoints with external snapshots; we could either have:
virDomainSnapshotCreateXML("<domainsnapshot>...") # existing virDomainBackupBegin("<domainbackup>...", "<domaincheckpoint>...") # this series virDomainSnapshotCheckpointCreateXML("<domainsnapshot>...", "<domaincheckpoint>...") # new
A slightly related question: when these new APIs (thanks for working on them!) are merged, am I right in assuming that they should be able to "replace" the existing (and provide additional): virDomainBlockRebase(); and virDomainBlockCopy() ... _provided_ that an application is adjusted to using libvirt that is new enough to drive QEMU's '-blockdev', QMP `blockdev-add` et al? Or is that (the new APIs being backward-compatible with blockRebase() and blockCopy()) an explicit non-goal? I'm only this out of curiosity.
or to make checkpoint creation part of the snapshot and backup XML, as in:
virDomainSnapshotCreateXML("<domainsnapshot><domaincheckpoint>...</domainsnapshot>") # extension of existing virDomainBackupBegin("<domainbackup><domaincheckpoint>...</domainbackup>") # tweak to this series
I'm also planning to add an API to query which job ids are currently running (right now, the code only supports one job at a time, and always calls it job 1, but that is not future-friendly) and an update to the redefine XML command to make it easier to redefine multiple checkpoints in one API call.
[...] -- /kashyap

On Mon, Feb 18, 2019 at 17:16:41 +0100, Kashyap Chamarthy wrote:
On Wed, Feb 06, 2019 at 01:17:58PM -0600, Eric Blake wrote:
The following is the latest version of my API proposal for incremental backups, and follows along from the demo I presented as part of my KVM Forum 2018 talk: https://kvmforum2018.sched.com/event/FzuB/facilitating-incremental-backup-er...
The patches are also available via https://repo.or.cz/libvirt/ericb.git at the tag backup-v4.
Integration with external snapshots is still not included in these patches, although I have been playing more with that locally, and I still haven't gotten a working demonstration of a push-mode incremental backup.
At this point, I'm posting mainly because there have been enough changes in qemu (the backup APIs are now stable! and in qemu.git in time for 4.0) and libvirt (several releases and code cleanups in between, such that the v3 no longer applies easily), while I continue to work locally on more features, addressing the review comments I got on v3, and remove the 'wip' tag on several of the later patches.
Among other things, I still haven't determined how we want to integrate checkpoints with external snapshots; we could either have:
virDomainSnapshotCreateXML("<domainsnapshot>...") # existing virDomainBackupBegin("<domainbackup>...", "<domaincheckpoint>...") # this series virDomainSnapshotCheckpointCreateXML("<domainsnapshot>...", "<domaincheckpoint>...") # new
A slightly related question: when these new APIs (thanks for working on them!) are merged, am I right in assuming that they should be able to "replace" the existing (and provide additional):
virDomainBlockRebase(); and virDomainBlockCopy()
... _provided_ that an application is adjusted to using libvirt that is new enough to drive QEMU's '-blockdev', QMP `blockdev-add` et al?
Or is that (the new APIs being backward-compatible with blockRebase() and blockCopy()) an explicit non-goal?
I'm only this out of curiosity.
The checkpoints are really orthogonal to the backing chain/shapshot managemet. Checkpoints don't really store any data but rather provide a way to determine which blocks were changed and thus need backup. Also one point is that checkpoints don't allow (or I did not notice it in the proposal) to capture memory state along with the disk state. In the ideal world of snapshots when deletion/revertion was implemented we'd never expose the virDomainBlockCommit and virDomainBlockPull APIs including the multi-use backdoor virDomainBlockRebase() which should have never existed and users would do equivalent operations using the snapshot APIs. virDomainBlockCopy is useful on it's own though but badly combines with snapshots. This will need some fixing. With the new checkpoint APIs the situation is even more "fun" as modification of the backing chain involves in some cases changes to the bitmaps. Ideally these would do "the right thing (TM)" during snapshot deletion/reversion. Given that at this time we don't support snapshot deletion/reversion for external snapshots we can use the excuse that snapshot management is not implemented so that checkpoints don't need to be modified. Since virDomainBlockCommit/virDomainBlockPull are basically a backdoor to do snapshot merging which is not actually recorded in the snapshot XML the same damage can happen to the checkpoints. I'm not sure how we want to deal with that at this point though.

On 2/18/19 10:34 AM, Peter Krempa wrote:
Among other things, I still haven't determined how we want to integrate checkpoints with external snapshots; we could either have:
virDomainSnapshotCreateXML("<domainsnapshot>...") # existing virDomainBackupBegin("<domainbackup>...", "<domaincheckpoint>...") # this series virDomainSnapshotCheckpointCreateXML("<domainsnapshot>...", "<domaincheckpoint>...") # new
A slightly related question: when these new APIs (thanks for working on them!) are merged, am I right in assuming that they should be able to "replace" the existing (and provide additional):
virDomainBlockRebase(); and virDomainBlockCopy()
... _provided_ that an application is adjusted to using libvirt that is new enough to drive QEMU's '-blockdev', QMP `blockdev-add` et al?
Or is that (the new APIs being backward-compatible with blockRebase() and blockCopy()) an explicit non-goal?
I'm only this out of curiosity.
The checkpoints are really orthogonal to the backing chain/shapshot managemet.
Indeed. Think of virDomainBlockRebase() as syntactic sugar (it really is a wrapper around either virDomainBlockPull() streaming operation, or around virDomainBlockRebase() for a mirror operation). Given your question grouping it with virDomainBlockCopy(), I'm assuming you are only asking about the latter. The biggest difference between virDomainBlockCopy() and virDomainBackupBegin() is point-in-time: with mirroring, you start the job up front, but you do not have a valid backup image until you cancel the job; but since both files have the same content (once the job has hit the sync phase), cancellation gives you a choice of whether to stay with the old file (the mirror image is the backup) or to pivot to the new file (the original file is now the backup). On the other hand, with virDomainBackupBegin(), you select the point-in-time at the point where you start the job, and the backup copy is created independently of the running image so there is no pivoting possible. virDomainBackupBegin() will work even without checkpoints (in which case, it is a full image backup); but their main power is that when used WITH checkpoints, the backup operation can be done with much less effort than a full copy. Ideally, we want external snapshots to also be points where checkpoints can be created; John Snow and I have had some ideas about what is needed, but our focus is first getting the API working without worrying about external snapshots, while ensuring that the XML has enough flexibility to add in those improvements later without needing more API. Another difference: virDomainBlockCopy() operates on only one block device at a time (you have to issue multiple calls if your guest has multiple disks, although the calls can at least be run in parallel). But virDomainBackupBegin() operates on the entire domain at once, with your choice of granularity on which disk(s) to have involved (similar to how virDomainSnapshotCreateXML() lets you choose which disks to snapshot).
Checkpoints don't really store any data but rather provide a way to determine which blocks were changed and thus need backup. Also one point is that checkpoints don't allow (or I did not notice it in the proposal) to capture memory state along with the disk state.
Correct - checkpoints ONLY track which portions of the disk have changed. In fact, when taking a differential backup, you can really only request all changes occurring between one point in the past and the present. In particular, if you have Check1 and Check2, you can request the backup of all sectors touched between Check1 and the present, or all sectors touched between Check2 and the present, but you cannot request the backup of only the sectors touched between Check1 and Check2 (at least not through the libvirt API), because those sectors may have changed again between Check2 and the present, and the bitmaps only requires which sectors have changed, and NOT what the data was at the time of Check2's creation.
In the ideal world of snapshots when deletion/revertion was implemented we'd never expose the virDomainBlockCommit and virDomainBlockPull APIs including the multi-use backdoor virDomainBlockRebase() which should have never existed and users would do equivalent operations using the snapshot APIs.
virDomainBlockCopy is useful on it's own though but badly combines with snapshots. This will need some fixing.
Indeed, and that's true regardless of whether the backup API goes in (although the backup API probably compounds the issue on the number of corner cases we have to think about; the conservative approach is that at least in the beginning, you won't be able to run a BlockCopy and a BackupBegin job at the same time).
With the new checkpoint APIs the situation is even more "fun" as modification of the backing chain involves in some cases changes to the bitmaps. Ideally these would do "the right thing (TM)" during snapshot deletion/reversion. Given that at this time we don't support snapshot deletion/reversion for external snapshots we can use the excuse that snapshot management is not implemented so that checkpoints don't need to be modified.
Somewhat correct - but we DO have to think about how we plan for the API to grow in the future when we eventually DO fix snapshot deletion/reversion. Hence my question - would we rather have the creation of a checkpoint at the same time as the creation of an external snapshot (which we DO know we will want) to occur via the existing API (by extending the snapshot XML to include the checkpoint XML as a sub-element), or via a new API (by passing the checkpoint XML as a second parameter)? Once we've answered that question, it then determines what signature we want for virDomainBackupBegin() (either two separate XML parameters, one for the backup job and one for the checkpoint creation, as presented in v4 of the series, OR as one single XML call where the checkpoint XML is a sub-element of the backup XML).
Since virDomainBlockCommit/virDomainBlockPull are basically a backdoor to do snapshot merging which is not actually recorded in the snapshot XML the same damage can happen to the checkpoints.
I'm not sure how we want to deal with that at this point though.
-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org

On Mon, Feb 18, 2019 at 15:50:52 -0600, Eric Blake wrote:
On 2/18/19 10:34 AM, Peter Krempa wrote:
[...]
In the ideal world of snapshots when deletion/revertion was implemented we'd never expose the virDomainBlockCommit and virDomainBlockPull APIs including the multi-use backdoor virDomainBlockRebase() which should have never existed and users would do equivalent operations using the snapshot APIs.
virDomainBlockCopy is useful on it's own though but badly combines with snapshots. This will need some fixing.
Indeed, and that's true regardless of whether the backup API goes in (although the backup API probably compounds the issue on the number of corner cases we have to think about; the conservative approach is that at least in the beginning, you won't be able to run a BlockCopy and a BackupBegin job at the same time).
We currently do this kind of interlocking between blockjobs and snapshots as well, so that will not be a problem. We don't even allow two blockjobs on non-conflicting parts of the backing chain while qemu allows that now. Allowing that will require some API design considerations/compromises as our job tracking and manipulation APIs and events identify the job by the disk. Given that a guest can detach a disk frontend without qemu interaction we will somehow need to be able to display jobs which don't have a disk assigned any more though which will have the same implications basically.
With the new checkpoint APIs the situation is even more "fun" as modification of the backing chain involves in some cases changes to the bitmaps. Ideally these would do "the right thing (TM)" during snapshot deletion/reversion. Given that at this time we don't support snapshot deletion/reversion for external snapshots we can use the excuse that snapshot management is not implemented so that checkpoints don't need to be modified.
Somewhat correct - but we DO have to think about how we plan for the API to grow in the future when we eventually DO fix snapshot deletion/reversion. Hence my question - would we rather have the creation of a checkpoint at the same time as the creation of an external snapshot (which we DO know we will want) to occur via the existing API (by extending the snapshot XML to include the checkpoint XML as a sub-element), or via a new API (by passing the checkpoint XML as a second parameter)? Once we've answered that question, it then determines what signature we want for virDomainBackupBegin() (either two separate XML parameters, one for the backup job and one for the checkpoint creation, as presented in v4 of the series, OR as one single XML call where the checkpoint XML is a sub-element of the backup XML).
This is an interesting point. Given that the snapshot creates new files (speaking of external snapshots obviously) you get an implicit "checkpoint" at that moment as basically all blocks changed since that moment are recorded in the overlay file. For any other snapshot you get the same if you apply the overlay file on top of it. Now the question is whether we need to be able to track that checkpoint explicitly or whether it needs an explicit bitmap, but I'm not that familiar with qemu implementation. If we don't require any explicit bitmap or marking in qemu to be able to track a checkpoint from a snapshot point we can e.g. allow the backup API to take an external checkpoint as a reference for the backup itself. Creating both a snapshot and a checkpoint is certainly possible but I fear that making the snapshot more complex than it is will be an unexpected can of worms.

On 2/19/19 1:30 AM, Peter Krempa wrote:
Somewhat correct - but we DO have to think about how we plan for the API to grow in the future when we eventually DO fix snapshot deletion/reversion. Hence my question - would we rather have the creation of a checkpoint at the same time as the creation of an external snapshot (which we DO know we will want) to occur via the existing API (by extending the snapshot XML to include the checkpoint XML as a sub-element), or via a new API (by passing the checkpoint XML as a second parameter)? Once we've answered that question, it then determines what signature we want for virDomainBackupBegin() (either two separate XML parameters, one for the backup job and one for the checkpoint creation, as presented in v4 of the series, OR as one single XML call where the checkpoint XML is a sub-element of the backup XML).
This is an interesting point. Given that the snapshot creates new files (speaking of external snapshots obviously) you get an implicit "checkpoint" at that moment as basically all blocks changed since that moment are recorded in the overlay file. For any other snapshot you get the same if you apply the overlay file on top of it.
Now the question is whether we need to be able to track that checkpoint explicitly or whether it needs an explicit bitmap, but I'm not that familiar with qemu implementation.
The easiest way to perform a differential backup is to bitwise-or all the bitmaps from the requested checkpoint to the present (where I've implemented things that only one bitmap is active at a time, to avoid qemu having to write to an ever-increasing number of bitmaps as more checkpoints are created). But unless I create a bitmap at the same time an external snapshot is created, there is no easy way to make current qemu expose that all clusters in the current active layer are dirty, to OR that with the clusters that were marked dirty prior to creating the snapshot. So yes, it would be best if the creation of a snapshot can ALSO be used as the creation of a bitmap, which in turn implies that creating an explicit checkpoint associated with the snapshot is the way to go. Fortunately, with blockdev-add, it is possible to create the qcow2 file, then create the bitmap, and only then perform the snapshot operation, so we don't need any new qemu commands to create an explicit bitmap, but we DO need to figure out where in the libvirt API to wire in this extra step when external snapshots and checkpoints are both used in the same image.
If we don't require any explicit bitmap or marking in qemu to be able to track a checkpoint from a snapshot point we can e.g. allow the backup API to take an external checkpoint as a reference for the backup itself.
Creating both a snapshot and a checkpoint is certainly possible but I fear that making the snapshot more complex than it is will be an unexpected can of worms.
I'm afraid that when I do integrate bitmaps with external snapshots, that I'll have to modify the creation of snapshots to create a bitmap, whether or not I also expose the ability in API to create an explicit Checkpoint object at the same time. So as long as we have to manage bitmaps, we might as well also manage the explicit creation of a checkpoint for both a snapshot as well as for a virDomainBackupBegin call. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
participants (4)
-
Eric Blake
-
John Ferlan
-
Kashyap Chamarthy
-
Peter Krempa