Sunday, September 26, 2010

Zeroing, defragmenting and thin provisioning existing VM disks

Here is a procedure I used to defragment, zero out free space and thin provision hundreds of Windows VMs after a recent migration to vSphere 4.1 from 3.5. I took advantage of a host with a number of local SSD drives to perform the maintenance. The procedure was as follows:

  • Create a list of VMs (in my case all of them of type Thick)
  • Copy utility files to each VM
  • Move each VM to the host with the local SSD drives using vmotion
  • Move each VM to the SSD datastore using svmotion
  • Defrag the VM's drives
  • Zero out any free space on the VM's drives to enable the best results on the conversion to thin disks
  • Move each VM back to its original datastore

I used a couple Microsoft utilities to accomplish this:
I also used a simple batch file "cleanhd.cmd" to accept the EULA and iterate thru all hard drives:

reg add HKCU\Software\Sysinternals\SDelete /v EulaAccepted /d 1 /t REG_DWORD /f
FOR %%I IN (B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO @if exist "%%I:\System Volume Information" defrag %%I: -f -v
FOR %%I IN (B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO @if exist "%%I:\System Volume Information" sdelete -c %%I:

Here is the PowerCLI that handles the work:

$vms=Get-VM | Get-HardDisk | where-object {$_.ProvisionedSpaceGB -lt 100} | select-object -expandproperty Parent -unique | sort
Foreach($vm in $vms){
$SSDDatastore=get-datastore 'datastore-ssd'
$SSDHost=get-vmhost 'ssdhost'
$originalds=($vm | get-datastore)
copy-item c:\windows\sdelete.exe -destination (convert-path ('\\' + $vm.Name + '\' + 'admin$'))
copy-item c:\windows\cleanhd.cmd -destination (convert-path ('\\' + $vm.Name + '\' + 'admin$'))
move-vm -destination $SSDHost -vm $vm
$vm | Get-HardDisk | set-harddisk -datastore $SSDDatastore -storageformat thin
psexec \\$VM cleanhd.cmd
$vm | Get-HardDisk | set-harddisk -datastore $originalds -storageformat thin}

Finding all VMs with thick hard drives

After a recent migration to VMware 4.1 and NFS datastores, I decided to convert all the hard disks of the VMs to thin format using storage vmotion. PowerCLI has a new parent property that makes it easy to get a list of the VMs using thick disks in a single line:

Get-VM | Get-HardDisk | where-object {$_.StorageFormat -eq 'Thick'} | select-object -expandproperty Parent -unique | sort