09 February 2019

PowerCLI - iSCSI Configuration

iSCSI storage configuration

Create vdSwitch for iSCSI traffic. Switch will have 2 uplinks, and MTU 9000 (Jumbo frames)

$Datacentre = Get-Datacenter -Name Datacentre_01
New-VDSwitch -Name vdSwitch_iSCSI -Location $Datacentre -NumUplinkPorts 2 -MaxPorts 256 -Version "6.0.0" -Mtu 9000 | Out-Null


The iSCSI vdSwitch will have 2 iSCSI port groups associated with vlan 230. vdPG_iSCSI_1 will only use dvUplink1 and vdPG_iSCSI_2 will only use dvUplink2.

Get-VDSwitch -Name vdSwitch_iSCSI | Get-VDPortgroup | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink1 -UnusedUplinkPort dvUplink2
Get-VDSwitch -Name vdSwitch_iSCSI | New-VDPortgroup -Name vdPG_iSCSI1 -NumPorts 120 -VLanId 230 -PortBinding Ephemeral
Get-VDSwitch -Name vdSwitch_iSCSI | New-VDPortgroup -Name vdPG_iSCSI2 -NumPorts 120 -VLanId 230 -PortBinding Ephemeral
Get-VDSwitch -Name vdSwitch_iSCSI | Get-VDPortgroup -Name vdPG_iSCSI2 | Get-VDPortgroupOverridePolicy | Set-VDPortgroupOverridePolicy -UplinkTeamingOverrideAllowed $true
Get-VDSwitch -Name vdSwitch_iSCSI | Get-VDPortgroup -Name vdPG_iSCSI2 | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink2 -UnusedUplinkPort dvUplink1


Add esx host to iSCSI switch

Get-VDSwitch -Name vdSwitch_iSCSI | Add-VDSwitchVMHost -VMHost esx-01.localdomain.local
$nic2 = Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter -Physical -Name vmnic2
$nic3 = Get-VMHost -Name esx-01.localdomain.local | Get-VMHostNetworkAdapter -Physical -Name vmnic3
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic2 -Confirm:$false
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic3 -Confirm:$false


Add to iSCSI vdSwitch 2 vmkernel ports for iSCSI traffic (this has to be done for each esx host)

New-VMHostNetworkAdapter -VMHost esx-01.localdomain.local -VirtualSwitch vdSwitch_iSCSI  -PortGroup vdPG_iSCSI1 -IP 192.168.101.1 -SubnetMask 255.255.255.0 -Mtu 9000
New-VMHostNetworkAdapter -VMHost esx-01.localdomain.local -VirtualSwitch vdSwitch_iSCSI  -PortGroup vdPG_iSCSI2 -IP 192.168.101.101 -SubnetMask 255.255.255.0 -Mtu 9000


Enable software iSCSI storage adapter on esx host

Get-VMHostStorage -VMHost $HostName | Set-VMHostStorage -SoftwareIScsiEnabled $true


Add iSCSI vmkernel ports to iSCSI vdSwitch

$iSCSI1_nic = Get-VMHostNetworkAdapter -VMHost esx-01.localdomain.local | where {$_.IP -like "192.168.101.1"} | select Name
$iSCSI2_nic = Get-VMHostNetworkAdapter -VMHost esx-01.localdomain.local | where {$_.IP -like "192.168.101.101"} | select Name
$HBANumber = Get-VMHostHba -VMHost esx-01.localdomain.local -Type iSCSI | %{$_.Device}
$esxcli = Get-EsxCli -VMHost esx-01.localdomain.local
$esxcli.iscsi.networkportal.add($HBANumber,$true,$iSCSI1_nic.Name)
$esxcli.iscsi.networkportal.add($HBANumber,$true,$iSCSI2_nic.Name)


Add iSCSI target and rescan for storage

$HBANumber = Get-VMHostHba -VMHost esx-01.localdomain.local -Type iSCSI | %{$_.Device}
New-IScsiHbaTarget -IScsiHba $HBANumber -Address "192.168.101.201"
Get-VMHostStorage -VMHost esx-01.localdomain.local -RescanAllHba
Get-VMHostStorage -VMHost esx-01.localdomain.local -RescanVmfs


Additionally you may want to change iSCSI software adapter Advances Settings. Typically you will disable "DelayedAck" and i.e. for Equallogic array increase "LoginTimeout". I couldn't find any direct command to do this so I have to use Get-View. More about storage manipulation you can find here.

$HostView = Get-VMHost esx-01.localdomain.local | Get-View
$HostStorageSystemID = $HostView.configmanager.StorageSystem
$HBANumber = ($HostView.config.storagedevice.HostBusAdapter | where {$_.Model -match "iSCSI Software"}).device

$options = New-Object VMWare.Vim.HostInternetScsiHbaParamValue[] (2)
$options[0] = New-Object VMware.Vim.HostInternetScsiHbaParamValue 
$options[0].key = "DelayedAck" 
$options[0].value = $false
$options[1] = New-Object VMware.Vim.HostInternetScsiHbaParamValue 
$options[1].key = "LoginTimeout" 
$options[1].value = 60

$HostStorageSystem = Get-View -ID $HostStorageSystemID 
$HostStorageSystem.UpdateInternetScsiAdvancedOptions($HBANumber , $null, $options)


