Skip to content

Commit a7ec873

Browse files
kvm: fix NPE while import KVM VMs from other hosts (#8720)
1 parent e86363b commit a7ec873

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,21 +419,30 @@ private MemBalloonDef parseMemBalloonTag(Element devices) {
419419
}
420420

421421
private static String getTagValue(String tag, Element eElement) {
422+
if (eElement == null) {
423+
return null;
424+
}
422425
NodeList tagNodeList = eElement.getElementsByTagName(tag);
423426
if (tagNodeList == null || tagNodeList.getLength() == 0) {
424427
return null;
425428
}
426429

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

432+
if (nlList == null || nlList.getLength() == 0) {
433+
return null;
434+
}
429435
Node nValue = nlList.item(0);
430436

431437
return nValue.getNodeValue();
432438
}
433439

434440
private static String getAttrValue(String tag, String attr, Element eElement) {
441+
if (eElement == null) {
442+
return null;
443+
}
435444
NodeList tagNode = eElement.getElementsByTagName(tag);
436-
if (tagNode.getLength() == 0) {
445+
if (tag == null || tagNode.getLength() == 0) {
437446
return null;
438447
}
439448
Element node = (Element)tagNode.item(0);

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,126 @@ public void testDomainXMLParser() {
259259
assertEquals(WatchDogDef.WatchDogModel.I6300ESB, watchDogs.get(0).getModel());
260260
assertEquals(WatchDogDef.WatchDogAction.RESET, watchDogs.get(0).getAction());
261261
}
262+
263+
@Test
264+
public void testDomainXMLParserWithoutModelName() {
265+
String xml = "<domain type='kvm'>\n" +
266+
" <name>testkiran</name>\n" +
267+
" <uuid>aafaaabc-8657-4efc-9c52-3422d4e04088</uuid>\n" +
268+
" <memory unit='KiB'>2097152</memory>\n" +
269+
" <currentMemory unit='KiB'>2097152</currentMemory>\n" +
270+
" <vcpu placement='static'>2</vcpu>\n" +
271+
" <os>\n" +
272+
" <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>\n" +
273+
" <boot dev='hd'/>\n" +
274+
" </os>\n" +
275+
" <features>\n" +
276+
" <acpi/>\n" +
277+
" <apic/>\n" +
278+
" </features>\n" +
279+
" <cpu mode='host-model' check='partial'>\n" +
280+
" <model fallback='allow'/>\n" +
281+
" </cpu>\n" +
282+
" <clock offset='utc'>\n" +
283+
" <timer name='rtc' tickpolicy='catchup'/>\n" +
284+
" <timer name='pit' tickpolicy='delay'/>\n" +
285+
" <timer name='hpet' present='no'/>\n" +
286+
" </clock>\n" +
287+
" <on_poweroff>destroy</on_poweroff>\n" +
288+
" <on_reboot>restart</on_reboot>\n" +
289+
" <on_crash>destroy</on_crash>\n" +
290+
" <pm>\n" +
291+
" <suspend-to-mem enabled='no'/>\n" +
292+
" <suspend-to-disk enabled='no'/>\n" +
293+
" </pm>\n" +
294+
" <devices>\n" +
295+
" <emulator>/usr/libexec/qemu-kvm</emulator>\n" +
296+
" <disk type='file' device='disk'>\n" +
297+
" <driver name='qemu' type='qcow2'/>\n" +
298+
" <source file='/var/lib/libvirt/images/ubuntu-22.04.qcow2'/>\n" +
299+
" <target dev='vda' bus='virtio'/>\n" +
300+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>\n" +
301+
" </disk>\n" +
302+
" <disk type='file' device='disk'>\n" +
303+
" <driver name='qemu' type='qcow2'/>\n" +
304+
" <source file='/var/lib/libvirt/images/testkiran.qcow2'/>\n" +
305+
" <target dev='vdb' bus='virtio'/>\n" +
306+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>\n" +
307+
" </disk>\n" +
308+
" <controller type='usb' index='0' model='ich9-ehci1'>\n" +
309+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>\n" +
310+
" </controller>\n" +
311+
" <controller type='usb' index='0' model='ich9-uhci1'>\n" +
312+
" <master startport='0'/>\n" +
313+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>\n" +
314+
" </controller>\n" +
315+
" <controller type='usb' index='0' model='ich9-uhci2'>\n" +
316+
" <master startport='2'/>\n" +
317+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>\n" +
318+
" </controller>\n" +
319+
" <controller type='usb' index='0' model='ich9-uhci3'>\n" +
320+
" <master startport='4'/>\n" +
321+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>\n" +
322+
" </controller>\n" +
323+
" <controller type='pci' index='0' model='pci-root'/>\n" +
324+
" <controller type='virtio-serial' index='0'>\n" +
325+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>\n" +
326+
" </controller>\n" +
327+
" <interface type='network'>\n" +
328+
" <mac address='52:54:00:09:73:b8'/>\n" +
329+
" <source network='default'/>\n" +
330+
" <model type='virtio'/>\n" +
331+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>\n" +
332+
" </interface>\n" +
333+
" <serial type='pty'>\n" +
334+
" <target type='isa-serial' port='0'>\n" +
335+
" <model name='isa-serial'/>\n" +
336+
" </target>\n" +
337+
" </serial>\n" +
338+
" <console type='pty'>\n" +
339+
" <target type='serial' port='0'/>\n" +
340+
" </console>\n" +
341+
" <channel type='spicevmc'>\n" +
342+
" <target type='virtio' name='com.redhat.spice.0'/>\n" +
343+
" <address type='virtio-serial' controller='0' bus='0' port='1'/>\n" +
344+
" </channel>\n" +
345+
" <input type='tablet' bus='usb'>\n" +
346+
" <address type='usb' bus='0' port='1'/>\n" +
347+
" </input>\n" +
348+
" <input type='mouse' bus='ps2'/>\n" +
349+
" <input type='keyboard' bus='ps2'/>\n" +
350+
" <graphics type='vnc' port='-1' autoport='yes'>\n" +
351+
" <listen type='address'/>\n" +
352+
" </graphics>\n" +
353+
" <graphics type='spice' autoport='yes'>\n" +
354+
" <listen type='address'/>\n" +
355+
" <image compression='off'/>\n" +
356+
" </graphics>\n" +
357+
" <sound model='ich6'>\n" +
358+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>\n" +
359+
" </sound>\n" +
360+
" <video>\n" +
361+
" <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>\n" +
362+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>\n" +
363+
" </video>\n" +
364+
" <redirdev bus='usb' type='spicevmc'>\n" +
365+
" <address type='usb' bus='0' port='2'/>\n" +
366+
" </redirdev>\n" +
367+
" <redirdev bus='usb' type='spicevmc'>\n" +
368+
" <address type='usb' bus='0' port='3'/>\n" +
369+
" </redirdev>\n" +
370+
" <memballoon model='virtio'>\n" +
371+
" <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>\n" +
372+
" </memballoon>\n" +
373+
" </devices>\n" +
374+
"</domain>";
375+
376+
LibvirtDomainXMLParser libvirtDomainXMLParser = new LibvirtDomainXMLParser();
377+
try {
378+
libvirtDomainXMLParser.parseDomainXML(xml);
379+
} catch (Exception e) {
380+
System.out.println("Got exception " + e.getMessage());
381+
throw e;
382+
}
383+
}
262384
}

0 commit comments

Comments
 (0)