Fun With VirtualBox Using Terminal

by The FOSS Convergence

The FOSS Convergence Blog

Used with permission

VirtualBox is “a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use.” This means that it emulates a computer graphically within your installed OS, allowing you to use different operating systems and applications virtually, without the need to install on “real metal.”

Whichever OS you use, out of Windows/Linux/*BSD, there is a version available for you to use. For the sake of this blog post I will assume that you are using a *BSD or Linux operating system (sorry Windows user, but I don’t use windows at all. I am sure that with some research, you may get the second part working within Windows. The first part should work OK). My second assumption is that you have already downloaded and installed VirtualBox, setup a virtual machine with an operating system in place and ready to use. If you have not, then read this blog post and decide if this is something you would like to try, and install VirtualBox from ports or your distribution’s repository (or the VirtualBox site).

The first part of this article is to show the terminal command-line version to launch your virtual machine directly into a window (or tile if using a tiling window manager). The second part will show you how to launch your VirtualBox image into a separate Xserver. The third part explains a couple of great practical applications of using this within your desktop environment or current window manager.

Part One

Starting a VirtualBox image from the command-line is actually remarkably simple. Start up a terminal and type:

VBoxManage startvm <Virtualbox Image Name>

For example, if I have created a virtualbox machine called “vbrowser,” it can be launched like this:

VBoxManage startvm vbrowser

This would launch the virtual image directly into its own window.

How simple is that?

Part Two

Ok, moving on. In order to launch your chosen virtualbox within its own (second or higher) Xserver, we need to set up a file called .vboximage. You can do this by using a text editor and add the following lines:

VBoxManage startvm <Virtualbox Image Name> &

exec <windowmanager>

Or, without a text editor, you can use the following directly from a terminal:

echo “VBoxManage startvm <Virtualbox Image Name> &” >> .vboximage

echo “exec <windowmanager>” >> .vboximage

Be careful to use >>, and not >, as the former appends to the file, while the latter will overwrite the file.

The <windowmanger> entry will be the one to host the virtual image. It’s best for it to be as small and light as possible. I have chosen “tinywm,” although “matchbox,” “ratpoison,” “dwm” or similar would do just as well. Install one on your real world install.

So, my .vboximage file looks like

VBoxManage startvm vbrowser &

exec tinywm

If you think this looks like a .xinitrc file, it is, but one for a dedicated virtual machine running in a separate Xserver.

Once you have your .vboximage file written, we can move to the cli command to launch it. Again, this is surprisingly simple:

startx /home/user-name/.vboximage — :1

So, to launch my Virtualbox image I would use:

startx /home/mrmysteryguest/.vboximage — :1

This tells the xserver to start up in the next tty. So, if your X is currently in “Alt+Ctl F8,″ it will start in “Alt+Ctl+F9,″ leaving your current Xsession untouched.

Part Three

OK, so now you know how to launch VirtualBox from the terminal, why would you want to?

One reason is Keybindings. You could add either of the terminal commands to your main DE or WM keybindings configuration file, allowing you to launch your virtual machine seamlessly, without having to launch the full VirtualBox control center to launch it.

I use SpectrWM, so to set up the keybindings, I just add the following to my .spectrwm.conf and restart the window manager using MOD+Q.

program[vbrowse] = VBoxManage startvm vbrowser #starts in a window

bind[vbrowse] = MOD+F4

program[newxvbox] = startx /home/mrmysteryguest/.vboximage — :1 #starts in new X

bind[newxvbox] = MOD+F5

This means that to launch the vbrowser virtual machine within a separate “window/tile”

and whichever virtual machine (windows even) is stipulated in .vboximage in a different Xserver.

I use these methods within FreeBSD to launch a cut down Linux ISO I remastered just to have Firefox & browser-plugins launch at starting ratpoison and a browser appliance, essentially serving as an alternative to adding the linux-binary-compatibility layer (which causes my machine to overheat somewhat).

Please let me know if you have any other tips and tricks of this type, sign up to the forum and share.