VMware Cloud Community
PorzioM
Contributor
Contributor
Jump to solution

How can I automate creating a scratch partition for a large number of hosts?

I would like some help with scripting the following steps to cerate a scratch partition on all hosts that do not have one and then reprot back to me which hosts had to have one created.

all of my ESXi hosts have a boot LUN with space, all the boot LUNs are named with the same naming conveiton i.e. "hostname-localstorage"

I'd like to come up with a script that will check each host in a cluster for a scaratch partition, if it does not have one create the folder ".locker-hostname" on local storrage and then set the scratch partition to the new location. Then report back to me which hosts already had a scratch partition and which ones it had to create a scratch parition for so I can then plan a good time to put the hosts with newly created scratch partitions into mainenance mode and reboot them.

I pasted the manual way to do this below. I'd appreciate any help anyone can give and it willbe much appreciated. I am trying to avoid doing the below 1000 times. Thanks in advance...

Note: Before proceeding, ensure that /tmp/scratch exists. If it does not exist, create it using the command #mkdir /tmp/scratch.
  1. Open a command prompt where the PowerCLI is installed.
  2. Connect to the ESXi host using the command:

    connect-viserver ESXHostnameOrIP

  3. Obtain a list of datastores reachable from this ESXi host using the command:

    Get-Datastore

  4. Mount a datastore read/write as a PSDrive using the command:

    New-PSDrive -Name "mounteddatastore" -Root \ -PSProvider VimDatastore -Datastore (Get-Datastore "DatastoreName")

  5. Access the new PSDrive using the command:

    Set-Location mounteddatastore:

  6. Create a uniquely-named directory for this ESXi host using the command:

    New-Item "DirectoryName" -ItemType directory

    For example:

    New-Item ".locker-ESXHostname" -ItemType directory

  7. Check the current value of the ScratchConfig.ConfiguredScratchLocation configuration option using the command:

    Get-VMHostAdvancedConfiguration -Name "ScratchConfig.ConfiguredScratchLocation"

  8. Change the ScratchConfig.ConfiguredScratchLocation configuration option, specifying the full path to the directory created in Step 6, using the command:

    Set-VMHostAdvancedConfiguration -Name "ScratchConfig.ConfiguredScratchLocation" -Value "/vmfs/volumes/DatastoreName/DirectoryName"

    For example:

    Set-VMHostAdvancedConfiguration  -Name "ScratchConfig.ConfiguredScratchLocation" -Value  "/vmfs/volumes/Datastore1/.locker-ESXHostname"

  9. Put the ESXi host in maintenance mode and reboot for the configuration change to take effect.

Mike P

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I use something similar to the below script.

I think you will find your steps back in there.

$configName = 'ScratchConfig.ConfiguredScratchLocation'

$mode,$warn = Get-PowerCLIConfiguration | %{$_.DefaultVIServerMode,$_.DisplayDeprecationWarnings}
Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -DisplayDeprecationWarnings:$false

Get-VMHost
| where {$_.ExtensionData.Config.Product.ProductLineId -eq "embeddedEsx"} | %{
   
Connect-VIServer -Server $_.Name -User root -Password MyPswd
   
$scratch = (Get-VMHostAdvancedConfiguration -VMHost $_)[$configName]
   
$tgtName = ".locker-" + $_.Name.Split('.')[0]
   
if($scratch -notmatch ($tgtName + "$")){
       
$ds = Get-Datastore -VMHost $_ | where {$_.Name -match "localstorage"}
       
if($ds){
           
Set-Location -Path $ds.DatastoreBrowserPath
           
New-Item $tgtName -ItemType directory
           
Set-VMHostAdvancedConfiguration -Name $configName -Value ('/vmfs/volumes/' + $ds.Name + '/' + $tgtName) -Confirm:$false
           
Set-VMHost -VMHost $_ -State 'Maintenance' -Confirm:$false
           
Restart-VMHost -VMHost $_ -Confirm:$false
           
Set-VMHost -VMHost $_ -State 'Connected' -Confirm:$false
        }
    }
   
Disconnect-VIServer -Server $_.Name
}

Set-PowerCLIConfiguration -DefaultVIServerMode $mode -DisplayDeprecationWarnings:$warn

