Product SiteDocumentation Site

4.3.2. Fetching the UUID of a domain

The UUID of a domain can be obtained by using the UUID or the UUIDString methods. The UUID method is not all that useful for Python programs because it is a binary value. The UUIDString method is much more useful because it returns a formatted string value that can be easily parsed.
The UUID is not dependant on the running state of the domain and always returns a valid UUID.

Example 4.8. Fetching the UUID of a domain

# Example-44.py
#!/usr/bin/env python3
import sys
import libvirt
from xml.dom import minidom

domName = 'CentOS7'

conn = None
try:
    conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
    print(repr(e), file=sys.stderr)
    exit(1)

dom = None
try:
    dom = conn.lookupByName(domName)
except libvirt.libvirtError as e:
    print(repr(e), file=sys.stderr)
    exit(1)

uuid = dom.UUIDString()
print('The UUID of the domain is ' + uuid)

conn.close()
exit(0)