I had a requirement to test backing up 100 VMs simultaneously using Avamar. I wasn’t particularly interested in creating the VMs through vSphere UI. Instead, I downloaded the vSphere Perl SDK and found a script called vmclone.pl. This script is able to create clones by connecting to a vCenter and specifying a source and target VM.
The command syntax is as follows:
/usr/lib/vmware-viperl/apps/vm/vmclone.pl –username <vcenter username> –password <vcenter password> –url https://<vcenter hostname>/sdk/webService –vmhost <esx hostname> –vmname <vm to clone> –vmname_destination <vm to create> –datastore <datastore>
The next challenge was to run the process as fast as possible and that requires parallelism. To achieve that in a shell context I used GNU parallel. This program is able to spawn multiple shell commands concurrently. A concurrency limit can be defined with the -j flag.
In this instance I wanted to run up to 8 clone operations in parallel until all 100 completed. The one line command I used to achieve this is below:
seq -w 0 99 | parallel -j8 -k /usr/lib/vmware-viperl/apps/vm/vmclone.pl –username administrator –password <password> –url https://winvc.mlab.local/sdk/webService –vmhost esx.mlab.local –vmname blank –vmname_destination blank{} –datastore raidarray
This created 100 VMs named blank00 to blank99 using the existing VM named blank.
I put all the 100 VMs into a resource pool called test100 using vmmigrate.pl.
seq -w 00 99 | parallel -j8 -k /usr/lib/vmware-viperl/apps/vm/vmmigrate.pl –username administrator –password <password> –url https://winvc.mlab.local/sdk/webService –targetpool test100 –vmname blank{} –sourcehost esx.mlab.local –targethost esx.mlab.local –targetdatastore raidarray
Job complete.
I was pretty chuffed with vmclone.pl. But it still took 4 hrs to copy from one thin VM to 100 thin VMs. There had to be a better way. Introducing the linked clone.
What is a linked clone? Essentially read/write snapshot of a source VM’s snapshot. In my case the source VM is called blank and the snapshot name is called lc.
To create the linked clones I used virtuallyGhetto. The viruallyGhetto package comes with a script vGhettoLinkedClone.pl which is a modified version of vmclone.pl that creates linked clones.
So here it is 100 VMs in 1 minute using one command line.
seq -w 0 99 | parallel -j10 -k PERL_LWP_SSL_VERIFY_HOSTNAME=0 vGhettoLinkedClone.pl –server winvc.mlab.local –vmhost esx.mlab.local –username administrator –password <password> –vmname blank –vmname_destination blank{} –snapname lc –convert sesparse –datastore raidarray
And if you are wondering it took 1m13.084s. Yes I cheated
Now you might say but this could be done with vSphere PowerCLI. Well that may be true but the UNIX shelll is my preferred terminal window for automation tasks.