ghettoClone.sh - Automate cloning VM(s) for ESX/ESXi 3.5u2+ utilizing VMware VIMA: RCLI + VI Perl Toolkit

ghettoClone.sh - Automate cloning VM(s) for ESX/ESXi 3.5u2+ utilizing VMware VIMA: RCLI + VI Perl Toolkit

Description

This script utilizes VMware RCLI (Remote command line interface) and VMware VI Perl Toolkit found in VMware VIMA.

VMware currently provides few automated options to clone existing virtual machines that require the use of Virtual Center. That is, virtual machine cloning can be achieved through the use of VI Client and tools like “vmclone.pl” (which is part of the VI Perl Toolkit) however it is a stringent requirement that Virtual Center must be present in the virtual infrastructure to take advantage of these options. In contrast, virtual machine cloning can also be achieved manually by executing a sequence of commands through the Service Console. The benefit of this option is that Virtual Center is not needed however it quickly becomes cumbersome and impractical if a large number of virtual machines are to be cloned.

The motivation for this script ( ghettoClone.sh ) then is to provide administrators with a means to automatically clone and deploy a large subset of virtual machines into a virtual environment without requiring tedious Service Console work or Virtual Center. Furthermore, for ease of accessibility, the free VMware VIMA appliance was chosen as the central launch point for the cloning process. ghettoClone.sh is executed from within VIMA and is compatible with both ESX and ESXi 3.5u2+. It accepts a single source virtual machine along with inputted configuration parameters to tailor cloned virtual machines.

Please note that ghettoClone.sh will generate full one to one copies of the source VM (which include all VMDK files associated with it). Each cloned virtual machine will inherit all attributes pertaining to the source VM except that of the UUID bio/location ID’s and display name which will be unique across all generated clones. If you're looking to deploy Linked Clones, please refer to this script instead: http://communities.vmware.com/docs/DOC-9020

Important Note: Upon execution of the cloning process, the user will be prompted to select an available datastore where cloned virtual machines will be stored. It is up to the user to ensure that there is enough space prior to executing the cloning process. This includes but is not limited to considering swap file sizes and room for possible snapshots. The cloning process will begin immediately after confirmation of the datastore so please proceed with caution. It is recommended that the dry run process is executed before committing to a configuration.

Requirements:

  • VMware VIMA (virtual appliance)

  • ESX/ESXi 3.5u2+

  • my-vmregister.pl (modified version of the sample code provided by VMware) [Download at the bottom]

Usage:

ghettoClone.sh input parameters:

Usage: ./ghettoClone.sh [-h SERVER] [-v VM_TO_CLONE] [-n NAMING_CONVENTION] [-s START] [-e END] [-f NAME_FILE] -d

Please quote your params if you dare to use spaces in your naming conventions!
Ensure this script is being executed on VMware VIMA and your ESX/ESXi hosts are 3.5u2+

OPTIONS:
   -h   ESX/ESXi 3.5+ Server IP or Hostname
   -v   VM to clone (VM display name)
   -n   Clone naming convention
   -s   Starting value
   -e   Ending value
   -f   File containg naming convention for VM clones (separate each name with newline)
   -d   Dry run, produces only text output of what would be executed based on input parameters

(e.g.)
        ./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB_TEST -n "UCSB-CLONE-" -s 1 -e 10
        ./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB_TEST -n "UCSB-CLONE-" -s 1 -e 10 -d
        ./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB_TEST -f cloneInputFile.txt
        ./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB_TEST -f cloneInputFile.txt -d

Use cases

There are 2 supported use cases:

Option 1 - Deploy clones from source VM by specifying a cloned VM naming convention and amount (start/end parameter) of clones.

Option 2 - Deploy clones from source VM by specifying a subset of VM names through an input file. The amount of cloned VMs will directly correlate with the amount of display names present in the input file.

Both use cases support the “Dry Run” feature which allows the user to simulate and validate execution of the script before actual execution of the cloning process.

Prerequisite

Prior to starting, ensure that the VMware VIMA host is managing the appropriate ESX or ESXi 3.5u2+ hosts (those that will be utilizing the ghettoClone.sh script). The script uses VMware VI Fast Pass authentication to connect to the ESX/ESXi server(s). The following steps will demonstrate how to set this up prior to executing the script.

1. Add ESX/ESXi hosts to VIMA managment using vifp

