虚拟机 xml定义
<domain type="kvm">
<name>ubuntu22.04</name>
<uuid>fe1761e1-a870-40a4-b987-a0162cecc544</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://ubuntu.com/ubuntu/22.04"/>
</libosinfo:libosinfo>
</metadata>
<memory unit="KiB">4194304</memory>
<currentMemory unit="KiB">4194304</currentMemory>
<vcpu placement="static">2</vcpu>
<os>
<type arch="x86_64">hvm</type>
<boot dev="hd"/>
</os>
<features>
<acpi/>
<apic/>
<vmport state="off"/>
</features>
<cpu mode="host-passthrough" check="none" migratable="on"/>
<clock offset="utc">
<timer name="rtc" tickpolicy="catchup"/>
<timer name="pit" tickpolicy="delay"/>
<timer name="hpet" present="no"/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2" discard="unmap"/>
<source file="/data/vm/3/ubuntu22.04.qcow2"/>
<target dev="vda" bus="virtio"/>
</disk>
<disk type="file" device="cdrom">
<driver name="qemu" type="raw"/>
<target dev="sda" bus="sata"/>
<readonly/>
</disk>
<interface type="network">
<source network="default"/>
<model type="virtio"/>
<address type="pci" />
</interface>
<serial type="pty">
<target type="isa-serial" port="0">
<model name="isa-serial"/>
</target>
</serial>
<console type="pty">
<target type="serial" port="0"/>
</console>
<channel type="unix">
<target type="virtio" name="org.qemu.guest_agent.0"/>
<address type="virtio-serial" controller="0" bus="0" port="1"/>
</channel>
<channel type="spicevmc">
<target type="virtio" name="com.redhat.spice.0"/>
<address type="virtio-serial" controller="0" bus="0" port="2"/>
</channel>
<input type="tablet" bus="usb">
<address type="usb" bus="0" port="1"/>
</input>
<input type="mouse" bus="ps2"/>
<input type="keyboard" bus="ps2"/>
<graphics type="spice" autoport="yes">
<listen type="address"/>
<image compression="off"/>
</graphics>
<sound model="ich9">
<address type="pci" />
</sound>
<audio id="1" type="spice"/>
<video>
<model type="qxl" ram="65536" vram="65536" vgamem="16384" heads="1" primary="yes"/>
<address type="pci" />
</video>
<memballoon model="virtio">
<address type="pci" />
</memballoon>
<rng model="virtio">
<backend model="random">/dev/urandom</backend>
<address type="pci" />
</rng>
</devices>
</domain>
java 代码
创建虚拟机
Connect connect = new Connect("qemu:///system", false);
SAXReader reader = new SAXReader();
Document docu = reader.read(new File("/home/mm/work/kvm/test.xml"));
String xmlDesc = docu.asXML();
Domain domain = connect.domainCreateXML(xmlDesc, 0);
获取ip
Connect connect = new Connect("qemu:///system", false);
Domain domain = connect.domainLookupByID(id);
Collection<DomainInterface> list = domain.interfaceAddresses(Domain.InterfaceAddressesSource.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_LEASE, 0);
for(DomainInterface interf : list) {
log.info("name:{}, hwAddr:{}", interf.name, interf.hwAddr);
for(DomainInterface.InterfaceAddress address : interf.addrs) {
log.info("ip:{}, prefixLen:{}", address.address, address.prefixLength);
}
}