1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-08 14:33:10 +03:00
parent d2383be914
commit 8f09812b7d
7 changed files with 41 additions and 22 deletions

View file

@ -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)

View file

@ -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"))

View file

@ -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": ""}],
})

View file

@ -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": "",

View file

@ -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",

View file

@ -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}));
});
}

View file

@ -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)