Libvirt is fast becoming the standard tool for managing virtual machines on Linux and Qpid is the Apache foundations new implementation of AMQP which is the first open standard for Enterprise Messaging. These two technologies have the potential to work in well together for large virtualization installations and luckily for us the good guys in the libvirt team have done just that http://libvirt.org/qpid/ but there are currently very few examples on how to use it. I am putting this brief tutorial in their wiki as a starting point for others but will continue to publish my experiences here.
Installation
libvirt-qpid is currently available in Fedora 10 repositories so you can install it using yum
yum -y install libvirt-qpid qpidd python-qpid
chkconfig libvirt-qpid on
chkconfig qpidd on
service libvirt-qpid start
service qpidd start
Testing that it is running
We can check that it is running using “qpid-tool” and the list command
# qpid-tool
Management Tool for QPID
qpid: list
Management Object Types:
ObjectType Active Deleted
============================================
com.redhat.libvirt:domain 6 0
com.redhat.libvirt:node 1 0
com.redhat.libvirt:pool 1 0
Simple client in python
Now that we have it running lets make a simple client to get information from it. To do this I use python. The following is a simple script that does some of the basics
#!/usr/bin/env python
from qmf.console import Session
from yaml import dump
sess = Session() # defaults to synchronous-only operation. It also defaults to user-management of connections.
# attempt to connect to a broker
try:
broker = sess.addBroker('amqp://localhost:5672')
print "Connection Success"
except:
print "Connection Failed"
domains = sess.getObjects(_class='domain', _package='com.redhat.libvirt.domain')
# Print a list of the domains
for d in domains:
print d
# Select the first domain
domain = domains[0]
# Print a list of the properties of the domain
print 'Properties:'
props = domain.getProperties()
for prop in props:
print "\t",prop
# Access a value of a property and print it
print domain.name
# Print a list of the methods of the domain
print 'Methods:'
meths = domain.getMethods()
for meth in meths:
print "\t",meth
# Ca method of the domain and print it
xmldesc = domain.getXMLDesc()
# Call another method of the domain and print the result
if domain.state == 'running':
result = domain.shutdown()
print result
else:
result = domain.create()
print result
# Disconnect from the broker (otherwise we hang the terminal
sess.delBroker(broker)