virtualbox.pool – machine pool management

Virtual Machine pool

The MachinePool manages a pool of linked clones against a defined “root machine”. This module works with multiple processes running on the host machine at a time. It manages a resource lock over the root virtual machine to ensure consistency.

In this example the machine win7 has a current version of guest editions installed and is in a powered off state.

Create multiple clones:

pool = MachinePool('win7')
sessions = []
for i in range(3):
    sessions.append(pool.acquire("Mick", "password"))

# You now have three running machines.
for session in sessions:
    with session.guest.create_session("Mick", "password") as gs:
        _, out, _ = gs.execute("ipconfig")
        print(out)

for session in sessions:
    pool.release(session)

A reliable version of the above code would look like this:

pool = MachinePool('win7')
sessions = []
try:
    for i in range(3):
        sessions.append(pool.acquire("Mick", "password"))

    # You now have three running machines.
    for session in sessions:
        with session.guest.create_session("Mick", "password") as gs:
            _, out, _ = gs.execute("ipconfig")
            print(out)

finally:
    for session in sessions:
        try:
            pool.release(session)
        except Exception as err:
            print("Error raised on release: %s" % err)

Code reference

class virtualbox.pool.MachinePool(machine_name)[source]

MachinePool manages a pool of resources and enable cross process coordination of a linked machine clone.

acquire(username, password, frontend=’headless’)[source]

Acquire a Machine resource.

release(session)[source]

Release a machine session resource.