1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 09:26:03 +00:00

fix(instances): saving a SPICE console's settings keeps it SPICE

The console type list offers VNC and Serial only, so the page of a SPICE
VM showed VNC and any save, of the keymap alone, switched the console:
libvirt refused the definition when the VM had a SPICE channel, and
without one the VM silently got a new VNC console. A console type the
panel does not create is now offered as it is, and kept on save.

A switch from SPICE that is chosen removes the SPICE channels, USB
redirection and smartcards libvirt refuses without SPICE graphics and
mutes SPICE audio. A new VNC console keeps the keymap and a password VNC
can take; a longer one is replaced by a generated password.

The console settings were read from the first graphics device that had
each attribute: with VNC and SPICE devices the page showed VNC's port
with SPICE's password. All are read from the first device, the one the
settings change.
This commit is contained in:
catborise 2026-11-13 22:27:31 +03:00
parent cd3edf4dff
commit 7fe79463fc
8 changed files with 276 additions and 176 deletions

View file

@ -1604,11 +1604,11 @@ class wvmInstance(wvmConnect):
def get_console_listener_addr(self, config=False):
listener_addr = util.get_xml_path(
self._XMLDesc(PERSISTENT_XML if config else 0), "/domain/devices/graphics/@listen"
self._XMLDesc(PERSISTENT_XML if config else 0), "/domain/devices/graphics[1]/@listen"
)
if listener_addr is None:
listener_addr = util.get_xml_path(
self._XMLDesc(PERSISTENT_XML if config else 0), "/domain/devices/graphics/listen/@address"
self._XMLDesc(PERSISTENT_XML if config else 0), "/domain/devices/graphics[1]/listen/@address"
)
if listener_addr is None:
return "127.0.0.1"
@ -1665,21 +1665,36 @@ class wvmInstance(wvmConnect):
serial console opens (libvirt adds the serial port for the console)."""
root = etree.fromstring(self._XMLDesc(PERSISTENT_XML))
devices = root.find("devices")
for graphic in devices.findall("graphics"):
graphics = devices.findall("graphics")
for graphic in graphics:
devices.remove(graphic)
# libvirt refuses SPICE channels, USB redirection and smartcards
# without SPICE graphics; SPICE audio goes silent
for device in devices.xpath("channel[@type='spicevmc' or @type='spiceport'] | redirdev[@type='spicevmc']"
" | smartcard[@type='spicevmc']"):
devices.remove(device)
for audio in devices.findall("audio[@type='spice']"):
audio.set("type", "none")
if console_type == "vnc":
etree.SubElement(devices, "graphics", type="vnc", port="-1", autoport="yes", listen="127.0.0.1")
vnc = etree.SubElement(devices, "graphics", type="vnc", port="-1", autoport="yes", listen="127.0.0.1")
# a SPICE console's keymap and password stay, a password VNC cannot take is replaced
if graphics and graphics[0].get("keymap"):
vnc.set("keymap", graphics[0].get("keymap"))
passwd = graphics[0].get("passwd") if graphics else None
if passwd is not None:
fits = len(passwd) <= 8 and all(" " <= c <= "~" for c in passwd)
vnc.set("passwd", passwd if fits else util.randomPasswd(8))
elif devices.find("console") is None:
etree.SubElement(devices, "console", type="pty")
return self._defineXML(etree.tostring(root).decode())
def get_console_socket(self):
socket = util.get_xml_path(self._XMLDesc(0), "/domain/devices/graphics/@socket")
socket = util.get_xml_path(self._XMLDesc(0), "/domain/devices/graphics[1]/@socket")
return socket
def get_console_type(self, config=False):
xml = self._XMLDesc(PERSISTENT_XML if config else 0)
console_type = util.get_xml_path(xml, "/domain/devices/graphics/@type")
console_type = util.get_xml_path(xml, "/domain/devices/graphics[1]/@type")
if console_type is None:
console_type = util.get_xml_path(xml, "/domain/devices/console/@type")
return console_type
@ -1689,13 +1704,13 @@ class wvmInstance(wvmConnect):
console_type = self.get_console_type()
port = util.get_xml_path(
self._XMLDesc(0),
"/domain/devices/graphics[@type='%s']/@port" % console_type,
"/domain/devices/graphics[1][@type='%s']/@port" % console_type,
)
return port
def get_console_passwd(self, config=False):
return util.get_xml_path(
self._XMLDesc(PERSISTENT_XML if config else VIR_DOMAIN_XML_SECURE), "/domain/devices/graphics/@passwd"
self._XMLDesc(PERSISTENT_XML if config else VIR_DOMAIN_XML_SECURE), "/domain/devices/graphics[1]/@passwd"
)
def set_console_passwd(self, passwd):
@ -1730,7 +1745,7 @@ class wvmInstance(wvmConnect):
def get_console_keymap(self, config=False):
return (
util.get_xml_path(
self._XMLDesc(PERSISTENT_XML if config else VIR_DOMAIN_XML_SECURE), "/domain/devices/graphics/@keymap"
self._XMLDesc(PERSISTENT_XML if config else VIR_DOMAIN_XML_SECURE), "/domain/devices/graphics[1]/@keymap"
)
or ""
)

View file

@ -0,0 +1,56 @@
"""A VM's console is its first graphics device: each setting is read from
it, and a switch away from SPICE leaves a definition libvirt accepts."""
import unittest
from lxml import etree
from vrtManager.instance import wvmInstance
SPICE = """<domain><devices>
<graphics type='spice' autoport='yes' keymap='de' passwd='%s'><listen type='address' address='0.0.0.0'/></graphics>
<channel type='spicevmc'><target type='virtio' name='com.redhat.spice.0'/></channel>
<channel type='unix'><target type='virtio' name='org.qemu.guest_agent.0'/></channel>
<redirdev bus='usb' type='spicevmc'/><smartcard mode='passthrough' type='spicevmc'/>
<audio id='1' type='spice'/><video><model type='qxl'/></video></devices></domain>"""
def vm(xml):
p = wvmInstance.__new__(wvmInstance) # no connection: only these calls
p._XMLDesc = lambda flags: xml
p.defined = []
p._defineXML = p.defined.append
return p
class ConsoleGraphicsTestCase(unittest.TestCase):
def test_a_switch_from_spice_drops_the_devices_that_need_it(self):
for console_type in ("vnc", "pty"):
with self.subTest(console_type):
p = vm(SPICE % "spice123")
p.set_console_type(console_type)
devices = etree.fromstring(p.defined[0]).find("devices")
self.assertEqual([c.get("type") for c in devices.findall("channel")], ["unix"])
self.assertEqual(devices.findall("redirdev") + devices.findall("smartcard"), [])
self.assertEqual(devices.find("audio").get("type"), "none")
self.assertEqual(devices.find("video/model").get("type"), "qxl")
def test_vnc_keeps_the_spice_keymap_and_a_password_it_can_take(self):
for passwd, kept in (("spice123", True), ("a-longer-spice-password", False)):
with self.subTest(passwd):
p = vm(SPICE % passwd)
p.set_console_type("vnc")
(graphic,) = etree.fromstring(p.defined[0]).findall("devices/graphics")
self.assertEqual((graphic.get("type"), graphic.get("listen"), graphic.get("keymap")),
("vnc", "127.0.0.1", "de"))
self.assertEqual(graphic.get("passwd") == passwd, kept)
self.assertEqual(len(graphic.get("passwd")), 8)
def test_every_setting_is_read_from_the_first_graphics_device(self):
# VNC without a password or keymap first: SPICE's must not be shown as its
p = vm("""<domain><devices><graphics type='vnc' port='5901'><listen type='address' address='127.0.0.1'/></graphics>
<graphics type='spice' port='5902' passwd='spice123' keymap='de'>
<listen type='address' address='0.0.0.0'/></graphics></devices></domain>""")
self.assertEqual((p.get_console_type(), p.get_console_port(), p.get_console_passwd(),
p.get_console_keymap(), p.get_console_listener_addr()),
("vnc", "5901", None, "", "127.0.0.1"))