Last year I wrote an article called Automating Storage DRS & Datastore Cluster Management in vSphere 5 and I provided a pretty comprehensive vSphere SDK for Perl script to help administrators automate Storage DRS configurations. These past few months I have noticed an increase in interest on the VMTN developer forums relating to Storage DRS. Majority of the questions has been related to which vSphere API methods to use and how to use these methods for cloning VMs to datastore clusters.
If you have cloned a VM before, the underlying vSphere API method being used is the CloneVM_Task(), but when cloning a VM to a datastore cluster, a different API must be used.
In vSphere 5, VMware introduced several new API methods in the StorageResourceManager and the two specific ones relating to provisioning VMs are RecommendDatastores() and ApplyStorageDrsRecommendation_Task(). The process to clone a VM to a datastore cluster is a two step process:
- Call RecommendDatastores() which accepts very similar input as the CloneVM_Task(). In addition, you need to specify the datastore cluster also known as a Storage Pod in the vSphere API as well as the "type" (create, clone, relocate, reconfigure), in this example we will be performing a clone operation. This method will then generate a recommendation on where to place the VM which is based on the SDRS placement engine. No provisioning will occur at this point, just a placement recommendation.
- To perform the actual provision of the VM, you will need to call ApplyStorageDrsRecommendation_Task() which accepts a recommendation ID that was generated from the first step. Once the recommendation is applied, the provisioning of the VM will start just like it did when you called the CloneVM_Task().
Note: The RecommendDatastores() will return multiple recommendations, the best one will be first entry in the array. This is the same algorithm used when performing this same operation in the vSphere Client, it also selects the first recommendation.
Now that we understand how the APIs work, let's take a look at how we can leverage this in a script for some automation! Here is a simple vSphere SDK for Perl script called datastoreClusterVMProvisioning.pl which allows you to clone an existing VM onto a datastore cluster. You will need a system that has vCLI 5.0 installed or you can use VMware vMA 5 to run the script. You will also need to connect to a vCenter Server 5 for all SDRS operations.
The script requires 4 input parameters:
- vmname - Name of the VM you wish to clone from
- clonename - Name of the cloned VM
- vmfolder - Name of a vCenter folder
- datastorecluster - Name of a datastore cluster
Here is a screenshot of cloning an existing VM onto a datastore cluster:
The script is pretty straight forward and it can easily be adapted to include other configurations as required in your own environment.
Hopefully this gives you a better idea on how to leverage the new provisioning APIs for Storage DRS and start automating your VM deployments onto datastore clusters and get the benefits Storage DRS in your vSphere environment.
MC says
Hm, why can't you just call the RecommendDatastores() and then stuff that result into the usual clone task as the datastore?
William says
@MC Not sure I understand what you mean by usual clone task? As the article mentions, provisioning onto a Datastore Cluster requires a different set of methods, not your usual CloneVM_Task(). It must be done as described above.
MC says
Sorry, that was more a comment on the API itself. It's unfortunate it requires completely different calls just because of the storage target... Thx for the working example of how to deal with it!
Anonymous says
William, did you have any luck with RecommendDatastores & reconfigure (add disk to be specific)? It keeps crapping out with InvalidRequest.
-SP
Anonymous says
I'm also very interested to know how to add a disk to a vm upon deployment. I am able to get it to reconfigure the CPU and the ram, even add an additional scsi controller. I try to add the disk and it just seems to ignore my request to add the drive.
-CH
Puru says
Hi William,
Even i would like to add disk , with RecommendDatastores & reconfigure . Its failing with below error :
2015-05-20T07:19:13.846Z [7F13211F3700 info 'commonvpxLro' opID=4c4f70e9] [VpxLRO] -- BEGIN task-internal-171354 -- -- vim.StorageResourceManager.recommendDatastores -- 3103693e-f972-e4e8-8bb5-4982e2edabf6(52500cdd-14b4-e652-57c3-46119bfeeb97)
2015-05-20T07:19:13.858Z [7F13211F3700 info 'drmLogger' opID=4c4f70e9] [VpxVmdkStatTable::GetHistoricalStats] historical stats for scsi0:0 not found in the map
2015-05-20T07:19:13.868Z [7F13211F3700 info 'drmLogger' opID=4c4f70e9] [VpxVmdkStatTable::GetHistoricalStats] historical stats for scsi0:1 not found in the map
2015-05-20T07:19:13.880Z [7F13211F3700 info 'drmLogger' opID=4c4f70e9] [CreateDrmDisksForVmSet] Processed 22 vms
2015-05-20T07:19:13.881Z [7F13211F3700 error 'drmLogger' opID=4c4f70e9] [FindDiskByKey] Device -100 is not a disk
2015-05-20T07:19:13.889Z [7F13211F3700 info 'commonvpxLro' opID=4c4f70e9] [VpxLRO] -- FINISH task-internal-171354 -- -- vim.StorageResourceManager.recommendDatastores --
2015-05-20T07:19:13.889Z [7F13211F3700 info 'Default' opID=4c4f70e9] [VpxLRO] -- ERROR task-internal-171354 -- -- vim.StorageResourceManager.recommendDatastores: vmodl.fault.InvalidArgument:
--> Result:
--> (vmodl.fault.InvalidArgument) {
--> dynamicType = ,
--> faultCause = (vmodl.MethodFault) null,
--> invalidProperty = "StoragePlacementSpec.diskPodLocation.diskId",
--> msg = "",
--> }
--> Args:
I am adding the new disk , not sure why its looking for the valid diskid.
If i try with diskId as 2000, 2001 series ..
2015-05-20T08:38:45.363Z [7F1320D68700 info 'commonvpxLro' opID=4b4030fd] [VpxLRO] -- BEGIN task-internal-171982 -- -- vim.StorageResourceManager.recommendDatastores -- 3103693e-f972-e4e8-8bb5-4982e2edabf6(52500cdd-14b4-e652-57c3-46119bfeeb97)
2015-05-20T08:38:45.363Z [7F1320D68700 error 'vpxdvpxdMoStorageResourceManager' opID=4b4030fd] [SanityCheckPodSelectionSpec] File backing not found for disk 2000 on pod vim.StoragePod:group-p544
2015-05-20T08:38:45.363Z [7F1320D68700 error 'vpxdvpxdMoStorageResourceManager' opID=4b4030fd] [SanityCheckStoragePlacementSpec] PodSelectionSpec does not match ConfigSpec
2015-05-20T08:38:45.371Z [7F1320D68700 info 'commonvpxLro' opID=4b4030fd] [VpxLRO] -- FINISH task-internal-171982 -- -- vim.StorageResourceManager.recommendDatastores --
2015-05-20T08:38:45.371Z [7F1320D68700 info 'Default' opID=4b4030fd] [VpxLRO] -- ERROR task-internal-171982 -- -- vim.StorageResourceManager.recommendDatastores: vmodl.fault.InvalidArgument:
--> Result:
--> (vmodl.fault.InvalidArgument) {
--> dynamicType = ,
--> faultCause = (vmodl.MethodFault) null,
--> invalidProperty = "StoragePlacementSpec.podSelectionSpec and configSpec",
Can you share your thoughts on this. I couldn't find KB article related to this ..
Ashwini says
I have tried to clone a vm from a template, I also sent the disk id as 2000 it did not work. Later I removed the disks from PodDiskLocator in initial vm config. It worked for me. In order to do a reconfigure I guess U need to give the key as -1 not 2000. Please try with -1.
Yang Tony says
Hi, William, I try the script,but when I execute the code, some error happened.
The errors say:Something goes wrong with Install VM! $VAR1 = 'Installation was not successful: eval error: Error occurred when connecting to Vcenter: No VirtualMachine found for ApplyStorageRecommendationResult=HASH(0x5ebec18)
could u give me some hints?
Anonymous says
Hi
When I am giving vmname as a template name, I get Error
Storage DRS placement: vm current host not connected to pod
Any help is appreciated.
Thanks,
Ajay
William says
Can you try specifying a regular VM versus a template? It might need to be modified if you wish to deploy from a template
Anonymous says
Thanks for your reply.
It's working with a regular VM but not with a template. Any pointers that I can explore further?
Thanks,
Ajay
William says
If you perform the operation deploying from a VM Template to Datastore Cluster and watch the Onyx output, you'll see that you just need to specify the VM Template as the source ... nothing you need to change. Would recommend taking a look at Onyx to help you with the code
Anonymous says
Greetings, and thanks for this script, it helped me a lot.
To use it to deploy a VM from a template, you need to specify a resource pool to the VirtualMachineRelocateSpec, as it is a required parameter in that case.
I'm using the ESX cluster's root pool for that (grabbing $clusterName as a parameter) :
my $clusterView = Vim::find_entity_view(view_type => 'ClusterComputeResource', filter => {name => $clusterName});
my $location = VirtualMachineRelocateSpec->new(pool => $clusterView->resourcePool);
Regards,
Daniel
Vassil says
William and Daniel, thank you for this job.
Its helped me a lot.
im using configured Resource pools (self-contained and isolated from other resource pools).
Im getting resource pool in this way. (grabbing resource pool name as parameter):
my $resourcepoolView = Vim::find_entity_view(view_type => 'ResourcePool', filter => {name => $resourcepool});
my $location = VirtualMachineRelocateSpec->new(pool=>$resourcepoolView);
Anonymous says
I realize you're focused on the perl SDK here.
Is there any chance you are familiar with a Power CLI cmdlet to retrieve a reference to StorageResourceManager?
Anonymous says
Hi,
I am trying to create new baremetal VM on datastore cluster using JAVA SDK.
That needs filepath & datastore information to be specified in different backing infos
e.g.
VirtualMachineFileInfo,
VirtualCdromIsoBackingInfo,
VirtualDiskFlatVer2BackingInfo etc.
Since I just know the name of datastorecluster i want to deploy VM on, and have no idea which datastore in that cluster will be finally selected to deploy that VM, i am not sure what values to specify there.
API does not accept datastore cluster's (StoragePod's) details at that place. If i leave those parameters unset, vmware API throws exceptions as its mandatory.
Does anyone has any idea about this?
Any sort of help will be appreciated,
Thanks,
mike says
I am having the same problem with the perl SDK and cannot figure out how to make the create task work without having those same properties. Did you figure it out in the Java SDK?
Gena says
Hello William!
We are trying to utilize your script, to clone to the datastorecluster.
However we have an error we cant figure out what causes it:
Cloning "sdktemp1" to "del1000" onto "cl_ds1"
Error:
SOAP Fault:
-----------
Fault string: The operation is not supported on the object.
Fault detail: NotSupported
This is on vSphere 5.5 and SDK 5.5 as well.
One theory the System guy who deals with the VMware infrastructure has, is that it is related to not having the FullyAutomated setting, anywhere in the configuration specifications for the clone.
I have been looking hard into the objects related to the calls in your script, and their API reference, and I cant find any way this setting could be set in the relevant object (it doesnt exist in those objects).
Could you please advice on this?
Anand Srinivasan says
Hi William,
I understand cloning virtual machine on datastore cluster is possible using recommendDatastores & storagePlacementSpec. But my doubt is whether this is applicable for bare metal virtual machine also ? Basically cloneVMTask() can be replaced by recommendDatastores & applyStorageDrsRecommendationTask. Is it possible to do the same with createVMTask() also?
Since is looking for basic mandatory parameter of vm* , i am not sure with createVMTask where we don't have template or vm name in place.
Requesting your help on this.
Learner says
Hi Wiliam,
Can we use RecommendDatastores() to get a list of recommended datastore names directly, using the call I get an object of StoragePlacementResult through which I can get a reference to StoragePod but however unable to fetch the exact datastore recommended. I would not like to use RecommendDatastores() in combination with ApplyStorageDrsRecommendation_Task(). I would just like to fetch the list of datastores recommended by SDRS.
Please let me know if we can achieve this. Thanks.
Vasumathy Jambunathan says
Hi William,
We want to use the API to create a linked clone using datastore cluster.
We are able to create a linked clone using datastore, but unable to do it using datastore cluster.
Can u pls provide some pointers for the same?
raghav says
Hi All
I was trying to add new disk to existing vm which exists on 'Datastore cluster', it is failing with below error
can anyone help?
javax.xml.ws.soap.SOAPFaultException: A specified parameter was not correct.
StoragePlacementSpec.podSelectionSpec.storagePod
Marat Khandros says
I realize this is years after the OP, but I am trying to do the same action on a NEW vm (not a clone) - this is the only reference that comes close. I am now stuck on a very vague error about the filebacking being invalid (it works when passed to createVM). Is there a chance of getting an end to end example? Any pointers in this space would be greatly appreciated.
Martin says
Same issue here. I would really appreciate help.