VMware Cloud Community
YanSi
Enthusiast
Enthusiast

How to calculate the correct disk usage

I use the following powercli code output

$FinalResult = @()
ForEach($vm in Get-View -ViewType "VirtualMachine" -Filter @{'Name'='vm01-a'}){
       $totalCapacity = $totalFree = 0
       $vm.Guest.Disk | %{
            $object = New-Object -TypeName PSObject
            $Capacity = "{0:N0}" -f [math]::Round($_.Capacity/1MB)
            $totalCapacity += $_.Capacity
            $totalFree += $_.FreeSpace

            $usedSpace 	= "{0:N0}" -f [math]::Round(($totalCapacity - $totalFree)/1MB)
	        
            $Freespace = "{0:N0}" -f [math]::Round($_.FreeSpace/1MB)
            $Percent = [math]::Round(($FreeSpace)/ ($Capacity) * 100)
            $PercentFree = "{0:P0}" -f ($Percent/100)
            
            $object | Add-Member -MemberType NoteProperty -Name "VM Name" -Value $vm.Name
            $object | Add-Member -MemberType NoteProperty -Name "Disk" -Value $_.DiskPath
            $object | Add-Member -MemberType NoteProperty -Name "Capacity(MB)" -Value $Capacity

            $object | Add-Member -MemberType NoteProperty -Name "Used(MB)" -Value $usedSpace

            $object | Add-Member -MemberType NoteProperty -Name "Free(MB)" -Value $FreeSpace
            $object | Add-Member -MemberType NoteProperty -Name "Free(%)" -Value $PercentFree
            $finalResult += $object
        }
        $object = New-Object -TypeName PSObject
        $object | Add-Member -MemberType NoteProperty -Name "VM Name" -Value $vm.Name
        $object | Add-Member -MemberType NoteProperty -Name "Disk" -Value 'Total'
        $object | Add-Member -MemberType NoteProperty -Name "Capacity(MB)" -Value ("{0:N0}" -f ($totalCapacity/1MB))

        $object | Add-Member -MemberType NoteProperty -Name "Used(MB)" -Value ("{0:N0}" -f ($usedSpace/1MB))

        $object | Add-Member -MemberType NoteProperty -Name "Free(MB)" -Value ("{0:N0}" -f ($totalFree/1MB))
        $object | Add-Member -MemberType NoteProperty -Name "Free(%)" -Value ("{0:P0}" -f ($totalFree/$totalCapacity))
        $finalResult += $object
    }
$finalResult | FT

 

Its output is as follows:

VM Name Disk  Capacity(MB) Used(MB) Free(MB) Free(%)
------- ----  ------------ -------- -------- -------
vm01-a  /     3,570        1,391    2,179    61%
vm01-a  /boot 1,014        1,559    847      84%
vm01-a  Total 4,584        0        3,025    66%

The / partition result is correct, but the used space of the /boot partition is incorrect.
How to solve?

 

The results displayed on the Linux terminal are as follows:

[root@vm01-a ~]# df -Thm
Filesystem              Type     1M-blocks  Used Available Use% Mounted on
devtmpfs                devtmpfs       485     0       485   0% /dev
tmpfs                   tmpfs          496     0       496   0% /dev/shm
tmpfs                   tmpfs          496     7       489   2% /run
tmpfs                   tmpfs          496     0       496   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs           3570  1393      2178  39% /
/dev/sda1               xfs           1014   168       847  17% /boot
tmpfs                   tmpfs          100     0       100   0% /run/user/0
[root@vm01-a ~]#

I don’t know why the used space of the /boot partition is 1,559MB instead of 168MB.

0 Kudos
1 Reply
LucD
Leadership
Leadership

Instead of relying on the VMware Tools to capture that info, you can use Invoke-VMScript and run the df command yourself.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos