Friday, July 13, 2012

Mounting NFS on VMware ESX with Powershell

This Friday I want to share with you a Powershell script I am using and re-using a lot of times these days to add a vMotion interface on my good ESX hosts and then mount a NFS filesystem. The code is pretty straightforward if you are used to administering VMware with Powershell. I have put a comments to make the reading more comfortable. Here's the code:
# let's load VMWare PowerCLI
Add-PSSnapIn VMware.VimAutomation.Core 
Connect-VIServer yourVCname 

# name of the ESX server to configure
$targethost = ""

$vMotionIP = ""
$vMotionGateway = ""

# name you want to give to the mounted nfs

# switch that contains the vMotion port
$vswitch = "vSwitch0" 

# vMotion network label
$vMotionName = "VMotion" 

# vMotion subnet mask
$vMotionSubnet = ""

# let's check that this IP is available
$qry = ('select statuscode from win32_pingstatus where address="' + $vMotionIP + '"')
$rslt = gwmi -query "$qry"
if ($res -eq 0) #if IP isn't free, exit
{Write-Host "vMotion IP already used, choose another";exit}

# let's configure the nfs lock behavior
$tgt=Get-VMHost $targethost
if (($tgt |Get-VMHostAdvancedConfiguration -Name "nfs.lockdisable")["NFS.LockDisable"] -ne 1)
{Write-Host "NFS.LockDisable parameter was NOT set! updating..."
$tgt | set-VMHostAdvancedConfiguration -Name "NFS.LockDisable" -Value 1
if (($tgt |Get-VMHostAdvancedConfiguration -Name "nfs.lockdisable")["NFS.LockDisable"] -ne 1){Write-Host "NFS.LockDisable parameter still NOT set! Exiting...";exit}}

# going to create the port group for vMotion
New-VMHostNetworkAdapter -VMHost $targethost -PortGroup $vMotionName -VirtualSwitch $vswitch -IP $vMotionIP -SubnetMask $vMotionSubnet -VMotionEnabled:$true

# going to set the default gateway for VMkernel vMotion
# on device vmk0 (it may differ for you
Get-VMHostNetwork -VMHost $targethost | Set-VMHostNetwork -VMKernelGateway $VMotionGateway -VMKernelGatewayDevice "vmk0" 

# going to mount the NFS store
New-datastore –nfs –vmhost $targethost –name $nfsname –path "/mnt/vol1/vol1/vol" –nfshost $nfshost
As you can see the last line is the one that actually mounts the NFS filesystem through the New-Datastore cmdlet. In my case it is an Openfiler volume shared as "/mnt/vol1/vol1/vol". If you have any question about the configuration of Openfiler to work as a NFS for VMWare do not hesitate to ask! I personally use version 2.3.
Here's a few useful links to learn from:

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...