Search code examples
javavirtual-machinevmwarevsphere

How to get UUID for VirtualDisk in vSphere 5.5 java api?


In virtualDisk object I can find diskObjectId, which is durable and unmutable identifier (according to VMware docs: https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.vm.device.VirtualDisk.html). But if I detach the virtual hard disk and attach it again to instance then also diskObjectId for that virtual hard disk remains same as before detaching. Eg, diskObjectId was "2086-2001" and even after detaching hard disk, I created a new hard disk and same diskObjectId("2086-2001") got assigned to it.

I want to identify virtual hard disk with unique identifier (uuid). How can I get uuid for Virtual Disk ?


Solution

  • VirtualDisk object has 2 identifiers (vmware documentation):

    • diskObjectId : Virtual disk durable and unmutable identifier. Virtual disk has a UUID field but that can be set through VirtualDiskManager APIs. This identifier is a universally unique identifier which is not settable. VirtualDisk can remain in existence even if it is not associated with VM.
    • uuid

    But I prefer using 'diskObjectId' because of the reason mentioned above.

    I am getting unique identifier by below mentioned way :

        VirtualMachineConfigInfo vmConfig = vm.getConfig();
        VirtualDevice[] vds = vmConfig.getHardware().getDevice();
        for(VirtualDevice vd : vds){
            if(vd instanceof VirtualDisk){
                System.out.println(vd.getDiskObjectId());
            }
        }