Finding VMDK path from PV VolumeHandle

I’ve been looking at ways in which we could query the mappings of objects between the Kubernetes layer and the vSphere layer. One thing that I really wanted to figure out is if I have the VolumeHandle from the Persistent Volume in Kubernetes, could I easily find the datastore and path using PowerCLI. It looks like I can.

Let’s begin with a look at the Persistent Volume or PV  for short. Note that this is a Kubernetes cluster that is using the new vSphere CSI driver. 

$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                  STORAGECLASS   REASON   AGE
pvc-8c774366-eb45-11e9-80e4-005056a239d9   1Gi        RWO            Delete           Bound    cassandra/cassandra-data-cassandra-0   cass-sc                 11d
pvc-8c7968d5-eb45-11e9-80e4-005056a239d9   1Gi        RWO            Delete           Bound    cassandra/cassandra-data-cassandra-1   cass-sc                 11d
pvc-8c7b2535-eb45-11e9-80e4-005056a239d9   1Gi        RWO            Delete           Bound    cassandra/cassandra-data-cassandra-2   cass-sc                 11d

$ kubectl describe pv pvc-8c7968d5-eb45-11e9-80e4-005056a239d9
Name:            pvc-8c7968d5-eb45-11e9-80e4-005056a239d9
Labels:          <none>
Finalizers:      [ external-attacher/csi-vsphere-vmware-com]
StorageClass:    cass-sc
Status:          Bound
Claim:           cassandra/cassandra-data-cassandra-1
Reclaim Policy:  Delete
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        1Gi
Node Affinity:   <none>
    Type:              CSI (a Container Storage Interface (CSI) volume source)
    VolumeHandle:      937b445e-bd7d-4576-987e-5f7e32147af1
    ReadOnly:          false
    VolumeAttributes:      fstype=
                           type=vSphere CNS Block Volume
Events:                <none>

Now the objective is to see if we can quickly determine the datastore and path to file from the VolumeHandle above. Note that the CSI driver uses First Class Disks (FCDs) also referred to Improved Virtual Disks (IVDs), a new kind of VMDK. The VolumeHandle above is the same as the FCD ID. Here is how to find that information with the FCD ID using a few PowerCLI commands:

$ pwsh
PowerShell v6.1.0-preview.2
Copyright (c) Microsoft Corporation. All rights reserved.
Type 'help' to get help.

PS /home/cormac/PowerCLI> Connect-VIServer -User administrator@vsphere.local -Password xxx -force

Name                           Port  User
----                           ----  ----         443   VSPHERE.LOCAL\Administrator

PS /home/cormac/PowerCLI> Get-vDisk | ft -autosize

Name                                     Disk Type CapacityGB Filename
----                                     --------- ----------                                                                                   --------
pvc-8c7b2535-eb45-11e9-80e4-005056a239d9 Flat      1.000      [vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/271c0c4065e545e7a2a1fefdd17cf4d3.vmdk
pvc-8c7968d5-eb45-11e9-80e4-005056a239d9 Flat      1.000      [vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/ff34579cbed54dc994976885e22c7cb9.vmdk
pvc-8c774366-eb45-11e9-80e4-005056a239d9 Flat      1.000      [vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/6a02671fb7ed4358a02f39e71e764bd2.vmdk

PS /home/cormac/PowerCLI> $vd = Get-vDisk

PS /home/cormac/PowerCLI> $

PS /home/cormac/PowerCLI> $vdFromId = Get-VDisk -Id "Datastore-datastore-33:57f69494-24a2-4b28-9d1b-bc43fe08a0d2"

PS /home/cormac/PowerCLI> $vdFromId.Filename
[vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/271c0c4065e545e7a2a1fefdd17cf4d3.vmdk

And there we have a relatively easy way to figure out what the datastore and path to VMDK are, given that the PV describe command only provides the FCD ID in the VolumeHandle field. Kudos to my colleague Manish Tiwari for his assistance in figuring this out.

[Update] Thanks to some feedback from my good pal William Lam, we are able to get the above into a one liner:

PS /home/cormac/PowerCLI> $VolumeHandleId = "d638ac80-88f8-40e4-bca7-281bc7b98fde"
PS /home/cormac/PowerCLI> (Get-vDisk -Id (Get-vDisk).id | where {$ -match $VolumeHandleId}).Filename
[vsanDatastore] 33d05a5d-e436-3297-94f7-246e962f4910/6a02671fb7ed4358a02f39e71e764bd2.vmdk