virt.init: fix the name of volumes reused in disk-types pools

Only compute the partition name if no source_file was provided by the
user for a pool of disk type.
This commit is contained in:
Cédric Bosdonnat 2020-05-29 14:13:33 +02:00 committed by Daniel Wozniak
parent 4de137d209
commit 66bcc5dee3
3 changed files with 28 additions and 12 deletions

1
changelog/57497.fixed Normal file
View file

@ -0,0 +1 @@
Fix volume name for disk-typed pools in virt.defined

View file

@ -1319,6 +1319,23 @@ def _fill_disk_filename(conn, vm_name, disk, hypervisor, pool_caps):
pool_xml = ElementTree.fromstring(pool_obj.XMLDesc())
pool_type = pool_xml.get("type")
# Disk pools volume names are partition names, they need to be named based on the device name
if pool_type == "disk":
device = pool_xml.find("./source/device").get("path")
all_volumes = pool_obj.listVolumes()
if disk.get("source_file") not in all_volumes:
indexes = [
int(re.sub("[a-z]+", "", vol_name)) for vol_name in all_volumes
] or [0]
index = min(
[
idx
for idx in range(1, max(indexes) + 2)
if idx not in indexes
]
)
disk["filename"] = "{}{}".format(os.path.basename(device), index)
# Is the user wanting to reuse an existing volume?
if disk.get("source_file"):
if not disk.get("source_file") in pool_obj.listVolumes():
@ -1346,18 +1363,6 @@ def _fill_disk_filename(conn, vm_name, disk, hypervisor, pool_caps):
else:
disk["format"] = volume_options.get("default_format", None)
# Disk pools volume names are partition names, they need to be named based on the device name
if pool_type == "disk":
device = pool_xml.find("./source/device").get("path")
indexes = [
int(re.sub("[a-z]+", "", vol_name))
for vol_name in pool_obj.listVolumes()
] or [0]
index = min(
[idx for idx in range(1, max(indexes) + 2) if idx not in indexes]
)
disk["filename"] = "{}{}".format(os.path.basename(device), index)
elif hypervisor == "bhyve" and vm_name:
disk["filename"] = "{}.{}".format(vm_name, disk["name"])
disk["source_file"] = os.path.join(

View file

@ -929,6 +929,16 @@ class VirtTestCase(TestCase, LoaderModuleMockMixin):
)
self.assertEqual(diskp[0]["filename"], ("vdb2"))
# Reuse existing volume case
diskp = virt._disk_profile(
self.mock_conn,
None,
"kvm",
[{"name": "mydisk", "pool": "test-vdb", "source_file": "vdb1"}],
"hello",
)
self.assertEqual(diskp[0]["filename"], ("vdb1"))
def test_gen_xml_volume(self):
"""
Test virt._gen_xml(), generating a disk of volume type