Skip to content

Instantly share code, notes, and snippets.

@elleryq
Created August 24, 2015 10:48
Show Gist options
  • Save elleryq/c3ba883e335a6363077a to your computer and use it in GitHub Desktop.
Save elleryq/c3ba883e335a6363077a to your computer and use it in GitHub Desktop.
A simple libvirt wrapper.
import libvirt
class LibVirtManager(object):
def __init__(self, uri):
self.conn = libvirt.open(uri)
def createDomainFromXMLFile(self, xmlFile, flags):
"""
if the libvirt.VIR_DOMAIN_START_PAUSED flags is set, the guest
domain will be started, but its CPUs will remain paused.
"""
xml = open(xmlFile).read()
self.conn.createXML(xml, flags)
def info(self, dom):
infos = dom.info()
print("""id = {dom_id}
name = {name}
state = {state}
max memory = {memory_size}
number of virt cpus = {cpus}
cpu time (in ns) = {cpu_time}
""".format(
dom_id=dom.ID(), name=dom.name(), state=infos[0],
memory_size=infos[1], cpus=infos[3], cpu_time=infos[2]
))
def __getattr__(self, attr):
if hasattr(self.conn, attr):
return getattr(self.conn, attr)
raise AttributeError
if __name__ == "__main__":
manager = LibVirtManager("qemu:///system")
domain = manager.lookupByName('newvm')
manager.info(domain)
manager.listDefinedDomains()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment