I was wondering if there is a KVM API which allows you to start a KVM virtual machine using a simple command, from a python script.
My Python script performs a series of checks to see whether or not we need to start a specific VM, and I would like to start a VM if I need to.
All I need now is to find the API calls, but I can't find a simple call to start them within the libvirt website. Does anybody know if this is possible?
You can use the create() function from the python API bindings of libvirt:
import libvirt
#connect to hypervisor running on localhost
conn = libvirt.open('qemu:///system')
dom0 = conn.lookupByName('my-vm-1')
dom0.create()
basically the python API is the C API, called by libvirt.C_API_CALL minus the virConnect part or conn.C_API_CALL minus the virDomain part.
see the libvirt API create call and here.
The simplest way, though probably not the best recommended way is to use the os.system using python to invoke qemu-kvm. This method will have the disadvantage that you will have to manually manage the VM.
Using libvirt, you will first have to define a domain by calling virt-install.
virt-install \
--connect qemu:///system \
--virt-type kvm \
--name MyNewVM \
--ram 512 \
--disk path=/var/lib/libvirt/images/MyNewVM.img,size=8 \
--vnc \
--cdrom /var/lib/libvirt/images/Fedora-14-x86_64-Live-KDE.iso \
--network network=default,mac=52:54:00:9c:94:3b \
--os-variant fedora14
I have picked this directly from http://wiki.libvirt.org/page/VM_lifecycle
Once you create the domain, you can use virsh start MyNewVM
to start the VM. Using this method, it is much easier to manage the VM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With