Fun with virtual machines

19 October 2009
4:43 am

virtualboxBecause I am very proud of myself, I'm going to brag a bit.

I do a lot of web development, both between this site and others (I fiddle with crap here to such an awful degree, I'm never satisfied), and it's nice to be able to edit things in a non-live capacity. I had created a test subdomain for a while, but the constant upload, refresh, download, upload, refresh back and forth was interrupting my work flow, so I turned towards running a local installation of a web host instead. It's an easy enough task to install Apache, PHP and MySQL (the three prime components to WordPress) and there are variations for pretty much any operating system, including Windows. That worked for me initially, but it was always a cumbersome, time-consuming process any time I had to reformat a machine and set it up from scratch again. Not to mention that things would get inconsistent unless I happened to diligently copy files back and forth across all of my computers.

After I complained about my Windows Live Sync saga and a temporary switch to SugarSync, I did a full-time migration to Dropbox because WLS hadn't been upgraded for Snow Leopard or Windows 7 and I needed to find a reliable replacement. Having any local Apache installations pointing to my Dropbox folder fixed the inconsistencies issue for the most part, but it didn't take care of the escapade of installing and setting up the software on a fresh machine. Especially with this past week where I've updated all my Windows machines to 7 now that it's finally out on MSDN, I didn't really want to go through this process again.

I reformat my computers a lot. Adam thinks I get some sort of high from the activity but it's mostly back to that fidgeting thing.

Anyway, I played around with all-in-one solutions such as XAMPP which have integral components of web host software lumped together in only a single program you have to install. Since XAMPP in particular was cross-platform (I need it for OSX and Windows) I thought this would be great and save me the trouble of reinstalling constantly. I simply drop a directory where I want it and press a button and I have a local website– at least, in theory. While it worked, I had so many permissions issues and was going through so much hassle that just installing the full versions of the software like I usually do was going to be a lot less time consuming. Since I really didn't want to go back to that route, Adam suggested I look into setting up a virtual machine image and install the web host software onto that.

While there's a lot of virtual machine software, VirtualBox was one of the few that were cross-platform and, best of all, free. After doing some research into how to set things up, this was the route I settled on, and with Adam's help we got to cracking on how to actually implement it.

For what I wanted to do, I had a rather unique test case:

1) I have three "machines"; one is running Windows 7 while the other is my MacBook Pro that boots between OSX and Windows 7. Ideally, I wanted to configure the VM once and then simply copy that one image to the other machines.
2) The web host itself has to point to each machine's local Dropbox. I briefly toyed with installing Dropbox inside the VM but tossed this because it would have gotten unwieldy fast– in particular, since each VM would only be on whenever I was doing web stuff, it would have to download whatever was in Dropbox that wasn't on the VM's hard drive, putting the VM out of commission until it was finished.
3) For the MacBook Pro, I wanted the VM accessible whether I was at home connected to the LAN, over WiFi, or had no internet connection at all (this latter part is because Adam and I do a fair amount of traveling, and international plane trips are a great way to get a lot of work done.)

I debated installing a Linux image but ultimately installed Windows XP instead because I'm a lot more familiar with it. The first bullet point was easy enough to solve; after configuring and setting up the VM, copying it to other computers worked very easily– at least between Windows. OSX gave me an error about an incompatible image but thankfully this was fixed by a simple UUID replace in VirtualBox's XML configuration file. After I did a couple of tweaks specific to each machine, I had a more or less duplicate image tailored to that specific computer.

The second and third points were not quite as easy. While VirtualBox will let a VM access a host machine's files (through setting up shared directories), it requires the user set up mapped network drives to use them. When I had Apache pointing to this drive to serve the site from, it threw up a lot of errors that Adam and I simply couldn't figure out solutions for after two very frustrating hours of debugging. Eventually we figured out that the built-in network mapping included with VirtualBox was a bit iffy– when I set the drive up using the network mapping included in Windows by default, it worked immediately with no further errors. However, Apache apparently can't be started as a service if its DocumentRoot is on a network drive (but this was a non-issue since I just created a shortcut to the command line executable and popped it in the Windows startup folder). Now I at least had the site working correctly and writing itself to Dropbox, so it was an issue of setting up the laptop to work whether I was on a LAN or WiFi– or without internet entirely.

VirtualBox lets you map network adapters for the virtual machine to use, so it was easy enough to create an adapter that I would switch on and off depending on what connection my laptop itself had to the internet. But when it came to setting things up so it would be accessible without a network connection at all that the bulk of the annoyance started. As long as the virtual machine is connecting to the internet, my other computers could access the web host as if it was just a normal computer. But things got a lot more frustrating if there was no available internet connection, because my MacBook Pro couldn't talk to the VM, and the VM in turn wasn't aware of the MacBook Pro.

Eventually Adam's google-fu (not to mention his angelic patience with me) figured out a way to set up a third adapter in a way that tricked the VM into believing it was connected on a local network back to the host computer, even when there was no such connection available at all. While getting Apache to run in this way took an extra step of work, it was the last solution necessary to get what I wanted: a virtual machine that I only had to set up once which would serve the same content from my Dropbox folder no matter what computer was actually hosting the VM, and one that would be accessible to me wherever I happened to be.

While the legwork involved was actually quite a production, it is thankfully something I will only have to do just this once; even if I format a computer in the future, getting things back up to how they are today is a simple matter of reinstalling VirtualBox and dropping the VM image into its directory. After everything was working correctly the first thing I did was copy a backup of each image to an off-site drive in case I ever need to roll back. VirtualBox itself is pretty impressive too. I've used the free Microsoft Virtual PC to create VMs in the past so I could debug how websites look on IE 6 and 7, and VirtualBox has the same functionality as Microsoft's product (and then some) with the added benefit of working on OSX as well. An unexpected bonus of this means that now I can test websites on IE through OSX, which hasn't been possible since Microsoft stopped supporting the browser on anything but Windows.

Comments are closed.