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

feat(vmbackups): VM owners back up and restore their VMs within a quota

A permission 'Can back up instances' lets a user back up the VMs they
may change into targets open to VM owners, restore those backups over
the VM or as a new VM, and delete the backups they took by hand. Each job
checks the rights at admission and again when it runs.

- A backup quota (GiB, in the user's quota) counts the owner's own
  backups: a backup being made holds its estimate, what its disks and the
  files below them hold. An owner's backups run one at a time, so each
  run's check sees the others.
- An owner restores as a new VM on the VM's compute, into its first
  disk's pool, within the VM quota; the job counts in it until its VM is
  recorded, as a clone does.
- An owner's restore over the VM removes the disks it ran from once the
  switch is done, unless another VM uses them: the owner cannot remove
  them.
- Recovery never begins a deletion the job had not begun.

Superusers keep everything they had.
This commit is contained in:
catborise 2026-11-17 19:29:07 +03:00
parent 1b0a54e3d1
commit db4cbf806a
22 changed files with 1155 additions and 520 deletions

View file

@ -53,7 +53,7 @@ class wvmBackup(wvmInstance):
def backup_plan(self):
"""What a backup of the VM copies, from its persistent definition:
{"definition", "disks": [{dev, path, pool, format, capacity}],
{"definition", "disks": [{dev, path, pool, format, capacity, allocation}],
"nvram": path or None, "tpm": whether it has a TPM (shut off: its
state is read by the job), "left_out": [{dev, why}]}. Refuses, naming
why, a VM whose backup could not be restored as it is."""
@ -99,8 +99,10 @@ class wvmBackup(wvmInstance):
raise
raise OperationError(gettext_noop("Disk %(dev)s (%(path)s) is in no storage pool of the host; it cannot be backed up"),
dev=dev, path=path) from err
info = vol.info()
disks.append({"dev": dev, "path": path, "pool": vol.storagePoolLookupByVolume().name(),
"format": disk_format, "capacity": vol.info()[1]})
"format": disk_format, "capacity": info[1],
"allocation": min(info[1], self._chain_allocation(vol))})
if self.instance.state()[0] != libvirt.VIR_DOMAIN_SHUTOFF:
# a running VM's disks are copied: the definition kept must name the same ones
@ -147,6 +149,20 @@ class wvmBackup(wvmInstance):
vol = self._find_volume(pool, os.path.basename(path))
return None if vol is None else self.download_volume(vol)
def _chain_allocation(self, vol):
"""The bytes a disk's volume and the files below it hold: its backup
copies them flat."""
total, seen = 0, set()
while vol is not None and vol.path() not in seen:
seen.add(vol.path())
total += vol.info()[2]
below = etree.fromstring(vol.XMLDesc(0)).findtext("backingStore/path")
try:
vol = self.get_volume_by_path(below) if below else None
except libvirt.libvirtError:
vol = None # in no pool: counted by its own size only
return total
def checkpoint_usable(self, name, devs):
"""Whether the checkpoint name can be an incremental backup's base:
on this host, with a bitmap of known size for each disk of devs