]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virDomainDefAddConsoleCompat: Fix numbering of console targets after modification
authorPeter Krempa <pkrempa@redhat.com>
Mon, 18 Dec 2023 15:41:01 +0000 (16:41 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 17 Jan 2024 16:31:12 +0000 (17:31 +0100)
The XML parser for consoles sets the 'port=' attribute of '<target' to
be always the index of the console.

Thus when the "really crazy backcompat stuff for consoles" function
modifies the order of consoles by inserting the default one for a serial
port it must re-number the ports to ensure that the value will not
change on subsequent parse.

This luckily didn't cause any visible changes to the VM as the port
number isn't used for anything.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/domain_conf.c
tests/qemuxml2xmloutdata/console-compat2.x86_64-latest.xml

index 54fd8dbcf0eff9af53049c8ded843b2982a3d378..8bddd2c1db8994bfcd70966a040fcdad351e7ea1 100644 (file)
@@ -4958,6 +4958,7 @@ virDomainDefHasDeviceAddress(virDomainDef *def,
 static int
 virDomainDefAddConsoleCompat(virDomainDef *def)
 {
+    bool renumber_consoles = false;
     size_t i;
 
     /*
@@ -5024,6 +5025,8 @@ virDomainDefAddConsoleCompat(virDomainDef *def)
             /* Create an console alias for the serial port */
             def->consoles[0]->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
             def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
+
+            renumber_consoles = true;
         }
     } else if (def->os.type == VIR_DOMAIN_OSTYPE_HVM && def->nserials > 0 &&
                def->serials[0]->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL) {
@@ -5051,6 +5054,8 @@ virDomainDefAddConsoleCompat(virDomainDef *def)
                 return -1;
             }
 
+            renumber_consoles = true;
+
             def->consoles[0]->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
             def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_SERIAL;
 
@@ -5066,6 +5071,16 @@ virDomainDefAddConsoleCompat(virDomainDef *def)
         }
     }
 
+    /* When consoles are parsed in 'virDomainDefParseXML' the value of
+     * 'target.port' is overriden by the index of the console in the
+     * 'def->consoles' array. Thus if we are modifying the list here we
+     * must ensure that the numbering will be identical as if we've parsed
+     * this definition */
+    if (renumber_consoles) {
+        for (i = 0; i < def->nconsoles; i++)
+            def->consoles[i]->target.port = i;
+    }
+
     return 0;
 }
 
index 81bbb554d0824952c06fed1528e639a36729c353..fd29155eaecca1043ad5d6fe3fd774a7d3ca1b56 100644 (file)
@@ -42,7 +42,7 @@
       <target type='serial' port='0'/>
     </console>
     <console type='pty'>
-      <target type='virtio' port='0'/>
+      <target type='virtio' port='1'/>
     </console>
     <input type='mouse' bus='ps2'/>
     <input type='keyboard' bus='ps2'/>