The Lowercase w

Post info:

Using PowerCLI to Create a Local Scratch Partition

My company recently purchased some Dell Servers with ESXi installed to local SD cards. The issue we had was that the 8gb SD cards that came with the server and there wasn’t enough space on the cards for a scratch partition.  We ordered the servers with a single 300gb HD in order to get the RAID controller, so we decided to use that drive to store a scratch partition.

The first step in the process, was to convert the drive from a RAID device to NON-RAID.  I was able to do this via the iDRAC console.

  1. Login to iDRAC
  2. Expand STORAGE
  3. Click PHYSICAL DISKS
  4. Click the SETUP tab.
  5. In the drop down for the physical disk, select CHANGE TO NON-RAID.  Then click APPLY.
  6. Click to display the JOB QUEUE to view the progress.
  7. When the progress hits 100%, the device is visible to vCenter.  No Reboot is required.

Once I went and did that, I was able to mount the drive as a datastore (named: local_scratch_hostname), create the .locker folder, and then select the path to the folder for the scratch volume.  After that, a reboot was required to activate it as a swap device.

Having to do this on 16 new hosts, I decided to see how much of the process I could automate.  Turns out, I could automate everything except the iDRAC steps (didn’t really look into automating that piece).

*** The script is currently setup to create the partition on an entire cluster.  As I did this on new builds it wasn’t an issue having multiple hosts reboot at once wasn’t an issue, but if you’re doing this on a live cluster, you’ll want to switch this from an entire cluster method to a per host basis.  See the comments for how to switch it. ***

 

#################################################
#
# PowerCLI script to create local scratch volume
# Created by BLiebowitz on 9/6/2017
#
#################################################

# Select which vCenter you want to connect to
# Build array for each vCenter
	Write-host "Select which vCenter to connect to:"
	Write-Host .
	Write-Host "1. vCenter1"
	Write-Host "2. vCenter2"
	Write-Host "3. vCenter3"
	Write-Host "4. vCenter4"

	$Ivcenter = read-host "Select a vCenter Server. Enter Number."
	 
	if ($Ivcenter -match "1") {
	$vcenter = "vCenter1"
	} elseif ($Ivcenter -match "2") {
	$vcenter = "vCenter2"
	} elseif ($Ivcenter -match "3") {
	$vcenter = "vCenter3"
	} else {
	$vcenter = "vCenter4"
	}
	 
	write-host .
	Write-Host "You Picked: $($vcenter)"
	write-host .
	start-sleep -s 3

# connect to selected vCenter
	connect-viserver $vcenter

# List Clusters
	write-host .
	Write-host "Choose which Cluster where the LUN you want to remove presented:"
	write-host "(it may take a few seconds to build the list)"
	write-host .
	$ICLUSTER = get-cluster | Select Name | Sort-object Name
	$i = 1
	$ICLUSTER | %{Write-Host $i":" $_.Name; $i++}
	$HCLUSTER = Read-host "Enter the number for the host to Patch."
	$SCLUSTER = $ICLUSTER[$HCLUSTER -1].Name
	write-host "You have selected $($SCLUSTER)."
	start-sleep -s 3
	
# List hosts to select (un-comment this section to do one host at a time)
#	write-host .
#	Write-host "Choose which vSphere host that has the LUN presented."
#	write-host "(it may take a few seconds to build the list)"
#	write-host .
#	$IHOST = get-cluster $SCLUSTER | Get-VMhost | Select Name | Sort-object Name
#	$i = 1
#	$IHOST | %{Write-Host $i":" $_.Name; $i++}
#	$DSHost = Read-host "Enter the number for the host to Patch."
#	$SHOST = $IHOST[$DSHost -1].Name
#	write-host "you have selected" $SHOST"."

# Perform work on each host in the cluster (comment the foreach line below as well as the close bracket on line 92 to turn back into single host operation)
	foreach ($SHOST in (Get-cluster $SCLUSTER | Get-vmhost)) {
	
# Place host into maintenance mode
	# Place selected host into Maintenance mode
	write-host "Placing host in Maintenance Mode"
	Get-VMHost -Name $SHOST | set-vmhost -State Maintenance

# Remove domain from vmhost variable
	$THOST = ($SHOST.Name).TrimEnd(".domain.com")
	write-host "The short name is: $($THOST)"
	
# Create datastore from Local Disk
	Write-host "Creating the local_scratch_$THOST Datastore" 
        new-datastore -vmhost $SHOST -Name "local_scratch_$THOST" -Path (get-scsilun -vmhost $SHOST -Luntype Disk | Where {$_.RuntimeName -Match "vmhba0"}) -vmfs

# Get Disk Path
	$s = get-datastore local_scratch_$($THOST)
	$d = ($s.ExtensionData.info.url).TrimStart("ds://")
        Write-Host "The path to the datastore is $d"
	
# Create .locker folder
	new-psdrive -name "scratch_$thost" -Root \ -PSProvider VimDatastore -Datastore (Get-Datastore "local_scratch_$($THOST)")
	set-location scratch_$($thost):
        Write-host "Creating the .locker folder."
	New-Item ".locker" -ItemType directory -confirm:$false
	
# Set Scratch volume
        Write-Host "Creating the Scratch partition..."
	Get-VMhost $SHOST | Get-AdvancedSetting -Name "ScratchConfig.ConfiguredScratchLocation" | Set-AdvancedSetting -Value "/$($d).locker" -confirm:$false
	
# Reboot the host
        Write-Host "The host $SHOST will now reboot."
	Restart-VMhost -vmhost $SHOST -confirm:$false
	
}

Enjoy!

Ben Liebowitz, VCP, vExpert
NJ VMUG Leader

Leave a Reply

Your email address will not be published. Required fields are marked *