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

fix(instances): a disk or NIC form sent twice adds the device once

libvirt takes the same disk file or MAC twice in a persistent
definition. A double click or a resent request then attached an
existing volume twice, and a shut-off VM did not start again (QEMU
opens no file twice for writing), or added a second NIC with the MAC
the form had filled in. Attaching a volume that is already a disk of
the VM, live or in its next start, and adding a NIC with a MAC the VM
has are now refused with the reason.
This commit is contained in:
catborise 2026-11-05 11:41:25 +03:00
parent 7f53676927
commit 5174c54982
6 changed files with 301 additions and 210 deletions

View file

@ -0,0 +1,62 @@
"""A device form sent twice (a double click, a resent request) adds the
device once: libvirt takes the same disk file or MAC twice in the
persistent definition."""
from unittest.mock import MagicMock, patch
from django.contrib.auth import get_user_model
from django.contrib.messages import get_messages
from django.test import TestCase
from django.urls import reverse
from computes.models import Compute
from instances.models import Instance
PATH = "/var/lib/libvirt/images/data.qcow2"
MAC = "52:54:00:aa:bb:cc"
class DuplicateDeviceTests(TestCase):
def setUp(self):
self.client.force_login(get_user_model().objects.create_superuser("dup-admin", "d@example.com", "pw"))
compute = Compute.objects.create(name="dup-c", hostname="192.0.2.40", login="root", password="", type=1)
self.vm = Instance.objects.create(compute=compute, name="dup-vm", uuid="12121212-0000-1111-2222-333333333333")
self.proxy = MagicMock()
self.proxy.get_disk_devices.return_value = []
self.proxy.get_media_devices.return_value = []
self.proxy.get_net_devices.return_value = []
self.proxy.get_disk_bus_types.return_value = ["virtio"]
self.proxy.get_cache_modes.return_value = {"default": "Default"}
patcher = patch("instances.models.wvmInstance", return_value=self.proxy)
patcher.start()
self.addCleanup(patcher.stop)
def post(self, name, data):
response = self.client.post(reverse(f"instances:{name}", args=[self.vm.pk]), data)
return [str(m) for m in get_messages(response.wsgi_request)]
@patch("instances.views.wvmStorage")
def test_a_disk_of_the_vm_is_not_attached_again(self, storage):
pool = storage.return_value
pool.get_volumes.return_value = ["data.qcow2"]
pool.get_type.return_value = "dir"
pool.get_volume_format_type.return_value = "qcow2"
pool.get_volume_type.return_value = "file"
pool.get_volume.return_value.path.return_value = PATH
form = {"selected_storage": "default", "vols": "data.qcow2", "bus": "virtio", "cache": "default"}
self.post("add_existing_vol", form)
self.proxy.attach_disk.assert_called_once()
# the first attach is in the persistent definition only (a shut-off VM)
self.proxy.get_disk_devices.side_effect = lambda config=False: [{"path": PATH, "dev": "vdb"}] if config else []
self.assertEqual(self.post("add_existing_vol", form), ["data.qcow2 is already a disk of this VM"])
self.proxy.attach_disk.assert_called_once()
def test_a_nic_with_the_mac_of_another_is_not_added(self):
form = {"add-net-network": "net:default", "add-net-mac": MAC, "add-net-model": "virtio"}
self.post("add_network", form)
self.proxy.add_network.assert_called_once()
self.proxy.get_net_devices.side_effect = lambda config=False: [{"mac": MAC}] if config else []
form["add-net-mac"] = MAC.upper()
self.assertEqual(self.post("add_network", form),
[f"The VM already has a network device with MAC {MAC.upper()}"])
self.proxy.add_network.assert_called_once()

View file

@ -1417,6 +1417,11 @@ def add_existing_vol(request, pk):
# an iSCSI volume's path is not the pool target plus its name
source = conn_create.get_volume(name).path()
# a form sent twice would attach the file twice, and the VM would not start again
if source in {disk["path"] for disk in instance.disks + instance.config_disks}:
messages.error(request, _("%(volume)s is already a disk of this VM") % {"volume": name})
return _back(request, pk, "disks")
target_dev = utils.get_new_disk_dev(
instance.media + instance.config_media, instance.disks + instance.config_disks, bus
)
@ -2014,6 +2019,10 @@ def add_network(request, pk):
messages.error(request, _("No network selected"))
return _back(request, pk, "network")
mac = request.POST.get("add-net-mac")
# the form fills in a random MAC: a form sent twice would add a second NIC with it
if mac and mac.lower() in {net["mac"].lower() for net in instance.networks + instance.config_networks}:
messages.error(request, _("The VM already has a network device with MAC %(mac)s") % {"mac": mac})
return _back(request, pk, "network")
nwfilter = request.POST.get("add-net-nwfilter")
(source, source_type) = utils.get_network_tuple(network)
model = request.POST.get("add-net-model")