How to get VMFS datastore UUIDs across all the datastores in a vCenter server using vSphere APIs

There are several vSphere API which requires vmfsUUID as one of its important properties. Some examples of such APIs are MountVmfsVolume(),mountVmfsVolumeEx(), UnmountForceMountedVmfsVolume(), unmapVmfsVolumeEx(), DeleteVmfsVolumeState(), unmountVmfsVolumeEx & unmountVmfsVolume(). Without vmfsUUID we can NOT operate on any of these APIs. Now question is how to get/know the UUID of the VMFS datastores connected to hosts using vSphere APIs. I thought its better to write the vSphere API script to solve this. The script that I am sharing is going to get all hosts and all of its associated datastores and each datastore and its associated UUID. Here we go.

package com.vmware.vijava;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.vmware.vim25.DatastoreInfo;
import com.vmware.vim25.VmfsDatastoreInfo;

public class findVMFSUUIDs {
public static void main(String[] args) throws Exception {
ServiceInstance si = new ServiceInstance(new URL(
""), "administrator@vsphere.local",
"Administrator!23", true);
// Get the rootFolder
Folder rootFolder = si.getRootFolder();

// Get all the hosts in the vCenter server
ManagedEntity[] hosts = new InventoryNavigator(rootFolder)

if (hosts == null) {
System.out.println("Host not found on vCenter");

// Map to store the datastore name as key and its UUID as the value
Map< String , String> vmfsdatastoreUUIDs = new HashMap< String , String>();

// Map to store host as key and all of its datastores as the value
Map< ManagedEntity , Datastore[]> hostDatastores = new HashMap< ManagedEntity , Datastore[]>();
for (ManagedEntity hostSystem : hosts) {
HostDatastoreBrowser hdb = ((HostSystem) hostSystem)
Datastore[] ds = hdb.getDatastores();
hostDatastores.put(hostSystem, ds);

System.out.println("Hosts and all of its associated datastores");
for (Map.Entry < ManagedEntity , Datastore[]> datastores : hostDatastores
.entrySet()) {
System.out.print("[" + datastores.getKey().getName() + "::");
for (Datastore datastore : datastores.getValue()) {
System.out.print(datastore.getName() + ",");
DatastoreInfo dsinfo = datastore.getInfo();
if (dsinfo instanceof VmfsDatastoreInfo) {
VmfsDatastoreInfo vdinfo = (VmfsDatastoreInfo) dsinfo;
vmfsdatastoreUUIDs.put(datastore.getName(), vdinfo

System.out.println(" ");
System.out.println("Datastore and its UUID");
for (Map.Entry< String , String> dsuuid : vmfsdatastoreUUIDs.entrySet()) {
System.out.println("[" + dsuuid.getKey() + "::" + dsuuid.getValue()
+ "]");


Program Output::

Hosts and all of its associated datastores

[,DS2,Local DS,DS4,NFS1,]

Datastore and its UUID

[Local DS::55b5dee1-ebd2895d-1263-002219574957]

Now you could enhance this code to operate on any vSphere API that requires VMFS UUID as one of parameters. Please do comment if you have any doubts or need any help.

If you have still not setup your VI JAVA Eclipse environment:Getting started tutorial
Important tutorials to start with: Part I & Part II

Note: VI JAVA open source project is forked into YA VIJAVA(Yet another VI JAVA), it will have support to all the new APIs introduced in vSphere 6.0. VI JAVA itself works fine even on vSphere 6.0 (except the new features in vSphere 6.0). Please do stay tuned my post on YA VIJAVA.

2 thoughts on “How to get VMFS datastore UUIDs across all the datastores in a vCenter server using vSphere APIs

  1. Hi , Thanks for your tuorial.It helped me a lot.
    I am trying to find a way to get list off all Vms and it s uuids ..Can u tell me a hint to enhance this code to get the information i want. ?

    1. Hi Shefeek,

      I am glad to know that tutorials helped you. Sorry for late response.
      Yes, you can get the VMs and its UUIDs.

      1. You need to get all the VMs using below code
      vms = (ManagedEntity[]) new InventoryNavigator(cluster)
      Above code would give you all the VMs in a cluster (we can make it VC level or datacenter level as well).

      2. Now iterate through each VM and get this object “” . UUID is one of the properties of this object.

      Let me know if you need any help.

Leave a Reply

Your email address will not be published.