Tag Archives: storage

vSphere 6.0 cool APIs to mark local host HDD to SSD, SSD to HDD: Sample API script

Recently I wanted to test vFRC (vSphere Flash Read Cache) feature interop with vSphere DRS and vSphere HA. I was figuring out ways to emulate my hosts local HDDs as SSD as I did not have real SSDs in my lab. There are couple of ways I used to fake/emulate local HDD as SSD earlier but this time I found other cool way to automate this quickly by using readily available API. The API that I am talking about is markAsSSD(). This API got introduced in vSphere 6.0. It is important to know why this API is made officially available. The reason this API primarily introduced is: Some time SSDs behind some controllers might not be recognized as SSD correctly. Other use cases for this API can be used to test VMware vSAN (just playing around), vFRC, flash host cache, interop testing etc. Of course, performance of real SSD is incomparable with fake SSD.

Below is the complete code sample which can help you quickly to Mark the local Lun of the host as SSD..


//:: # Author: Vikas Shitole
//:: # Website: www.vThinkBeyondVM.com
//:: # Product/Feature: vCenter Server/Storage
//:: # Description: Mark the local Lun of the host as SSD for testing purpose.

package com.vmware.yavijava;
import java.net.MalformedURLException;
import java.net.URL;
import com.vmware.vim25.ScsiLun;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.HostStorageSystem;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class MarkAsSSD {

public static void main(String[] args) throws Exception {
if(args.length!=4)
{
System.out.println("Usage: MarkAsSSD url username password hostip/fqdn");
System.exit(-1);
}

URL url = null;
try
{
url = new URL(args[0]);
} catch ( MalformedURLException urlE)
{
System.out.println("The URL provided is NOT valid. Please check it.");
System.exit(-1);
}
String username = args[1]; //vCenter username
String password = args[2]; //vCenter password
String hostname = args[3]; // host IP on which local HDD is available
String LunDisplayName="Local VMware Disk (mpx.vmhba1:C0:T2:L0)"; //Add the Display name of the lun that can be seen from VI client or NGC
// Initialize the system, set up web services
ServiceInstance si = new ServiceInstance(url, username,
password, true);

Folder rootFolder = si.getRootFolder();
HostSystem host = null;

host = (HostSystem) new InventoryNavigator(rootFolder)
.searchManagedEntity("HostSystem", hostname);

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

HostStorageSystem hhostsystem = host.getHostStorageSystem();
ScsiLun[] scsilun = hhostsystem.getStorageDeviceInfo().getScsiLun();
boolean flag = false;
for (ScsiLun lun : scsilun) {
System.out.println("Display Name"+lun.getDisplayName());
if (lun.getDisplayName().equals(
LunDisplayName)) {
hhostsystem.markAsSsd_Task(lun.getUuid());
flag = true;
break;
// hhostsystem.markAsNonSsd_Task(lun.getUuid());

}

}
if (flag) {
System.out.println("LUN is marked as SSD successfully");

}else{
System.out.println("LUN is NOT marked as SSD, plz check if local lun is in use");

}

si.getServerConnection().logout();

}
}

Notes:
– For the sake of simplicity, I have hard-coded LUN display name, you can change it based on your environment.
– You can scale the same code to mark all the local HDDs to SSDs in all the hosts across datacenter or multiple datacenter.
– I would like you to have your attention on other new useful/handy related vSphere 6.0 APIs such as “MarkAsNonSSD” , “MarkAsLocal”, “MarkAsNonLocal“. Same sample can be leveraged to automate these related APIs.

If you have still not setup your YAVI JAVA Eclipse environment:Getting started tutorial

Important tutorials to start with: Part I & Part II

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

}
}

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]

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.

How to get Datastore Summary for all data-stores connected to a ESXi host using vSphere API

This post is motivated by this VMTN thread , also I wanted to start with small tutorials on vSphere APIs using JAVA, I thought lets start with answering this query which can cover tutorial on DatastoreSummary data object as well. I hope you have already setup your eclipse environment by referring my  Getting started  blog post. Here we go.

Query on VMTN was: How to get datastore type connected to ESXi programmatically using vSphere APIs. As datastore type is part of DatastoreSummary data object, lets see how to get datastore summary (Datastore Name, Datastore Type, Datastore Capacity, Datastore free capacity etc.)

DatastoreSummary Data Object Description:

Datastore Summary Data object description

Complete code :

package com.vmware.vim25.mo.Samples.cluster;
import java.net.URL;
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.ServiceInstance;

public class DatastoreSummary {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: java SearchDatastore "
+ "username password");
return;
}

/*
* you need to pass 3 parameters
* 1. https://ESXi_IP/sdk
* 2. username
* 3. password
*/
ServiceInstance si = new ServiceInstance(new URL(args[0]), args[1],
args[2], true);
String hostname = "XYZ.vmware.com"; //Pass the FQDN i.e. DNS name of the ESXi host, ESXi host IP will not work
Folder rootFolder = si.getRootFolder();
HostSystem host = null;

host = (HostSystem) new InventoryNavigator(rootFolder)
.searchManagedEntity("HostSystem", hostname);

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

HostDatastoreBrowser hdb = host.getDatastoreBrowser();

System.out.println("Datastore Summary connected to ESXi host");
System.out.println();
Datastore[] ds = hdb.getDatastores();

for (int i = 0; ds != null && i < ds.length; i++) {
System.out.println("DatastoreName:" + ds[i].getName() + " "
+ "DSType:" + ds[i].getSummary().getType() + " "
+ "TotalCapacity(in GB):"
+ (ds[i].getSummary().getCapacity()) / (1024 * 1024 * 1024)
+ " " + "FreeSpace (in GB): "
+ (ds[i].getSummary().getFreeSpace())
/ (1024 * 1024 * 1024) + " ");
System.out.println();

}
si.getServerConnection().logout();
}
}

Program Output::


Datastore Summary connected to host

DatastoreName:Local Datastore DSType:VMFS TotalCapacity(in GB):131 FreeSpace (in GB): 130

DatastoreName:Shared DS_3 DSType:VMFS TotalCapacity(in GB):24 FreeSpace (in GB): 8

DatastoreName:Shared DS_1 DSType:VMFS TotalCapacity(in GB):49 FreeSpace (in GB): 8

DatastoreName:Shared DS_4 DSType:VMFS TotalCapacity(in GB):49 FreeSpace (in GB): 13

DatastoreName:Shared DS_2 DSType:VMFS TotalCapacity(in GB):49 FreeSpace (in GB): 15

Compare output with datastore summary from VI client
Datastore summary

You could see output is matching with screenshot from VI client, is not it cool?

Refer: vSphere API reference documentation I highly recommend you to spend some time on getting familiarize with vSPhere API reference.

Stay tuned for more specific/basic tutorials. please let me know if you want specific tutorial on vSphere API