Keeping your VM Swapfile and Guest OS virtual memory away from everything – Part 4

automationIn the last part for this series I will be focusing on automating the deployment process using PowerCLI and VB script combination.

 

 

 

 

 

You are probably familiar with the fact that after deploying a Windows VM from template, the hard drive letters layout is back to default.

After deploying the template I’ve configured in part 3 with its virtual memory configuration, I get the Windows yelling at me that a temporary paging file has been created.

temporary paging file has been created

The reason is that all my drive letters layout returned to default.

drive letter layout returned to default

So, my goal is to simplify the deployment process as much as possible while taking the following objectives under consideration:

  • Deploy a VM from the W2008-Profile-Template using PowerCLI script.
  • Make sure all VMDKs are in their dedicated datastores.
  • Make sure the hard drive letters layout are still in place after the deployment is finished using a VBscript (C, P, and X)
  • Get a nice message telling me the VM is good to go.
  • Make sure the VM is in compliant to its VM storage profiles.

Disclaimer: Unfortunately, VMware is not yet to release the Profile-Driven Storage PowerCLI cmdlets so the part of assigning VM storage profile to each hard disk is still done manually.

Preparations (All scripts can be found in this ISO file)

  • I have created to following VB scripts and registry key which will handle the drive letter assignment and the message box notification. Make sure you copy the file to C:\Temp in your template.

DriveLetter.vbs

[sourcecode language=”vbnet”]
‘ Script that changes drive letters
‘ Note: Do NOT use it on SYSTEM or BOOT partition drive letters !!!

set objShell = CreateObject("WScript.Shell")
objShell.Run("regedit /s C:\Temp\MsgBox.reg")

sComputer = "."
Const HKLM = &H80000002

‘ from/to
If ChangeDrvLetter("E:", "X:") Then
End If

If ChangeDrvLetter("D:", "P:") Then
End If

Function ChangeDrvLetter(sSourceDrive, sTargetDrive)
bOK = True ‘ Init value
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& sComputer & "\root\default:StdRegProv")

sKeyPath = "SYSTEM\MountedDevices"
sSrc = "\DosDevices\" & UCase(sSourceDrive)

iRC = oReg.GetBinaryValue(HKLM, sKeyPath, sSrc, sValue)

If iRC = 0 Then
sTrg = "\DosDevices\" & UCase(sTargetDrive)

iRC = oReg.SetBinaryValue(HKLM, sKeyPath, sTrg, sValue)

If iRC = 0 Then
oReg.DeleteValue HKLM, sKeyPath, sSrc
Else
bOK = False
End If
Else
bOK = False
End If
ChangeDrvLetter = bOK
End Function
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"
[/sourcecode]

MsgBox.vbs

[sourcecode language=”vbnet”]
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\Temp\DriveLetter.vbs", True
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\Temp\MsgBox.reg", True

Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
r = msgbox("Virtual Machine is ready, enjoy!")
[/sourcecode]

MsgBox.reg

[sourcecode language=”text”]
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]
"MsgBox"="C:\\Temp\\MsgBox.vbs"
[/sourcecode]

  • I’ve created a new customization specification named “Windows Server 2008 with Profiles and Scripts”. In the Run Once part I pointed the answer file to the script created earlier.

customization specification

  • For the deployment process itself I’ve created the following PowerCLI script

[sourcecode language=”powershell”]
#Creted for imallvirtual.com
#Deploy VM From template with svMotion to a dedicated datastore
#Created on: 10/04/2013

Connect-VIServer "Enter your vCenter IP\name"
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}
# Map Deployment Variables
$VMName = "Name of the VM"
$Template = "Template Name"
$ResourcePool = "Resource Pool Name (ifany)"
$NumCpu = 4
$MemoryMB = 4096
$Customization = "Name of the customization specification"
$OS_Datastore = "OS Datastore name"
$PageFile_vDISK = "Hard disk 2"
$PageFile_Datastore = "PageFile Datastore name"
$Folder = "VM folder name"

# Start the deploy and VM configurations process
New-VM -Name $VMName -OSCustomizationSpec $Customization -ResourcePool $ResourcePool -Location $Folder -Template $Template -Datastore $OS_Datastore -Confirm:$false -ea stop | Out-Null
Set-VM -VM $VMName -NumCpu $NumCpu -MemoryMB $MemoryMB -Confirm:$false

# 1. Select the VM pagefile virtual vDisk and svMotion it to the dedicated PageFile datastore
# 2. Start the VM
Get-VM -Name $VMName | Get-HardDisk | Where {$_.Name -eq $PageFile_vDISK} | `
% {Set-HardDisk -HardDisk $_ -Datastore $PageFile_Datastore -Confirm:$false}
Start-VM $VMName -Confirm:$false
[/sourcecode]

Now that we have finished with preparations, edit the PowerCLI script according to your environment variables and hit the play button.

VM Automated Deployment Process HLD

After the VM will finish its customization process you will get this cool message box and the drive letters layout will be in place.

cool message box

drive letters layout will be in place

  • Now all we have to do in order for the VM to be in compliant mode with the datastores storage profile we need to configure its VM storage profiles the same way we did in part 3 and we are done!

and we are done

3 Comments

  1. Hi Lior

    Great articles, just read them all..

    I’m not sure i follow, what is the benefit of this… I understand the OS side of moving the page file off the OS disk – But the benefit of move all(?) pagefile disks to one(or more) datastore(s) – There’s no IOPS saved, no disk space saved… and wont the problem with boot storms be even worse when all pagefiles have to be zeroed out on boot ?

    If you wanna distribute load isn’t an anti-affinity rule better ?

    • Hi Michael, thank you for your comment!
      Moving the OS pagefile VMDK to a dedicated datastore is not a way to save your IOPS but it’s a good way to reduce backups window time or storage replication into a DR site.
      The VMDK anti-affinity rules are a bit limited for this one. With VMDK anti-affinity rule you have no control on where the VM VMDK files will be placed, as long as they get separated the rule will be fulfilled.
      Unfortunately, it is not an option to create an VMDK anti-affinity rule between two SDRS clusters.

1 Trackback / Pingback

  1. VBS reboot and wait on same server | BlogoSfera

Leave a Reply