A password prompt for the vi-admin account and root password to the pertinent ESX/ESXi host(s) will be presented.

[vi-admin@vima-primp-industries ~]$ sudo vifp addserver olga.resnet.ucsb.edu
root@olga.resnet.ucsb.edu's password:
[vi-admin@vima-primp-industries ~]$

2. Verify that the appropriate hosts are being managed by VIMA

[vi-admin@vima-primp-industries ~]$ sudo vifp listservers
olga.resnet.ucsb.edu
himalaya.primp-industries.com

3. Lastly, prior to running any of the RCLI or VI Perl Toolkits that utilize VI Fast Pass (not all can), run the following command on the appropriate ESX/ESXi host(s).

[vi-admin@vima-primp-industries ~]$ vifpinit olga.resnet.ucsb.edu

To remove a server from VIMA, use the argument “removeserver” on the host and provide the appropriate credentials.

Now, you're ready to begin!

-


Example execution

Environment

  • ESXi 3.5u3 host

  • Source VM 1 is Windows XP SP2 with 2 VMDK's (10 GB and 50 MB) named UCSB_TEST stored on local storage

  • Source VM 2 is Windows XP SP2 with 2 VMDK's (10 GB and 100 MB) named UCSB-ENGINEERING stored on FC LUN

  • Clone 3 VMs

Option 1

1. Dry run execution

./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB_TEST -n "UCSB-RESNET-CLONE-" -s 1 -e 3 -d

[vi-admin@vima-primp-industries ~]$ ./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB_TEST -n "UCSB-RESNET-CLONE-" -s 1 -e 3 -d

Retrieving info from: olga.resnet.ucsb.edu ...
Selection    Available space       Maximum Capacity     Type                 Name
1)           34.18 GB              124.75 GB            VMFS                 dlgCore-FC-LUN200.Templates
2)           244.61 GB             255.25 GB            VMFS                 olga-local-SAS.Storage
3)           91.58 GB              151.75 GB            VMFS                 dlgCore-FC-LUN202.VMstorage2
4)           29.19 GB              152.75 GB            VMFS                 dlgCore-FC-LUN203.VMstorage3
5)           53.29 GB              150.75 GB            VMFS                 dlgCore-FC-LUN201.VMstorage1
6)           exit

Please select a datastore in which the cloned virtual machines will be stored:
2
#################### DRY RUN MODE ####################
# Temp working directory: tmp_clone_2009-01-09_144356
# ESX Server to initiate clone: olga.resnet.ucsb.edu
# SOURCE VM: UCSB_TEST
# SOURCE VM PATH: [http://olga-local-SAS.Storage|http://olga-local-SAS.Storage] UCSB_TEST/UCSB_TEST.vmx
# SOURCE VM DIR: [http://olga-local-SAS.Storage|http://olga-local-SAS.Storage] UCSB_TEST
# SOURCE VM DATASTORE: [http://olga-local-SAS.Storage|http://olga-local-SAS.Storage]
# DESTINATION VM DATASTORE: [http://olga-local-SAS.Storage|http://olga-local-SAS.Storage]
# VMDK-[0]: ->UCSB_TEST_1.vmdk<-
# VMDK-[1]: ->UCSB_TEST.vmdk<-
# 2 valid VMDK(s) total
# Clone naming convention: UCSB-RESNET-CLONE-{1-3}
# 3 clone(s) to create
##################################################

Start time: Fri Jan  9 14:36:59 PST 2009
End   time: Fri Jan  9 14:37:04 PST 2009
Duration  : 5 Seconds

2. Select a datastore and start clone duplication ( note: in this instance, duplication will take place on the same datastore [local storage: source and destination]. Duplication may be faster on FC/iSCSI/NFS and dissimilar source and destination datastores.)

./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB_TEST -n "UCSB-RESNET-CLONE-" -s 1 -e 3

[vi-admin@vima-primp-industries ~]$ ./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB_TEST -n "UCSB-RESNET-CLONE-" -s 1 -e 3

