With vSphere 6.5 recently released, a nice feature was the automated execution of the UNMAP command against thin provisioned datastores to reclaim space. This requires no user interaction, it’s fully automated for you and can free up significant amounts of space from your datastores.
There’s some good information regarding the v6.5 release on these blogs;
ESX Virtualization Vladan Seget
VirtualizationIsLife Anthony Spiteri
But you don’t necessarily have to upgrade to 6.5 to avail yourself of this cool enhancement, it’s possible, with a little effort to get it working in your existing environment, as long as you are running Esxi 5.5 or 6.0x.
DELL/EMC customers can make use of the VSI Client for vSphere Web Client which includes this functionality, amongst other cool features. Version 7 supports a range of kit including XtremIO and VPLEX, VNX etc.
PowerCLI and run via regular Windows Scheduled Tasks is my preferred method to get this done and comments inline should be clear. Tested Using PowerClI 6.5R1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<# .DESCRIPTION This script connects to an ESXi host and runs an UNMAP against each datastore to reclaim space from thin file operations. Usage: AutoUnmap.ps1 ESXiHostname Requires ESXi version >= 5.5 & PowerCLI 6.5 R1 .NOTES Version: 1.0 Author: Brett Sinclair Github: http://www.github.com/brett-sinclair Twitter: @Pragmatic_IO Website: http://www.pragmaticio.com #> Param ( [Parameter(Mandatory=$true)] $HostName ) $User = "your_username" $Password = "yourpassword" $Rest = "300" #below sets the vmfs block per iteration value. Default is 200 $blocks = "300" #ensure snap in is loaded so PS execution is possible and remove any cert warnings If ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin VMware.VimAutomation.Core } Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false #it's go time Connect-VIServer -Server $HostName -user $User -password $Password $HostEsxCli = Get-EsxCli -VMHost $HostName $DataStores = Get-Datastore | Where-Object {$_.ExtensionData.Summary.Type -eq 'VMFS' -And $_.ExtensionData.Capability.PerFileThinProvisioningSupported} | Sort-Object Name ForEach ($DStore in $DataStores) { Write-Host " ------------------------------------------------------------ " -ForegroundColor 'yellow' Write-Host " -- Starting Unmap on DataStore $DStore -- " -ForegroundColor 'yellow' Write-Host " ------------------------------------------------------------ " -ForegroundColor 'yellow' $HostEsxCli.storage.vmfs.unmap($blocks,"$DStore", $null) Write-Host " ------------------------------------------------------------ " -ForegroundColor 'green' Write-Host " -- Unmap has completed on DataStore $DStore -- " -ForegroundColor 'green' Write-Host " ------------------------------------------------------------ " -ForegroundColor 'green' Start-Sleep -s $Rest } |
Script execution in progress;
The XML to add as a scheduled task is at the bottom of this page; (change values as required)
Running the process adhoc is also very easy. Here run it against a Datastore after already connecting to a server over ssh;
1 |
esxcli storage vmfs unmap -l DataStore |
….or remotely using plink;
1 |
./plink.exe -ssh ESXiHostname -P 22 -l admin -pw pass esxcli storage vmfs unmap -l DataStore |
So until you have the go ahead (for whatever reason) schedule it and enjoy the benefits.
Run the process be executed outside of peak hours. It does tend to tax the disk subsystems.
<Scheduled Task XML > Set to run from c:\Scripts on the 1st Sunday of each Month @ 10.00 pm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?xml version="1.0" encoding="UTF-16"?> <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> <RegistrationInfo> <Date>2016-12-04T20:24:48.0114455</Date> <Author>Admin</Author> </RegistrationInfo> <Triggers> <CalendarTrigger> <StartBoundary>2016-12-04T22:30:00</StartBoundary> <Enabled>true</Enabled> <ScheduleByMonthDayOfWeek> <Weeks> <Week>1</Week> </Weeks> <DaysOfWeek> <Sunday /> </DaysOfWeek> <Months> <January /> <February /> <March /> <April /> <May /> <June /> <July /> <August /> <September /> <October /> <November /> <December /> </Months> </ScheduleByMonthDayOfWeek> </CalendarTrigger> </Triggers> <Principals> <Principal id="Author"> <UserId>ChangeMe</UserId> <LogonType>Password</LogonType> <RunLevel>LeastPrivilege</RunLevel> </Principal> </Principals> <Settings> <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> <AllowHardTerminate>true</AllowHardTerminate> <StartWhenAvailable>false</StartWhenAvailable> <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> <IdleSettings> <StopOnIdleEnd>true</StopOnIdleEnd> <RestartOnIdle>false</RestartOnIdle> </IdleSettings> <AllowStartOnDemand>true</AllowStartOnDemand> <Enabled>true</Enabled> <Hidden>false</Hidden> <RunOnlyIfIdle>false</RunOnlyIfIdle> <WakeToRun>false</WakeToRun> <ExecutionTimeLimit>PT8H</ExecutionTimeLimit> <Priority>7</Priority> </Settings> <Actions Context="Author"> <Exec> <Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command> <Arguments>c:\scripts\AutoUnmap.ps1 esxihostname</Arguments> <WorkingDirectory>c:\scripts</WorkingDirectory> </Exec> </Actions> </Task> |
thank you for the script, I was looking for something like this.
I try it but I face issue with Get-PSSnapin -Name VMware.VimAutomation.Core
I don’t have “VMware.VimAutomation.Core” how I can get it ?
I’m running the script on my client machine.