[script] Cloning VMs using ESXi shell/admin console


Recommended Posts

I've been searching for a script to help with cloning VMs, but I couldn't find much out there that didn't rely on extra tools or applications.  I just wanted something simple to run from the ESXi shell, rather than mess about with PowerCLI / Powershell.  I ended up rolling my own.  It's probably not great, but has worked for me and takes away the tedium of working directly with vmkfstools

 

I attach a copy here in case its of use to anyone else.  Alternatively, if someone has a better script, please let me know.

 

The script clones a virtual machines settings and disks, renaming and updating the config as it goes.  The idea being you should end up with an exact copy of the source VM, but with the config (VMX) file all disk (VMDK) volumes renamed to reflect the new VM.  You can optionally add the newly created VM into the ESXi host inventory.

 

Usual caveats apply.  Don't get running it on a production platform without suitable testing.  That being said, the script does a few sanity checks so should be fairly safe to run;

  • It attempts to check if the source VM is in the ESXi inventory and if found it'll check whether it is powered on.  If it is, it won't go any further and will request you power off the VM
    • However, it isn't necessary for the VM you wish to clone to be in the inventory.  This covers the scenario where you may already have a VM created but not in the inventory.  If it only allowed cloning from what is known to the ESXi inventory, then you'd not be able to perform a clone. The script only checks the inventory for the purpose of working out if the VM you intend to clone is powered on.

    [*]The script will terminate if the source VM path is not valid

    [*]The script will terminate if the target path already exists

Run the script without arguments or with "-h" for help.  It's pretty self explanatory.  You need to include the whole path to the source and target VMs.  The reason for this is that you may, or may not, intend to clone across datastores.  By specifying the full path including datastore, the script isn't confined to performing the clone on a single datastore.

 

Oh, it's hard-coded to perform a "thin" provision.

 

Syntax: CloneVM.sh [full.path.to.source.vm] [full.path.to.target.vm] [-r]

 

eg.

CloneVM.sh /vmfs/volumes/store1/srcvm /vmfs/volumes/store2/tgtvm -r

where you can optionally include the third -r parameter.

 

I believe the script is compatible with ESXi 4.1 and 5.x releases.

 

 

 

Regards,

overbyrn

CloneVM.zip

Link to comment
  • 11 months later...
  • 6 years later...

Hey guys, 

 

 thank you for the script.. it copy the files very nice but end in an error.. did you face the same issue? 

 

root@nas:/boot/config/plugins/user.scripts/scripts/clone_vm# bash clonevm.sh /mnt/user/domains/vm-win10-002/ /mnt/user/domains/vm-win10-001 -r
clonevm.sh: line 33: vim-cmd: command not found

CloneVM.sh for VMWare ESXi 4.1 & ESXi 5.x
-----------------------------------------

OLD VM DATASTORE : /mnt/user/domains/vm-win10-002
OLD VM           : 

NEW VM DATASTORE : /mnt/user/domains
NEW VM           : vm-win10-001

-r specified, cloned VM will be added to ESXi host inventory

Cloning VMDK : 
sed: -e expression #1, char 0: no previous regular expression
clonevm.sh: line 87: vmkfstools: command not found

Copying Supporting Files:
sed: -e expression #1, char 0: no previous regular expression
Copying /mnt/user/domains/vm-win10-002//vdisk1.img to /mnt/user/domains/vm-win10-001/

Updating /mnt/user/domains/vm-win10-001/vm-win10-001.vmx

sed: can't read /mnt/user/domains/vm-win10-001/vm-win10-001.vmx: No such file or directory
Registering vm-win10-001 into ESXi Host Inventory

clonevm.sh: line 115: vim-cmd: command not found
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.