It uses the DatastoreBrowserPath as Vitali suggested.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
9 Replies
PorzioM
Contributor
Contributor
Jump to solution

I guess i could start with a script to list each host and its current if any scratch partition location

0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi Mike,

It seems that your approach is the right one. I found this article: http://blogs.vmware.com/esxi/2011/03/ops-changes-part-5-scratch-partition.html . It explains several ways to configure the scratch partition and one of them is the PowerCLI way, which looks like yours.

Several tips about your code:

You can omit creation of PSDrive, because PowerCLI provides two automatically created drives - vmstore and vmstores. Just check them to see what they offer.

The really good thing is that Datastore objects have property DatastoreBrowserPath, which points to the root of given datastore based on vmstores drive:

PS> $ds = Get-Datastore <datastoreName>

PS> $ds.DatastoreBrowserPath

vmstores:\myHost@443\ha-datacenter\Storage1

So your steps 3, 4, 5, 6 could look like this:

PS> $ds = Get-Datastore "DatastoreName"

PS> cd $ds.DatastoreBrowserPath

PS> mkdir ".local....."

Vitali

PowerCLI Team

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I use something similar to the below script.

I think you will find your steps back in there.

$configName = 'ScratchConfig.ConfiguredScratchLocation'

$mode,$warn = Get-PowerCLIConfiguration | %{$_.DefaultVIServerMode,$_.DisplayDeprecationWarnings}
Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -DisplayDeprecationWarnings:$false

Get-VMHost
| where {$_.ExtensionData.Config.Product.ProductLineId -eq "embeddedEsx"} | %{
   
Connect-VIServer -Server $_.Name -User root -Password MyPswd
   
$scratch = (Get-VMHostAdvancedConfiguration -VMHost $_)[$configName]
   
$tgtName = ".locker-" + $_.Name.Split('.')[0]
   
if($scratch -notmatch ($tgtName + "$")){
       
$ds = Get-Datastore -VMHost $_ | where {$_.Name -match "localstorage"}
       
if($ds){
           
Set-Location -Path $ds.DatastoreBrowserPath
           
New-Item $tgtName -ItemType directory
           
Set-VMHostAdvancedConfiguration -Name $configName -Value ('/vmfs/volumes/' + $ds.Name + '/' + $tgtName) -Confirm:$false
           
Set-VMHost -VMHost $_ -State 'Maintenance' -Confirm:$false
           
Restart-VMHost -VMHost $_ -Confirm:$false
           
Set-VMHost -VMHost $_ -State 'Connected' -Confirm:$false
        }
    }
   
Disconnect-VIServer -Server $_.Name
}

Set-PowerCLIConfiguration -DefaultVIServerMode $mode -DisplayDeprecationWarnings:$warn

It uses the DatastoreBrowserPath as Vitali suggested.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Almero
Enthusiast
Enthusiast
Jump to solution

Hi Team , Why is the directory we have to create .locker-ESXHostname and not just "ESXHostname"  ?

I don't like implementing without understanding. Still a novice .

Also I found the the LET Get-VMHostAdvancedConfiguration has been phased out . So have to use get-AdvancedSetting | Set-AdvancedSettng now .

0 Kudos
esxi1979
Expert
Expert
Jump to solution

LucD

Can we use a NFS mount for this over local disk ?

If yes, Can you help modify this scrip ? idea is in case the host down in that cluster we will have shared storage anyways so this shared NAS mount say will be useful. Kindly suggest

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should work afaik.

There is KB2004978 you will have to verify if that can/will impact you.

Since the script uses the datastorename to define the scratch partition (independent of the type), there shouldn't be any changes to be made to the script.

You will have to update the following line to located the desired datastore.

$ds = Get-Datastore -VMHost $_ | where {$_.Name -match "localstorage"}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

esxi1979
Expert
Expert
Jump to solution

Thanks LucD

0 Kudos
esxi1979
Expert
Expert
Jump to solution

LucD

in above the code below needs some modifications

...

        Restart-VMHost -VMHost $_ -Confirm:$false
       

Set-VMHost -VMHost $_ -State 'Connected' -Confirm:$false

..

..

As soon as the host is restarted the next command get trigger & fails.

Kindly Suggest.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You should build in a loop that waits till the host is available again in that case


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos