VMware {code} Community
pomattic
Contributor
Contributor
Jump to solution

cloneVM_Task() over datastores and folders

Cloning always seems to cause problems for me.

Some questions I would like to pose to the community. I will continue to work on them in an attempt to solve them , but any help would be appreciated since I lack experience in this area.

Scenario:

(ESX 3.5 VI-Java SDK)

(Let's say) I have two datastores. The datastores, when viewed with the Datastore Browser have folders within them. These folders have VMs in them. (example. \[DS1\] folder/ folderWithVms/ vm.vmx)

Now what I want to be able to do is when cloning a VM to be able to specify what folder, on which Datastore it is going to be on.

I seem not to be able to do this.

And so I noticed the VirtualMachineRelocateSpec. Looking at it's params though it does not seem to go any lower then the datastore in terms of location.

The cloneVM_Task itself takes a Folder. Currently I am passing in the vmFolder. Is this where I should be passing in a more specific folder to the location where the VM is to go. If so how do I get a MOR to the folder from a datastore path?

Of course I may be way off in what I am trying to do.

Here is a snippet of code, that is used for cloning by me, currently the destination is the same as the source.

//baseRef previously set mor to machine to clone

//vmFolder previously set mor to vmFolder of Datacenter

//clone String name of clone

VirtualMachineCloneSpec vmcs = new VirtualMachineCloneSpec();

VirtualMachineRelocateSpec vmrs = new VirtualMachineRelocateSpec();

vmcs.setLocation(vmrs);

vmcs.setPowerOn(false);

vmcs.setTemplate(false);

ManagedObjectReference ref = connexion.cloneVM_Task(baseRef, vmFolder, clone, vmcs);

Any help would be appreciated.

M@

Message was edited by: pomattic (changed to more descriptive thread name)

Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

The functionality you are looking for is currently not available through VI SDK.

The location parameter is restricted to the manged object reference of a folder. These folders are created at the top level of the hierarchy and maybe nested or they may be created under a datacenter.

The folders you are referring to are under a datastore to which you cannot get a managed object reference through vi sdk. You will note the same functionality using VI Client, if you try cloning a virtual machine. The location to choose from the inventory will comprise of choosing a datacenter, any folder under a datacenter, a datastore when selecting storage, selecting a resource pool.

Thus when you are using these methods, to the move the files from "[ds1] to "[ds2] , where ds1 and ds2 are datastores, as you have already noted.

Using the "CloneVM_Task" API, you would be creating the clone of the machine "vm1", but the files for this new VM would be created in the folder with the name provided for the new vm that would be ready after the cloning.

View solution in original post

Reply
0 Kudos
7 Replies
admin
Immortal
Immortal
Jump to solution

You can refer the Folder Managed Object fromt the reference guide:

Basically, there is a root folder and it can have descendant folders.A data center's host folder and its descendant foldersA data center's vm folder and its descendant folders

Now, to get a managed object referernce to these folders, you can specify the inventory path to these and use FindByInventoryPath api to retrieve the mor to Folder Managed Entity.

The correct order of specifying the inventory path is:

ManagedObjectReference folderRef =

cb.getConnection().getService().findByInventoryPath(content.getSearchIndex(),

pathToFolder);

"myfolder" is a folder - I am assuming myfolder is a folder underneath the root folder.

"myDC" is a data center

"vm" is a folder holding the vm

Inventory Path for pathToFolder could be: myfolder/myDC/vm

Reply
0 Kudos
pomattic
Contributor
Contributor
Jump to solution

Thanks for the response.

I am still extremely lost and confused. : (

My inventory (as in the Inventory->Datatstores view in the Virtual Infrastructure client) looks like such:

| Datastores

|-> folderName

|--> datastore1

|--> datastore2

Now when I click on a datastore (all have same structure) I see a hierarchy like this:

| /

|->myVirtualMachineFolder

|--> baseMachines

|---> machineBase

|--> liveMachines

|---> machineLive1

|---> machineLive2

When I look at the childEntities of rootFolder I only have "folderName" as a child.

What I want to be able to do is clone say "\[datastore1\] myVirtualMachineFolder/baseMachines/machineBase" to "\[datastore2\] myVirtualMachineFolder/liveMachines/machineLive3"

So this is what I have:

VirtualMachineCloneSpec vmcs = new VirtualMachineCloneSpec();

VirtualMachineRelocateSpec vmrs = new VirtualMachineRelocateSpec();

vmrs.setDatastore(dataStore); //mor to datastore

vmcs.setLocation(vmrs);

vmcs.setPowerOn(false);

vmcs.setTemplate(false);

ManagedObjectReference ref = connexion.cloneVM_Task(baseRef, vmFolder, clone, vmcs);

// vmFolder is a ref to the rootFolder

// clone is a String for clone name

// baseRef is mor to base machine

Do I want the Folder to be something else? I am confused as to what this folder is used for. (Ref says "The location of the new virtual machine." but how deep is that?)

My use of the VirtualMachineRelocateSpec is wrong. I am not sure which values are needed...

I have a sketchy method of getting the HostSystem, but cannot seem to find any way to get a ResourcePool (well I can get this info for a VM that already exists (what Pool it belongs to), but I do not want to relly on that)

What I really need to get working is to clone from a datastore to another datastore, the folder thing can be left on the back burner till that works.

Any further help in these areas would be greatly appreciated.

M@

Message was edited by: pomattic (added: "(well I can get this info for a VM that already exists (what Pool it belongs to), but I do not want to relly on that)")

Reply
0 Kudos
pomattic
Contributor
Contributor
Jump to solution

Alright so one step further.

I can clone from a datastore to another one now. Using this:

VirtualMachineCloneSpec vmcs = new VirtualMachineCloneSpec();

VirtualMachineRelocateSpec vmrs = new VirtualMachineRelocateSpec();

vmrs.setDatastore(dataStore); // mor ref to datastore

vmrs.setHost(findHostByName(serverName)); // findInventoryPath is used

vmrs.setPool(getResourcePool(serverName));

vmcs.setLocation(vmrs);

vmcs.setPowerOn(false);

vmcs.setTemplate(false);

ManagedObjectReference ref = connexion.cloneVM_Task(baseRef, vmFolder, clone, vmcs);

getResourcePool(serverName) is a method written with help from the solution found at: http://communities.vmware.com/thread/100144

So now if I can figure out this folder thing we will be good! : )

M@

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

The functionality you are looking for is currently not available through VI SDK.

The location parameter is restricted to the manged object reference of a folder. These folders are created at the top level of the hierarchy and maybe nested or they may be created under a datacenter.

The folders you are referring to are under a datastore to which you cannot get a managed object reference through vi sdk. You will note the same functionality using VI Client, if you try cloning a virtual machine. The location to choose from the inventory will comprise of choosing a datacenter, any folder under a datacenter, a datastore when selecting storage, selecting a resource pool.

Thus when you are using these methods, to the move the files from "[ds1] to "[ds2] , where ds1 and ds2 are datastores, as you have already noted.

Using the "CloneVM_Task" API, you would be creating the clone of the machine "vm1", but the files for this new VM would be created in the folder with the name provided for the new vm that would be ready after the cloning.

Reply
0 Kudos
pomattic
Contributor
Contributor
Jump to solution

Well that puts closure on that matter.

I am thinking that maybe the move moveDatastoreFile_Task() maybe could be used after the cloning to do what I want... but that is very case specific and so won't burden you guys anymore with that.

Thanks a lot for the continued support!

M@

Reply
0 Kudos
sasama1997
Contributor
Contributor
Jump to solution

Hello.

I want occasionally to hear it.

Where can "moveDatastoreFile_Task()" be acquired?

For instance, "cloneVM_Task()" can be acquired from "S1.getService().cloneVM_Tas".

As for "sourceDatacenter" and "destinationDatacenter", can the same value be done?

Thanks for the support!

Reply
0 Kudos
pomattic
Contributor
Contributor
Jump to solution

If destination data centre is not specified source is assumed. Meaning that they can both be the same.

As for the moveDatastoreFile_Task(), you will find it much the same as cloneVM_Task()

(I use the same variable:VimPortType to call both methods)

See this for help:

http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/index.html

Reply
0 Kudos