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

There are several vSphere API which requires datastore UUID 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 datastore 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.

[java]
package com.vmware.vijava;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.vmware.vim25.DatastoreInfo;
import com.vmware.vim25.VmfsDatastoreInfo;
import com.vmware.vim25.mo.Datastore;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.HostDatastoreBrowser;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class findVMFSUUIDs {
public static void main(String[] args) throws Exception {
ServiceInstance si = new ServiceInstance(new URL(
"https://10.120.30.40/sdk"), "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)
.searchManagedEntities("HostSystem");

if (hosts == null) {
System.out.println("Host not found on vCenter");
si.getServerConnection().logout();
return;
}

// 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)
.getDatastoreBrowser();
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.println("");
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
.getVmfs().getUuid());
}

}
System.out.print("]");
}
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()
+ "]");
}

}
}
[/java]

Program Output::

Hosts and all of its associated datastores

[192.168.1.1::DS3,DS2,Local DS,DS4,NFS1,]
[192.168.1.2::LocalDS2,LocalDS1,LocalDS3,]

Datastore and its UUID

[LocalDS1::55d9b324-bad019fa-147a-f04da20356f7]
[Local DS::55b5dee1-ebd2895d-1263-002219574957]
[DS2::51e3e9f7-8ab3eac5-4715-00221957495d]
[LocalDS2::5592e051-da224dfc-ad16-f04da20356f7]
[LocalDS3::55d9b367-d145aef6-dcfe-f04da20356f7]
[DS3::51398f9b-e591008a-1b35-002219574a65]
[DS4::55cc410f-604db805-6fc0-002219574a65]
[/java]

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 datastore UUID 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)
      .searchManagedEntities(“VirtualMachine”);
      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 “http://www.yavijava.com/docs/vim.vm.ConfigInfo.html” . UUID is one of the properties of this object.

      Let me know if you need any help.

Comments are closed.