Tek Mentor
Tek Mentor

Reputation: 367

Creating .img file from .xml file for creating kvm in ubuntu using libvirt api and java

I am trying to create a kvm using libvirt api using java. the problem i am facing is that i need a device(image file (eg: kvm.img)). I am using .xml file to create the kvm using libvirt api and java. Now what i am doing is that create the .img file first using the qemu-img create from terminal. qemu-img create /var/lib/libvirt/images/kvm.img 10G is the code i am using and in the xml to create kvm i mention <source file='var/lib/libvirt/images/kvm.img'/>

Now i need to know is there any method in libvirt api to create the .img file bu passing the xml file and i need to know about the xml file(create img file) too

I am using libvirt 1.0

the xml file that i use to create kvm is : <domain type='kvm'><name>ft</name><memory>131072</memory><currentMemory>131072</currentMemory><vcpu>1</vcpu><os><type arch='x86_64' machine='pc-0.12'>hvm</type><boot dev='hd'/></os><features><acpi/></features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff><on_reboot>restart</on_reboot><on_crash>destroy</on_crash> <devices><emulator>/usr/bin/kvm</emulator><disk type='file' device='disk'><source file='var/lib/libvirt/images/ft.img'/><target dev='hda' bus='ide'/></disk><interface type='network'><mac address='52:54:00:8b:08:dd'/><source network='default'/><model type='virtio'/></interface><input type='mouse' bus='ps2'/><graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'/><video><model type='cirrus' vram='9216' heads='1'/> </video></devices></domain>

in java am using the libvirt metod as Domain createVm = con.domainCreateXML(str, 0); as str i am passing xml string

Upvotes: 1

Views: 1613

Answers (1)

shawnzhu
shawnzhu

Reputation: 7585

Image file creation is out of libvirt API because it is qemu who cares about the actual disk device. Technically the VM disk device could be raw image file like *.img, or loop device or storage device like mapped iscsi target, while libvirt doesn't know where it comes from.

So you need to handle the VM disk file creation on your own. both qemu-img or dd do the job. See the answer to this question. If you just want to create raw file for a KVM VM, actually you just need to create a sparse file via Java File API. see question Create file with given size in Java, which does the exactly same job as qemu-img create does.

Upvotes: 0

Related Questions