And finally I have put all commands together to make easy script to configure iSCSI on a cluster

$VIUser="administrator"
$VIPassword="password"

$DatacentreName="Datacentre_01"
$vdSwitch_iSCSI_Name="vdSwitch_iSCSI"
$vdPG_iSCSI1="vdPG_iSCSI_1"
$vdPG_iSCSI2="vdPG_iSCSI_2"
$iSCSI1_Net="192.168.101."
$iSCSI1_StartIP=1
$iSCSI1_NetMask="255.255.255.0"
$iSCSI2_Net="192.168.101."
$iSCSI2_StartIP=101
$iSCSI2_NetMask="255.255.255.0"
$iSCSI_Targets=@("192.168.101.201","192.168.101.202")
$HostNames=@("esx-01.localdomain.local","esx-02.localdomain.local")

Connect-VIServer vcentre.localdomain.local -User $VIUser -Password $VIPassword | Out-Null

$Datacentre = Get-Datacenter -Name $DatacentreName
New-VDSwitch -Name $vdSwitch_iSCSI_Name -Location $Datacentre -NumUplinkPorts 2 -MaxPorts 256 -Version "6.0.0" -Mtu 9000 | Out-Null

Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Get-VDPortgroup | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink1 -UnusedUplinkPort dvUplink2
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | New-VDPortgroup -Name $vdPG_iSCSI1 -NumPorts 120 -VLanId 230 -PortBinding Ephemeral
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | New-VDPortgroup -Name $vdPG_iSCSI2 -NumPorts 120 -VLanId 230 -PortBinding Ephemeral
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Get-VDPortgroup -Name $vdPG_iSCSI2 | Get-VDPortgroupOverridePolicy | Set-VDPortgroupOverridePolicy -UplinkTeamingOverrideAllowed $true
Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Get-VDPortgroup -Name $vdPG_iSCSI2 | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -ActiveUplinkPort dvUplink2 -UnusedUplinkPort dvUplink1

$options = New-Object VMWare.Vim.HostInternetScsiHbaParamValue[] (2)
$options[0] = New-Object VMware.Vim.HostInternetScsiHbaParamValue 
$options[0].key = "DelayedAck" 
$options[0].value = $false
$options[1] = New-Object VMware.Vim.HostInternetScsiHbaParamValue 
$options[1].key = "LoginTimeout" 
$options[1].value = 60

foreach ($HostName in $HostNames){
    $iSCSI1_IP=$iSCSI1_Net+$iSCSI1_StartIP
    $iSCSI2_IP=$iSCSI2_Net+$iSCSI2_StartIP
    Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchVMHost -VMHost $HostName
    $nic2 = Get-VMHost -Name $HostName | Get-VMHostNetworkAdapter -Physical -Name vmnic2
    $nic3 = Get-VMHost -Name $HostName | Get-VMHostNetworkAdapter -Physical -Name vmnic3
    Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic2 -Confirm:$false
    Get-VDSwitch -Name $vdSwitch_iSCSI_Name | Add-VDSwitchPhysicalNetworkAdapter -VMHostPhysicalNic $nic3 -Confirm:$false
    New-VMHostNetworkAdapter -VMHost $HostName -VirtualSwitch $vdSwitch_iSCSI_Name  -PortGroup $vdPG_iSCSI1 -IP $iSCSI1_IP -SubnetMask $iSCSI1_NetMask -Mtu 9000 | Out-Null
    $iSCSI1_nic = Get-VMHostNetworkAdapter -VMHost $HostName | where {$_.IP -like $iSCSI1_IP} | select Name
    New-VMHostNetworkAdapter -VMHost $HostName -VirtualSwitch $vdSwitch_iSCSI_Name  -PortGroup $vdPG_iSCSI2 -IP $iSCSI2_IP -SubnetMask $iSCSI2_NetMask -Mtu 9000 | Out-Null
    $iSCSI2_nic = Get-VMHostNetworkAdapter -VMHost $HostName | where {$_.IP -like $iSCSI2_IP} | select Name

    Get-VMHostStorage -VMHost $HostName | Set-VMHostStorage -SoftwareIScsiEnabled $true  | Out-Null

    $HBANumber = Get-VMHostHba -VMHost $HostName -Type iSCSI | %{$_.Device}

    $esxcli = Get-EsxCli -VMHost $HostName
    $esxcli.iscsi.networkportal.add($HBANumber,$true,$iSCSI1_nic.Name)
    $esxcli.iscsi.networkportal.add($HBANumber,$true,$iSCSI2_nic.Name)

    $HostView = Get-VMHost $HostName | Get-View
    $HostStorageSystemID = $HostView.configmanager.StorageSystem
    $HostStorageSystem = Get-View -ID $HostStorageSystemID 
    $HostStorageSystem.UpdateInternetScsiAdvancedOptions($HBANumber , $null, $options)

    foreach ($iSCSI_Target in $iSCSI_Targets){
        New-IScsiHbaTarget -IScsiHba $HBANumber -Address $iSCSI_Target
    }

    Get-VMHostStorage -VMHost $HostName -RescanAllHba
    Get-VMHostStorage -VMHost $HostName -RescanVmfs
}

Disconnect-VIServer -Confirm:$false

No comments:

Post a Comment

Popular