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

fix(storages): a volume another compute's VM uses on shared storage is not deleted

A pool on shared storage (an NFS share, or a directory mounted from one)
holds the same file at the same path on every host that mounts it. A
VM's delete checks the panel's other computes before it deletes its disk
files, but the pool page, its API and a VM's disk delete checked only
their own host: a disk of a VM on another compute could be deleted. They
now ask the other computes too, and refuse while one uses the file or
cannot be reached. The other computes are asked before this host's lock
is taken, and before a VM's disk is detached.
This commit is contained in:
catborise 2026-11-13 10:31:22 +03:00
parent fb3b2f479e
commit 143a74b145
11 changed files with 277 additions and 196 deletions

View file

@ -4,6 +4,7 @@ from admin.permissions import IsSuperUser
from appsettings.settings import app_settings
from computes.models import Compute
from computes.utils import libvirt_compute_lock
from instances.utils import paths_used_on_other_computes
from django.shortcuts import get_object_or_404
from django.utils.translation import gettext as _
from drf_spectacular.utils import extend_schema, inline_serializer
@ -206,9 +207,11 @@ class VolumeViewSet(viewsets.ViewSet):
subject, action = f"Storage pool {storage_pk}", f"delete volume {pk}"
if conn.is_active():
# no VM operation attaches it between the check and the delete
with audit(request, compute, subject, action), libvirt_compute_lock(compute):
conn.del_volume(pk)
with audit(request, compute, subject, action):
conn.check_unused_elsewhere(pk, lambda paths: paths_used_on_other_computes(compute, paths))
# no VM operation attaches it between the check and the delete
with libvirt_compute_lock(compute):
conn.del_volume(pk)
return Response({"status": "Volume: " + pk + " is deleted"})
else:
failed(request, compute, subject, action, "pool is not active")

View file

@ -113,6 +113,31 @@ class VolumeDeleteTests(TestCase):
request()
self.assertEqual(inside, [[(self.compute.pk, False)]])
def test_the_other_computes_are_asked_before_the_lock(self):
# shared storage: another host's VM may use the file at the same path;
# a host that does not answer does not hold this one's lock meanwhile
for module, request in (
("storages.views", lambda: self.client.post(reverse("storage", args=[self.compute.id, "default"]),
{"del_volume": "1", "volname": "disk.qcow2"})),
("storages.api.viewsets", lambda: self.client.delete(
f"/api/v1/computes/{self.compute.id}/storages/default/volumes/disk.qcow2/")),
):
with self.subTest(module), patch(f"{module}.wvmStorage") as mock_wvm, \
patch(f"{module}.libvirt_compute_lock") as lock, \
patch(f"{module}.paths_used_on_other_computes", return_value=set()) as others:
conn = mock_wvm.return_value
conn.is_active.return_value = True
conn.get_size.return_value = (1, 1)
def check(name, elsewhere, lock=lock):
lock.assert_not_called()
elsewhere(["/pool/disk.qcow2"])
conn.check_unused_elsewhere.side_effect = check
request()
others.assert_called_once_with(self.compute, ["/pool/disk.qcow2"])
conn.del_volume.assert_called_once_with("disk.qcow2")
@patch("storages.views.wvmStorage")
def test_a_busy_compute_is_a_message(self, mock_wvm):
conn = self.refused(mock_wvm)

View file

@ -5,6 +5,7 @@ from admin.decorators import superuser_only
from appsettings.settings import app_settings
from computes.models import Compute
from computes.utils import libvirt_compute_lock
from instances.utils import paths_used_on_other_computes
from django.contrib import messages
from django.http import HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
@ -162,9 +163,11 @@ def storage(request, compute_id, pool):
if "del_volume" in request.POST:
volname = request.POST.get("volname", "")
try:
# no VM operation attaches it between the check and the delete
with audit(request, compute, subject, f"delete volume {volname}"), libvirt_compute_lock(compute):
conn.del_volume(volname)
with audit(request, compute, subject, f"delete volume {volname}"):
conn.check_unused_elsewhere(volname, lambda paths: paths_used_on_other_computes(compute, paths))
# no VM operation attaches it between the check and the delete
with libvirt_compute_lock(compute):
conn.del_volume(volname)
except OperationError as err:
messages.error(request, str(err))
except claims.Busy as busy: