Sunday, March 29, 2015

CloneVM scripts for Free ESXi 5.5

Overview

    Update:  In an earlier version of this post, I advertised the clonevm.sh script as producing a linked clone.  In reality it was producing a full clone.  I have since gone back and written a script to perform linked cloning (linkedclonevm.sh) and renamed the clonevm.sh script to fullclonevm.sh to more accurately capture it's function.

    For users of VMWare Workstation or (finally) VMWare Fusion, clones are a highly useful feature.  Full clones are a simple copy of the original hard drive, useful for quickly creating standalone copies of a VM.  Linked clones make copies that are one better.  For those not aware of this feature, linked clones allow you to use one VM as the basis for multiple clones.  Each clone uses the base VMs hard drive as the basis for its own hard drive leaving the clone responsible only for tracking its own changes (vs. maintaining a full hard drive).  The practical benefit of doing this is ESXi storage savings.  For instance, without cloning, 5 identical VMs each with a 20GB hard drive will take up 100GB of storage.  With cloning, the 5 clones all use the same hard drive resulting in an initial allocation of 20GB for all 5 VMs that slowly grows as the 5 VM hard drives diverge from the source disk.

    While ESXi has facilities for producting a full clone of a hard drive using vmkfstools, it stops well short of what is required for a fully functioning clone.    For linked cloning, ESXi provides even less support.  As a comparison, both Workstation and Fusion clone not only the hard drive, but also all of the associated virtual hardware needed to run the VM.  A friend of mine tells me that the full up vSphere install behaves similarly, but at $5K+/year, that's not a solution that is going to work at my price point.

    Not finding what I was looking for on the internet, I decided to write my own ESXi clone scripts (get them from my github repo here).  For linked clones, the script is by-and-large based upon the following post at sanbarrow.com.  The target ESXi version for these scripts was 5.5 and they have only been tested against 5.5 so if you have a different version, your mileage may vary.  Here's what they do:

fullclonevm.sh
  • Create a dummy VM with the name you specify
  • Delete the dummy VMs hard drive 
  • Clone the specified source hard disk into the dummy VMs directory
  • Add the cloned hard drive to the dummy VM
  • Add the requested number of network interfaces to the VM
  • Add the requested amount of memory to the VM
linkedclonevm.sh (must snapshot source VM first, as it is the basis for the linked clone)

  • Create the linked clone's directory
  • Copy the target VMX and snapshot VMDKs into the linked clone directory
  • Edit the snapshot VMDK to point back to the source VMs VMDK file
  • Change the memory allocated to the linked clone, if requested
  • Change the display name of the linked clone
  • Register the linked clone with the system
  • Change the number of NICs, if requested


What follows is a how-to on installing and using my tools, fullclonevm.sh and linkedclonevm.sh