Skip to content

Commit

Permalink
virt.init: fix the name of volumes reused in disk-types pools
Browse files Browse the repository at this point in the history
Only compute the partition name if no source_file was provided by the
user for a pool of disk type.
  • Loading branch information
cbosdo committed Jun 9, 2020
1 parent a49109c commit 47b7f73
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog/57497.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix volume name for disk-typed pools in virt.defined
29 changes: 17 additions & 12 deletions salt/modules/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,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():
Expand Down Expand Up @@ -1347,18 +1364,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"] = "{0}.{1}".format(vm_name, disk["name"])
disk["source_file"] = os.path.join(
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/modules/test_virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,16 @@ def test_disk_profile_pool_disk_type(self):
)
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
Expand Down

0 comments on commit 47b7f73

Please sign in to comment.