0

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 ?

2 Answers 2

1

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());
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Below is how I am fetching Hard Disk Details

First get all Devices of your VM

     VirtualDevice[] device=vm.getConfig().getHardware().getDevice();

Then Find HardDisk in fetched Devices, Find their Backing Info and Fetch UUID from there

Sample Code

VirtualDevice[] device=vm.getConfig().getHardware().getDevice();



        for (VirtualDevice disk : device)
        {
            if(disk.getDeviceInfo().getLabel().contains("Hard disk"))
            {
                 VirtualDiskFlatVer2BackingInfo backing = (VirtualDiskFlatVer2BackingInfo) disk.getBacking();
                System.out.println(backing.uuid);
            }
        }

1 Comment

It is always better to check if virtualDevice is instance of VirtualDisk class before getting uuid. Filtering virtualDisk from all virtualDevice based on label is not the right way to do it. VirtualMachineConfigInfo vmConfig = vm.getConfig(); VirtualDevice[] vds = vmConfig.getHardware().getDevice(); for(VirtualDevice vd : vds){ if(vd instanceof VirtualDisk){ System.out.println(vd.getDiskObjectId()); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.