One of the biggest benefit of VSAN is the ability to specify granular storage policies on a per Virtual Machine basis. These storage policies is managed through VMware's Storage Policy Based Management system and is automatically enforced by VSAN to ensure compliance. A VM Storage Policy can be assigned during the initial deployment of a Virtual Machine or it can be modified afterwards, for example if the Virtual Machine's SLA's has changed because the workload has changed. From the vSphere Web Client, modifying a Virtual Machine's VM Storage Policy is simply selecting the VM Storage Policy and re-applying which is also available programmatically through the vSphere API.

Using the vSphere API method ReconfigVM_Task(), you will be able to modify the VM Storage Policy for the VM Home Namespace and/or individual Virtual Disks. To modify the VM Home Namespace, there is a property defined at the root of the Virtual Machine config spec called vmProfile which accepts the VM Storage Policy ID extracted from the SPBM API. To modify the VM Storage Policy for an individual Virtual Disk, you will need to set the profile property which is exposed on a Virtual Device with the VM Storage Policy ID. To demonstrate this functionality, I have created a sample vSphere SDK for Perl script called changeVMStoragePolicy.pl

Disclaimer:  These scripts are provided for informational and educational purposes only. It should be thoroughly tested before attempting to use in a production environment.

In my environment, I have a Virtual Machine called VM1 which has been defined with a VM Storage Policy called "Copper" as seen in screenshot below:

change-vm-storage-policy-0
Let's say I want to change the Virtual Machine's VM Storage Policy to another policy called "Aluminum", I first need to extract the VM Policy ID from SPBM API and then pass it into the script like the following:

./changeVMStoragePolicy.pl --server vcenter55-1.primp-industries.com --username root --vmname VM1 --profileid cd6908b2-0704-4733-ad9b-a9a8f200ab0a

change-vm-storage-policy-1
Once the Virtual Machine has been reconfigured, we can then take a look in our vSphere Web Client and we can see the VM Storage Policy has now been changed and VSAN will automatically enforce these new requirements.

change-vm-storage-policy-2
If you wish to assign a VM Storage Policy as part of a new Virtual Machine creation, you just need to set the vmProfile and profile properties which is similar to a reconfiguration operation.

  1. Exploring VSAN APIs Part 1 – Enable VSAN Cluster
  2. Exploring VSAN APIs Part 2 – Query available SSDs
  3. Exploring VSAN APIs Part 3 – Enable VSAN Traffic Type
  4. Exploring VSAN APIs Part 4 – VSAN Disk Mappings
  5. Exploring VSAN APIs Part 5 – VSAN Host Status
  6. Exploring VSAN APIs Part 6 – Modifying Virtual Machine VM Storage Policy
  7. Exploring VSAN APIs Part 7 – VSAN Datastore Folder Management
  8. Exploring VSAN APIs Part 8 – Maintenance Mode
  9. Exploring VSAN APIs Part 9 – VSAN Component count
  10. Exploring VSAN APIs Part 10 – VSAN Disk Health

5 thoughts on “Exploring VSAN APIs Part 6 – Modifying Virtual Machine VM Storage Policy

  1. William,
    Thanks for all your posts on the vSphere APIs!
    I have read these posts on the VM storage policies.
    http://www.virtuallyghetto.com/2013/11/spbm-storage-policy-based-management.html
    http://www.virtuallyghetto.com/2013/09/vm-storage-policy-apis-aka-storage.html

    Setup the following
    1. Deployed a 3 node VSAN using your template.
    http://www.virtuallyghetto.com/2014/04/ovf-template-for-creating-nested-esxi-3-or-32-node-vsan-cluster.html

    2. Created a storage policy with a rule set of just a single tag.
    3. Tagged the vsanDatastore.
    4. Created a VM with the vSanDatastore and associated the policy.

    From this post I see how to set policy information when calling ReconfigVM_Task but how would I just query the current VM policy ID using the vSphere API so that can be used to lookup the policy details in the policy API? I don’t see any objects off of the VM MOR, Datastore MOR or Virtual Disk MOR that has a policyID.

  2. Hi,

    Very helpful article. Thank you!!!

    Is there a way to list all the VM Storage Policies using vSphere API?

    Thanks.

  3. Hi Atoka,

    You’ve seemed to have answered you own question already 🙂 by posting over in http://www.virtuallyghetto.com/2013/09/vm-storage-policy-apis-aka-storage.html As I’ve mentioned, you’ll need to use the combination of the two APIs, as they’re different endpoints. If you just want to list them or perform general operations on them, you’ll need to use the VM Storage Policy API which is NOT part of the vSphere API. Once you’re ready to assign, then you’ll use the vSphere API.

    An alternative to that would be to look at using PowerCLI which includes VM Storage Policy cmdlets and with their abstraction, it’ll feel like a single interface but again both APIs are different

Thanks for the comment!