Alex headshot

AlBlue’s Blog

Macs, Modularity and More

Running headless VMs under Mac OS X

2009, article, howto, mac

I recently needed to be able to install some software which wasn't available as a pre-compiled binary for Mac OSX, but was for a variety of Linux distributions. As a result, I decided to finally roll out a virtual Linux box running on my Mac OSX box in order to be able to test against a Linux environment as well as an OSX one (which, of course, is already UNIX based).

I'm using a run-of-the-mill Mac Mini, dual-core 2GHz with 4GB of memory (only 3 accessible, I suspect) with 10.5 (no, I've not updated to Snow Leopard yet). Getting an installation of Debian up and running on a virtual environment was actually pretty easy; the following should apply equally well for other (Intel x86) operating systems. It should be noted that since I'm on 10.5, I'm using a 32-bit kernel, along with 32-bit app and guest OS. I don't know how well it would fare if the guest OS and/or OS were not matched.

I downloaded VirtualBox for Intel x86 (it has two separate binaries, one suffixed -x86, and one suffixed -amd64, which has the x86_64 binary type; so I guess it would work on a 64-bit OS). Normally, this is a client-app; installing it gives you /Applications/VirtualBox.app but it's possible to run headless as well.

Inside /Applications/VirtualBox.app/Contents/MacOS there's a plethora of executables; but the two to look out for are VBoxManage (for creating and looking after VMs) and VBoxHeadless (for running them). I created a daemon user to run the VM images, and set that up in the PATH for the login, but you could run them from that directory as well if you want.

Setting up the disks. In order to have a VM, I needed a (virtual) disk image. VBoxManage createhd --filename Debian.hd --size 10000 creates you a file (in ~/Library/VirtualBox/HardDisks) which can be subsequently mounted. For reasons best left to random conjecture, once you've created it, you have to then do VBoxManage openmedium disk Debian.hd before you can do anything useful with the image.

Getting the Boot CD. Most Linux distros come with a bootable CD (or DVD) with everything you could want on it. Typically, they'll end in .iso; you might also see .cdr, but it's pretty much the same thing. You can mount this as a file (no need to burn it unless you feel like it) by doing: VBoxManage openmedium dvd /path/to/linux.iso. In the case of Debian, I used the NetInst CD although there's no reason necessarily to use that instead of the larger ones. If you end up defining networking, it'll pull the rest down as needed anyway. If you're not doing networking, then you might want to download one of the larger CD or DVD images; but the netinst is preferrable, because it will go out of date less quickly than the others.

Creating the VM. Once we've got the (virtual) media together, we're ready to go onto our (virtual) box. VBoxManage createvm --name Debian --ostype Debian --register will give you a new VM defined with the name “Debian”. The OS type Debian is one of the IDs in VBoxManage list ostypes, though in reality, one distro is pretty much like another as far as virtualisation is concerned (I suspect it's got more to do with installing the host extras, which I largely suspect are unnecessary for a headless/server box).

Tweaking the VM. Once you've got an empty box, we'll need to configure it to tell it where the hard disks are and what other parameters to use. There's a wealth of data in the user doc (in the VirtualBox.app/MacOSX folder, of all places...) but we do need to get a few things configured. VBoxManage modifyvm Debian --memory 512 --acpi on --vram 128 --hda Debian.hd --dvd /path/to/linux.iso --nic1 bridged --bridgeadapter1 "en0: Ethernet" --macaddress1 0123456789.

Quite a mouthful, and some parts should be obvious. The networking setup defines adapter 1 to be bridged (i.e. this VM is visible to the outside world) and based upon the ethernet connection "en0", which is the wired connection on the box. For some reason, specifying "en0" didn't work, but "en0: Ethernet" did. I suspect a regular expression is looking for the colon, and complaining if it's not there; the stuff to the right I believe to be a comment. Incidentally, you can specify a Mac address (otherwise it changes each time) which will help with those using DHCP to map to a fixed network address. For reasons best left to Sun, the second digit in the mac address must be even (0, 2 etc.) - if it's odd, you'll get "ERROR: Invalid MAC address format". Obviously.

Getting ready to go. So, we can turn on our machine now, by running VBoxHeadless -s Debian. However, this will boot from the DVD, and we can't see what's going on. VirtualBox has a nifty remote desktop featurette which gives a remote viewing experience. Unfortunately, it's based on Microsoft's RDP, but that's probably a commercially sensible plan anyway. You can download a Microsoft RDC client for Mac (the page is pretty poor; the 'Downloads' section is hidden in a pane in the bottom middle; and when you click that link, it populates a pane on the right which you have to click again to get going).

To run with a remote connection, kill (Ctrl+C) the running box and start again with VBoxHeadless -s Debian -v on. This will give you a port 3389 remote desktop connection (the default) and you can simply put the name of the host OS in the remote desktop client. (If you have port collisions, you can specify -p 1234 and then connect to hostos:1234 from remote desktop).

Now that you've booted from the DVD, you can go through the joyous steps of installing your favourite Linux flavour.

Time passes. ...

Once you're ready to finish the installation, you need to modify the VM to get rid of the DVD. Simply run VBoxManaage modifyvm --dvd none to eject the DVD (I did this after having stopped the install) and then start again.

Once you're convinced that everything is running, you can stop the RDP with VBoxHeadless -s Debian -v off and continue to log in remotely via SSH or whatever flavour of remote access you're going to use.

Summary. Installing is easy, configuration takes a bit of work. If you prefer, you can set up the image locally on a machine tow which you have a GUI, and then copy the settings over to the remote host. And don't forget to eject the installation CD when you reboot to prevent going round the loop again.

Here's a summary of the steps:

  • Downloads
  • Setup
    • VBoxManage createhd --filename Debian.hd --size 10000
    • VBoxManage openmedium disk Debian.hd
    • VBoxManage openmedium dvd /path/to/linux.iso
    • VBoxManage createvm --name Debian --ostype Debian --register
    • VBoxManage modifyvm Debian --memory 512 --acpi on --vram 128 --hda Debian.hd --nic1 bridged --bridgeadapter1 "en0: Ethernet" --macaddress1 123456789012 --dvd /path/to/linux.iso
  • Running
    • VBoxHeadless -s Debian -v on -p 1234
    • VBoxHeadless -s Debian -v off