janetdoyle
Contributor
Contributor

Looking for Sample Code to Clone VM

Jump to solution

Does anyone have any sample code for cloning a VM using the VISDK webservices? I programming in C++ but a Java/C# implementation will help also.

Thanks,

Jan

0 Kudos
1 Solution

Accepted Solutions
lamw
Community Manager
Community Manager

It can include both existing and newly added disks and other components.

That is correct, you need to make sure the unitNumber is unique, I believe if you pass in "-1" it'll automatically increment the counters for you

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

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

View solution in original post

0 Kudos
8 Replies
janetdoyle
Contributor
Contributor

... I'm having trouble with a FULL CLONE.

0 Kudos
lamw
Community Manager
Community Manager

Take a look at the vSphere SDK for Perl Utility script vmclone.pl which is part vCLI installation that includes a complete working clone script for a VM. You can go through the script and it'll show you which calls to the API is required to perform the clone operation and that should get you started in writing your own in C++, remember the clone operation requires vCenter.

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

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

0 Kudos
janetdoyle
Contributor
Contributor

Thanks -- that was very helpful.

Follow up question,

How do I add additional disks to the already existing disks? I can clone using the existing disk, no problem. But how do I add disk?

I'm guessing: VirtualMachineConfigSpec and then setDeviceChange()?

Thanks,

Jan

0 Kudos
lamw
Community Manager
Community Manager

As part of the CloneVM_Task, you're allowed to modify the VM configuration, this includes changing the name of the VM, adding/removing network adapters,disks,cdrom,etc.

Take a look at the API reference for the CloneVM_Task: http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html#c...

and yes, you'll need to create a new VirtualMachineCloneSpec which will specify a new VirtualMachineConfigSpec that contains the changes you would like to make, in your case, you're adding an additional disk so you'll create the necessary backing and add the appropriate number of disks

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

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

janetdoyle
Contributor
Contributor

VirtualMachineCloneSpec.setChangeDevice() will include the existing and new disks in vector or just the additional disks?

Also If I'm creating a new disks I have to make sure the unit number don't overlap with the existing disks, right?

0 Kudos
lamw
Community Manager
Community Manager

It can include both existing and newly added disks and other components.

That is correct, you need to make sure the unitNumber is unique, I believe if you pass in "-1" it'll automatically increment the counters for you

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

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

View solution in original post

0 Kudos
janetdoyle
Contributor
Contributor

Where can I find -1 documented? Because that would make my life real simple Smiley Happy

Thanks,

Jan

0 Kudos
lamw
Community Manager
Community Manager

I don't' recall if it's formally documented, even in the API docs, but this was through some testing and I also noticed some of the canned vSphere SDK for Perl scripts used -1 to denote a new entry.

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

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

Twitter: @lamw

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

If you find this information useful, please award points for "correct" or "helpful".

0 Kudos