Tuesday, November 25, 2008

Python libvirt example

Xen sucks for my purposes. Xen is an extremely powerful virtualization platform and is why I don't need it. Few people do for development or experimental purposes. This is where KVM, QEMU, and Libvirt come in handy. I can use Python to easily write a simple application to create virtual machines and manipulate them as needed using the Libvirt Python binding. So, here is a simple example of how to get started using Libvirt in Python.
#Simple Python libvirt example.

import libvirt

if __name__=='__main__':
conn=libvirt.open('qemu:///system')
print 'Listing running domains'
for id in conn.listDomainsID():
dom=conn.lookupByID(id)
print dir(dom)

print 'Listing defined domains'
for id in conn.listDefinedDomains():
dom=conn.lookupByName(id)
print dir(dom)
The great thing about Libvirt is that, in theory, should decide I want to use Xen, this same code should work. Libvirt is still in its' infancy with a long way to go but the ideas are right.