Isolate Projects with Virtual Machines

Today I’m talking about damage control. If you can relate to the comic below, this article is for you.

(Thanks to Randall Munroe at XKCD for very generous permissions on the reuse of his work.)

I think there’s a lot of broad applicability in VMs, but here I’ll go into developer concerns and leave out the kid-wrecks-family-computer scenarios and numerous others we could conjure up from collective experience. Developers are good at creating their own worst computer nightmares. We love to razz less technical users for the silly mistakes they make; from PEBKACs to ID10T errors, there’s a whole slew of inside jokes and insults for the “dummies” crowd. (BTW: did you know there’s a Twitter for Dummies? My wife brought a copy home as a joke one time. Seriously, it’s a real thing.)

Years ago I read an article by a developer and hobbyist working on some kind of emulator for an application-specific machine. I was in my teens at the time, and this was before most people could hope to afford machines powerful enough to run usable virtual machines. The author speculated with some humor about the philosophical implications of a computer simulating a computer. Or a simulated computer in turn simulating a computer itself. What’s funny about that is that Java was already several years old at the time, and Java runs in a virtual machine: in essence, a computer simulating a computer. And you could write a VM in Java, though the overhead might have been prohibitive years ago.

The virtual machines of interest here are very different from the JVM and other language-specific or application-specific VMs. Servers run these en masse using packages like Xen Hypervisor. You can run Xen on a desktop machine just as well, but if you want to run a VM more or less as if it were just another program in the system, I recommend VirtualBox. I looked into it on a recommendation when I was doing computer support for the Physics & Astronomy department in college. Since then I’ve used VBox on nearly every job I’ve held. It’s easy enough to run a setup of an O/S from scratch, and that’s what I’ve done for years. I’ll have a pet VM as a local development server, and it keeps my host system clean. I just run all the projects at work on that VM, mucking with the configuration as I see fit.

Recently I came to a realization. A common feature in VM software is the ability to clone a machine. Clones in VBox can be completely independent from their originals, or they can be minimal such that the clone depends on the original and stores only deltas for its own operation. The latter solution saves space and is fine as long as you don’t have to transfer a cloned VM to a new environment (new machine). I prefer complete clones because I can trash the original if I don’t want it as a basis anymore, and my clones survive. My new approach is to set up a single model VM that I never use, and that I never fully configure to a specific application. The system is loaded with a variety of tools: a complete LAMP stack, Mongo, Python, Node, a C/C++ compiler, Git, VIm, SSH (server and client), and so on. A lot of services (like the LAMP stack) are not running by default; they’re just ready to be switched on when required. Without a lot of the typical userland tools expected on a desktop machine, the VM is relatively compact (by today’s standards, even a couple of GiB’s is compact, but not everyone will agree with me). It supports shared host directories, so transferring files is easy.

When I start a new project of any kind I’ve already approached, it’s very easy to clone this model VM, boot up the clone, and configure to the requirements of the project at hand. If I need a unique server config, want to do something experimental, need to isolate resources in any way, whatever, I can ensure fairly complete isolation with minimal spin-up time. The host system doesn’t need to run anything exotic, because all the exotic software is siloed in guest VMs. A configuration is no longer needed, or something is broken beyond repair? I can roll the whole system back to a previous snapshot (assuming I’ve taken them), or just trash it if I prefer. None of this capability is that new in terms of computer technology. What’s new (at least to me) is the amount of latitude I can create for myself using virtual machines as disposable items rather than carefully crafted virtual devices. You might be doing much of this yourself already, or you might have gone much further down the rabbit hole. If so, post a reply and tell me what you’re doing with virtual machines to save yourself headaches.

Image sources

  • server_problem: Xkcd