Posts tagged: Virtualbox

Automated VirtualBox + LVM Snapshot VM Provisioning

Just a quick follow-up to my previous post about using LVM to clone logical volumes to create new VM’s. I’ve written up a quick script to help automate the process which allows you to rapidly deploy a fleet of test VM’s without having to mess about.

The script is attached below and requires PHP. Configure the config options within the script and then to provision a new VM you simply sudo ./makevm.php <vmname> and it will take care of everything.

  • Creates the snapshot of the original logical volume
  • Sets the permissions on the device so that the user can access it
  • Creates the raw VMDK file and registers it with VirtualBox
  • Creates the base VM with the name provided
  • And finally it configures the VM to use the correct disk and network setting etc

That’s all there is to it :)

  makevm.tar (10.0 KiB, 940 hits)

Cloning VM’s with VirtualBox, LVM and Snapshots!

Recently I wanted to setup a development environment to test some Active Directory setups. Basically I wanted a whole stack of domain controllers and client machines across multiple sites, domains and forests to see how they tick and replicate between each other. Easy enough to do, just create a bunch of virtual machines using your favourite virtualization package, I prefer VirtualBox but you could do the same with VirtualPC or VMWare. But say you want to quickly deploy this and be able to easily tear down machines and bring new ones up, again easy enough just clone the disks and create new VM’s. All pretty straight forward. However there is a much neater and more elegant way of doing this which allows you to quickly snapshot a VM and redeploy in a matter of a seconds rather than minutes to hours copying full disk images around.

So how does one accomplish this task, well it’s really quite simple, LVM snapshots. If you have never heard or used LVM before it is a logical volume manager. It’s job is to take a bunch of physical volumes such as hard disks or raid arrays and combine them into a storage group. From this storage group you can then create multiple logical volumes (similar to luns in the Netapp world) which act like standard disk devices allowing you to easily carve up your storage in a more logical way. Now LVM is really quite a powerful tool and has many extra features which are outside of the scope of this article but see my links at the end to find out more on the specifics.

Anyway the feature we are going to use as I already mentioned is snapshots, what these are is an instantaneous snapshot of a volume which we can then mount separate to the real volume and use as a normal disk. The beauty of this is that your original volume remains completely untouched and any changes that are written to the snapshot are stored as just that changes or differences between that and the original volume. This means for our VM’s that only a few hundred meg or so will change that our disk usage is significantly reduced. You can see now just how useful this can be. The other big advantage is that they are fast, you can create a snapshot in a matter of seconds whereas to copy a vmdk or vdi file it takes a lot longer for it to copy, especially if using a dynamic disk as it locates all the used blocks.

Now lets get down to using it in the real world.

First things first get your VM installed using a raw device mapping.

lvcreate -L20G -nvbox_win2003std_base storage /dev/md2
Logical volume “vbox_win2003_base” created

In this instance I have created a 20gb volume called “vbox_win2003_base” on my storage volume group and forced it to allocate all its extents on /dev/md2. I prefer to do this just to make sure no logical volumes cross over physical volumes unless absolutely needed.

Now you have your volume ready you will need to create a disk for VirtualBox to use.

VBoxManage internalcommands createrawvmdk -filename “win2003_base.vmdk” -rawdisk /dev/storage/vbox_win2003std_base -register
VirtualBox Command Line Management Interface Version 2.2.2
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

RAW host disk access VMDK file win2003_base.vmdk created successfully.

This creates a vmdk file with a raw mapping to the logical volume and automatically registers it for use within VirtualBox.

Now prepare your VM like you would any other Windows machine being cloned. Preinstall all your OS, applications, software, any patches or tweaks and get it configured just the way you like. However try to keep things reasonably simple, this means don’t promote the machine to a DC or any other service which when cloned will break. For my builds I have been testing with Windows 2003 Standard edition. So this means you need to extract the from your Windows 2003 CD to c:\sysprep and run the setupmgr.exe app to generate a sysprep.inf. If you want to avoid problems don’t set a password for the admin account in the sysprep if you have already set one for the machine as it appears to fail when updating it. So you have generated your sysprep.inf, do a quick check to ensure everything is in it’s place and run sysprep.exe –reseal –mini –pnp. The machine will do its bit and then shut down meaning it is ready to be cloned.

Now the fun part, time to make a snapshot of your logical volume.

lvcreate -L5G -s -nvbox_vodka_dc /dev/storage/vbox_win2003std_base
Logical volume “vbox_vodka_dc” created

Now in the blink of an eye it has created a snapshot of your base VM’s logical volume with a maximum growth of 5gb. This means the VM can have a maximum of 5gb of changes before it runs out of space. You will need to fine tune this depending on your VM’s and workload. To keep an eye on it simply use lvdisplay as per normal.

lvdisplay storage/vbox_vodka_dc
— Logical volume —
LV Name                /dev/storage/vbox_vodka_dc
VG Name                storage
LV UUID                IOFUF4-Bxze-Xpij-Qd5b-nH7k-FeEx-k6936B
LV Write Access        read/write
LV snapshot status     active destination for /dev/storage/vbox_win2003std_base
LV Status              available
# open                 0
LV Size                20.00 GB
Current LE             5120
COW-table size         5.00 GB
COW-table LE           1280
Allocated to snapshot  0.00%
Snapshot chunk size    4.00 KB
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           253:4

Now that we have the snapshot volume all we need to do is create another raw device vmdk.

VBoxManage internalcommands createrawvmdk -filename “Vodka_DC.vmdk” -rawdisk /dev/storage/vbox_vodka_dc
VirtualBox Command Line Management Interface Version 2.2.2
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.

RAW host disk access VMDK file Vodka_DC.vmdk created successfully.

And that’s it, you now have a complete clone of that machine ready to load into a new VM within VirtualBox and boot. It will run through the mini-setup as per normal for any cloned machine and that’s it. All done!

With a few batch commands you can quickly automate the creating of a whole fleet of VM’s ready to plug into your dev environment.

Hope this helps a few people out there :)