VMware {code} Community
mohd1024
Contributor
Contributor

How to clone a template to virtual machine?

Hello All,

I want to clone a template to a virtual machine, I could create a virtual machine to a virtual machine but when cloning a template I get the following error :

A specified parameter was not correct. spec.location.pool

I think that the problem in that the template is not attached to a resource pool and the newly cloned virtual machine should have a resource pool.

How could I do that to look like the deploy instruction in the virtual infrastructure client so that the newly cloned virtual machine is automatically attached to a resource pool.

this is the segment of the code Iam using to clone the template :

string PathToVm = "/Jordan/vm/Temp";

string PathToFolder = "/Jordan/vm";

ManagedObjectReference vmMoRef = service.FindByInventoryPath(sic.searchIndex, PathToVm);

ManagedObjectReference folderMoRef = service.FindByInventoryPath(sic.searchIndex, PathToFolder);

VirtualMachineRelocateSpec vmRelocSpec = new VirtualMachineRelocateSpec();

vmRelocSpec.transform = VirtualMachineRelocateTransformation.sparse;

VirtualMachineCloneSpec vmCSpec = new VirtualMachineCloneSpec();

vmCSpec.location = vmRelocSpec;

vmCSpec.template = false;

vmCSpec.powerOn = false;

ManagedObjectReference taskMoRef = service.CloneVMTask(vmMoRef, folderMoRef, "CLoned", vmCSpec);

Reply
0 Kudos
11 Replies
admin
Immortal
Immortal

You can get the resource pool view as illustrated below from your host view and then use it as a parameter to be passed in the VirtualMachineRelocateSpec.

my $comp_res_view = Vim::get_view(mo_ref => $host_view->parent);

my $relocate_spec =

VirtualMachineRelocateSpec->new(datastore => $ds_info\{mor},

host => $host_view,

pool => $comp_res_view->resourcePool);

For a sample illustrating the cloning of machines, you can also refer the vmclone.pl, vi perl sample provided in the beta perl toolkit releases.

Reply
0 Kudos
SaranshG
Enthusiast
Enthusiast

Please check the total amount of resources the current resource pool has available to run virtual machines, because for a relocate or clone operation to a virtual machine, if the argument is not supplied, the current resource pool of virtual machine is used. And incase your resource usage already exceeds maximum allowed limit, then newly cloned virtual machine cannot attach to the current resource pool.

Reply
0 Kudos
mohd1024
Contributor
Contributor

Thanks dkaur for replying :

As you see Iam using C# for my application.

I couldn't find the API get_view() in the web service stub

I will be gratefull if you know the equevilant API in C#.

Thank you very much

Reply
0 Kudos
mohd1024
Contributor
Contributor

Thanks SaranshG for replying :

You right when I clone an ordinary Virtual machine (Not a template) it works successfully and the newly cloned machine is attached to the same resource pool to which the source machine is attached.

But templates are not attached to any resource pools, So when I clone template -> Virtual machine, the newly cloned machine is not attached to any resource pools which fires an exception.

Thank you very much

Reply
0 Kudos
hrobinson
VMware Employee
VMware Employee

In your vmRelocSpec, you need to specify either a host or a pool that you want to assign the VM to.

vmRelocSpec.host or

vmRelocSpec.pool

Even though these arguments are listed as Optional, for a clone of a template you need to specify at least one of them. Otherwise, you aren't telling VC where to put that VM.

Both arguments take a MoRef. In .NET, you also need to say something like

vmRelocSpec.host = moRef;

vmRelocSpec.hostSpecifed = true (or something like that).

H

Reply
0 Kudos
mohd1024
Contributor
Contributor

Thanks hrobinson :

I have tried that but I didn't find the following :

vmRelocSpec.hostSpecifed = true

but instead I found the follwoing property :

vmRelocSpec.transformSpecified = true

I tried that but it is still asking to specify a resource pool.

Please reply if you have other solutions.

Reply
0 Kudos
ullbergm
Enthusiast
Enthusiast

Were you able to find a solution to this? I'm having the same issue.

Check out my orchestration blog here: http://ullberg.us/orchestrate/
Reply
0 Kudos
mohd1024
Contributor
Contributor

I found that I have to specify the Host at which the new cloned VM will be deployed you have to do the following if it suits you : ManagedObjectReference

you first have to get a reference to the Resource Pool of the host in which the new VM is deployed by the following code :

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

string hostPath="[your data center]/host/[hostName]"; //The Inventory path of the host

hostReference=_service.FindByInventoryPath(_sic.searchIndex, hostPath);

PropertySpec pSpec = new PropertySpec();

pSpec.all = false;

pSpec.type = "ComputeResource";

ObjectSpec oSpec;

pSpec.pathSet = new String[] { "resourcePool" };

oSpec = new ObjectSpec();

oSpec.obj = hostReference;

PropertyFilterSpec pfSpec = new PropertyFilterSpec();

pfSpec.objectSet = new ObjectSpec[] ;

pfSpec.propSet = new PropertySpec[] ;

ObjectContent objs = service.RetrieveProperties(sic.propertyCollector, new PropertyFilterSpec[http://] objs = service.RetrieveProperties(sic.propertyCollector, new PropertyFilterSpec[] );

if (objs == null)

return;

DynamicProperty[] dProps = objs[0].propSet;

resourcePool = (ManagedObjectReference)dProps[0].val; // This is the resource pool to be assigned to the new VM

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

Then you use the resource pool and assign it to the new VM by this code :

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

string source="[your data center]/vm/[template name]"; // the inventory path of the Template to be cloned

string distination="[your data center]/vm"; // The inventory path of the destination folder to place the new VM in

ManagedObjectReference vmMoRef = service.FindByInventoryPath(sic.searchIndex, source);

ManagedObjectReference folderMoRef = service.FindByInventoryPath(sic.searchIndex, distination);

VirtualMachineRelocateSpec vmRelocSpec = new VirtualMachineRelocateSpec();

vmRelocSpec.transform = VirtualMachineRelocateTransformation.sparse;

VirtualMachineCloneSpec vmCSpec = new VirtualMachineCloneSpec();

vmRelocSpec.pool = resourcePool; // Assign the resource pool

vmCSpec.location = vmRelocSpec;

vmCSpec.template = false;

vmCSpec.powerOn = false;

// Clone the VM and obtain a reference to the new clone.

ManagedObjectReference taskMoRef = service.CloneVMTask(vmMoRef, folderMoRef, name, vmCSpec);

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

Best wishes

do not hesitate to contact me if you need any more help

Reply
0 Kudos
ullbergm
Enthusiast
Enthusiast

Thanks.

I should have posted earlier, everything is working great for me now. Thanks for pointing me in the right direction.

In the end i ended up having the user select a cluster but your scripts helped me understand how this stuff works Smiley Happy

1120_1120.JPG

Magnus

Check out my orchestration blog here: http://ullberg.us/orchestrate/
Reply
0 Kudos
meistermn
Expert
Expert

Where can we download this tool?

Can you help the people with your tool?

In this thread they are asking for it:

http://communities.vmware.com/message/799953

Reply
0 Kudos
ullbergm
Enthusiast
Enthusiast

Right now it is just an internal tool that i developed to make life easier for myself when tasked to deploy a bunch of machines.

It still has some issues to resolve and i would also have to check with the boss to see if i would be allowed to post the app out there since it was developed on company time.

I'll post in that thead also to let them know.

Check out my orchestration blog here: http://ullberg.us/orchestrate/
Reply
0 Kudos