mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 09:26:03 +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:
parent
fb3b2f479e
commit
143a74b145
11 changed files with 277 additions and 196 deletions
|
|
@ -3,7 +3,7 @@ Disk and media mutation views must be superuser-only (matching the UI, where
|
|||
the Disk tab is only rendered for superusers), and even for superusers the
|
||||
views must not act on volumes or option values taken blindly from the client.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import ANY, patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.messages import get_messages
|
||||
|
|
@ -118,6 +118,22 @@ class DiskViewsTenantIsolationTestCase(TestCase):
|
|||
mock_wvm.return_value.detach_disk.assert_called_once_with("vda")
|
||||
self.assertEqual(mock_storage.call_args[0][-1], "default")
|
||||
mock_storage.return_value.del_volume.assert_called_once_with("vm-disk.qcow2")
|
||||
mock_storage.return_value.check_unused_elsewhere.assert_called_once_with("vm-disk.qcow2", ANY)
|
||||
|
||||
def test_delete_vol_of_a_volume_another_compute_uses_keeps_the_disk_attached(self):
|
||||
from vrtManager.util import InUse
|
||||
|
||||
self.client.force_login(self.superuser)
|
||||
with patch("instances.models.wvmInstance") as mock_wvm, patch(
|
||||
"instances.views.wvmStorage"
|
||||
) as mock_storage:
|
||||
mock_wvm.return_value.get_disk_devices.return_value = [VM_DISK]
|
||||
mock_wvm.return_value.get_managed_save_image.return_value = 0
|
||||
mock_storage.return_value.check_unused_elsewhere.side_effect = InUse("in use on another compute")
|
||||
res = self._post("delete_vol", {"dev": "vda", "path": VM_DISK["path"]})
|
||||
self.assertEqual([str(m) for m in get_messages(res.wsgi_request)], ["in use on another compute"])
|
||||
mock_wvm.return_value.detach_disk.assert_not_called()
|
||||
mock_storage.return_value.del_volume.assert_not_called()
|
||||
|
||||
def test_delete_vol_refuses_dev_not_attached_to_vm(self):
|
||||
self.client.force_login(self.superuser)
|
||||
|
|
|
|||
|
|
@ -1578,6 +1578,12 @@ def delete_vol(request, pk):
|
|||
_("Volume %(vol)s is used by another VM; detach it instead") % {"vol": disk["image"]},
|
||||
)
|
||||
return _back(request, pk, "disks")
|
||||
try: # before the disk is detached
|
||||
conn_delete.check_unused_elsewhere(
|
||||
disk["image"], lambda paths: utils.paths_used_on_other_computes(instance.compute, paths))
|
||||
except util.OperationError as err:
|
||||
messages.error(request, str(err))
|
||||
return _back(request, pk, "disks")
|
||||
|
||||
volume = f"{disk['storage']}/{disk['image']}"
|
||||
with audit_vm(request, instance, f"detach disk {dev}"):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue