VMware {code} Community
sajuptpm
Contributor
Contributor

how get total and used disk space of a vm using disk path

Hi,

How get VM's disk size using storage url or disk path ?

does VMware API has this feature ?

Reply
0 Kudos
8 Replies
sajuptpm
Contributor
Contributor

Note, I know host and vmPathName ( "[datastore1] vCenter/vCenter.vmx" )

So have any way (API call)  to get VM using host and vmPathName ?.

If i get vm then i can easly find disk size.

I checked managed objects Datastore, Hostsytem and VirtualMachines, but could not find any thing to solve this issue.

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

Take a look at the HostDatastoreBrowswer object: HostDatastoreBrowser

The SearchDatastore_Task will return a results object which contains a FileInfo object with properties on the file.  You can use this same method to get files that aren't associated with a VM.

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
sajuptpm
Contributor
Contributor

Hi,

I got it

Thanks

Reply
0 Kudos
sajuptpm
Contributor
Contributor

I am using https://bitbucket.org/jkinred/psphere

state not updating properly

    I am using following code to get disk_size.
    When running this code getting error AttributeError: TaskInfo instance has no attribute 'result'.
    Note: that time "info.state" showing "running"
    Some time this code working well and that time  "info.state" showing "success"
    I put a while loop to wait until "info.state" become "success", but that not working.

client = Client("192.168.0.116", "root", "Bman1547")
hosts = HostSystem.all(client)
host = hosts[0]
print "Host: ", host.name

db = host.datastoreBrowser
mor = db.SearchDatastore_Task(datastorePath="[datastore1] vCenter/",
searchSpec={"matchPattern":"vCenter.vmx",
                "details":{"fileType":True,
                                "fileSize":True,
                                "modification":True,
                                "fileOwner":True},
                "searchCaseInsensitive":False,
                "sortFoldersFirst":False,
                "query":None
        })
info = mor.info
while True:
print "-------info.state------", info.state
if info.state == "success":
        break

result = info.result
file = result.file

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

If you want to get the disk used space of a VM, the easier way will probably be to just get the layoutEx property from the VM.

You could also use the storage property to get the per Datastore usage.  This includes committed and uncommitted values to calculate thin provisioned rates.  The value isn't updated in real time, but if you can deal with some delay, you can refresh a datastores info with a seperate call to each datastore.  However, without a refresh the data is typically useful enough for most tools.

If you wanted to just scan a datastore and calculate usage metrics based on file type or to find files that may not be associated with a VM, then the DatastoreBrowser is more appropriate.

In your tool, is the idea to give the VMX path as an input or to just pick a VMDK and return what VM it's associated with and the VM's total disk usage?

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos
sajuptpm
Contributor
Contributor

I have disk like "[datastore1] vCenter/vCenter.vmx"

I want to find it's total size, used and free size

I can take it using following call

result = host.datastoreBrowser.SearchDatastore_Task()

But some time i can't access "result.info.result.file" attribute since that time "result.info.state" showing "error"

Thanks

Reply
0 Kudos
sajuptpm
Contributor
Contributor

I have disk like "[datastore1] vCenter/vCenter.vmx"

I want to find it's total(size given when creating the disk), used and free size

I can take used size using following call

result = host.datastoreBrowser.SearchDatastore_Task()

Howto find total and free size using call  "host.datastoreBrowser.SearchDatastore_Task()"

Have any other way to find total (size given when creating the disk), used and free size of a disk using name like "[datastore1] vCenter/vCenter.vmx"

Reply
0 Kudos
AshleyBanks
Contributor
Contributor

Old thread but I wanted to do the same but never found a solution so hope this helps someone.

I created the following script to get each disk of a VM and check the actual size of VMDK file and output both.

It also outputs the Guest OS Sizes also.

A bit crude but it did the job for me.

$vms = Get-VM "VMNAME1","VMNAME2"

"ServerName" + "," + "Disk Path" +","+ "Capacity" + "," + "VMDK Size"
Foreach ($vm in $vms){

$view=get-view $vm

foreach($disk in $view.config.hardware.Device | where {$_.GetType().name -eq 'VirtualDisk'}){
  
   foreach ($VMDK in $VM.ExtensionData.LayoutEx.File){
       $DISKNAME = ($disk.backing.filename)
       $DISKNAME = $DISKNAME.replace(".vmdk","-flat.vmdk")

       if ($DISKNAME -eq $VMDK.name)
       {
           $UsedGB = $VMDK.Size
           $VM.Name + "," + $DISKNAME + "," +$disk.CapacityInKB / 1024 + "," +  $UsedGB / 1024 / 1024
       }
   }
}
}

" "
"Server Name" + "," + "Disk Path" +","+ "OS Capacity"+","+ "OS FreeSpace"

$guest = $vm | get-vmguest
foreach ($server in $vms)
{
$guest = $server | get-vmguest
foreach($disk in $guest.disks){
$guest.vmname + "," + $disk.Path + "," + $disk.CapacityGB  + "," + $disk.FreeSpaceGB
}
}

Reply
0 Kudos