Archive for May, 2012

Debian on MacOSX with VirtualBox in Headless Mode

Orignally I was searching for a way to build Buildroot images on MacOSX. Because my IMac has much more power than my Thinkpad X220i (i5 Quad-Core vs. i3 Dual Core). Therefore I tried to install all “build-essentials” (the Debian/Ubuntu users know what I mean) via MacPorts. Unfortunately some packets aren’t available through MacPorts, and missing packets aren’t the only problem, the fact that MacOSX not always complies to standards makes it hard/impossible to create uImages with Buildroot on MacOSX. Thus I was searching for a way to have a full Debian system available on my Mac.

My First idea was a Debian Chroot, but in my knowledge there is no way to get a Debian Chroot to run on MacOSX (although there is a Debian port for the Mach Microkernel). My second thought was then a Virtual Machine. I know with a VM I loose a lot of performance. But I gave it a try, because while my Mac executes build tasks I still can use my Laptop without any constrains and do something else at the time.

Therefore I was looking for an approach to have an “always on” Debian on my Mac (By “always on” I mean, starts when I boot my system, and runs in the background without needing too much CPU cycles when idling). At first I tried to automate the start of the VirtualBox application during boot. But I don’t wanted to have the running application always in my sidebar (two times actually, first, the VirtualBox icon itself, and second the VM icon).

Luckily on a website I found an explanation on how to start a VM in headless mode with VirtualBox’s command line tools. First I downloaded the Debian Netboot image (16MBytes), then I started the VirtualBox application and set up a new Linux system (For the name I chose “debian_shell”, I set the amount of Ram to 2Gbytes (from my 4GBytes) and for the harddisk I selected 8GBytes “dynamically allocated”. Afterwards I booted the downloaded Debian Netboot image to install a minimal system. During installation, in the packet selection screen I deselected “Desktop” and selected “SSH Server”. It was important for me to only have a terminal based system no X window overhead. That was it for now, my system was installed, configured and ready to be used.
After finishing the installation I configured the network behavior of my guest system (In the VirtualBox application “Right Click->Settings” on the “debian_shell” entry, then “Click” on “Network->Adapter1->”Port Forwarding”). I added an entry like the following to forward SSH connection attempts on my host machine on port 9003 to the default SSH port (22) on the guest system.


Then I created a small shell script with the following content to start my VM in headless mode.

RUNNING=`ps aux | grep -v grep | grep VBoxHeadless | cut -d ' ' -f 1`

if [ x$RUNNING == "x" ]; then
    VBoxHeadless --startvm debian_shell --vrdp=off > /dev/null 2>&1 &
    sleep 10
ssh death-jester@ -p 9003

I stored this script under “/usr/local/bin” with the name “debian”. Now when I called the script, it checked if the VM was already running if not it started the VM, and then logged into the machine via SSH, if the VM already ran it just logged in. For a more comfortable login I also stored the public key of my host-machine on the guest system.

Now have fun with your new “headless” Virtual Machine.