2020-12-06

VirtualBox Selection Menu

linux   virtualization   index

In a previous post, I showed my dialog based login menu.

The menu included an option to start a VirtualBox virtual machine.

The idea is to open a dmenu or rofi selection list of all virtual machines available. It can work either from an existing session, or by itself on login, without a window manager.

virtual machines menu

virtual machines menu

We can get the list of virtual machines, and strip the unnecessary parts like this.

vboxmanage list -s vms | cut -d'"' -f 2 

This list can be passed to rofi, and can then be used to start the VM.

The standard way of starting a VirtualBox VM is as follows.

vboxmanage startvm "${VMNAME}"

However, there is a problem with this method, because vboxmanage spawns another process and disappears.

While this will work if ran from an existing session, it will not work on login, without a window manager, since the X session will end immediately and kill all processes including the virtual machine.

Fortunately, by looking at the processes, we can see that vboxmanage runs the following process /usr/lib/virtualbox/VirtualBoxVM. We can use that directly, and the VM can now start without a window manager.

Here is the menu script in full.

getvms() {
    vboxmanage list -s vms | cut -d'"' -f 2
}

CHOSEN=$(getvms | rofi -dmenu -i -p "Select VirtualBox Machine")

[[ -z ${CHOSEN} ]] && exit

/usr/lib/virtualbox/VirtualBoxVM --comment "${CHOSEN}" \
    --startvm "${CHOSEN}" \
    --no-startvm-errormsgbox

When ran without a window manager, powering off the VM will exit the X session automatically.

One last note, sometimes vboxmanage list is very slow, and takes a few seconds to display the menu. For my case, I am taking the list directly from the directory names. Both methods have the same result.