<dependency> <groupId>org.libvirt</groupId> <artifactId>libvirt</artifactId> <version>0.5.3</version> </dependency>
<domain type='kvm' id='56'> <name>v-5-VM</name> <uuid>3360ea73-0483-4adc-8c5a-31b32da89d11</uuid> <description>Debian GNU/Linux 5.0 (64-bit)</description> <memory unit='KiB'>1048576</memory> <currentMemory unit='KiB'>1048576</currentMemory> <vcpu placement='static'>1</vcpu> <cputune> <shares>500</shares> </cputune> <resource> <partition>/machine</partition> </resource> <sysinfo type='smbios'> <system> <entry name='manufacturer'>Apache Software Foundation</entry> <entry name='product'>CloudStack KVM Hypervisor</entry> <entry name='uuid'>3360ea73-0483-4adc-8c5a-31b32da89d11</entry> </system> </sysinfo> <os> <type arch='x86_64' machine='pc-i440fx-6.2'>hvm</type> <boot dev='cdrom'/> <boot dev='hd'/> <smbios mode='sysinfo'/> </os> <features> <acpi/> <apic/> <pae/> </features> <cpu mode='custom' match='exact' check='full'> <model fallback='forbid'>qemu64</model> <feature policy='require' name='x2apic'/> <feature policy='require' name='hypervisor'/> <feature policy='require' name='lahf_lm'/> <feature policy='disable' name='svm'/> </cpu> <clock offset='utc'> <timer name='kvmclock'/> </clock> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2' cache='none'/> <source file='/mnt/c1397ddd-b286-3b71-a4dd-dade77a27f50/ed7a268d-7a2a-4191-b647-bb2f4d939234' index='2'/> <backingStore type='file' index='3'> <format type='qcow2'/> <source file='/mnt/c1397ddd-b286-3b71-a4dd-dade77a27f50/aab85423-41de-4806-b2ac-78cb71310c78'/> <backingStore/> </backingStore> <target dev='vda' bus='virtio'/> <serial>ed7a268d7a2a4191b647</serial> <alias name='virtio-disk0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu'/> <target dev='hdc' bus='ide'/> <readonly/> <alias name='ide0-1-0'/> <address type='drive' controller='0' bus='1' target='0' unit='0'/> </disk> <controller type='usb' index='0' model='piix3-uhci'> <alias name='usb'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> </controller> <controller type='pci' index='0' model='pci-root'> <alias name='pci.0'/> </controller> <controller type='ide' index='0'> <alias name='ide'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <controller type='virtio-serial' index='0'> <alias name='virtio-serial0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </controller> <interface type='bridge'> <mac address='0e:00:a9:fe:0b:42'/> <source bridge='cloud0'/> <virtualport type='openvswitch'> <parameters interfaceid='d3935e79-98f7-48de-999b-f65a20543881'/> </virtualport> <target dev='vnet6'/> <model type='virtio'/> <link state='up'/> <alias name='net0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <interface type='bridge'> <mac address='1e:00:bf:00:00:03'/> <source bridge='cloudbr'/> <virtualport type='openvswitch'> <parameters interfaceid='d5c32dd5-cdc0-43c3-a853-267d9e27de87'/> </virtualport> <target dev='vnet7'/> <model type='virtio'/> <link state='up'/> <alias name='net1'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </interface> <interface type='bridge'> <mac address='1e:00:ed:00:00:0d'/> <source bridge='cloudbr'/> <virtualport type='openvswitch'> <parameters interfaceid='8942c706-01cd-473e-a62e-b284fc25fb4d'/> </virtualport> <target dev='vnet8'/> <model type='virtio'/> <link state='up'/> <alias name='net2'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </interface> <serial type='pty'> <source path='/dev/pts/9'/> <target type='isa-serial' port='0'> <model name='isa-serial'/> </target> <alias name='serial0'/> </serial> <console type='pty' tty='/dev/pts/9'> <source path='/dev/pts/9'/> <target type='serial' port='0'/> <alias name='serial0'/> </console> <channel type='unix'> <source mode='bind' path='/var/lib/libvirt/qemu/v-5-VM.org.qemu.guest_agent.0'/> <target type='virtio' name='org.qemu.guest_agent.0' state='connected'/> <alias name='channel0'/> <address type='virtio-serial' controller='0' bus='0' port='1'/> </channel> <input type='tablet' bus='usb'> <alias name='input0'/> <address type='usb' bus='0' port='1'/> </input> <input type='mouse' bus='ps2'> <alias name='input1'/> </input> <input type='keyboard' bus='ps2'> <alias name='input2'/> </input> <graphics type='vnc' port='5902' autoport='yes' listen='192.168.157.139'> <listen type='address' address='192.168.157.139'/> </graphics> <video> <model type='cirrus' vram='16384' heads='1' primary='yes'/> <alias name='video0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <watchdog model='i6300esb' action='none'> <alias name='watchdog0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/> </watchdog> <memballoon model='virtio'> <alias name='balloon0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/> </memballoon> </devices> <seclabel type='dynamic' model='dac' relabel='yes'> <label>+0:+0</label> <imagelabel>+0:+0</imagelabel> </seclabel> </domain>
import org.libvirt.Connect; import org.libvirt.Domain; import org.libvirt.LibvirtException; public class CreateVMExample { public static void main(String[] args) { Connect conn = null; try { // 连接到libvirt conn = new Connect("qemu:///system"); // 定义虚拟机XML配置 String xmlConfig = "<domain>...</domain>"; // 替换为您的虚拟机XML配置 // 创建虚拟机 Domain domain = conn.domainCreateXML(xmlConfig, 0); System.out.println("虚拟机创建成功,ID: " + domain.getID()); } catch (LibvirtException e) { System.out.println("创建虚拟机失败:" + e.getMessage()); } finally { // 断开与libvirt的连接 if (conn != null) { try { conn.close(); } catch (LibvirtException e) { // 处理异常 } } } } }