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

fix(instances): a disk form loaded before the disk changed changes nothing

The Edit, Detach and Delete forms of a VM's disk name it by its device:
the view acted on whatever disk had that device by then. A form loaded
before a disk move, an external snapshot or another tab's change wrote the
old path back over the new one (the VM then starts from an old or deleted
file), or detached and deleted the volume that replaced it.

The forms already send the path they show. The views now refuse a disk
whose path is not that one, and tell the user to reload the page. Turkish
and Chinese texts included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-11 12:39:40 +03:00
parent a53a3623d2
commit 34048dce83
10 changed files with 433 additions and 374 deletions

View file

@ -227,7 +227,7 @@
<div class="col-12 col-md-4 d-flex flex-wrap justify-content-md-end gap-2">
<form class="d-inline" action="{% url 'instances:edit_volume' instance.id %}" method="post" aria-label="{% trans "Edit instance volume form" %}">
{% csrf_token %}
<input type="hidden" name="path" value="{{ disk.path }}">
<input type="hidden" name="path" value="{{ disk.path|default:'' }}">
<input type="hidden" name="dev" value="{{ disk.dev }}">
<input type="hidden" name="storage" value="{{ disk.storage }}">
<input type="hidden" name="name" value="{{ disk.image }}">
@ -236,7 +236,7 @@
{% if request.user.is_superuser %}{% include 'instances/move_disk.html' with id=forloop.counter0 %}{% endif %}
<form class="d-inline" action="{% url 'instances:detach_vol' instance.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="path" value="{{ disk.path }}">
<input type="hidden" name="path" value="{{ disk.path|default:'' }}">
<input type="hidden" name="dev" value="{{ disk.dev }}">
<input type="hidden" name="storage" value="{{ disk.storage }}">
<input type="hidden" name="name" value="{{ disk.image }}">
@ -244,7 +244,7 @@
</form>
<form class="d-inline" action="{% url 'instances:delete_vol' instance.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="path" value="{{ disk.path }}">
<input type="hidden" name="path" value="{{ disk.path|default:'' }}">
<input type="hidden" name="dev" value="{{ disk.dev }}">
<input type="hidden" name="storage" value="{{ disk.storage }}">
<input type="hidden" name="name" value="{{ disk.image }}">

View file

@ -213,7 +213,7 @@ class DestroyOrderTestCase(TransactionTestCase):
proxy.paths_used_by_other_domains.return_value = set()
proxy.wait_disk_detached.return_value = True
storage.return_value.del_volume.side_effect = OperationError("Volume data.qcow2 is in use")
response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vdb"})
response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vdb", "path": "/pool/data.qcow2"})
self.assertEqual(response.status_code, 302)
proxy.detach_disk.assert_called_once_with("vdb")
messages = [str(m) for m in get_messages(response.wsgi_request)]

View file

