From 8f09812b7d1808f5296819490623290919b082fc Mon Sep 17 00:00:00 2001 From: catborise Date: Sun, 8 Nov 2026 14:33:10 +0300 Subject: [PATCH] fix(instances): a new VM's existing volume or template is taken from the chosen pool The create job looked volumes up by name in every active pool and took the first match, so a volume of the same name in another pool could be attached or copied in place of the chosen one. The form now sends each existing volume as pool/volume and the template's pool, and the job looks each up in its own pool. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- instances/forms.py | 3 ++- instances/jobs.py | 11 ++++++----- instances/tests/test_create_form_input.py | 6 +++--- instances/tests/test_create_instance.py | 21 ++++++++++++++++++++- instances/tests/test_live_views.py | 2 +- static/js/create_instance.js | 3 ++- vrtManager/create.py | 17 +++++++---------- 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/instances/forms.py b/instances/forms.py index 41c9c677..d4dd78bf 100644 --- a/instances/forms.py +++ b/instances/forms.py @@ -117,7 +117,8 @@ class NewVMForm(forms.Form): net_model = forms.CharField(max_length=256, required=False) storage = forms.CharField(max_length=256, required=False) template = forms.CharField(max_length=256, required=False) - images = forms.CharField(max_length=256, required=False) + template_pool = forms.CharField(max_length=256, required=False) + images = forms.CharField(max_length=256, required=False) # existing volumes, each "pool/volume" cache_mode = forms.CharField(max_length=16) hdd_size = forms.IntegerField(min_value=1, required=False) meta_prealloc = forms.BooleanField(required=False) diff --git a/instances/jobs.py b/instances/jobs.py index c8a2c926..9d1343f4 100644 --- a/instances/jobs.py +++ b/instances/jobs.py @@ -811,7 +811,7 @@ def _seed_pool(conn, data): """The pool of a new VM's cloud-init seed: the one of its disk.""" if data["storage"]: return conn.get_storage(data["storage"]) - return conn.get_storage_by_vol_path(conn.get_volume_path(data["images"].split(",")[0])) + return conn.get_storage(data["images"].split(",")[0].partition("/")[0]) @register @@ -822,7 +822,7 @@ class CreateInstance(Kind): def normalize(self, raw): # what the API leaves out means what create_instance did by default - params = {"template": "", "images": "", "firmware": "", "net_model": "virtio", "add_cdrom": "sata", + params = {"template": "", "template_pool": "", "images": "", "firmware": "", "net_model": "virtio", "add_cdrom": "sata", "add_input": "default", "meta_prealloc": False, "hdd_size": 0, "cloud_init": False, "tpm": False, "mac": "", "nwfilter": ""} params.update({k: v for k, v in raw.items() if k not in ("submission_key", "create", "csrfmiddlewaretoken")}) @@ -922,13 +922,14 @@ class CreateInstance(Kind): created.append(path) volume_list.append(volume(path)) elif data["template"]: - path = conn.clone_from_template(data["name"], conn.get_volume_path(data["template"]), data["storage"], + path = conn.clone_from_template(data["name"], conn.get_volume_path(data["template"], data["template_pool"]), data["storage"], meta_prealloc, default_disk_owner_uid, default_disk_owner_gid) created.append(path) volume_list.append(volume(path)) else: - for vol, (device, bus) in zip(data["images"].split(","), data["devices"]): - volume_list.append(volume(conn.get_volume_path(vol), device, bus)) + for image, (device, bus) in zip(data["images"].split(","), data["devices"]): + pool, _sep, name = image.partition("/") # an existing volume is "pool/volume" + volume_list.append(volume(conn.get_volume_path(name, pool), device, bus)) if data["cloud_init"]: ctx.set_phase(gettext_noop("Writing the cloud-init seed")) diff --git a/instances/tests/test_create_form_input.py b/instances/tests/test_create_form_input.py index a5b80120..71646d65 100644 --- a/instances/tests/test_create_form_input.py +++ b/instances/tests/test_create_form_input.py @@ -256,13 +256,13 @@ class SubmittedListsTests(TestCase): def test_a_refused_form_sends_back_its_lists(self): _, response = render_create_page(self, valid( - name="bad name!", disk_source="existing", **{"storage-control": "default"}, images="a.qcow2,b.qcow2", + name="bad name!", disk_source="existing", **{"storage-control": "default"}, images="default/a.qcow2,default/b.qcow2", device0="cdrom", bus0="sata", device1="disk", bus1="virtio", networks="default,isolated", mac="52:54:00:00:00:01")) self.assertEqual(self.lists(response), { "pool": "default", - "disks": [{"volume": "a.qcow2", "device": "cdrom", "bus": "sata"}, - {"volume": "b.qcow2", "device": "disk", "bus": "virtio"}], + "disks": [{"volume": "default/a.qcow2", "device": "cdrom", "bus": "sata"}, + {"volume": "default/b.qcow2", "device": "disk", "bus": "virtio"}], "networks": [{"name": "default", "mac": "52:54:00:00:00:01"}, {"name": "isolated", "mac": ""}], }) diff --git a/instances/tests/test_create_instance.py b/instances/tests/test_create_instance.py index 0931de31..d0bcf1c4 100644 --- a/instances/tests/test_create_instance.py +++ b/instances/tests/test_create_instance.py @@ -45,6 +45,25 @@ class CreateInstanceViewTests(TestCase): self.assertEqual(conn.create_instance.call_args.kwargs["volumes"][0]["bus"], "virtio") self.assertTrue(Instance.objects.filter(name="new-vm").exists()) + @patch("instances.views.wvmCreate", new=wvm_create) + @patch("instances.views.NewVMForm") + def test_a_volume_is_taken_from_its_own_pool(self, form_class): + # another pool may have a volume of the same name + _, conn = self.create(form_class) + run(Operation.objects.get()) + conn.get_volume_path.assert_called_once_with("base.qcow2", "pool-b") + + Operation.objects.all().delete() + Instance.objects.all().delete() + paths = {("base.qcow2", "pool-b"): "/pool-b/base.qcow2"} # the copy's name is free + conn.get_volume_path.side_effect = lambda name, pool: paths.get((name, pool)) + try: + _, conn = self.create(form_class, images="", template="base.qcow2", template_pool="pool-b", storage="default") + run(Operation.objects.get()) + finally: + conn.get_volume_path.side_effect = None + self.assertEqual(conn.clone_from_template.call_args.args[:3], ("new-vm", "/pool-b/base.qcow2", "default")) + @patch("instances.views.wvmCreate", new=wvm_create) @patch("instances.views.NewVMForm") def test_secure_boot_and_a_tpm_reach_the_definition(self, form_class): @@ -120,7 +139,7 @@ class CreateInstanceViewTests(TestCase): form.is_valid.return_value = True form.cleaned_data = { "name": "new-vm", "meta_prealloc": False, "hdd_size": 0, - "template": "", "images": "base.qcow2", "cache_mode": "default", + "template": "", "template_pool": "", "images": "pool-b/base.qcow2", "cache_mode": "default", "firmware": "BIOS", "net_model": "virtio", "memory": 1024, "vcpu": 1, "vcpu_mode": "host-model", "networks": "default", "virtio": True, "listener_addr": "0.0.0.0", "nwfilter": "", diff --git a/instances/tests/test_live_views.py b/instances/tests/test_live_views.py index 689e12f4..9a41e634 100644 --- a/instances/tests/test_live_views.py +++ b/instances/tests/test_live_views.py @@ -175,7 +175,7 @@ class InstancesTestCase(TestCase): "memory": 128, "device0": "disk", "bus0": "virtio", - "images": "wvc-test-volume.qcow2", + "images": f"{livetest.POOL}/wvc-test-volume.qcow2", "storage-control": livetest.POOL, "image-control": "wvc-test.qcow2", "networks": "default", diff --git a/static/js/create_instance.js b/static/js/create_instance.js index 1642ffd3..cabdae4e 100644 --- a/static/js/create_instance.js +++ b/static/js/create_instance.js @@ -155,7 +155,8 @@ function load_list(list, url, fill) { function load_volumes(pool) { images_control.clearOptions(); // keeps the chosen volumes return load_list(images_control, volumes_url(pool), function (data) { - data['vols'].forEach(item => images_control.addOption({value: item, text: item})); + // a pool's volumes have their own names; the chosen ones may come from several pools + data['vols'].forEach(item => images_control.addOption({value: pool + "/" + item, text: item})); }); } diff --git a/vrtManager/create.py b/vrtManager/create.py index 727065df..3d66a87f 100644 --- a/vrtManager/create.py +++ b/vrtManager/create.py @@ -101,16 +101,13 @@ class wvmCreate(wvmConnect): vol = self.get_volume_by_path(path) return util.vol_driver_type(util.get_xml_path(vol.XMLDesc(0), "/volume/target/format/@type")) - def get_volume_path(self, volume, pool=None): - storages = [pool] if pool else self.get_storages(only_actives=True) - for storage in storages: - stg = self.get_storage(storage) - if stg.info()[0] != 0: - stg.refresh(0) - for img in stg.listVolumes(): - if img == volume: - vol = stg.storageVolLookupByName(img) - return vol.path() + def get_volume_path(self, volume, pool): + """The path of that volume of the pool; None if an active pool has no such volume.""" + stg = self.get_storage(pool) + if stg.info()[0] != 0: + stg.refresh(0) + if volume in stg.listVolumes(): + return stg.storageVolLookupByName(volume).path() def get_storage_by_vol_path(self, vol_path): vol = self.get_volume_by_path(vol_path)