Skip to content

Commit

Permalink
kvm: fix NPE while import KVM VMs from other hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhouapache committed Feb 28, 2024
1 parent 45d267c commit 26475f8
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -419,21 +419,30 @@ private MemBalloonDef parseMemBalloonTag(Element devices) {
}

private static String getTagValue(String tag, Element eElement) {
if (eElement == null) {
return null;
}
NodeList tagNodeList = eElement.getElementsByTagName(tag);
if (tagNodeList == null || tagNodeList.getLength() == 0) {
return null;
}

NodeList nlList = tagNodeList.item(0).getChildNodes();

if (nlList == null || nlList.getLength() == 0) {
return null;
}
Node nValue = nlList.item(0);

return nValue.getNodeValue();
}

private static String getAttrValue(String tag, String attr, Element eElement) {
if (eElement == null) {
return null;
}
NodeList tagNode = eElement.getElementsByTagName(tag);
if (tagNode.getLength() == 0) {
if (tag == null || tagNode.getLength() == 0) {
return null;
}
Element node = (Element)tagNode.item(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,126 @@ public void testDomainXMLParser() {
assertEquals(WatchDogDef.WatchDogModel.I6300ESB, watchDogs.get(0).getModel());
assertEquals(WatchDogDef.WatchDogAction.RESET, watchDogs.get(0).getAction());
}

@Test
public void testDomainXMLParser2() {
String xml = "<domain type='kvm'>\n" +
" <name>testkiran</name>\n" +
" <uuid>aafaaabc-8657-4efc-9c52-3422d4e04088</uuid>\n" +
" <memory unit='KiB'>2097152</memory>\n" +
" <currentMemory unit='KiB'>2097152</currentMemory>\n" +
" <vcpu placement='static'>2</vcpu>\n" +
" <os>\n" +
" <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>\n" +
" <boot dev='hd'/>\n" +
" </os>\n" +
" <features>\n" +
" <acpi/>\n" +
" <apic/>\n" +
" </features>\n" +
" <cpu mode='host-model' check='partial'>\n" +
" <model fallback='allow'/>\n" +
" </cpu>\n" +
" <clock offset='utc'>\n" +
" <timer name='rtc' tickpolicy='catchup'/>\n" +
" <timer name='pit' tickpolicy='delay'/>\n" +
" <timer name='hpet' present='no'/>\n" +
" </clock>\n" +
" <on_poweroff>destroy</on_poweroff>\n" +
" <on_reboot>restart</on_reboot>\n" +
" <on_crash>destroy</on_crash>\n" +
" <pm>\n" +
" <suspend-to-mem enabled='no'/>\n" +
" <suspend-to-disk enabled='no'/>\n" +
" </pm>\n" +
" <devices>\n" +
" <emulator>/usr/libexec/qemu-kvm</emulator>\n" +
" <disk type='file' device='disk'>\n" +
" <driver name='qemu' type='qcow2'/>\n" +
" <source file='/var/lib/libvirt/images/ubuntu-22.04.qcow2'/>\n" +
" <target dev='vda' bus='virtio'/>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>\n" +
" </disk>\n" +
" <disk type='file' device='disk'>\n" +
" <driver name='qemu' type='qcow2'/>\n" +
" <source file='/var/lib/libvirt/images/testkiran.qcow2'/>\n" +
" <target dev='vdb' bus='virtio'/>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>\n" +
" </disk>\n" +
" <controller type='usb' index='0' model='ich9-ehci1'>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>\n" +
" </controller>\n" +
" <controller type='usb' index='0' model='ich9-uhci1'>\n" +
" <master startport='0'/>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>\n" +
" </controller>\n" +
" <controller type='usb' index='0' model='ich9-uhci2'>\n" +
" <master startport='2'/>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>\n" +
" </controller>\n" +
" <controller type='usb' index='0' model='ich9-uhci3'>\n" +
" <master startport='4'/>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>\n" +
" </controller>\n" +
" <controller type='pci' index='0' model='pci-root'/>\n" +
" <controller type='virtio-serial' index='0'>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>\n" +
" </controller>\n" +
" <interface type='network'>\n" +
" <mac address='52:54:00:09:73:b8'/>\n" +
" <source network='default'/>\n" +
" <model type='virtio'/>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>\n" +
" </interface>\n" +
" <serial type='pty'>\n" +
" <target type='isa-serial' port='0'>\n" +
" <model name='isa-serial'/>\n" +
" </target>\n" +
" </serial>\n" +
" <console type='pty'>\n" +
" <target type='serial' port='0'/>\n" +
" </console>\n" +
" <channel type='spicevmc'>\n" +
" <target type='virtio' name='com.redhat.spice.0'/>\n" +
" <address type='virtio-serial' controller='0' bus='0' port='1'/>\n" +
" </channel>\n" +
" <input type='tablet' bus='usb'>\n" +
" <address type='usb' bus='0' port='1'/>\n" +
" </input>\n" +
" <input type='mouse' bus='ps2'/>\n" +
" <input type='keyboard' bus='ps2'/>\n" +
" <graphics type='vnc' port='-1' autoport='yes'>\n" +
" <listen type='address'/>\n" +
" </graphics>\n" +
" <graphics type='spice' autoport='yes'>\n" +
" <listen type='address'/>\n" +
" <image compression='off'/>\n" +
" </graphics>\n" +
" <sound model='ich6'>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>\n" +
" </sound>\n" +
" <video>\n" +
" <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>\n" +
" </video>\n" +
" <redirdev bus='usb' type='spicevmc'>\n" +
" <address type='usb' bus='0' port='2'/>\n" +
" </redirdev>\n" +
" <redirdev bus='usb' type='spicevmc'>\n" +
" <address type='usb' bus='0' port='3'/>\n" +
" </redirdev>\n" +
" <memballoon model='virtio'>\n" +
" <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>\n" +
" </memballoon>\n" +
" </devices>\n" +
"</domain>";

LibvirtDomainXMLParser libvirtDomainXMLParser = new LibvirtDomainXMLParser();
try {
libvirtDomainXMLParser.parseDomainXML(xml);
} catch (Exception e) {
System.out.println("Got exception " + e.getMessage());
throw e;
}
}
}

0 comments on commit 26475f8

Please sign in to comment.