@ -112,7 +112,7 @@ class DiskViewsTenantIsolationTestCase(TestCase):
mock_wvm.return_value.get_managed_save_image.return_value = 0
res = self._post(
"delete_vol",
{"storage": "other-pool", "name": "victim.qcow2", "dev": "vda"},
{"storage": "other-pool", "name": "victim.qcow2", "dev": "vda", "path": VM_DISK["path"]},
)
self.assertEqual(res.status_code, 302)
mock_wvm.return_value.detach_disk.assert_called_once_with("vda")
@ -149,6 +149,7 @@ class DiskViewsTenantIsolationTestCase(TestCase):
{
"edit_volume": "1",
"dev": "vda",
"path": VM_DISK["path"],
"vol_path": VM_DISK["path"],
"vol_bus_old": "virtio",
"vol_bus": "virtio",
@ -276,6 +277,7 @@ class DiskViewsTenantIsolationTestCase(TestCase):
{
"edit_volume": "1",
"dev": "vda",
"path": VM_DISK["path"],
"vol_path": VM_DISK["path"],
"vol_bus_old": "virtio",
"vol_bus": "virtio",
@ -297,6 +299,7 @@ class DiskViewsTenantIsolationTestCase(TestCase):
data = {
"edit_volume": "1",
"dev": "vda",
"path": VM_DISK["path"],
"vol_path": VM_DISK["path"],
"vol_bus_old": "virtio",
"vol_bus": "virtio",
@ -306,6 +309,34 @@ class DiskViewsTenantIsolationTestCase(TestCase):
data.update(extra)
return self._post("edit_volume", data)
def test_a_form_of_a_disk_replaced_since_changes_nothing(self):
# another tab moved vda to another volume after this page was loaded
moved = {**VM_DISK, "path": "/var/lib/libvirt/images/moved.qcow2"}
self.client.force_login(self.superuser)
for name in ("edit_volume", "detach_vol", "delete_vol"):
with self.subTest(name), patch("instances.models.wvmInstance") as mock_wvm, \
patch("instances.views.wvmStorage") as mock_storage:
proxy = mock_wvm.return_value
proxy.get_disk_devices.return_value = [moved]
res = self._post(name, {"edit_volume": "1", "dev": "vda", "path": VM_DISK["path"],
"vol_path": VM_DISK["path"], "vol_bus": "virtio"})
self.assertIn("Disk vda has changed since the page was loaded; reload the page",
[str(m) for m in get_messages(res.wsgi_request)])
proxy.edit_disk.assert_not_called()
proxy.detach_disk.assert_not_called()
mock_storage.return_value.del_volume.assert_not_called()
def test_a_disk_without_a_path_is_detached(self):
# a network disk with only a host (NBD): the form posts an empty path
self.client.force_login(self.superuser)
with patch("instances.models.wvmInstance") as mock_wvm:
mock_wvm.return_value.get_disk_devices.return_value = [{**VM_DISK, "path": None}]
self._post("detach_vol", {"dev": "vda", "path": ""})
mock_wvm.return_value.detach_disk.assert_called_once_with("vda")
mock_wvm.return_value.detach_disk.reset_mock()
self._post("detach_vol", {"dev": "vda"}) # a form without the path
mock_wvm.return_value.detach_disk.assert_not_called()
def test_edit_volume_bus_change_keeps_the_disk_and_applies_options(self):
from vrtManager.instance import wvmInstance

View file

@ -201,7 +201,7 @@ class PendingEditsTestCase(TestCase):
proxy = wvm.return_value
live, pending = disk("vda", "default", ""), {**disk("vda", "default", ""), "path": "/pool/b.qcow2"}
proxy.get_disk_devices.side_effect = lambda **kw: [pending] if kw.get("config") else [live]
self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda"})
self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda", "path": "/pool/b.qcow2"})
proxy.detach_disk.assert_not_called()
storage.return_value.del_volume.assert_not_called()
@ -211,7 +211,7 @@ class PendingEditsTestCase(TestCase):
proxy = wvm.return_value
proxy.get_managed_save_image.return_value = 1
proxy.get_disk_devices.side_effect = lambda **kw: [disk("vda", "default", "")]
response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda"})
response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda", "path": "/pool/vda.qcow2"})
self.assertEqual([str(m) for m in get_messages(response.wsgi_request)],
["The VM has a saved state; start it or remove the saved state first"])
proxy.detach_disk.assert_not_called()

View file

@ -150,9 +150,9 @@ class VMRowTests(TestCase):
self.proxy.paths_used_by_other_domains.return_value = set()
with patch("instances.views.wvmStorage"):
self.proxy.wait_disk_detached.return_value = False
self.post("delete_vol", {"dev": "vdb"})
self.post("delete_vol", {"dev": "vdb", "path": "/pool/data.qcow2"})
self.proxy.wait_disk_detached.return_value = True
self.post("delete_vol", {"dev": "vdb"})
self.post("delete_vol", {"dev": "vdb", "path": "/pool/data.qcow2"})
self.assertEqual(self.rows(), [
("VM vm: detach disk vdb", True),
("VM vm: delete volume default/data.qcow2: the guest has not released the disk", False),

View file

@ -569,6 +569,18 @@ def _back(request, pk, tab):
return redirect(same_origin_referer(request, reverse("instances:instance", args=[pk])) + "#" + tab)
def _disk_changed(request, disks):
"""Whether the disk a form was loaded with, its device and path, is no
longer one of disks: another tab or user replaced it since. The user is
told to reload."""
dev = request.POST.get("dev", "")
# a network disk may have no path: the form posts an empty one
if any(disk["dev"] == dev and (disk["path"] or "") == request.POST.get("path") for disk in disks):
return False
messages.error(request, _("Disk %(dev)s has changed since the page was loaded; reload the page") % {"dev": dev})
return True
def get_instance(user, pk, perm_type="view"):
"""
Check that instance is available for user, if not raise 404 or PermissionDenied.
@ -1429,6 +1441,8 @@ def edit_volume(request, pk):
shareable = bool(request.POST.get("vol_shareable", False))
readonly = bool(request.POST.get("vol_readonly", False))
disks = instance.proxy.get_disk_devices(config=True)
if _disk_changed(request, disks):
return _back(request, pk, "disks")
current = next((disk for disk in disks if disk["dev"] == target_dev), {})
new_bus = request.POST.get("vol_bus", current.get("bus"))
serial = request.POST.get("vol_serial", "")
@ -1458,8 +1472,6 @@ def edit_volume(request, pk):
format=format or None,
serial=serial,
)
if not current:
invalid.append("dev")
if invalid:
return reject_disk_options(request, pk, invalid)
@ -1501,6 +1513,8 @@ def delete_vol(request, pk):
# Resolve the volume from the VM's own XML; never trust the posted
# storage/name, which could point at any volume on the compute.
if _disk_changed(request, instance.config_disks):
return _back(request, pk, "disks")
disk = next((d for d in instance.disks if d["dev"] == dev), None)
if disk is None or not disk["storage"] or not disk["image"]:
messages.error(
@ -1565,6 +1579,8 @@ def delete_vol(request, pk):
def detach_vol(request, pk):
instance = get_instance(request.user, pk, perm_type="change")
dev = request.POST.get("dev", "")
if _disk_changed(request, instance.config_disks):
return _back(request, pk, "disks")
with audit_vm(request, instance, f"detach disk {dev}"):
instance.proxy.detach_disk(dev)