Reputation: 93
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?
Upvotes: 5
Views: 12858
Reputation: 55
you can use virsh commands if you need to manage your KVM. here is the list of virsh commands;
list deleted because it was not coming in proper format
you can use the help from virsh to list all the options, there start option might help you.
if you are using the python script for managing you KVM I would suggest to go through the following script as well. it will provide you a good idea. http://russell.ballestrini.net/series/virt-back/
Upvotes: 0
Reputation: 313
Seems like using libvirt or calling [qemu-]kvm command are the two alternatives for pythonistas. May be you could find interesting snippets in kvmtools project code: http://www.linux-kvm.org/page/Kvmtools (see ./kvmtools/kvm/build_command.py and kvm_boot_action in ./kvmtools/kvm/action.py making use of subprocess module instead of os.system)
Upvotes: 0
Reputation: 1930
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.
Upvotes: 9
Reputation: 526
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.
Upvotes: 2