Skip to content

Commit 4b7d229

Browse files
nvazquezrohityadavcloud
authored andcommitted
[KVM] Fix VM migration error due to VNC password on libvirt limiting versions (apache#6404)
* [KVM] Fix VM migration error due to VNC password on libvirt limiting versions * Fix passwd value * Simplify implementation (cherry picked from commit b1c8b5a) Signed-off-by: Rohit Yadav <[email protected]>
1 parent 957b64b commit 4b7d229

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ Use VIR_DOMAIN_XML_SECURE (value = 1) prior to v1.0.0.
145145

146146
final String target = command.getDestinationIp();
147147
xmlDesc = dm.getXMLDesc(xmlFlag);
148-
xmlDesc = replaceIpForVNCInDescFile(xmlDesc, target);
148+
149+
// Limit the VNC password in case the length is greater than 8 characters
150+
// Since libvirt version 8 VNC passwords are limited to 8 characters
151+
String vncPassword = org.apache.commons.lang3.StringUtils.truncate(to.getVncPassword(), 8);
152+
xmlDesc = replaceIpForVNCInDescFileAndNormalizePassword(xmlDesc, target, vncPassword);
149153

150154
String oldIsoVolumePath = getOldVolumePath(disks, vmName);
151155
String newIsoVolumePath = getNewVolumePathIfDatastoreHasChanged(libvirtComputingResource, conn, to);
@@ -420,16 +424,20 @@ protected MigrateDiskInfo searchDiskDefOnMigrateDiskInfoList(List<MigrateDiskInf
420424
* </graphics>
421425
* @param xmlDesc the qemu xml description
422426
* @param target the ip address to migrate to
427+
* @param vncPassword if set, the VNC password truncated to 8 characters
423428
* @return the new xmlDesc
424429
*/
425-
String replaceIpForVNCInDescFile(String xmlDesc, final String target) {
430+
String replaceIpForVNCInDescFileAndNormalizePassword(String xmlDesc, final String target, String vncPassword) {
426431
final int begin = xmlDesc.indexOf(GRAPHICS_ELEM_START);
427432
if (begin >= 0) {
428433
final int end = xmlDesc.lastIndexOf(GRAPHICS_ELEM_END) + GRAPHICS_ELEM_END.length();
429434
if (end > begin) {
430435
String graphElem = xmlDesc.substring(begin, end);
431436
graphElem = graphElem.replaceAll("listen='[a-zA-Z0-9\\.]*'", "listen='" + target + "'");
432437
graphElem = graphElem.replaceAll("address='[a-zA-Z0-9\\.]*'", "address='" + target + "'");
438+
if (org.apache.commons.lang3.StringUtils.isNotBlank(vncPassword)) {
439+
graphElem = graphElem.replaceAll("passwd='([^\\s]+)'", "passwd='" + vncPassword + "'");
440+
}
433441
xmlDesc = xmlDesc.replaceAll(GRAPHICS_ELEM_START + CONTENTS_WILDCARD + GRAPHICS_ELEM_END, graphElem);
434442
}
435443
}

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapperTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -571,53 +571,55 @@ public void setup() throws Exception {
571571
@Test
572572
public void testReplaceIpForVNCInDescFile() {
573573
final String targetIp = "192.168.22.21";
574-
final String result = libvirtMigrateCmdWrapper.replaceIpForVNCInDescFile(fullfile, targetIp);
574+
final String result = libvirtMigrateCmdWrapper.replaceIpForVNCInDescFileAndNormalizePassword(fullfile, targetIp, null);
575575
assertTrue("transformation does not live up to expectation:\n" + result, targetfile.equals(result));
576576
}
577577

578578
@Test
579-
public void testReplaceIpForVNCInDesc() {
579+
public void testReplaceIpAndPasswordForVNCInDesc() {
580580
final String xmlDesc =
581581
"<domain type='kvm' id='3'>" +
582582
" <devices>" +
583-
" <graphics type='vnc' port='5900' autoport='yes' listen='10.10.10.1'>" +
583+
" <graphics type='vnc' port='5900' autoport='yes' listen='10.10.10.1' passwd='123456789012345'>" +
584584
" <listen type='address' address='10.10.10.1'/>" +
585585
" </graphics>" +
586586
" </devices>" +
587587
"</domain>";
588588
final String expectedXmlDesc =
589589
"<domain type='kvm' id='3'>" +
590590
" <devices>" +
591-
" <graphics type='vnc' port='5900' autoport='yes' listen='10.10.10.10'>" +
591+
" <graphics type='vnc' port='5900' autoport='yes' listen='10.10.10.10' passwd='12345678'>" +
592592
" <listen type='address' address='10.10.10.10'/>" +
593593
" </graphics>" +
594594
" </devices>" +
595595
"</domain>";
596596
final String targetIp = "10.10.10.10";
597-
final String result = libvirtMigrateCmdWrapper.replaceIpForVNCInDescFile(xmlDesc, targetIp);
597+
final String password = "12345678";
598+
final String result = libvirtMigrateCmdWrapper.replaceIpForVNCInDescFileAndNormalizePassword(xmlDesc, targetIp, password);
598599
assertTrue("transformation does not live up to expectation:\n" + result, expectedXmlDesc.equals(result));
599600
}
600601

601602
@Test
602-
public void testReplaceFqdnForVNCInDesc() {
603+
public void testReplaceFqdnAndPasswordForVNCInDesc() {
603604
final String xmlDesc =
604605
"<domain type='kvm' id='3'>" +
605606
" <devices>" +
606-
" <graphics type='vnc' port='5900' autoport='yes' listen='localhost.local'>" +
607+
" <graphics type='vnc' port='5900' autoport='yes' listen='localhost.local' passwd='123456789012345'>" +
607608
" <listen type='address' address='localhost.local'/>" +
608609
" </graphics>" +
609610
" </devices>" +
610611
"</domain>";
611612
final String expectedXmlDesc =
612613
"<domain type='kvm' id='3'>" +
613614
" <devices>" +
614-
" <graphics type='vnc' port='5900' autoport='yes' listen='localhost.localdomain'>" +
615+
" <graphics type='vnc' port='5900' autoport='yes' listen='localhost.localdomain' passwd='12345678'>" +
615616
" <listen type='address' address='localhost.localdomain'/>" +
616617
" </graphics>" +
617618
" </devices>" +
618619
"</domain>";
619620
final String targetIp = "localhost.localdomain";
620-
final String result = libvirtMigrateCmdWrapper.replaceIpForVNCInDescFile(xmlDesc, targetIp);
621+
final String password = "12345678";
622+
final String result = libvirtMigrateCmdWrapper.replaceIpForVNCInDescFileAndNormalizePassword(xmlDesc, targetIp, password);
621623
assertTrue("transformation does not live up to expectation:\n" + result, expectedXmlDesc.equals(result));
622624
}
623625

@@ -789,5 +791,4 @@ public void testReplaceDPDKPorts() throws ParserConfigurationException, IOExcept
789791
Assert.assertTrue(replaced.contains("csdpdk-7"));
790792
Assert.assertFalse(replaced.contains("csdpdk-1"));
791793
}
792-
793794
}

0 commit comments

Comments
 (0)