Python API to the VirtualBox VM
By nike on Sep 05, 2008
One of the important advantages of the VirtualBox virtualization solution is powerful public API allowing to control every aspect of virtual machine configuration and execution. Last month I was working on Python and Java bindings to that API. Those bindings are shipped with VirtualBox 2.0 SDK.
There are two families of API bindings:
- download VirtualBox 2.0 for your platform (Linux and Solaris Python bindings officially supported)
- download SDK
- unpack SDK
export VBOX_PROGRAM_PATH=/opt/VirtualBox-2.0.0/ PYTHONPATH=..:$VBOX_PROGRAM_PATH
./vboxshell.pyto start the shell
def showvdiCmd(ctx, args): mach = argsToMach(ctx,args) if mach == None: return 0 hdd = mach.getHardDisk(ctx['ifaces'].StorageBus.IDE, 0, 0) print 'HDD0 info: id=%s desc="%s" size=%dM location=%s' %(hdd.id,hdd.description,hdd.size,hdd.location) return 0and add following line to
'vdiinfo':['Show VDI info', showvdiCmd],
Then you can run it like this:
vdiinfo Win32 (or however your VM of interest is named).
Easy, isn't it? Moreover this command will work not only with XPCOM bindings, but with SOAP too.
This example also shows how to access VirtualBox constants in toolkit neutral manner - 'ifaces' field of context contains reflection information
useadble to get values of the constant.
Actually, there are other languages bindings to VirtualBox API shipped with SDK, including Java and C++, but I personally find Python easiest for start. You can ask here questions on VirtualBox language bindings (not only Python), and I will try to help.