TheVMinator
Expert
Expert

Determining unused space in virtual hard disks

Jump to solution

I would like to determine the unused space in my virtual hard disks.  I would like to:

  • Select a vm
  • the first vmdk disk is always the OS disk in this environment.  How much space was provisioned, and how much is actually being used?

  • The other disk(s) are application data.  What is the total space provisioned on these non-os disks, and how much of this space is actually being used?

thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Do you then get the results when you run this adapted version ?

Get-VM  |

Select Name,

  @{N="System disk capacity";E={

    $_.HardDisks | Where {$_.Name -eq "Hard disk 1"} |

    Select -ExpandProperty CapacityGB

  }},

  @{N="System disk used";E={

    $hdC = Get-VMGuest -VM $_ | %{$_.Guest.Disks} | Where {$_.Path -eq "C:\"}

    [math]::Round(($hdC.CapacityGB - $hdC.FreeSpaceGB),1)

  }},

  @{N="Other disks capacity";E={

    $_.HardDisks | Where {$_.Name -ne "Hard disk 1"} |

    Measure-Object -Property CapacityGB -Sum |

    Select -ExpandProperty Sum

  }},

  @{N="Other disks used";E={

    [math]::Round((Get-VMGuest -VM $_ | %{$_.Guest.Disks} | Where {$_.Path -ne "C:\"} | %{

      $_.CapacityGB - $_.FreeSpaceGB

    } | Measure-Object -Sum |

    select -ExpandProperty Sum),1)

  }}


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

View solution in original post

0 Kudos
20 Replies
LucD
Leadership
Leadership

To see what is actually used you will have to be able to map the OS partitions to the vDisks.

And there is, afaik, no fool-proof way of doing that.

For example, inside the guest you can find out how much space is used on the D-partition, but there is no sure way to know on which vDisk the D-partition is located I'm afraid.

In some cases you could use the disksize you see inside the guest OS for a disk, to map it to a vDisk.

But if you have 2 vDisks of the same size, that will be much more difficult.

Arnim did a great post on this in his PowerCLI: Match VM and Windows harddisks – Part 2


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

TheVMinator
Expert
Expert

OK thanks for the info.  But just to be clear:

- When I look in the vSphere client, it shows for each vmdk either "Harddisk 1" or "harddisk 2".  Since all the VMs were created from standard templates, I know my OS disk is always "Harddisk 1".  Is it not possible to pull the "harddisk 1" field using powercli, and select on that?  If I could do that, then I would always know I'm looking at an OS vmdk.

0 Kudos
LucD
Leadership
Leadership

Yes, you can select on the harddiskname.

But the problem comes when you want to link this to the properties you have in $vm.Guest.Disks.

That data is collected by the VMware Tools inside the guest OS.

It will show the capacity and the freespace for partition C:, but it will not tell you which harddisk that is.

And you are correct, In your situation, the C partition will always be Hard disk 1.

But that doesn't work if you have more harddisks connected to the VM


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

0 Kudos
TheVMinator
Expert
Expert


OK thanks.  If I always know that even if my VM has 5 vmdks, that the OS disk is always the first one that was created, and that the OS disk is always on the "C:" partitition and therefore is always "harddisk 1", wouldn't I be able to select "harddisk 1" and calculate the provisioned size of the vmdk vs. the amount of data actually used?  I would know that that is the amount of data used by my OS vmdk right?

For the other harddisks, I'm not concerned about knowing what the windows partition letter is.  I know any other harddisk other than 1 is data, not OS.  So if I total all other harddisks other than "harddisk 1", I know I'm getting the total of my data.

Am I thinking correctly?

0 Kudos
LucD
Leadership
Leadership

Correct, just remember you need to have the VMware Tools installed to be able to retrieve the disk usage.

You could do something like this

Get-VM  |
Select Name,
 
@{N="System disk capacity";E={
   
$_.HardDisks | Where {$_.Name -eq "Hard disk 1"} |
   
Select -ExpandProperty CapacityGB
  }}
,
 
@{N="System disk used";E={
   
$hdC = $_.Guest.Disks | Where {$_.Path -eq "C:\"}
    [
math]::Round(($hdC.CapacityGB - $hdC.FreeSpaceGB),1)
  }}
,
 
@{N="Other disks capacity";E={
   
$_.HardDisks | Where {$_.Name -ne "Hard disk 1"} |
   
Measure-Object -Property CapacityGB -Sum |
   
Select -ExpandProperty Sum
  }}
,
 
@{N="Other disks used";E={
    [
math]::Round(($_.Guest.Disks | Where {$_.Path -ne "C:\"} | %{
     
$_.CapacityGB - $_.FreeSpaceGB
    }
| Measure-Object -Sum |
   
select -ExpandProperty Sum),1)
  }}


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

TheVMinator
Expert
Expert

OK thanks.  The only problem is that the system disk used column is blank for every vm when I run the report.  However, the "other disks used" has a value for every vm and is fine.  Any ideas on why this is?

0 Kudos
LucD
Leadership
Leadership

Could it be that the Path property is not filled in correctly on your VMs ?

Do you see a C:\ path when you do this for one of the VMs that has a blank entry in that column ?

Get-VM -Name MyVM | %{

   $_.Guest.Disks

}


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

0 Kudos
TheVMinator
Expert
Expert

I ran the script you recommended using the name of a VM that has a blank field in "System Disk Used":

Get-VM -Name MyVM | %{

   $_.Guest.Disks

}

but it doesn't give any output at all.

If I run

get-vm -name MyVM | get-harddisk

It shows the filenames though

Any ideas?

Thanks again.

0 Kudos
TheVMinator
Expert
Expert

(In fact the result is the same for every VM in the environment - none of them return a value for "system disk used")

0 Kudos
LucD
Leadership
Leadership

And you have the VMware Tools installed and running on all these VMs ?


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

0 Kudos
TheVMinator
Expert
Expert

There are some that VMware tools are not installed on like some proprietary linux distributions.  But 95% have vmware tools installed and running.  However, of a set of over 100 vms,  none of them are able to pull the field though.

0 Kudos
LucD
Leadership
Leadership

And I assume the following also returns nothing ?

Get-VM MyVM | Get-VMGuest | select -ExpandProperty Disks


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

0 Kudos
TheVMinator
Expert
Expert

Actually, when I do that it shows me capacityGB, FreepaceGB and Path, for both C:\ and d:\ partitions.

0 Kudos
LucD
Leadership
Leadership

Do you then get the results when you run this adapted version ?

Get-VM  |

Select Name,

  @{N="System disk capacity";E={

    $_.HardDisks | Where {$_.Name -eq "Hard disk 1"} |

    Select -ExpandProperty CapacityGB

  }},

  @{N="System disk used";E={

    $hdC = Get-VMGuest -VM $_ | %{$_.Guest.Disks} | Where {$_.Path -eq "C:\"}

    [math]::Round(($hdC.CapacityGB - $hdC.FreeSpaceGB),1)

  }},

  @{N="Other disks capacity";E={

    $_.HardDisks | Where {$_.Name -ne "Hard disk 1"} |

    Measure-Object -Property CapacityGB -Sum |

    Select -ExpandProperty Sum

  }},

  @{N="Other disks used";E={

    [math]::Round((Get-VMGuest -VM $_ | %{$_.Guest.Disks} | Where {$_.Path -ne "C:\"} | %{

      $_.CapacityGB - $_.FreeSpaceGB

    } | Measure-Object -Sum |

    select -ExpandProperty Sum),1)

  }}


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

View solution in original post

0 Kudos
CarlosDionizio
Contributor
Contributor

Hi Friends!

But if OS Guest is linux?  Smiley Wink

0 Kudos
LucD
Leadership
Leadership

Then you would need to test if the Path is '/' or not.


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

0 Kudos
TheVMinator
Expert
Expert

I reformatted the report using the second version and it seems to be working now. This looks good thanks again!

0 Kudos
TheVMinator
Expert
Expert

I had to make this tweak to the System disk used calculation to get it to work:

@{N="System Disk Used";E={  

     [math]::Round(($_.Guest.Disks | Where {$_.Path -eq "C:\"} | %{

     $_.CapacityGB - $_.FreeSpaceGB} |

     Measure-Object -Sum |   

     select -ExpandProperty Sum),1)

     }}

It seems to report the right result.  In this case, round() is outside of the calculation whereas in the version 2 code, round() was nested inside.

In this case, it seems it is doing a sum unnecessarily as there is only one c:\ disk. It seems to error out without the measure-object and the select -expandproperty though

0 Kudos
LucD
Leadership
Leadership

Sorry for not getting back to you sooner.

Glad you fixed the problem.


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

0 Kudos