From 0ac078b9ccb4e4ad36c8ac04d27b56c9735688c7 Mon Sep 17 00:00:00 2001 From: catborise Date: Sun, 8 Nov 2026 14:42:20 +0300 Subject: [PATCH] fix(vrtManager): an RBD disk takes its pool's auth and monitors as they are A disk of an RBD pool, at VM creation or added later, had the pool's user, secret uuid and monitors put into its XML as strings: a user with a quote or ampersand broke the XML, and a secret named by usage gave IndexError on attach and uuid='None' at creation. One function now copies the pool's auth (uuid or usage) and host elements into the disk's source with lxml, replacing the two string builders. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- instances/views.py | 6 ++-- vrtManager/create.py | 39 ++--------------------- vrtManager/instance.py | 16 ++-------- vrtManager/storage.py | 45 ++++++++++---------------- vrtManager/tests/test_rbd_disk.py | 52 +++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 82 deletions(-) create mode 100644 vrtManager/tests/test_rbd_disk.py diff --git a/instances/views.py b/instances/views.py index 40274517..fb392061 100755 --- a/instances/views.py +++ b/instances/views.py @@ -58,7 +58,7 @@ from vrtManager.create import cpu_choices, wvmCreate from vrtManager.instance import USAGE_MAX_WINDOW, wvmInstances from vrtManager.interface import wvmInterface from vrtManager.nodedevs import device_names, hostdev_problems, node_devices, pci_problems -from vrtManager.storage import wvmStorage +from vrtManager.storage import rbd_source_children, wvmStorage from vrtManager.util import randomPasswd from instances.models import Instance @@ -1307,7 +1307,7 @@ def add_new_vol(request, pk): format = util.vol_driver_type(conn_pool.get_volume_format_type(os.path.basename(source)), pool_type) if pool_type == "rbd": - source_info = conn_pool.get_rbd_source() + source_info = rbd_source_children(conn_pool.pool) else: # add more disk types to handle different pool and disk types source_info = None @@ -1382,7 +1382,7 @@ def add_existing_vol(request, pk): format_type = util.vol_driver_type(conn_create.get_volume_format_type(name), pool_type) disk_type = conn_create.get_volume_type(name) if pool_type == "rbd": - source_info = conn_create.get_rbd_source() + source_info = rbd_source_children(conn_create.pool) path = conn_create.get_source_name() source = f"{path}/{name}" else: diff --git a/vrtManager/create.py b/vrtManager/create.py index 7d016c7a..669fdd9a 100644 --- a/vrtManager/create.py +++ b/vrtManager/create.py @@ -5,7 +5,7 @@ import string import pycdlib from vrtManager import util from vrtManager.connection import wvmConnect -from vrtManager.storage import FILE_POOL_TYPES, wvmStorage +from vrtManager.storage import FILE_POOL_TYPES, rbd_source_children, wvmStorage # libvirt's CPU modes a new VM can have; any other value is a CPU model's name (mode custom) CPU_MODES = ("host-model", "host-passthrough", "maximum") @@ -38,27 +38,6 @@ def nocloud_seed(instance_id, hostname, user_data): return out.getvalue() -def get_rbd_storage_data(stg): - xml = stg.XMLDesc(0) - ceph_user = util.get_xml_path(xml, "/pool/source/auth/@username") - - def get_ceph_hosts(doc): - hosts = list() - for host in doc.xpath("/pool/source/host"): - name = host.get('name') - if name: - port = host.get('port') - if port: - hosts.append({"name": name, "port": port}) - else: - hosts.append({"name": name}) - return hosts - - ceph_hosts = util.get_xml_path(xml, func=get_ceph_hosts) - secret_uuid = util.get_xml_path(xml, "/pool/source/auth/secret/@uuid") - return ceph_user, secret_uuid, ceph_hosts - - class wvmCreate(wvmConnect): def create_volume(self, storage, name, size, image_format, metadata=False, disk_owner_uid=0, disk_owner_gid=0): """A new volume of size GiB in the pool storage, as the Storage page makes one; its path.""" @@ -242,21 +221,9 @@ class wvmCreate(wvmConnect): add_cdrom = "None" if stg_type == "rbd": - ceph_user, secret_uuid, ceph_hosts = get_rbd_storage_data(stg) xml += f""" - """ - xml += f""" - - - """ - if isinstance(ceph_hosts, list): - for host in ceph_hosts: - if host.get("port"): - xml += f""" - """ - else: - xml += f"""""" - xml += """""" + + {rbd_source_children(stg)}""" else: xml += f"""""" xml += f""" """ diff --git a/vrtManager/instance.py b/vrtManager/instance.py index 67be0836..142f94ec 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -1226,20 +1226,8 @@ class wvmInstance(wvmConnect): elif disk_type == "block": # a volume of an LVM, disk or iSCSI pool xml_disk += f"" elif disk_type == "network": - if pool_type == "rbd": - auth_type = source_info.get("auth_type") - auth_user = source_info.get("auth_user") - auth_uuid = source_info.get("auth_uuid") - xml_disk += f""" - - """ - xml_disk += f"""""" - for host in source_info.get("hosts"): - if host.get("hostport"): - xml_disk += f"""""" - else: - xml_disk += f"""""" - xml_disk += """""" + if pool_type == "rbd": # source_info: rbd_source_children of the pool + xml_disk += f"{source_info}" else: raise Exception("Not implemented disk type") else: diff --git a/vrtManager/storage.py b/vrtManager/storage.py index e0f99fb5..4b6b44ca 100644 --- a/vrtManager/storage.py +++ b/vrtManager/storage.py @@ -10,6 +10,22 @@ from vrtManager.connection import wvmConnect FILE_POOL_TYPES = ("dir", "fs", "netfs") +def rbd_source_children(pool): + """The and elements of an RBD disk's , as XML text: + the disk reaches Ceph as its pool does, with its user, its secret (by + uuid or usage) and its monitors.""" + source = etree.fromstring(pool.XMLDesc(0)).find("source") + children = [etree.Element("host", host.attrib) for host in source.findall("host")] + auth = source.find("auth") + if auth is not None: + disk_auth = etree.Element("auth", username=auth.get("username")) + secret = auth.find("secret") + etree.SubElement(disk_auth, "secret", type=auth.get("type"), + **{key: value for key, value in secret.attrib.items() if key in ("uuid", "usage")}) + children.insert(0, disk_auth) + return "".join(etree.tostring(child, encoding="unicode") for child in children) + + def volume_details(vol): """A volume's name, capacity (size), allocation and format, from its XML: one libvirt call, which refreshes the volume as info() does. Logical and @@ -220,35 +236,6 @@ class wvmStorage(wvmConnect): def get_source_name(self): return util.get_xml_path(self._XMLDesc(0), "/pool/source/name") - def get_rbd_source(self): - def hosts(doc): - hosts_array = [] - - for host in doc.xpath("/pool/source/host"): - name = host.get("name") - if name: - port = host.get("port") - if port: - hosts_array.append({"hostname": name, "hostport": port}) - else: - hosts_array.append({"hostname": name}) - - name = doc.get("name") - auth = doc.xpath("/pool/source/auth") - auth_type = auth[0].get("type") - auth_user = auth[0].get("username") - auth_uuid = auth[0].xpath("secret/@uuid")[0] - - return { - "name": name, - "auth_type": auth_type, - "auth_user": auth_user, - "auth_uuid": auth_uuid, - "hosts": hosts_array, - } - - return util.get_xml_path(self._XMLDesc(0), func=hosts) - def get_volumes(self): return self.pool.listVolumes() diff --git a/vrtManager/tests/test_rbd_disk.py b/vrtManager/tests/test_rbd_disk.py new file mode 100644 index 00000000..cda8bdbf --- /dev/null +++ b/vrtManager/tests/test_rbd_disk.py @@ -0,0 +1,52 @@ +"""An RBD disk reaches Ceph as its pool does: the pool's user, its secret by +uuid or by usage and its monitors go into the disk's source, as text.""" + +import unittest +from unittest.mock import MagicMock + +from lxml import etree +from vrtManager.instance import wvmInstance +from vrtManager.storage import rbd_source_children + +ODD = "a'b\"c&e" + + +def pool(auth): + rbd = MagicMock() + rbd.XMLDesc.return_value = ( + f"cephrbd" + f"{auth}") + return rbd + + +def source(children): + return etree.fromstring(f"{children}") + + +class RbdSourceTests(unittest.TestCase): + def test_the_secret_by_uuid_or_usage_and_the_monitors(self): + for secret, attrib in (("uuid='aaaaaaaa-0000-4000-8000-000000000001'", {"uuid": "aaaaaaaa-0000-4000-8000-000000000001"}), + ("usage='ceph-key'", {"usage": "ceph-key"})): + with self.subTest(secret): + rbd = pool(f"") + disk_source = source(rbd_source_children(rbd)) + self.assertEqual(disk_source.find("auth").get("username"), "libvirt") + self.assertEqual(disk_source.find("auth/secret").attrib, {"type": "ceph", **attrib}) + self.assertEqual([host.attrib for host in disk_source.findall("host")], + [{"name": "mon1.example.com", "port": "6789"}, {"name": "mon2.example.com"}]) + + def test_values_are_text_and_a_pool_without_auth_has_none(self): + escaped = ODD.replace("&", "&").replace("<", "<").replace("'", "'") + disk_source = source(rbd_source_children(pool(f""))) + self.assertEqual(disk_source.find("auth").get("username"), ODD) + self.assertIsNone(source(rbd_source_children(pool(""))).find("auth")) + + def test_an_attached_disk_takes_them(self): + inst = wvmInstance.__new__(wvmInstance) + inst.instance = MagicMock() + inst.instance.isActive.return_value = False + inst.attach_disk("vdb", "rbd/vm", rbd_source_children(pool("")), + pool_type="rbd", target_bus="virtio", disk_type="network") + disk = etree.fromstring(inst.instance.attachDeviceFlags.call_args.args[0]) + self.assertEqual(disk.find("source").get("name"), "rbd/vm") + self.assertEqual(disk.find("source/auth/secret").get("usage"), "k")