Jump to content
Sign in to follow this  
overbyrn

[script] Cloning VMs using ESXi shell/admin console

2 posts in this topic Last Reply

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

Share this post


Link to post

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.

Sign in to follow this