1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(vmbackups): back up a running or shut-off VM into a target

A superuser backs a VM up from its new Backups tab, or with POST
/api/v1/backups/ (202 with the job); GET lists and reads backups. The
backup.create job holds the VM, its compute (shared) and the target, so
migrations, snapshots and power changes of the VM wait meanwhile.

- A shut-off VM's disks are copied one at a time into the target pool,
  flat, in their format (createXMLFrom); a cancel stops before the next.
- A running or paused VM's disks go by libvirt's push backup into
  volumes made first, as the live clone: the file systems frozen for its
  start when the guest agent answers (frozen), crash-consistent
  otherwise, or refused when frozen file systems are required. A guest
  that shuts down meanwhile does not end the backup.
- A running VM's NVRAM file is kept only when it reads the same after
  the copy as before it (three reads), so it is the capture point's.
- Then the NVRAM file, the definition and the manifest (written last)
  are uploaded and read back, and the row becomes ready with its files.
- Recovery thaws a frozen guest first, watches a push backup still
  running, finishes a copied backup, removes the files of a failed or
  cancelled one, and leaves a copy whose end libvirt cannot tell
  uncertain with its files named. The target pool is checked by UUID and
  its free space before anything is made.
This commit is contained in:
catborise 2026-11-17 07:56:09 +03:00
parent 89835f2b06
commit f4559acbc7
17 changed files with 1703 additions and 353 deletions

View file

@ -2764,11 +2764,15 @@ class wvmInstance(wvmConnect):
)
def backup_targets(self):
"""The target paths of the running backup; None when none runs."""
"""The target paths of the running backup; None when none runs, also
when the domain stopped (a guest that shut down during its backup
stops when the backup ends: PRESERVE_SHUTDOWN_DOMAIN)."""
if not self.instance.isActive():
return None
try:
xml = self.instance.backupGetXMLDesc(0)
except libvirtError as err:
if err.get_error_code() == VIR_ERR_NO_DOMAIN_BACKUP:
if err.get_error_code() in (VIR_ERR_NO_DOMAIN_BACKUP, VIR_ERR_OPERATION_INVALID):
return None
raise
return set(etree.fromstring(xml).xpath("disks/disk/target/@file"))
@ -2776,7 +2780,14 @@ class wvmInstance(wvmConnect):
def backup_progress(self):
"""(bytes copied, bytes to copy) of the running backup; None when
none runs."""
stats = self.instance.jobStats(0)
if not self.instance.isActive():
return None
try:
stats = self.instance.jobStats(0)
except libvirtError as err:
if err.get_error_code() == VIR_ERR_OPERATION_INVALID: # stopped since: its backup ended
return None
raise
if (
stats.get("type", VIR_DOMAIN_JOB_NONE) == VIR_DOMAIN_JOB_NONE
or stats.get("operation") != VIR_DOMAIN_JOB_OPERATION_BACKUP

View file

@ -210,6 +210,19 @@ class BackupTestCase(unittest.TestCase):
}
self.assertIsNone(p.backup_progress())
def test_a_domain_stopped_after_its_backup_has_no_backup_running(self):
# PRESERVE_SHUTDOWN_DOMAIN: libvirt stops a guest that shut down once its backup ends
p = proxy()
p.instance.isActive.return_value = False
self.assertIsNone(p.backup_progress())
self.assertIsNone(p.backup_targets())
p.instance.jobStats.assert_not_called()
p.instance.isActive.return_value = True # stopped between the check and the read
p.instance.jobStats.side_effect = error(libvirt.VIR_ERR_OPERATION_INVALID)
p.instance.backupGetXMLDesc.side_effect = error(libvirt.VIR_ERR_OPERATION_INVALID)
self.assertIsNone(p.backup_progress())
self.assertIsNone(p.backup_targets())
def test_result(self):
p = proxy()
backup = {"type": libvirt.VIR_DOMAIN_JOB_COMPLETED, "operation": libvirt.VIR_DOMAIN_JOB_OPERATION_BACKUP}