dvSwitch scripting – Part 3 – Service Console & vmKernel
In the previous dvSwitch posts (see Part1 & Part2) I created a dvSwitch with a dvPortgroup, and we migrated some guests to this dvSwitch .
In this part I’ll show you how to create Service Console and vmKernel portgroups over the dvSwitch. Again, all the shown scripts will do a minimal configuration of the new portgroups. More advanced configurations will be discussed in future posts in the dvSwitch series.
This is the schematic view of the configuration that we currently have.
The Service Console and the vmKernel portgroups will have to be created on each ESX host that is connected to the dvSwitch, separately. Or you can create these portgroups on one ESX host and then use a Host Profile to push the configuration to the other ESX hosts.
To create these two portgroups I will use the AddServiceConsoleVirtualNic and the AddVirtualNic methods.
Upon closer examination of both methods it is obvious that they both use the same parameters. Only the name of the method changes. This allows me, like any good administrator
, to use the path of least effort and write one function to do the work and one “wrapper” functions for each of the two methods.
function Get-dvSwitch{
param($dcName,$dvSwName)
$dcNetFolder = Get-View (Get-Datacenter $dcName | Get-View).NetworkFolder
$found = $null
foreach($net in $dcNetFolder.ChildEntity){
if($net.Type -eq "VmwareDistributedVirtualSwitch"){
$temp = Get-View $net
if($temp.Name -eq $dvSwName){
$found = $temp
}
}
}
$found
}
function New-InternaldvSwvNic{
param($method, $esxName, $dvSwName, $dvPgName, $ipAddr, $ipNetmask)
$esx = Get-View -ViewType HostSystem -Filter @{"Name"=$esxName}
$hostNetSys = Get-View $esx.configManager.NetworkSystem
# Find Datacenter
$node = $esx
while($node.gettype().Name -ne "Datacenter"){
$node = Get-View $node.Parent
}
$dvSw = Get-dvSwitch $node.Name $dvSwName
# Find PortGroup
$dvSwPg = $dvSw.Portgroup | %{Get-View $_} | where {$_.Name -eq $dvPgName}
$nic = New-Object VMware.Vim.HostVirtualNicSpec
$nic.ip = New-Object VMware.Vim.HostIpConfig
$nic.ip.dhcp = $false
$nic.ip.ipAddress = $ipAddr
$nic.ip.subnetMask = $ipNetmask
$nic.distributedVirtualPort = New-Object VMware.Vim.DistributedVirtualSwitchPortConnection
$nic.distributedVirtualPort.switchUuid = $dvSw.Uuid
$nic.distributedVirtualPort.portgroupKey = $dvSwPg.Key
switch($method){
"SC"{
$hostNetSys.AddServiceConsoleVirtualNic($null, $nic)
}
"Kern"{
$hostNetSys.AddVirtualNic($null, $nic)
}
}
}
function New-dvSwConsole{
param($esxName, $dvSwName, $dvPgName, $ipAddr, $ipNetmask)
New-InternaldvSwvNic "SC" $esxName $dvSwName $dvPgName $ipAddr $ipNetmask
}
function New-dvSwvmKernel{
param($esxName, $dvSwName, $dvPgName, $ipAddr, $ipNetmask)
New-InternaldvSwvNic "Kern" $esxName $dvSwName $dvPgName $ipAddr $ipNetmask
}
$esxName = "esx41.test.local"
$dvSwitchName = "dvSw1"
$dvPortgroupName = "dvPg1"
$dvSCIp = "192.168.111.151"
$dvSCMask = "255.255.255.0"
$dvKernIp = "192.168.145.191"
$dvKernMask = "255.255.255.0"
$dvSCName = New-dvSwConsole $esxName $dvSwitchName $dvPortgroupName $dvSCIp $dvSCMask
Write-Host "Created Service Console pg:" $dvSCName
$dvSCName = New-dvSwvmKernel $esxName $dvSwitchName $dvPortgroupName $dvKernIp $dvKernMask
Write-Host "Created vmKernel pg:" $dvSCName
Annotation:
Line 1: the function Get-dvSwitch already appeared in Part 2 of the series.
Line 17: this is actual function that creates the Service Console portgroup or the vmKernel portgroup depending on the value of the $method parameter.
Line 52: wrapper function for the creation of a Service Console portgroup
Line 58: wrapper function for the creation of a vmKernel portgroup
To recap, this is the schematic view of what we have on the dvSwitch after the end of Part 3.
In the next part I’ll be looking at NIC Teaming and some of the policies that can be used on a dvSwitch and it’s portgroups.
If there are any dvSwitch-related scripting questions please let me know and I’ll try to take it up in one of the future posts in the dvSwitch series.
Thanks Luc! Worked like a charm!
Luc -
I really appreciate the stuff you have posted. It has helped a lot. One problem that I am currently struggling with is how to change the physical nics that are assigned to a distributed switch. I have the code to remove the nic. But not how to add a nic if the switch already exists. Or how to add a second nic and team them. What I was trying was:
$h = get-vmhost $ESXhost
$hview = get-view $h.id
$hostnum = $h.id
$vdsUUID = ($myvds = Get-VDS | Where-Object {$_.Name -like “vDS-Production”}).Uuid
$vdsID = (Get-VDS | Where-Object {$_.Name -like “vds-Production”}).id
$config = New-Object VMware.Vim.HostNetworkConfig
$config.proxySwitch = New-Object VMware.Vim.HostProxySwitchConfig[] (1)
$config.proxySwitch[0] = New-Object VMware.Vim.HostProxySwitchConfig
$config.proxySwitch[0].changeOperation = “edit”
$config.proxySwitch[0].uuid = $vdsUUID
$config.proxySwitch[0].spec = New-Object VMware.Vim.HostProxySwitchSpec
$config.proxySwitch[0].spec.backing = “vmnic2″
$changeMode = “modify”
$b = [regex]::split($hostnum,”-”)
$hostnet = “HostNetworkSystem-networkSystem-” + $b[2]
$_this = Get-View -Id $hostnet
$_this.UpdateNetworkConfig($config, $changeMode)
If $config.proxySwitch[0].spec.backing = $null it will delete the current nic. But when I try to hand it a string with the nic I want I get an error. I know I am not formatting spec.backing correctly — it does not want a string. But I am not sure how to point it to the vmnic (or vmnics) that I want to assign.
I am sure there is a better way to get $hostnet as well.
Thanks!!
Thanks Daniel.
The next part in the dvSwitch series will cover NIC teaming.
I will add some functions for adding and removing pNICs. Good idea btw.
Should be published in the coming days.
I suspect the backing for a pNIC should be defined like this:
…
$config = New-Object VMware.Vim.HostNetworkConfig
$config.proxySwitch = New-Object VMware.Vim.HostProxySwitchConfig
$config.proxySwitch[0] = New-Object VMware.Vim.HostProxySwitchConfig
$config.proxySwitch[0].changeOperation = “edit”
$config.proxySwitch[0].uuid = $vdsUUID
$config.proxySwitch[0].spec = New-Object VMware.Vim.HostProxySwitchSpec
$config.proxySwitch[0].spec.backing = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking
$config.proxySwitch[0].spec.backing.pnicSpec = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
$config.proxySwitch[0].spec.backing.pnicSpec[0].pnicDevice = “vmnic2″
…