Contributor
Contributor

Check free space before svMotion

Jump to solution

Hello,

 

I'm looking to get some extra set of eyes on why something isn't working all the time; some back story first: when we decom a VM server, we move it to a specific datastore and found this thread on how to have the script calculate the free space left on a datastore before proceeding with the svMotion of the decom'd server.  We like to keep 20% free on the datastore, so this is what I have code-wise:

 

$vmview = Get-VM -Name $decomname|Get-View

#calculating size of VM in GB
$VMspace = "{0:n}" -f ($vmview.Summary.Storage.Committed / 1GB)

#calculating size free space of DS
$DSfree = Get-Datastore -Name $ds| Select-Object FreeSpaceMB
$DSspace = "{0:n}" -f ($DSfree.FreeSpaceMB / 1024)
if ($VMspace -lt ($DSspace - 3200))
{
Move-VM -VM $decomname -Datastore $ds -DiskStorageFormat 'Thin'
}
else{
Write-Host -ForegroundColor Yellow "----------------------------"
Write-Host -ForegroundColor Yellow "$ds does not have enough free space for this VM!"
Write-Host -ForegroundColor Yellow "Make room on the datastore or svMotion it manually later."
Write-Host -ForegroundColor Yellow "----------------------------"
}
}

 

At times it works fine, but other times it gives the "doesn't have enough free space" message when the datastore clearly has space for the decom'd VM.  Case in point: a VM that is 75 GBs in size and the datastore it should go to has 5 TBs free...

 

What am I missing or not doing properly?

0 Kudos
1 Solution

Accepted Solutions
Leadership
Leadership

Can you try something like this?
Btw, are those VM's harddisks Thick or Thin?

$vm = Get-VM -Name $decomname

#calculating size of VM in GB
$vmSpace = $vmview.ExtensionData.Summary.Storage.Committed/1GB

#calculating size free space of DS
$dsSpace = (Get-Datastore -Name $ds).FreeSpaceGB

if ($vmSpace -lt ($dsSpace * 0.8)){
    Move-VM -VM $vm -Datastore $ds -DiskStorageFormat 'Thin'
}
else
{
    Write-Host -ForegroundColor Yellow "----------------------------"
    Write-Host -ForegroundColor Yellow "$ds does not have enough free space for this VM!"
    Write-Host -ForegroundColor Yellow "Make room on the datastore or svMotion it manually later."
    Write-Host -ForegroundColor Yellow "----------------------------"
}


 


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

View solution in original post

0 Kudos
16 Replies
Leadership
Leadership

Can you try something like this?
Btw, are those VM's harddisks Thick or Thin?

$vm = Get-VM -Name $decomname

#calculating size of VM in GB
$vmSpace = $vmview.ExtensionData.Summary.Storage.Committed/1GB

#calculating size free space of DS
$dsSpace = (Get-Datastore -Name $ds).FreeSpaceGB

if ($vmSpace -lt ($dsSpace * 0.8)){
    Move-VM -VM $vm -Datastore $ds -DiskStorageFormat 'Thin'
}
else
{
    Write-Host -ForegroundColor Yellow "----------------------------"
    Write-Host -ForegroundColor Yellow "$ds does not have enough free space for this VM!"
    Write-Host -ForegroundColor Yellow "Make room on the datastore or svMotion it manually later."
    Write-Host -ForegroundColor Yellow "----------------------------"
}


 


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

View solution in original post

0 Kudos
Contributor
Contributor

That worked, thanks again! 🙂

 

And the VMs are thick'd in production but turn them to Thin when they get decom'd so that they take up less space (we wait a bit of time before actually deleting from disk as we've had people come back saying, "hey wait, what happened to the server?" -_-).

0 Kudos
Contributor
Contributor

Did some quick testing and the code seems to work but if the datastore that I'm moving the decom'd servers to is full (or the decom'd VM is too big to fit in the datastore), it doesn't svMotion the VM (which is good) but it did 'fail ugly' with errors seen below...what, if anything, could be changed in the code?

 

ds-full-error.PNG

0 Kudos
Leadership
Leadership

I suspect that this error says that more than 1 datastore is returned, meaning you are dealing with an array.
And hence the references to the properties fail.

Does the Get-Datastore cmdlet with the name you specified actually return only 1 datastore?


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

0 Kudos
Contributor
Contributor

You are definitely correct; so in the script I have 3 vCenters and all the actions mapped out so far. 

On one of the vCs, when I have it dump the $ds out (Write-Host "$ds") it displays just one DS (which is good! heh) and it does svMotion the test VM to the correct DS (I need to test the script when the DS is full and see what happens though).

 

The other vC that I've been testing with is the one that gives errors (as seen in my previous post with the picture).  When I do the Write-Host "$ds" in that one, it displays the same datastore twice; not sure how that is happening when I copied and pasted the section of code.  😐  

Note that in this vC, no svMotions work (doesn't matter if destination DS is close to full or has plenty of free space) due to the double datastore entries.

0 Kudos
Leadership
Leadership

Are you sure your are only connected to 1 vCenter?

Check what $global:defaultVIServers says


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

0 Kudos
Contributor
Contributor

I do see that I'm connected to all of the vCenters that are in the script (9 total currently), but I don't understand how/why it's duplicating the datastore name on only one of the vCenters. 

 

This is what I have currently:

switch(([uri]$vm.ExtensionData.Client.ServiceUrl).Host){

'VC-1' {
Write-Host -ForegroundColor Yellow "$($vm.Name) is on $(([uri]$vm.ExtensionData.Client.ServiceUrl).Host)"
Write-Host -ForegroundColor Green "$global:defaultVIServers"
$dsname = 'specific_datastore-name1'
$ds = Get-Datastore -Name $dsname
$decomname = "~DelAfter-$a-$vm-$sctask-$initials"
if($vm.Guest.State -eq "Running"){
Write-Host -ForegroundColor Yellow "----------------------------------"
Write-Host -ForegroundColor Yellow "$vm2decom is still powered on! Exiting script!"
Write-Host -ForegroundColor Yellow "Please verify that you're decommissioning the correct server and it's powered off first before running this script!"
Write-Host -ForegroundColor Yellow "----------------------------------"
exit
}
else{
Write-Host -ForegroundColor Green "$ds"
Get-Networkadapter -VM $vm | Set-Networkadapter -StartConnected:$false -Confirm:$false | Out-Null
Move-VM -VM $vm -InventoryLocation DecomHoldingCell | Out-Null
Get-VM -Name $vm | Set-VM -Name $decomname -Confirm:$false | Out-Null

$vmview = Get-VM -Name $decomname|Get-View

#calculating size of VM in GB
$vmSpace = $vmview.ExtensionData.Summary.Storage.Committed/1GB

#calculating size free space of DS
$dsSpace = (Get-Datastore -Name $ds).FreeSpaceGB
if ($vmSpace -lt ($dsSpace * 0.8))
{
Move-VM -VM $decomname -Datastore $ds -DiskStorageFormat 'Thin' | Out-Null
}
else{
Write-Host -ForegroundColor Yellow "----------------------------"
Write-Host -ForegroundColor Yellow "$ds does not have enough free space for this VM!"
Write-Host -ForegroundColor Yellow "Make room on the datastore or svMotion it manually later."
Write-Host -ForegroundColor Yellow "----------------------------"
}
}
}

 

I have that code copied/pasted two more times for VC-2 and VC-3, the $global:defaultVIServers shows the same 9 vCenters in all 3 of the switches (for VC-2 & VC-3); however, only in VC-1 does it duplicate the datastore name.

 

I even just copied and pasted from one of the VC switches that is working properly and changed what needed to be changed (vC name and datastore name) to see if that may work, but to no avail.

0 Kudos
Leadership
Leadership

Are there perhaps 2 datastores that have simular names, and that are both returned?

For example, when you have datastores DS1 and DS11, the following will return both of them

$dsname = 'DS1'
Get-Datastore -Name $dsname

 


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

0 Kudos
Contributor
Contributor

Well, I verified that there aren't two datastores that show up if I search for part of the name (the DS' name starts with "Clone").

 

So with that train of thought, I changed that vC's datastore from the "Clone" one to a different one just for kicks...and it works.  No duplicates.  -_-

 

Therefore, I think I'll rename the datastore...maybe something in vCenter's inventory has it duplicated somewhere?

0 Kudos
Contributor
Contributor

Ah, you know what...I realized what the issue is after looking in some of the other vCenter environments...

 

I forgot that this Clone DS that was being duplicated is in another vCenter as well.  It was our 'swing' datastore to move between vCs...so that explains why it is showing up twice. XD

 

Now to solve the problem...heh

0 Kudos
Leadership
Leadership

Add the Server parameter on the Get-Datastore cmdlet, then you can explicitly say on which vCenter too look


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

0 Kudos
Contributor
Contributor

That would have worked but I renamed the Clone DS that was in the 2nd vCenter to something else (easier to see in the GUI this way anyways).  🙂

 

One last question: so I was experimenting with making the Clone DS 'full' (at/near or over the 20% threshold that we like) and then running the script to see if the script would stop the svMotion and throw the "hey man, the DS doesn't have enough free space for this VM" message.

So a minute ago, I added some thick provisioned disks to a couple of the test VMs that were in the Clone DS; the Clone DS read (in the GUI) that it only had 2.5 TBs free (below the 20% threshold; the DS is 16 TBs total, so we don't want the free space to go below 3.2 TBs). 

I ran the script on a test VM that was ~450 GBs in size (this VM was already thin'd provisioned so it was actually using ~450 GBs of space) and it still proceeded with the svMotion.

Thoughts?

0 Kudos
Leadership
Leadership

That Summary.Storage.Committed property is not updated immediately, but on a schedule.
I don't know the length of the interval, but it will eventually refresh


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

0 Kudos
Contributor
Contributor

I think I'm getting close to the answer; this is what I have so far:

 

$vmview = Get-VM -Name $decomname|Get-View

#calculating size of VM in GB
$vmSpace = $vmview.ExtensionData.Summary.Storage.Committed/1GB

#calculating size free space of DS
$dsSpace = (Get-Datastore -Name $ds).FreeSpaceGB
$workfree = $dsSpace - 3200
Write-Host -ForegroundColor Green "$workfree"
Write-Host -ForegroundColor DarkGreen "$vmSpace"
if ($vmSpace -ge ($workfree))

 

In the environment, the DS I would move decom'd VMs to is 16 TBs and always want to keep ~ 3.2 TBs free (hence why I take the free space total of the DS and subtract 3200 from it); then I want to compare the vmSpace number and the workfree number and if the vmSpace number is greater or equal to the workfree, then do not svMotion it; otherwise, svMotion it.

 

However, when I'm printing out (Write-Host) the value of $vmSpace, it shows 0.

 

Am I going about this the wrong [long] way or something else?

0 Kudos
Leadership
Leadership

The error is that you're using EXtensionData while you already did Get-View.
That should be $vmview.Summary.Storage.Committed


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

0 Kudos
Contributor
Contributor

That did it; hooray!  😄

 

Thank you very much!

0 Kudos