user3580316 user3580316 - 6 months ago 21
Python Question

Libvirt python add vCPU and memory

I am using the python bindings for Libvirt. How can I add or remove VCPU and memory for domains in Xen with libvirt?

In command line I am using:

Vcpu:

xm vcpu-set [domain-id] [count in #cores]


Memory:

Memory: xm mem-set [domain-id] [count in MB]


How can I run these commands in python with the libvirt bindings? Without using the subprocess.

Answer
from xen.util.xmlrpcclient import ServerProxy
server = ServerProxy(serverURI)
def xm_vcpu_pin(args):

    def cpu_make_map(cpulist):
        cpus = []
        for c in cpulist.split(','):
            if c == '':
                continue
            if c.find('-') != -1:
                (x, y) = c.split('-')
                for i in range(int(x), int(y) + 1):
                    cpus.append(int(i))
            else:
                # remove this element from the list
                if c[0] == '^':
                    cpus = [x for x in cpus if x != int(c[1:])]
                else:
                    cpus.append(int(c))
        cpus.sort()
        return ",".join(map(str, cpus))

    dom = args[0]
    vcpu = args[1]
    cpumap = cpu_make_map(args[2])

    server.xend.domain.pincpu(dom, vcpu, cpumap)

Full example of pinning vcpus with python