Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to virsh (libvirt)

I am using virsh list to display the list of vms running on the computer. I want the information printed in the process in the form of a 2d array.

One way to go about this is to have the output, use tokenizer and store it in the array. But is there some other way where I can get directly this into the form of an array or something so that code is much more scalable. (Something that I could think of was using libvirt api in python)

like image 816
w2lame Avatar asked Feb 13 '11 18:02

w2lame


People also ask

Is Virsh part of libvirt?

Most virsh operations rely upon the libvirt library being able to connect to an already running libvirtd service. This can usually be done using the command service libvirtd start. Most virsh commands require root privileges to run due to the communications channels used to talk to the hypervisor.

Is there a GUI for KVM?

While KVM works in kernel-space, we use QEMU as the machine emulator for user-space. This QEMU KVM combination gives the users lightweight virtualization and good performance (but with no GUI).

Does Kubevirt use libvirt?

It uses libvirt to get all supported cpu models and cpu features on host and then Node-labeller creates labels from cpu models. Kubevirt can then schedule VM on node which has support for VM cpu model and features.

Does VirtualBox use libvirt?

6, the VirtualBox driver will always run inside the libvirtd daemon, instead of being built-in to the libvirt.so library directly.


1 Answers

There are indeed libvirt Python API bindings.

import libvirt

conn = libvirt.openReadOnly(None)  # $LIBVIRT_DEFAULT_URI, or give a URI here
assert conn, 'Failed to open connection'

names = conn.listDefinedDomains()
domains = map(conn.lookupByName, names)

ids = conn.listDomainsID()
running = map(conn.lookupByID, ids)

columns = 3

states = {
    libvirt.VIR_DOMAIN_NOSTATE: 'no state',
    libvirt.VIR_DOMAIN_RUNNING: 'running',
    libvirt.VIR_DOMAIN_BLOCKED: 'blocked on resource',
    libvirt.VIR_DOMAIN_PAUSED: 'paused by user',
    libvirt.VIR_DOMAIN_SHUTDOWN: 'being shut down',
    libvirt.VIR_DOMAIN_SHUTOFF: 'shut off',
    libvirt.VIR_DOMAIN_CRASHED: 'crashed',
}
def info(dom):
    [state, maxmem, mem, ncpu, cputime] = dom.info()
    return '%s is %s,' % (dom.name(), states.get(state, state))

print 'Defined domains:'
for row in map(None, *[iter(domains)] * columns):
    for domain in row:
        if domain:
            print info(domain),
    print
print

print 'Running domains:'
for row in map(None, *[iter(running)] * columns):
    for domain in row:
        if domain:
            print info(domain),
    print
like image 153
ephemient Avatar answered Oct 12 '22 01:10

ephemient