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

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-08 14:42:20 +03:00
parent f94124c891
commit 0ac078b9cc
5 changed files with 76 additions and 82 deletions

View file

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

View file

@ -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"""<disk type='network' device='disk'>
<driver name='qemu' type='{volume["type"]}' {disk_opts} />"""
xml += f""" <auth username='{ceph_user}'>
<secret type='ceph' uuid='{secret_uuid}'/>
</auth>
<source protocol='rbd' name='{volume["path"]}'>"""
if isinstance(ceph_hosts, list):
for host in ceph_hosts:
if host.get("port"):
xml += f"""
<host name='{host.get("name")}' port='{host.get("port")}'/>"""
else:
xml += f"""<host name='{host.get("name")}'/>"""
xml += """</source>"""
<driver name='qemu' type='{volume["type"]}' {disk_opts} />
<source protocol='rbd' name='{util.xml_escape(volume["path"])}'>{rbd_source_children(stg)}</source>"""
else:
xml += f"""<disk type='file' device='{volume["device"]}'>"""
xml += f""" <driver name='qemu' type='{volume["type"]}' {disk_opts}/>"""

View file

@ -1226,20 +1226,8 @@ class wvmInstance(wvmConnect):
elif disk_type == "block": # a volume of an LVM, disk or iSCSI pool
xml_disk += f"<source dev='{source}'/>"
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"""<auth username='{auth_user}'>
<secret type='{auth_type}' uuid='{auth_uuid}'/>
</auth>"""
xml_disk += f"""<source protocol='{pool_type}' name='{source}'>"""
for host in source_info.get("hosts"):
if host.get("hostport"):
xml_disk += f"""<host name="{host.get('hostname')}" port='{host.get('hostport')}'/>"""
else:
xml_disk += f"""<host name="{host.get('hostname')}"/>"""
xml_disk += """</source>"""
if pool_type == "rbd": # source_info: rbd_source_children of the pool
xml_disk += f"<source protocol='rbd' name='{source}'>{source_info}</source>"
else:
raise Exception("Not implemented disk type")
else:

View file

@ -10,6 +10,22 @@ from vrtManager.connection import wvmConnect
FILE_POOL_TYPES = ("dir", "fs", "netfs")
def rbd_source_children(pool):
"""The <auth> and <host> elements of an RBD disk's <source>, 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()

View file

@ -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<d>&e"
def pool(auth):
rbd = MagicMock()
rbd.XMLDesc.return_value = (
f"<pool type='rbd'><name>ceph</name><source><name>rbd</name>"
f"<host name='mon1.example.com' port='6789'/><host name='mon2.example.com'/>{auth}</source></pool>")
return rbd
def source(children):
return etree.fromstring(f"<source protocol='rbd' name='rbd/vm'>{children}</source>")
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"<auth type='ceph' username='libvirt'><secret {secret}/></auth>")
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("&", "&amp;").replace("<", "&lt;").replace("'", "&apos;")
disk_source = source(rbd_source_children(pool(f"<auth type='ceph' username='{escaped}'><secret usage='k'/></auth>")))
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("<auth type='ceph' username='libvirt'><secret usage='k'/></auth>")),
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")