Retrieving info from: olga.resnet.ucsb.edu ...
Selection    Available space       Maximum Capacity     Type                 Name
1)           34.18 GB              124.75 GB            VMFS                 dlgCore-FC-LUN200.Templates
2)           244.61 GB             255.25 GB            VMFS                 olga-local-SAS.Storage
3)           91.57 GB              151.75 GB            VMFS                 dlgCore-FC-LUN202.VMstorage2
4)           29.19 GB              152.75 GB            VMFS                 dlgCore-FC-LUN203.VMstorage3
5)           53.29 GB              150.75 GB            VMFS                 dlgCore-FC-LUN201.VMstorage1
6)           exit

Please select a datastore in which the cloned virtual machines will be stored:
2

0% Complete! - Creating clone "UCSB-RESNET-CLONE-1" - 0/3 - Time elapsed: 5 Seconds
0% Complete! - Creating clone "UCSB-RESNET-CLONE-1" - 0/3 - Time elapsed: 31 Seconds
...
0% Complete! - Creating clone "UCSB-RESNET-CLONE-1" - 0/3 - Time elapsed: 6.67 Minutes
33% Complete! - Creating clone "UCSB-RESNET-CLONE-2" - 1/3 - Time elapsed: 6.77 Minutes
...
33% Complete! - Creating clone "UCSB-RESNET-CLONE-2" - 1/3 - Time elapsed: 13.43 Minutes
66% Complete! - Creating clone "UCSB-RESNET-CLONE-3" - 2/3 - Time elapsed: 13.52 Minutes
...
66% Complete! - Creating clone "UCSB-RESNET-CLONE-3" - 2/3 - Time elapsed: 20.45 Minutes
100% Complete! - Creating clone "UCSB-RESNET-CLONE-3" - 3/3 - Time elapsed: 20.53 Minutes

Start time: Fri Jan  9 14:48:23 PST 2009
End   time: Fri Jan  9 15:08:56 PST 2009
Duration  : 20.55 Minutes

Option 2

0. Input file

[vi-admin@vima-primp-industries ~]$ cat toCloneList
ALima
AAmbrosio
MKerr

The script accepts a file listing display names of the VM clones to be created. Each needs to be separated by a newline.

1. Dry run execution

./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB-ENGINEERING -f toCloneList -d

[vi-admin@vima-primp-industries ~]$ ./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB-ENGINEERING -f toCloneList -d

Retrieving info from: olga.resnet.ucsb.edu ...
Selection    Available space       Maximum Capacity     Type                 Name
1)           52.83 GB              124.75 GB            VMFS                 dlgCore-FC-LUN200.Templates
2)           244.61 GB             255.25 GB            VMFS                 olga-local-SAS.Storage
3)           91.50 GB              151.75 GB            VMFS                 dlgCore-FC-LUN202.VMstorage2
4)           29.19 GB              152.75 GB            VMFS                 dlgCore-FC-LUN203.VMstorage3
5)           53.29 GB              150.75 GB            VMFS                 dlgCore-FC-LUN201.VMstorage1
6)           exit

Please select a datastore in which the cloned virtual machines will be stored:
3
#################### DRY RUN MODE ####################
# Temp working directory: tmp_clone_2009-01-09_151630
# ESX Server to initiate clone: olga.resnet.ucsb.edu
# SOURCE VM: UCSB-ENGINEERING
# SOURCE VM PATH: [http://dlgCore-FC-LUN200.Templates|http://dlgCore-FC-LUN200.Templates] UCSB-ENGINEERING/UCSB-ENGINEERING.vmx
# SOURCE VM DIR: [http://dlgCore-FC-LUN200.Templates|http://dlgCore-FC-LUN200.Templates] UCSB-ENGINEERING
# SOURCE VM DATASTORE: [http://dlgCore-FC-LUN200.Templates|http://dlgCore-FC-LUN200.Templates]
# DESTINATION VM DATASTORE: [http://dlgCore-FC-LUN202.VMstorage2|http://dlgCore-FC-LUN202.VMstorage2]
# VMDK-[0]: ->UCSB-ENGINEERING_1.vmdk<-
# VMDK-[1]: ->UCSB-ENGINEERING.vmdk<-
# 2 valid VMDK(s) total
# Clone naming convention: [ AAmbrosio ALima MKerr ]
# 3 clone(s) to create
##################################################

Start time: Fri Jan  9 15:16:36 PST 2009
End   time: Fri Jan  9 15:16:41 PST 2009
Duration  : 5 Seconds

2. Select datastore and start clone duplication ( note: in this instance, duplication will take place from one source FC datastore to different destination FC datastore)

