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

Installation  

Once you've downloaded the scripts from my repo, just copy them to your ESXi server to install (to do this, you'll have to enable SSH and ESXi shell first).  As I'm using Linux, as an example, here is how I would accomplish the task from my terminal window:

    cd /path/to/clonevm/script 
          #Change to the directory with the script
    scp ./*clonevm.sh root@ESXiServer: 
          #Copy the script to / on the ESXi Server
    ssh root@ESXiServer 
          #SSH onto the ESXi Server
    mkdir /vmfs/volumes/DATASTORENAME/bin 
          #make a bin directory for the script
    mv ./vmclone.sh /vmfs/volumes/DATASTORENAME/bin/ 
          #move the script to the bin directory
    export PATH="/vmfs/volumes/DATASTORENAME/bin:$PATH"  
          #add the new bin directory to $PATH (so I can run vmclone.sh without typing the whole path).

Usage

To get the full usage statement, just type:
     fullclonevm.sh -h 
or
   partialclonevm.sh -h

Here is the linkedclone usage statement, reproduced:

USAGE:  linkedclonevm.sh -s /path/to/source/vmdk -d /path/to/new/vm [-n X] [-m Y] [-f] [-q] [-h]

    where:
        -s  - Source drive.  Path to source vmx file.  VMX must be linked to a snapshot for this to work
        -d  - Destination.  Directory that should be created to house the new vm files
        -n  - Number of network interfaces (1-10).  Optional argument.  Default is to not change number specified in vmx file.
        -m  - Amount of memory allocated to machine in megabytes.  Use G suffix to specify gigabytes.  Optional argument.  Default is to not to change amount defined in vmx file.
        -f  - Force cloning.  Bypasses user prompts, for use with scripting.  Optional argument.
        -q  - Quiet.  Outputs only vmid of vmcreated.  Useful for scripting.  Implies -f.
        -h  - Help.  this usage statement.

For linked cloning, you must snapshot your source disk first (for full cloning, snapshoting is not necessary, but recommended).  When looking in the source VM directory, snapshots end in -000xx.vmdk, the highest number vmdk should be the most recent snapshot.  Note that while you must provide the exact vmdk you wish to use as the basis of your clone, you need only provide the name of the destination directory (i.e., VM name) to clone the VM. 

    As an example, if I wanted to create a clone vm from snapshot 00001 of the ubuntu vm with 2 NICs and 2 GB of memory, I would use the following command:
    
    linkedclonevm.sh -s /vmfs/volumes/datastore/ubuntu/ubuntu-00001.vmdk -d /vmfs/volumes/datastore/clone -n 2 -m 2G

    Before linkedclonevm.sh starts the cloning, it presents you with all of the options you are about to use for cloning.  Press ENTER to start cloning, or CTRL-C to abort.  For purposes of scripting (to clone multiple VMs), this can be disabled by using the -f flag.  For instance, to create 5 VMs at once, you could use the following for loop:

    for num in $(seq 1 5); do linkedclonevm.sh -s /vmfs/volumes/datastore/ubuntu/ubuntu-00001.vmdk -d /vmfs/volumes/datastore/clone$num -n 2 -m 2G -f; done

Any questions/comments regarding this script?  Let me know in the comments.

No comments:

Post a Comment