Re: [libvirt] ESX with Python binding

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/11/3 Dave Bryson <daveb@xxxxxxxxxx>:
> I'm new to libvirt.  Is it possible to use the python binding with the
> latest ESX support? If so, does anyone have a simple example I can try?
>
> Thanks in advance!
> Dave
>

Yes, just use the libvirt Python bindings as you would with Xen or
QEmu. There are some basic examples in the examples/python directory.
The tricky part is the authentication. None of the available examples
demonstrates how to use the authentication callback with the
openAuth() method in Python. So I took one of the existing example
scripts and switched it from openReadOnly() to openAuth().

python esxlist.py <esx-hostname>

It'll prompt for username and password and then list all active
virtual machines on this ESX server.

You may see remote errors complaining about missing certificates:

  Cannot access CA certificate '/usr/local/etc/pki/CA/cacert.pem': No
such file or directory

This is expected, libvirt tries to find network and storage drivers
for ESX, but those are not implemented yet. While searching for this
drivers, libvirt may try to start a local libvirtd instance, but fails
because of the missing certificates. It'll warn about that:

  Failed to find the network: Is the daemon running?

This is also expected and can be ignored.

PS: There is currently no user documentation on the libvirt website
about the ESX support. It's on my todo list to write such
documentation soon. So feel free to ask questions.

Matthias
#! /usr/bin/python
# esxlist - list active domains of an ESX host and print some info

import libvirt
import sys
import os
import libxml2
import getpass

def usage():
    print "Usage: %s HOSTNAME" % sys.argv[0]
    print "       List active domains of HOSTNAME and print some info"

def request_credentials(credentials, data):
    for credential in credentials:
        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
            credential[4] = raw_input(credential[1] + ": ")
            if len(credential[4]) == 0:
                credential[4] = credential[3]
        elif credential[0] == libvirt.VIR_CRED_NOECHOPROMPT:
            credential[4] = getpass.getpass(credential[1] + ": ")
        else:
            return -1

    return 0

def print_section(title):
    print "\n%s" % title
    print "=" * 60

def print_entry(key, value):
    print "%-10s %-10s" % (key, value)

def print_xml(key, ctx, path):
    res = ctx.xpathEval(path)
    if res is None or len(res) == 0:
        value = "Unknown"
    else:
        value = res[0].content
    print_entry(key, value)
    return value

if len(sys.argv) != 2:
    usage()
    sys.exit(2)

hostname = sys.argv[1]

# Connect to libvirt
uri = "esx://%s/?no_verify=1" % hostname
auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],
        request_credentials, None]
conn = libvirt.openAuth(uri, auth, 0)

if conn is None:
    print "Failed to open connection to %s" % hostname
    sys.exit(1)

state_names = { libvirt.VIR_DOMAIN_RUNNING  : "running",
                libvirt.VIR_DOMAIN_BLOCKED  : "idle",
                libvirt.VIR_DOMAIN_PAUSED   : "paused",
                libvirt.VIR_DOMAIN_SHUTDOWN : "in shutdown",
                libvirt.VIR_DOMAIN_SHUTOFF  : "shut off",
                libvirt.VIR_DOMAIN_CRASHED  : "crashed",
                libvirt.VIR_DOMAIN_NOSTATE  : "no state" }

for id in conn.listDomainsID():
    domain = conn.lookupByID(id)
    info = domain.info()

    print_section("Domain " + domain.name())
    print_entry("ID:", id)
    print_entry("UUID:", domain.UUIDString())
    print_entry("State:", state_names[info[0]])
    print_entry("MaxMem:", info[1])
    print_entry("UsedMem:", info[2])
    print_entry("VCPUs:", info[3])

    # Read some info from the XML desc
    print_section("Devices of " + domain.name())

    xmldesc = domain.XMLDesc(0)
    doc = libxml2.parseDoc(xmldesc)
    ctx = doc.xpathNewContext()
    devs = ctx.xpathEval("/domain/devices/*")
    first = True

    for d in devs:
        ctx.setContextNode(d)

        if not first:
            print "------------------------------------------------------------"
        else:
            first = False

        print_entry("Device", d.name)

        type = print_xml("Type:", ctx, "@type")

        if type == "file":
            print_xml("Source:", ctx, "source/@file")
            print_xml("Target:", ctx, "target/@dev")
        elif type == "block":
            print_xml("Source:", ctx, "source/@dev")
            print_xml("Target:", ctx, "target/@dev")
        elif type == "bridge":
            print_xml("Source:", ctx, "source/@bridge")
            print_xml("MAC Addr:", ctx, "mac/@address")
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]