./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB-ENGINEERING -f toCloneList

[vi-admin@vima-primp-industries ~]$ ./ghettoClone.sh -h olga.resnet.ucsb.edu -v UCSB-ENGINEERING -f toCloneList

Retrieving info from: olga.resnet.ucsb.edu ...
Selection    Available space       Maximum Capacity     Type                 Name
1)           52.83 GB              124.75 GB            VMFS                 dlgCore-FC-LUN200.Templates
2)           244.61 GB             255.25 GB            VMFS                 olga-local-SAS.Storage
3)           91.50 GB              151.75 GB            VMFS                 dlgCore-FC-LUN202.VMstorage2
4)           29.19 GB              152.75 GB            VMFS                 dlgCore-FC-LUN203.VMstorage3
5)           53.29 GB              150.75 GB            VMFS                 dlgCore-FC-LUN201.VMstorage1
6)           exit

Please select a datastore in which the cloned virtual machines will be stored:
3

0% Complete! - Creating clone "AAmbrosio" - 0/3 - Time elapsed: 10 Seconds
...
0% Complete! - Creating clone "AAmbrosio" - 0/3 - Time elapsed: 2.62 Minutes
33% Complete! - Creating clone "ALima" - 1/3 - Time elapsed: 2.70 Minutes
...
33% Complete! - Creating clone "ALima" - 1/3 - Time elapsed: 5.42 Minutes
66% Complete! - Creating clone "MKerr" - 2/3 - Time elapsed: 5.50 Minutes
...
66% Complete! - Creating clone "MKerr" - 2/3 - Time elapsed: 8.12 Minutes
100% Complete! - Creating clone "MKerr" - 3/3 - Time elapsed: 8.20 Minutes

Start time: Fri Jan  9 15:21:35 PST 2009
End   time: Fri Jan  9 15:29:49 PST 2009
Duration  : 8.23 Minutes

Labels (1)
Attachments
Comments

Note, VMware ESXi 3.5u4 was just released and the internal API seems to have been fixed and VI API will have only read-only access when using the free licensed version of ESXi. This script will most likely not work on U4, please be advised if you're going to upgrade. I have not had a chance to verify but I believe this will be the case: http://tinyurl.com/cxayy6 U3 will continue to have this hole in the API to provide both read/write access to the VI API.

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at: http://engineering.ucsb.edu/~duonglt/vmware/

http://twitter.com/lamw

You can also do this connecting to the ESXi server using putty ( ssh ) just run the cp ( copy command )

Enable ssh on your esxi server ( unsupported )

http://www.vdi.co.nz/tag/esx4i/

I am using esx4i

putty in to your server

1. cd to your datastore.

2. Make a new directory for the clone VM: mkdir /vmfs/volumes/Data1/web2/

3. In your vsphereclient, make sure VM is powered off,

4. copy vm to new folder,

eg: cp /vmfs/volumes/Data1/web/* /vmfs/volumes/Data1/web2/

5. In client, browse datastore, to new vm , add to inventory

6. Start the new VM in the vshpere client, when asked confirm " I copied "

7. Start original VM to prove they are both working.

keep it simple.

Hi I am trying to automate cloning

I used this document

http://communities.vmware.com/docs/DOC-9321

I have VIMA setup also i was able to add the target servers as well . I did download the ./ghettoClone.sh script

./ghettoClone.sh -h 128.231.xxx.xxx -v fesx-1.xxx.xxx.xxx -n "esx_test_clone" -s 1 -e 10

but when i execute it created some 10 .vmx files in my datastore of 2.35 KB . I thought when it clones all files like .vmdx, .vmx etc etc should be copied over

I am using ESX 4.1 along with vcenter ( ESX server is licensed ) . VIMA :- 4.1 as well , i had to along convert the script to be used in unix so i used dos2unix command .

One more thing i had to change in the script is :- VI_PERL_TOOLKIT_PATH=/usr/lib/vmware-vcli/apps

Am i missing somthing ?

Hi,

      I was wondering if i can use Perl SDK to edit the network configuration( i mean IP address, netmask, Gateway etc) of the Virtual machine once the cloning is complete ?

     Any hint would be helpful.

Thanks,

Lokesh Gondane

Version history
Revision #:
1 of 1
Last update:
‎01-09-2009 03:57 PM
Updated by: