ADVERTISE HERE

PowerCLI: Easy NFS datastore setup

Title: PowerCLI: Easy NFS datastore setup
Author(s): (ICT-Freak.nl) Arne Fokkema
Target Audience: Technical - Intermediate
Current Revision:
First Published: 17 January 2011
Products: VMware, vSphere
UID: XD10371

It’s a new year so let’s start with a new PowerCLI post

Punchy Text: 

It’s a new year so let’s start with a new PowerCLI post

By Arne Fokkema - ICT-Freak.nl


It’s a new year so let’s start with a new PowerCLI post. This post is inspired by the blog post of @alanrenouf: PowerCLI easy vswitch portgroup setup. I love the whole idea of taking a good working config from a vSphere host and using it on a fresh installed vSphere host to make sure it’s compliant.  In this post I will show you how to perform the same trick with NFS datastores like Alan did with the vSwitches and Portgroups.

My home lab contains two HP ml110 g5 and a simple P4 box with some hard disks to add shared storage to the lab. It’s running Debian linux and is capable of presenting iSCSI targets, NFS and SAMBA shares. I use NFS as shared storage for my vSphere lab.

On esx2.ict-freak.local I use the following NFS datastores:

Datastore

But I messed up the configuration of the other vSphere host in my lab called esx1.ict-freak.local. So I had to reset the network settings and lost all the NFS datastores:

Local_Esx1

So I needed to add the five NFS shares. This is a nice task for PowerCLI and the New-Datastore cmdlet:

New-Datastore -Nfs -VMHost 10.23.112.345 -Name Datastore -Path $remotePath -NfsHost $remoteHost

I can create this one-liner for all the NFS datastores and run it. But what I really wanted to do, is to grab all the NFS datastores from esx2.ict-freak.local and mount them on esx1.ict-freak.local.

What kind of info do we need to get from the reference host esx2.ict-freak.local. According to the help file from PowerCLI we need the following parameters:

-Name Datastore -Path $remotePath -NfsHost $remoteHost

So lets see if we can find this information on the reference host. First we start with a get-datastore:

$REFHOST = Get-VMHost "esx2.ict-freak.local"
$REFHOST | Get-Datastore | Where {$_.Type -eq "NFS"}

The output will look like this:

vSphere PowerCLI

The only thing we see in the above output is the Name of the share. So lets see what the Get-View cmdlet can show us. I use the NFS_Iso in this example:

$nfsView = $REFHOST | Get-Datastore "NFS_Iso" | Get-View

you can see the members via $nfsView | Get-Member:

vSphere PowerCLI

Hmm lots of methods and properties in here. But lets take a look at the Summary property:

vSphere PowerCLI Datastore

That’s interesting the Summary property does have the Name and the Url of the NFS datastore. This is the information we need to accomplish the task. But before we can continue we need to split the Url so we can use the IP address for the –NfsHost  parameter and the /home/nfs/iso/ for the -Path parameter. I used the following script to accomplish this task:

$REFHOST = Get-VMHost "esx2.ict-freak.local"
foreach($nfs in (Get-VMhost $REFHOST | Get-Datastore | Where {$_.type -eq "NFS"} | Get-View)){
$share = $nfs.Summary.Url
$share = $share.Replace("//","/")
$share = $share.TrimStart("netfs:/")
if($share -match "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"){
$ip = $matches[0]
}
$remotePath = $share.Trim("$ip")
$remoteHost = $ip
$shareName = $nfs.Name

Write-Host "NFS Share info: `nPath: $($remotePath) `nHost: $($remoteHost) `nShare: $($shareName) `n"
}

The foreach loop will load all the NFS datastores and will loop through them one by one. The first $share variable loads the Url vai $nfs.Summary.Url. The second $share will replace the double // with a single / and the latest $share variable removes the netfs:/ from the Url. The $ip variable will add the IP address if it’s present and accepted by the little regex query. The last tricky one was to split the IP address from the Url. I used the .Trim() method to remove the IP address from the $ip variable. Now lets run the script to see the output:

vSphere PowerCLI NFS Share

So finally we got the variables we need, so we can use the new-datastore cmdlet with all the parameters. But before we can do that we need to find a way to check if the datastore already exists on the new host (esx1.ict-freak.local). I used the trick @alanrenouf used in his PowerCLI easy vswitch portgroup setup post:

    $NEWHost | Get-Datastore | Where{$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue
If (($NEWHost | Get-Datastore | Where{$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue )-eq $null){
Write-Host "NFS mount $shareName doesn't exist on $($NEWHOST.Name)" -fore Red
Write-Host "NFS Share info: `nPath: $($remotePath) `nHost: $($remoteHost) `nShare: $($shareName) `n"

}

So this is the final script to check if the Datastore exists or not:

$REFHOST = Get-VMHost "esx2.ict-freak.local"
$NEWHost = Get-VMHost "esx1.ict-freak.local"
foreach($nfs in (Get-VMhost $REFHOST | Get-Datastore | Where {$_.type -eq "NFS"} | Get-View)){
$share = $nfs.Summary.Url
$share = $share.Replace("//","/")
$share = $share.TrimStart("netfs:/")
if($share -match "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"){
$ip = $matches[0]
}
$remotePath = $share.Trim("$ip")
$remoteHost = $ip
$shareName = $nfs.Name

$NEWHost | Get-Datastore | Where{$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue
If (($NEWHost | Get-Datastore | Where{$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue )-eq $null){
Write-Host "NFS mount $shareName doesn't exist on $($NEWHOST.Name)" -fore Red
Write-Host "NFS Share info: `nPath: $($remotePath) `nHost: $($remoteHost) `nShare: $($shareName) `n"

}
}

and this is the output:

vSphere PowerCLI NFS

The final step to accomplish my goal is to add the new-datastore cmdlet to the above script. This is the easy part of the script:

New-Datastore -Nfs -VMHost $NEWHost -Name $Sharename -Path $remotePath -NfsHost $remoteHost

The final script:

$REFHost = Get-VMHost "esx2.ict-freak.local"
$NEWHost = Get-VMHost "esx1.ict-freak.local"
foreach($nfs in (Get-VMhost $REFHost | Get-Datastore | Where {$_.type -eq "NFS"})){
$share = ($nfs | get-view).summary.url
$share = $share.Replace("//","/")
$share = $share.TrimStart("netfs:/")
if($share -match "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"){
$ip = $matches[0]
}
$remotePath = $share.Trim("$ip")
$remoteHost = $ip
$shareName = $nfs

$NEWHost | Get-Datastore | Where{$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue
If (($NEWHost | Get-Datastore | Where{$_.Name -eq $shareName -and $_.type -eq "NFS"} -ErrorAction SilentlyContinue )-eq $null){
Write-Host "Mounting NFS datastore $($Sharename)" -fore Yellow
New-Datastore -Nfs -VMHost $NEWHost -Name $Sharename -Path $remotePath -NfsHost $remoteHost | Out-Null
}
}

and its output:

vSphere PowerCLI NFS Datastore

Next stop: PowerCLI easy iSCSI setup

Spotlight:

VMware Documentation Downloader v11.08.30

Updated for vSphere 5 - A free tool for those on the move who need information FAST

vSphere 5 License Entitlement Changes

See what has changed in the license entitlement in vSphere 5?

Thin Client vs Zero Client

The differences between Thin and Zero desktop clients for VDI

Technology Exchange: