Wednesday, April 1, 2009

Setting up a virtual development environment

I'm back in the LAMP development game, and for almost as long as I've been doing LAMP development, I've been setting up local development environments for my code. This way I can monkey with my code to my heart's desire, and if I break something, it doesn't affect the production site. (In theory, anyway). In the past, I've installed MySQL, Apache, and PHP/Perl directly on my workstation, and when a project has ended, I simply deleted the old database, Apache configs, etc.

Now, however, I've discovered virtualization and fallen in love. I have a Windows XP VM on my Linux desktop for the (rare) times I need a Windows box, and I've just created a VM to use as a development environment for the contract I'm currently working on.

To begin with, I created a base system using Debian Lenny (the current "stable" release) with qemu. This involved creating a disk image file 5 GB in size. That's awfully small these days, but this particular image file isn't going to have a lot of writing done to it. Instead I'll create a copy-on-write image that will have all the client-specific data in it. I do this by:

sudo kvm-img create /opt/qemu/lampsystem.img 5G

This creates a sparse 5G file in "raw" format.

Next, I want to install Debian on it:

sudo kvm -localtime -m 512 -cdrom debian-500-i386-businesscard.iso -boot d /opt/qemu/lampsystem.img

This mounts the Debian 5.0 network install ISO as a virtual CD-ROM and attaches it as the "D" drive (the tendrils of DOS are everywhere!) on the VM. Installing Debian is well-documented elsewhere, so I won't go into it here. I will note, however, that I'm installing everything in a single partition on the virtual disk because I don't see any point in setting up multiple partitions or LVM here.

After installing Debian, I'll install mysql-client, mysql-server, libapache2-mod-php5, openssh-server, and php5-cli. That should take care of the basic LAMP stuff. If I need Catalyst, Django, or something else, I'll tack those on later. I can always resize the root filesystem if I need to.

Note that after installing Debian, an automatic reboot of the VM won't work - it will reboot, but the boot process won't finish. Instead, you'll see this:


Booting from CD-Rom...
CDROM boot failure code : 0003
Boot from CD-Rom failed: could not read the boot disk
FATAL: No bootable device.


That's not a big deal. Just close qemu, then start it again. I'll use:

sudo kvm -localtime -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 /opt/qemu/lampsystem.img

Which gives my VM an IP I can ping from my workstation. I've also got IP masquerading set up for the VM, but that's another post for another time. I'll also generate an SSH key and copy it over to the VM so that I can SSH in without a password.

After the base VM image is ready, it's time to create the copy-on-write image:

sudo kvm-img create -b /opt/qemu/lampsystem.img -f qcow2 /opt/qemu/client_a.qcow2

Now I have a copy-on-write image just for "Client A"'s files. I can boot it by:

sudo kvm -localtime -m 512 -net nic,vlan=0 -net tap,vlan=0,ifname=tap0 /opt/qemu/client_a.qcow2

No comments: