I recently wrote a post on Automatic Space Reclamation (VMFS UNMAP) that was introduced in vSphere 6.5 and I got a lot of good response from the readers. The article can be found here.
To use Automatic Space Reclamation, you need to have a VMFS6 datastore which is available only with vSphere 6.5. So, if you have VMFS5 datastores, then you still have to do this using esxcli on the ESXi host.
While discussing with one of the readers, they wanted to if there was a script that would perform space reclamation on all datastores connected to an ESXi host?
There are plenty of scripts available online that do the job, but nobody had written a function that would do the same job.
Hence, I decided to write one and the article is titled Perform VMFS UNMAP using PowerCLI.
The function is shown below and it accepts two parameters at this time, which are the ESXi hostname and the Datastore name.
Function Perform-VMFSUnmap { [CmdletBinding()] param( [Parameter( Mandatory=$true)] [String[]]$Datastore, [String]$ESXiHost ) Set-PowerCLIConfiguration -WebOperationTimeoutSeconds -1 -Scope Session -Confirm:$false $ESXHost = Get-VMHost $ESXiHost $DatastoreName = Get-Datastore $Datastore Write-Host 'Using ESXCLI and connecting to $VMHost' -ForegroundColor Green $esxcli = Get-EsxCli -VMHost $ESXHost Write-Host 'Unmapping $Datastore on $VMHost' -ForegroundColor Green $esxcli.storage.vmfs.unmap($null,$DatastoreName,$null) }
Before you run this command, make sure that you are connected to a vCenter Server using the Connect-VIServer command.
Using the Set-PowerCLIConfiguration we are disabling the timeout as the reclamation operation can go on for hours depending on how much space can be reclaimed from the thin-provisioned LUN from the array.
The command can be used as shown below.
Perform-VMFSUnmap -ESXiHost ESXi65-A.lab.local -Datastore ISOs
As you can see from the output, the command ran successfully but it errored out in my case because the datastore created from the backend LUN is not thin-provisioned.
This is because in my lab environment. I am using OpenFiler as a Virtual Appliance to present the LUNs to the ESXi hosts. So, ideally, you should not have any problems running this in your environment when you have thin-provisioned LUNs created.
I hope this has been informative and thank you for reading!
3 Comments
Hi, Adil- will this do all the LUNs at once, or do them sequentially? I’d rather have it work one a time to prevent a drag on I/O- unless you think I”m being too careful.
Thanks.
Hello David,
This will work on one datastore at a time.
I think PowerCLI 11.3 just broke this function. Or else my install went wrong (not unusual around here).