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

fix: a volume a job writes is not copied, attached or its pool stopped; download progress each second

A download into a pool listed its volume while it was written. The page
let a superuser copy it (the copy got a half-written file, live), attach
it to a VM, or stop the pool under it. Now:

- the copy job claims its source volume shared, and the worker refuses
  a copy whose name the source's format changed before the claim;
- attaching an existing volume holds it exclusive from its first read,
  so neither a download nor a copy is under way, and its format is read
  after the download ended;
- stopping or deleting a pool (page and API) holds the pool exclusive,
  and its backup target's claim if it is one; restores claim the pools
  they write into shared.

Each is refused with the job in the way named, as a volume delete
already was.

The download recorded its progress every 16 MiB, so a slow server's
bar stayed empty for a long time; it records it each second, with the
cancel check. The worker checks the name is still free before it
starts. A snapshot name or tag typed in the VM list's bulk form keeps
the list from reloading, as a chosen VM did.
This commit is contained in:
catborise 2026-11-20 15:12:20 +03:00
parent 7c8f5b3150
commit 7e70595fd2
19 changed files with 588 additions and 450 deletions

View file

@ -11,6 +11,8 @@ from django.urls import reverse
from computes.models import Compute
from instances.models import Instance
from operations.claims import admit
from operations.models import Claim
PATH = "/var/lib/libvirt/images/data.qcow2"
MAC = "52:54:00:aa:bb:cc"
@ -51,6 +53,23 @@ class DuplicateDeviceTests(TestCase):
self.assertEqual(self.post("add_existing_vol", form), ["data.qcow2 is already a disk of this VM"])
self.proxy.attach_disk.assert_called_once()
@patch("instances.views.wvmStorage")
def test_a_volume_a_job_writes_is_not_attached(self, storage):
# a download holds the volume it writes: the VM would get a half-written disk; nothing is read before the claim
pool = storage.return_value
pool.get_volumes.return_value = ["data.qcow2"]
pool.get_volume_format_type.return_value = "qcow2"
pool.get_type.return_value = "dir"
pool.get_volume_type.return_value = "file"
pool.get_volume.return_value.path.return_value = PATH
# a copy reads it (shared): a running VM would write it meanwhile
admit({f"volume:{self.vm.compute_id}:default/data.qcow2": Claim.SHARED}, holder="other-node:1:1")
form = {"selected_storage": "default", "vols": "data.qcow2", "bus": "virtio", "cache": "default"}
self.assertEqual(self.post("add_existing_vol", form),
["A short operation is in progress on this resource; try again in a moment."])
self.proxy.attach_disk.assert_not_called()
pool.get_volume_format_type.assert_not_called()
@patch("instances.views.wvmStorage")
def test_a_backup_volume_is_not_attached(self, storage):
# the VM would write into the backup, and its deletion would remove the VM's disk

View file

@ -55,6 +55,7 @@ from logs.audit import audit, audit_vm, failed_vm
from lxml import etree
from operations import claims, jobs
from operations.jobs import KEY_RE
from operations.models import Claim
from operations.utils import HOST_BUSY, busy_message, summary
from webvirtcloud.middleware import error_text, same_origin_referer
from vrtManager import util
@ -1597,6 +1598,14 @@ def add_existing_vol(request, pk):
instance = get_instance(request.user, pk, perm_type="change")
storage = request.POST.get("selected_storage", "")
name = request.POST.get("vols", "")
# the volume exclusive from its first read to the attach: not while a download writes it
# or a copy reads it (Busy names the job)
with claims.hold({claims.volume_key(instance.compute_id, storage, name): Claim.EXCLUSIVE}):
return _attach_volume(request, instance, storage, name)
def _attach_volume(request, instance, storage, name):
pk = instance.pk
bus = request.POST.get("bus", app_settings.INSTANCE_VOLUME_DEFAULT_BUS)
cache = request.POST.get("cache", app_settings.INSTANCE_VOLUME_DEFAULT_CACHE)