Skip to content

Instantly share code, notes, and snippets.

@praveenkumar
Last active August 29, 2015 13:56
Show Gist options
  • Save praveenkumar/3d7a536134ee48c50c74 to your computer and use it in GitHub Desktop.
Save praveenkumar/3d7a536134ee48c50c74 to your computer and use it in GitHub Desktop.
import libvirt
# Get a connection to Hypervisor (optional URI).
# Return virConnect object if successfull otherwise NULL
# More: http://libvirt.org/html/libvirt-libvirt.html#virConnectOpen
# Check openAuth and openReadOnly methods also.
virConnect_obj = libvirt.open(URI)
# List of defined domains which are not active on hypervisor.
# Return a List of defined domains or -1 in case of error
# More: http://libvirt.org/html/libvirt-libvirt.html#virConnectListDefinedDomains
inactive_defined_domain = virConnect_obj.listDefinedDomains()
# List of domains which are active on hypervisor
# Return a List of active domains ID or -1 in case of error
# More: http://libvirt.org/html/libvirt-libvirt.html#virConnectListDomains
active_domain = virConnect_obj.listDomains()
# Define a VM using xml description of domain. Previous definition will be overwritten if already exist.
# Return virDomain obj in case of success otherwise NULL.
# More: http://libvirt.org/html/libvirt-libvirt.html#virDomainDefineXML
xml_description = <String containing xml desc>
virDomain_obj = virConnect_obj.defineXML(xml_description)
# Create a VM, here it will also start the VM.
# Return 0 in case of success, -1 if error
# More: http://libvirt.org/html/libvirt-libvirt.html#virDomainCreate
# Check out virDomainCreateWithFiles, virDomainCreateWithFlags .. etc. Use whatever best suit.
status = virDomain_obj.create()
# Delete a VM
# Return 0 in case of success and -1 if error
# More: http://libvirt.org/html/libvirt-libvirt.html#virDomainDestroy
status = virDomain_obj.destroy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment