mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
fix(vmbackups): a backup restored over its VM again takes the next free name
The copies were named from the VM, the disk and the backup's minute only. Restoring the same backup over the VM a second time, or a second backup of the same minute, chose the name of the disk the VM already ran from, and the job refused it. The names are now chosen at submission, numbered from 2 when the pool has one, and kept in the job's intent apart from the VM state the job checks again before and after the copies.
This commit is contained in:
parent
8cd7aee19a
commit
fb6a427a47
4 changed files with 63 additions and 29 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import datetime
|
||||
import hashlib
|
||||
import itertools
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
|
|
@ -961,8 +962,8 @@ class RestoreBackup(Kind):
|
|||
|
||||
|
||||
def _over_plan(backup, proxy, owner=False):
|
||||
"""What a restore over the backup's VM makes: {"disks": {dev: [pool,
|
||||
volume name]}, "old": {dev: the disk's path now}, "nvram": the VM's NVRAM
|
||||
"""What a restore over the backup's VM replaces: {"disks": {dev: the
|
||||
disk's pool}, "old": {dev: the disk's path now}, "nvram": the VM's NVRAM
|
||||
path or None, "tpm": bool}.
|
||||
Refuses a VM that is not shut off, has snapshots, or whose disks, UEFI
|
||||
firmware or TPM are not the backup's."""
|
||||
|
|
@ -985,12 +986,28 @@ def _over_plan(backup, proxy, owner=False):
|
|||
raise util.OperationError(_("The VM's UEFI firmware changed since the backup; restore it as a new VM"))
|
||||
if plan["tpm"] != (backup.manifest.get("tpm", "none") != "none"):
|
||||
raise util.OperationError(_("The VM's TPM changed since the backup; restore it as a new VM"))
|
||||
time = timezone.localtime(backup.created_at).strftime("%Y%m%d-%H%M")
|
||||
return {"disks": {dev: [now[dev]["pool"], f"{proxy.instance.name()}-{dev}-{time}.{'qcow2' if f.format == 'qcow2' else 'raw'}"]
|
||||
for dev, f in files.items()},
|
||||
return {"disks": {dev: now[dev]["pool"] for dev in files},
|
||||
"old": {dev: now[dev]["path"] for dev in files}, "nvram": plan["nvram"], "tpm": plan["tpm"]}
|
||||
|
||||
|
||||
def _over_names(backup, proxy, over):
|
||||
"""The copies' volume names, {dev: name}: the VM's, the disk's and the
|
||||
backup's time, numbered from 2 when its pool has that name (the backup
|
||||
restored over the VM before, or another one of the same minute)."""
|
||||
time = timezone.localtime(backup.created_at).strftime("%Y%m%d-%H%M")
|
||||
pools = proxy.file_pools()
|
||||
names = {}
|
||||
for f in backup.files.filter(role=BackupFile.DISK):
|
||||
pool = pools.get(over["disks"][f.dev])
|
||||
if pool:
|
||||
pool[0].refresh(0)
|
||||
taken = set(pool[0].listVolumes()) if pool else set()
|
||||
base, ext = f"{proxy.instance.name()}-{f.dev}-{time}", "qcow2" if f.format == "qcow2" else "raw"
|
||||
names[f.dev] = next(name for name in itertools.chain([f"{base}.{ext}"], (f"{base}-{n}.{ext}" for n in itertools.count(2)))
|
||||
if name not in taken)
|
||||
return names
|
||||
|
||||
|
||||
@register
|
||||
class RestoreOver(Kind):
|
||||
"""A ready backup restored over its own shut-off VM: its disks copied as
|
||||
|
|
@ -1015,16 +1032,18 @@ class RestoreOver(Kind):
|
|||
raise util.OperationError(_("The backup's VM is no longer in the panel; restore it as a new VM"))
|
||||
_may_use(user, instance, backup.target)
|
||||
_target_row(backup.target, instance.compute)
|
||||
over = _over_plan(backup, _proxy(instance), owner=not user.is_superuser)
|
||||
proxy = _proxy(instance)
|
||||
over = _over_plan(backup, proxy, owner=not user.is_superuser)
|
||||
names = _over_names(backup, proxy, over)
|
||||
keys = {claims.compute_key(instance.compute_id): Claim.SHARED, claims.instance_key(instance.uuid): Claim.EXCLUSIVE,
|
||||
claims.backup_key(backup.uuid): Claim.SHARED, claims.backup_target_key(backup.target_id): Claim.EXCLUSIVE}
|
||||
for pool, name in over["disks"].values(): # no other job makes a volume of these names meanwhile
|
||||
keys[claims.volume_key(instance.compute_id, pool, name)] = Claim.EXCLUSIVE
|
||||
for dev, name in names.items(): # no other job makes a volume of these names meanwhile
|
||||
keys[claims.volume_key(instance.compute_id, over["disks"][dev], name)] = Claim.EXCLUSIVE
|
||||
return Plan(
|
||||
claims=keys,
|
||||
resource=claims.instance_key(instance.uuid),
|
||||
label=f"{instance.compute.name}/{instance.name}",
|
||||
intent={"compute": instance.compute_id, "uuid": instance.uuid, "over": over,
|
||||
intent={"compute": instance.compute_id, "uuid": instance.uuid, "over": over, "names": names,
|
||||
"time": timezone.localtime(backup.created_at).strftime("%Y-%m-%d %H:%M")},
|
||||
)
|
||||
|
||||
|
|
@ -1043,20 +1062,22 @@ class RestoreOver(Kind):
|
|||
_tpm_files(backup)
|
||||
disks = {f.dev: f for f in backup.files.filter(role=BackupFile.DISK)}
|
||||
pools = proxy.file_pools()
|
||||
for dev, (pool, name) in op.intent["over"]["disks"].items():
|
||||
if pool not in pools:
|
||||
raise util.OperationError(_("This host has no active directory storage pool %(pool)s"), pool=pool)
|
||||
wvmInstance._refuse_taken(pools[pool][0], name)
|
||||
for pool in {pool for pool, _name in op.intent["over"]["disks"].values()}:
|
||||
check_target_space(pools[pool][0], [{"capacity": disks[dev].capacity} for dev, (p, _n) in
|
||||
op.intent["over"]["disks"].items() if p == pool], **_free_space())
|
||||
over, names = op.intent["over"], op.intent["names"]
|
||||
for dev, name in names.items():
|
||||
if over["disks"][dev] not in pools:
|
||||
raise util.OperationError(_("This host has no active directory storage pool %(pool)s"),
|
||||
pool=over["disks"][dev])
|
||||
wvmInstance._refuse_taken(pools[over["disks"][dev]][0], name)
|
||||
for pool in set(over["disks"].values()):
|
||||
check_target_space(pools[pool][0], [{"capacity": disks[dev].capacity} for dev, p in over["disks"].items()
|
||||
if p == pool], **_free_space())
|
||||
|
||||
ctx.set_phase(_("Copying disks"))
|
||||
c = instance.compute
|
||||
paths = {}
|
||||
for dev, (pool, name) in op.intent["over"]["disks"].items():
|
||||
for dev, name in names.items():
|
||||
ctx.record_intent(copying=dev)
|
||||
storage = wvmStorage(c.hostname, c.login, c.password, c.type, pool)
|
||||
storage = wvmStorage(c.hostname, c.login, c.password, c.type, over["disks"][dev])
|
||||
try:
|
||||
paths[dev] = storage.clone_volume(tpool.storageVolLookupByName(disks[dev].volume), name,
|
||||
vol_fmt=disks[dev].format, disk_owner_uid=_owner()[0],
|
||||
|
|
@ -1148,14 +1169,14 @@ class RestoreOver(Kind):
|
|||
row = _target_row(backup.target, instance.compute)
|
||||
return Outcome(Operation.SUCCEEDED,
|
||||
result=self._switch(op, backup, proxy, target_pool(proxy, row.pool, row.pool_uuid)))
|
||||
volumes = {dev: name for dev, (_pool, name) in op.intent["over"]["disks"].items()}
|
||||
volumes = op.intent["names"]
|
||||
if op.intent.get("copying"): # libvirt's answer to that copy is unknown
|
||||
return Outcome(Operation.UNCERTAIN, error=say(_(
|
||||
"A disk was being copied when the job stopped; check this volume, then resolve this job: %(volumes)s"),
|
||||
volumes=volumes[op.intent["copying"]]))
|
||||
pools = proxy.file_pools()
|
||||
for dev in op.intent.get("copied", []):
|
||||
pool = op.intent["over"]["disks"][dev][0]
|
||||
pool = op.intent["over"]["disks"][dev]
|
||||
if pool not in pools: # recovery waits: the copies are removed once it runs
|
||||
raise util.OperationError(_("Storage pool %(pool)s is not active; the restore's copies in it are "
|
||||
"removed when it is"), pool=pool)
|
||||
|
|
|
|||
|
|
@ -216,7 +216,7 @@ class OwnerTests(TransactionTestCase):
|
|||
from vmbackups.jobs import RestoreOver
|
||||
|
||||
backup = self.ready_backup()
|
||||
over = {"disks": {"vda": ["default", "vm1-vda-x.qcow2"]}, "old": {"vda": "/pool/a.qcow2"}, "nvram": None, "tpm": False}
|
||||
over = {"disks": {"vda": "default"}, "old": {"vda": "/pool/a.qcow2"}, "nvram": None, "tpm": False}
|
||||
old_vol = MagicMock()
|
||||
self.proxy.get_volume_by_path.return_value = old_vol
|
||||
self.proxy.paths_used_by_other_domains.return_value = set()
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ class RestoreOverTests(TransactionTestCase):
|
|||
|
||||
def test_the_disks_are_copied_as_new_volumes_then_switched(self):
|
||||
op = self.submit()
|
||||
self.assertEqual(op.intent["over"]["disks"], {"vda": ["default", "vm1-vda-20261117-1140.qcow2"]})
|
||||
self.assertEqual((op.intent["over"]["disks"], op.intent["names"]), ({"vda": "default"}, {"vda": "vm1-vda-20261117-1140.qcow2"}))
|
||||
self.assertTrue(Claim.objects.filter(operation=op, resource=f"volume:{self.compute.pk}:default/vm1-vda-20261117-1140.qcow2",
|
||||
mode=Claim.EXCLUSIVE).exists())
|
||||
self.assertTrue(Claim.objects.filter(operation=op, resource=f"instance:{self.vm.uuid}", mode=Claim.EXCLUSIVE).exists())
|
||||
|
|
@ -218,11 +218,22 @@ class RestoreOverTests(TransactionTestCase):
|
|||
with self.assertRaisesMessage(OperationError, "new VM"):
|
||||
self.submit()
|
||||
|
||||
def test_a_taken_name_is_refused_and_nothing_removed(self):
|
||||
self.dest.storageVolLookupByName.side_effect = None # restored over from this backup before
|
||||
self.refused("vm1-vda-20261117-1140.qcow2")
|
||||
def test_a_name_taken_after_the_job_was_queued_is_refused_and_nothing_removed(self):
|
||||
op = self.submit()
|
||||
self.dest.storageVolLookupByName.side_effect = None
|
||||
with self.assertRaisesMessage(OperationError, "vm1-vda-20261117-1140.qcow2"):
|
||||
self.run_job(op)
|
||||
self.storage.clone_volume.assert_not_called()
|
||||
self.assertEqual(self.removed, [])
|
||||
|
||||
def test_the_same_backup_restored_over_again_takes_the_next_free_name(self):
|
||||
# the VM runs from the first restore's copy; an administrator's restore kept the disk before it too
|
||||
self.dest.listVolumes.return_value = ["vm1.qcow2", "vm1-vda-20261117-1140.qcow2", "vm1-vda-20261117-1140-2.qcow2"]
|
||||
op = self.submit()
|
||||
self.assertEqual(op.intent["names"], {"vda": "vm1-vda-20261117-1140-3.qcow2"})
|
||||
self.run_job(op)
|
||||
self.assertEqual(self.defined().xpath("devices/disk/source/@file"), ["/pool/vm1-vda-20261117-1140-3.qcow2"])
|
||||
|
||||
def test_disks_changed_after_the_job_was_queued_are_refused(self):
|
||||
op = self.submit()
|
||||
self.proxy.backup_plan.return_value["disks"][0]["pool"] = "other"
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ from vrtManager.connection import wvmConnect
|
|||
from vrtManager.util import OperationError
|
||||
|
||||
from .forms import PolicyForm, RestoreDiskForm, RestoreForm, TargetForm, TargetPoolForm
|
||||
from .jobs import _over_plan, _proxy, _vm_pool
|
||||
from .jobs import _over_names, _over_plan, _proxy, _vm_pool
|
||||
from .models import Backup, BackupPolicy, BackupTarget
|
||||
from .utils import inside_vm_pool, pending_deletes
|
||||
|
||||
|
|
@ -283,11 +283,13 @@ def restore_over(request, uuid):
|
|||
return redirect("operations:job", op.pk)
|
||||
over, problem = None, None
|
||||
try:
|
||||
over = _over_plan(backup, _proxy(backup.instance), owner=not request.user.is_superuser)
|
||||
proxy = _proxy(backup.instance)
|
||||
over = _over_plan(backup, proxy, owner=not request.user.is_superuser)
|
||||
names = _over_names(backup, proxy, over)
|
||||
except (OperationError, libvirtError) as err:
|
||||
problem = error_text(request, err)
|
||||
disks = [{"dev": dev, "old": over["old"][dev], "pool": pool, "name": name}
|
||||
for dev, (pool, name) in sorted(over["disks"].items())] if over else []
|
||||
disks = [{"dev": dev, "old": over["old"][dev], "pool": pool, "name": names[dev]}
|
||||
for dev, pool in sorted(over["disks"].items())] if over else []
|
||||
return render(request, "vmbackups/restore_over.html", {"backup": backup, "disks": disks, "over": over,
|
||||
"problem": problem})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue