Openbox pipemenu to control virtual machines of VirtualBox

Openbox, Virtualbox… Coincidence? I don’t know. Anyway, I am a huge fan of VirtualBox, for the virtual machines are extremely fast, stable and nice to control. With the command line tool VBoxManage comes a nice tool for all those CLI-lovers out there. You can literally do anything with it, what the GUI can do. (at least it looks like that)

Now, starting the GUI to start a VM is a bit slow, so VBoxManage startvm "vm" is a huge time- and memorysaver.
Since I work with Openbox, the natural thing for me was to google for a suitable pipemenu. Unfortunately, I couldn’t find one. Long story short: Here is my self-written Openbox pipemenu to control VirtualBox’s VMs. (It can start and stop them, at least.)

Put it somewhere, make it executable and set it as a pipemenu in your menu.xml file.

#!/usr/bin/python

import subprocess,re

# kill action: 0 -> power off, 1 -> save state
killAction = 1

# Only change anything below this comment if you know, what you're doing!!
print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<openbox_pipe_menu>")

killActions   = ['poweroff', 'savestate']
notRunningVms = []
runningVms    = []
vmNamePattern = re.compile('"(.+)" .*')
for vm in subprocess.check_output(['VBoxManage', 'list', 'vms']).decode("utf-8").split("\n"):
    match = vmNamePattern.match(vm)
    if match:
        notRunningVms.append(match.group(1))
for vm in subprocess.check_output(['VBoxManage', 'list', 'runningvms']).decode("utf-8").split("\n"):
    match = vmNamePattern.match(vm)
    if match:
        runningVms.append(match.group(1))
        notRunningVms.remove(match.group(1))

if len(notRunningVms):
    print("<separator label=\"Start VM\"/>")
    for vm in notRunningVms:
        print("<item label=\"" + vm + "\">")
        print("<action name=\"Execute\">")
        print("<execute>VBoxManage startvm \"" + vm + "\"</execute>")
        print("</action>\n</item>")
if len(runningVms):
    print("<separator label=\"Stop VM\"/>")
    for vm in runningVms:
        print("<item label=\"" + vm + "\">")
        print("<action name=\"Execute\">")
        print("<execute>VBoxManage controlvm \"" + vm + "\" " + killActions[killAction] + "</execute>")
        print("</action>\n</item>")

print("</openbox_pipe_menu>")

To set whether VMs should be powered off or if their state should be saved, edit the value of killAction in the script.

2 Responses to “Openbox pipemenu to control virtual machines of VirtualBox”


Leave a Reply