Showing posts with label kvm. Show all posts
Showing posts with label kvm. Show all posts

Wednesday, March 31, 2010

Polymorphic Libvirt

The Libvirt virtualization library is great for interacting with heterogeneous hypervisors. Depending on the hypervisor connection, most domains support the same virtual machine interfaces. For instance, you can you the same code to start and stop virtual machines using Libvirt for both KVM and Xen.

But when the operation isn't supported for a particular hypervisor type, you better be prepared to handle this scenario. Libvirt only defines a single exception type but we can determine that it is a hypervisor interface support issue by looking at the error code.

This is shown below. The following operation on the domain will fail on KVM.
import libvirt

if __name__ == "__main__":

conn = libvirt.open("qemu:///system")

for id in conn.listDomainsID():

domain = conn.lookupByID(id)

try:
domain.reboot(0)
except libvirt.libvirtError, e:
if e.get_error_code() == libvirt.VIR_ERR_NO_SUPPORT:
print "Cannot Reboot"

Friday, November 28, 2008

Libvirt 0.5.0

This may be old news for some but libvirt 0.5.0 is now available. As usual, the bug fixes and improvements are spread throughout the library. There looks to be some interesting new features in the Qemu/KVM driver. Namely, "domain lifecycle event support" and "migration support".

The domain life cycle support includes Python bindings according to the change log whereas nothing is mentioned about Python support regarding the migration support for Qemu/KVM. Also, the domain life cycle support mentions only Qemu and Xen. I haven't actually tried using 0.5.0 yet but if I find anything else interesting to write about I will.

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.