1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 09:26:03 +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

@ -48,6 +48,8 @@ def proxy(xml=None, live=None, state=libvirt.VIR_DOMAIN_SHUTOFF, managed_save=Fa
def volume(path):
vol = MagicMock()
vol.info.return_value = [0, (10 if path.endswith("qcow2") else 20) * GIB, GIB]
vol.path.return_value = path
vol.XMLDesc.return_value = "<volume/>"
vol.storagePoolLookupByVolume.return_value = pool
return vol
@ -67,14 +69,32 @@ class BackupPlanTestCase(unittest.TestCase):
self.assertEqual(
plan["disks"],
[
{"dev": "vda", "path": "/pool/a.qcow2", "pool": "default", "format": "qcow2", "capacity": 10 * GIB},
{"dev": "vdb", "path": "/pool/b.img", "pool": "default", "format": "raw", "capacity": 20 * GIB},
{"dev": "vda", "path": "/pool/a.qcow2", "pool": "default", "format": "qcow2", "capacity": 10 * GIB,
"allocation": GIB},
{"dev": "vdb", "path": "/pool/b.img", "pool": "default", "format": "raw", "capacity": 20 * GIB,
"allocation": GIB},
],
)
self.assertEqual(plan["left_out"], [{"dev": "sda", "why": "CD-ROM: not copied"}])
self.assertIsNone(plan["nvram"])
self.assertIn("<name>vm1</name>", plan["definition"])
def test_a_disks_allocation_counts_its_backing_files(self):
# an overlay holds what changed since its snapshot: the backup copies the chain flat
p = proxy()
def volume(path):
vol = MagicMock()
vol.info.return_value = [0, 10 * GIB, GIB]
vol.path.return_value = path
backing = {"/pool/a.qcow2": "/pool/a.base"}.get(path)
vol.XMLDesc.return_value = (f"<volume><backingStore><path>{backing}</path></backingStore></volume>"
if backing else "<volume/>")
vol.storagePoolLookupByVolume.return_value.name.return_value = "default"
return vol
p.get_volume_by_path = MagicMock(side_effect=volume)
disks = {d["dev"]: d for d in p.backup_plan()["disks"]}
self.assertEqual((disks["vda"]["allocation"], disks["vdb"]["allocation"]), (2 * GIB, GIB))
def test_the_nvram_is_named(self):
xml = domain(os_extra="<nvram>/var/lib/libvirt/qemu/nvram/vm1_VARS.fd</nvram>")
self.assertEqual(proxy(xml).backup_plan()["nvram"], "/var/lib/libvirt/qemu/nvram/vm1_VARS.fd")