ghettoCloneVM.pl (no vCenter required, ESX or ESXi licensed version)

    Table of Contents

    • Author

    • Description

    • Category

    • Features

    • Requirements

    • Version Support

    • Sample Execution

     

    Author

    William Lam

     

    Description

     

    The cloning functionality is a very useful feature to have when deploying multiple VMs of the same OS, though unfortunately, this feature is only available with VMware vCenter. You can manually clone each VM by copying over the VM's .vmx configuration file and manually calling vmkfstools to clone the VMDK, but it's a repetitive and un-fun process.

     

    Wouldn't it be nice to be able to clone on an individual ESX(i) host? This script does exactly that, it requires either ESX or ESXi (licensed version) and allows a user to setup an initial VM that is configured with everything he/she would like the subsequent clones to have and using a small supplied configuration file to deploy n-number of VM clones. The configuration file allows the user to specify the displayName of each VM, the disk format and the adapter type and where the clones will be stored on either the same datastore as the base image or on another accessible datastore by the ESX(i) host.

     

    Note: Guest customization is not available, user's must still go into the guest and configure the peroper hostname/IP,etc.

     

    Category

    • Initial Server Set Up / Provisioning

     

    Features

    • Supports running on both Windows and Linux

    • Allow configuration of clone's displayName, disk format, disk adapter and datastore location

    • Clones VMs will have annotation noting which VM it was cloned from

    • Logging output to file

     

    Requirements

    • ESX(i) 4.0

    • vSphere SDK for Perl/vCLI 4.0 Windows or Linux OR vMA 4.0

     

    Version Support

    • Supports both ESX and ESXi (licensed version only)

     

    Sample Execution

     

    Let's say the scenario is the following, you've configured a single VM running DOS (any OS works) and you would like to create 3 clones of this VM, each VM will have a slightly different configuration but the OS itself will be the same.

     

    Here is sample input file:

    [vi-admin@scofield demo]$ cat vmclone_list
    #VM_CLONE_NAME - Please don't use spaces in the VM names
    
    #DISK_FORMAT
    # zeroedthick (default)
    # eagerzeroedthick
    # thin
    
    #ADAPTER_FORMAT
    # lsilogic
    # buslogic (default)
    # ide
    
    #DATASTORE
    # Name of datastore to store new cloned VMs
    # default (uses baseVM datastore)
    
    # VM_CLONE_NAME         DISK_FORMAT             ADAPTER_FORMAT          DATASTORE
    vm-clone-1              thin                    buslogic                default
    vm-clone-2              zeroedthick             buslogic                default
    vm-clone-3              eagerzeroedthick        lsilogic                dlgCore-NFS-bigboi.VM-Backups
    

     

    1. You'll need to create an input file such as the one above using the template that's been included with the script. You'll need to modify all 4 available params VM_CLONE_NAME, DATASTORE and most importantly DISK_FORMAT and ADAPTER_FORMAT, the defaults are zeroedthick and buslogic, so ensure you lookup the base VM and check on how it was setup.

     

    2. Your base VM image should be in a powered off state prior to executing the clone process, online cloning is not supported.

     

     

    3. Execute the script

     

    Script execution using default loglevel (info)

    [vi-admin@scofield demo]$ ./ghettoCloneVM.pl --server esxi4-2.primp-industries.com --username root --basevm dos-base --vmclonelist vmclone_list
    Enter password:
            12-23-2009 17:46:26 --  info: Start cloning of vm-clone-1 ...
            12-23-2009 17:46:26 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base.vmdk" to "[esxi4-2-local-storage] vm-clone-1/vm-clone-1_0.vmdk"
            12-23-2009 17:46:31 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk" to "[esxi4-2-local-storage] vm-clone-1/vm-clone-1_1.vmdk"
            12-23-2009 17:46:36 --  info: Registering "[esxi4-2-local-storage] vm-clone-1/vm-clone-1.vmx" as "vm-clone-1"
            12-23-2009 17:46:42 --  info: Completed cloning for vm-clone-1 ...
    
            12-23-2009 17:46:42 --  info: Start cloning of vm-clone-2 ...
            12-23-2009 17:46:43 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base.vmdk" to "[esxi4-2-local-storage] vm-clone-2/vm-clone-2_0.vmdk"
            12-23-2009 17:46:48 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk" to "[esxi4-2-local-storage] vm-clone-2/vm-clone-2_1.vmdk"
            12-23-2009 17:46:53 --  info: Registering "[esxi4-2-local-storage] vm-clone-2/vm-clone-2.vmx" as "vm-clone-2"
            12-23-2009 17:46:59 --  info: Completed cloning for vm-clone-2 ...
    
            12-23-2009 17:46:59 --  info: Start cloning of vm-clone-3 ...
            12-23-2009 17:46:59 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base.vmdk" to "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3_0.vmdk"
            12-23-2009 17:47:39 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk" to "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3_1.vmdk"
            12-23-2009 17:48:19 --  info: Registering "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3.vmx" as "vm-clone-3"
            12-23-2009 17:48:26 --  info: Completed cloning for vm-clone-3 ...
    

     

     

    Script execution using default loglevel (debug)

    [vi-admin@scofield demo]$ ./ghettoCloneVM.pl --server esxi4-2.primp-industries.com --username root --basevm dos-base --vmclonelist vmclone_list --loglevel debug
    Enter password:
            12-23-2009 18:00:23 --  info: Start cloning of vm-clone-1 ...
            12-23-2009 18:00:23 --  debug: Create remote directory "[esxi4-2-local-storage] vm-clone-1" successfully
            12-23-2009 18:00:23 --  debug:  SOURCE      DISK: "[esxi4-2-local-storage] dos-base/dos-base.vmdk"
            12-23-2009 18:00:23 --  debug:  DESTINATION DISK: "[esxi4-2-local-storage] vm-clone-1/vm-clone-1_0.vmdk"
            12-23-2009 18:00:23 --  debug:  DISK        TYPE: "thin"
            12-23-2009 18:00:23 --  debug:  ADAPTER     TYPE: "buslogic"
            12-23-2009 18:00:23 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base.vmdk" to "[esxi4-2-local-storage] vm-clone-1/vm-clone-1_0.vmdk"
            12-23-2009 18:00:28 --  debug: Successfully cloned disk
            12-23-2009 18:00:28 --  debug:  SOURCE      DISK: "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk"
            12-23-2009 18:00:28 --  debug:  DESTINATION DISK: "[esxi4-2-local-storage] vm-clone-1/vm-clone-1_1.vmdk"
            12-23-2009 18:00:28 --  debug:  DISK        TYPE: "thin"
            12-23-2009 18:00:28 --  debug:  ADAPTER     TYPE: "buslogic"
            12-23-2009 18:00:28 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk" to "[esxi4-2-local-storage] vm-clone-1/vm-clone-1_1.vmdk"
            12-23-2009 18:00:34 --  debug: Successfully cloned disk
            12-23-2009 18:00:34 --  debug: Downloading VMX configuration "[esxi4-2-local-storage] dos-base/dos-base.vmx" to "/tmp/ghettoCloneVM/vm-clone-1.vmx"
            12-23-2009 18:00:34 --  debug: Downloaded file to /tmp/ghettoCloneVM/vm-clone-1.vmx successfully
            12-23-2009 18:00:34 --  debug: Modifying VMX configuration "/tmp/ghettoCloneVM/vm-clone-1.vmx"
            12-23-2009 18:00:34 --  debug: Uploading VMX configuration "/tmp/ghettoCloneVM/vm-clone-1.vmx" to "[esxi4-2-local-storage] vm-clone-1/vm-clone-1.vmx"
            12-23-2009 18:00:34 --  debug: Uploaded file /tmp/ghettoCloneVM/vm-clone-1.vmx to vm-clone-1/vm-clone-1.vmx successfully
            12-23-2009 18:00:34 --  info: Registering "[esxi4-2-local-storage] vm-clone-1/vm-clone-1.vmx" as "vm-clone-1"
            12-23-2009 18:00:35 --  debug: Successfully registered VM
            12-23-2009 18:00:35 --  debug: Updating annotation field for VM
            12-23-2009 18:00:40 --  debug: Sucessfully updated annotation for "vm-clone-1"!
            12-23-2009 18:00:40 --  info: Completed cloning for vm-clone-1 ...
    
            12-23-2009 18:00:40 --  info: Start cloning of vm-clone-2 ...
            12-23-2009 18:00:40 --  debug: Create remote directory "[esxi4-2-local-storage] vm-clone-2" successfully
            12-23-2009 18:00:40 --  debug:  SOURCE      DISK: "[esxi4-2-local-storage] dos-base/dos-base.vmdk"
            12-23-2009 18:00:40 --  debug:  DESTINATION DISK: "[esxi4-2-local-storage] vm-clone-2/vm-clone-2_0.vmdk"
            12-23-2009 18:00:40 --  debug:  DISK        TYPE: "zeroedthick"
            12-23-2009 18:00:40 --  debug:  ADAPTER     TYPE: "buslogic"
            12-23-2009 18:00:40 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base.vmdk" to "[esxi4-2-local-storage] vm-clone-2/vm-clone-2_0.vmdk"
            12-23-2009 18:00:45 --  debug: Successfully cloned disk
            12-23-2009 18:00:45 --  debug:  SOURCE      DISK: "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk"
            12-23-2009 18:00:45 --  debug:  DESTINATION DISK: "[esxi4-2-local-storage] vm-clone-2/vm-clone-2_1.vmdk"
            12-23-2009 18:00:45 --  debug:  DISK        TYPE: "zeroedthick"
            12-23-2009 18:00:45 --  debug:  ADAPTER     TYPE: "buslogic"
            12-23-2009 18:00:45 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk" to "[esxi4-2-local-storage] vm-clone-2/vm-clone-2_1.vmdk"
            12-23-2009 18:00:50 --  debug: Successfully cloned disk
            12-23-2009 18:00:50 --  debug: Downloading VMX configuration "[esxi4-2-local-storage] dos-base/dos-base.vmx" to "/tmp/ghettoCloneVM/vm-clone-2.vmx"
            12-23-2009 18:00:50 --  debug: Downloaded file to /tmp/ghettoCloneVM/vm-clone-2.vmx successfully
            12-23-2009 18:00:50 --  debug: Modifying VMX configuration "/tmp/ghettoCloneVM/vm-clone-2.vmx"
            12-23-2009 18:00:50 --  debug: Uploading VMX configuration "/tmp/ghettoCloneVM/vm-clone-2.vmx" to "[esxi4-2-local-storage] vm-clone-2/vm-clone-2.vmx"
            12-23-2009 18:00:50 --  debug: Uploaded file /tmp/ghettoCloneVM/vm-clone-2.vmx to vm-clone-2/vm-clone-2.vmx successfully
            12-23-2009 18:00:50 --  info: Registering "[esxi4-2-local-storage] vm-clone-2/vm-clone-2.vmx" as "vm-clone-2"
            12-23-2009 18:00:51 --  debug: Successfully registered VM
            12-23-2009 18:00:51 --  debug: Updating annotation field for VM
            12-23-2009 18:00:56 --  debug: Sucessfully updated annotation for "vm-clone-2"!
            12-23-2009 18:00:56 --  info: Completed cloning for vm-clone-2 ...
    
            12-23-2009 18:00:56 --  info: Start cloning of vm-clone-3 ...
            12-23-2009 18:00:56 --  debug: Create remote directory "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3" successfully
            12-23-2009 18:00:56 --  debug:  SOURCE      DISK: "[esxi4-2-local-storage] dos-base/dos-base.vmdk"
            12-23-2009 18:00:56 --  debug:  DESTINATION DISK: "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3_0.vmdk"
            12-23-2009 18:00:56 --  debug:  DISK        TYPE: "eagerzeroedthick"
            12-23-2009 18:00:56 --  debug:  ADAPTER     TYPE: "lsilogic"
            12-23-2009 18:00:56 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base.vmdk" to "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3_0.vmdk"
            12-23-2009 18:01:37 --  debug: Successfully cloned disk
            12-23-2009 18:01:37 --  debug:  SOURCE      DISK: "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk"
            12-23-2009 18:01:37 --  debug:  DESTINATION DISK: "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3_1.vmdk"
            12-23-2009 18:01:37 --  debug:  DISK        TYPE: "eagerzeroedthick"
            12-23-2009 18:01:37 --  debug:  ADAPTER     TYPE: "lsilogic"
            12-23-2009 18:01:37 --  info: Cloning disk "[esxi4-2-local-storage] dos-base/dos-base_1.vmdk" to "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3_1.vmdk"
            12-23-2009 18:02:17 --  debug: Successfully cloned disk
            12-23-2009 18:02:17 --  debug: Downloading VMX configuration "[esxi4-2-local-storage] dos-base/dos-base.vmx" to "/tmp/ghettoCloneVM/vm-clone-3.vmx"
            12-23-2009 18:02:17 --  debug: Downloaded file to /tmp/ghettoCloneVM/vm-clone-3.vmx successfully
            12-23-2009 18:02:17 --  debug: Modifying VMX configuration "/tmp/ghettoCloneVM/vm-clone-3.vmx"
            12-23-2009 18:02:17 --  debug: Uploading VMX configuration "/tmp/ghettoCloneVM/vm-clone-3.vmx" to "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3.vmx"
            12-23-2009 18:02:17 --  debug: Uploaded file /tmp/ghettoCloneVM/vm-clone-3.vmx to vm-clone-3/vm-clone-3.vmx successfully
            12-23-2009 18:02:17 --  info: Registering "[http://dlgCore-NFS-bigboi.VM-Backups|http://dlgCore-NFS-bigboi.VM-Backups] vm-clone-3/vm-clone-3.vmx" as "vm-clone-3"
            12-23-2009 18:02:19 --  debug: Successfully registered VM
            12-23-2009 18:02:19 --  debug: Updating annotation field for VM
            12-23-2009 18:02:24 --  debug: Sucessfully updated annotation for "vm-clone-3"!
            12-23-2009 18:02:24 --  info: Completed cloning for vm-clone-3 ...
    
    

     

    Script execution on Windows system using vCLI for Windows and logging output

    C:\Users\stylez\Desktop>ghettoCloneVM.pl --server esxi4-2.primp-industries.com --username root --basevm dos-base --vmclonelist vmclone_list --logreport C:\ghettoClone.log
    Enter password: