1

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

Tek Mentor
  • 327
  • 2
  • 8
  • Please show your code, what you tried, and how it didn't work out. Without that, we can't do too much, but wish you great luck... – ppeterka Mar 06 '13 at 10:01
  • @ppeterka i have added the xml file and method i used in my post.please check and reply soom. – Tek Mentor Mar 06 '13 at 11:18

1 Answers1

0

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.

Community
  • 1
  • 1
shawnzhu
  • 7,233
  • 4
  • 35
  • 51