Creating iSCSI luns in Windows Server using Powershell

This tutorial shows how to create iSCSI luns in Windows Server using Powershell. It is based on Windows Server 2016, but it will probably also work for Windows Server 2012 (R2) and Windows Server 2019.

A virtual machine that has Windows Server 2016 Server Standard edition (without Desktop Experience) installed is used in this tutorial. It has two network cards, one for normal LAN traffic (such as management and domain network traffic) and one for iSCSI network traffic. Each network card is connected to a different (virtual) switch. It has two disks, one 40 GB disk for the System and Boot partitions and a second 80 GB disk for the iSCSI virtual disks. The server has joined a domain named lab01.local. Therefore its name is ‘strg01.lab01.local’.

All Powershell commands will be used from a management server. There is no need to logon to the iSCSI server using the console or an RDP  session.

Logon to the management server. For this tutorial a Domain Administrator account has been used. Open a Powershell session and set the iSCSI servername in a variable. If you would create a script that you can use to quickly deploy an iSCSI server, you only have to change the variable in the script once or use it as an input for the script.

$Server = "strg01.lab01.local"

Setup the iSCSI network adapter

The second network adapter is used for iSCSI traffic. It will be configured using an ip-address from a different subnet than the network adapter that is used for ‘normal’ network traffic. The iSCSI network in this tutorial uses the subnet 10.0.0.0/24.

Use the following command to display the network adapters on the iSCSI server.

Invoke-Command ($Server) {Get-NetIPAddress}

Because there is no DHCP server on the iSCSI network, the iSCSI network adapter is the adapter that has an APIPA address. It has an InterfaceAlias named ‘Ethernet1’.

Set the ip-address to ‘10.0.0.11’ or any other available ip-address with a prefixlength of 24 (255.255.255.0). A gateway is not required because routing is not used on this network. The iSCSI server and clients are on the same subnet.

Invoke-Command ($Server) {New-NetIPAddress -InterfaceAlias Ethernet1 -IPAddress 10.0.0.11 -PrefixLength 24}

Disable DNS registration to make sure that this ip-address is not registered in DNS for the server’s hostname.

Invoke-Command ($Server) {Set-DnsClient -InterfaceAlias Ethernet1 -RegisterThisConnectionsAddress $false}

To check if the setting has applied, use the following command.

Invoke-Command ($Server) {Get-DnsClient -InterfaceAlias Ethernet1}

RegisterThisConnectionsAddress and UseSuffixWhenRegistering should have been set to $False.

Install the iSCSI target-server role

To remotely install the iSCSI target-server role, use the following command.

Install-WindowsFeature -ComputerName $Server FS-iSCSITarget-Server

Create an iSCSI target-server

On the iSCSI server-side, an iSCSI target-server is required for iSCSI clients to connect to. To create an iSCSI target-server that will be named ‘Target01’, use the following command.

New-IscsiServerTarget -ComputerName $Server -TargetName Target01

By default all ipv4 ip-addresses from all network adapters are bound to the iSCSI target-server. You can see this using the following command.

Get-IscsiTargetServerSetting -ComputerName $Server

All ip-addresses that start with a ‘+’ are bound to the iSCSI target-server. The ip-addresses that start with a ‘-‘ are not bound to the iSCSI target-server.

To disable unwanted ip-addresses (in this tutorial 192.168.20.20), use the following command.

Set-IscsiTargetServerSetting -ComputerName $Server -IP 192.168.20.20 -Enable $false

Create an iSCSI Virtual Disk

To create an iSCSI virtual disk that can be used by one or more iSCSI clients (initiators), use the following command to create a 60 GB virtual disk.

New-IscsiVirtualDisk -ComputerName $Server -Path "E:\iSCSIVirtualDisks\vDisk01.vhdx" -Size 60GB

or use this command to create a fixed virtual disk, which means that all diskspace is allocated immediately.

New-IscsiVirtualDisk -ComputerName $Server -Path "E:\iSCSIVirtualDisks\vDisk01.vhdx" -Size 60GB -UseFixed

Add the iSCSI Virtual Disk to the iSCSI target-server

To add the iSCSI virtual disk to the iSCSI target-server, use the following command.

Add-IscsiVirtualDiskTargetMapping -ComputerName $Server -TargetName target01 -Path 'E:\iSCSIVirtualDisks\vDisk01.vhdx'

If you create a new virtual disk later, you can add it to the same iSCSI target-server to make it available to the same iSCSI initiators or create a new iSCSI target-server to make it available to one or more other iSCSI initiators.

Add iSCSI InitiatorIds to the iSCSI target-server

To add the InitiatorIds of the iSCSI clients to the iSCSI target-server, use the following command. In this example we add the initiator ids as multiple ip-addresses. You would add multiple initiator ids if you would add a shared disk to a cluster of multiple servers, e.g. a Hyper-V cluster or VMware vSphere cluster.

Set-IscsiServerTarget -ComputerName $Server -TargetName Target01 -InitiatorIds IPAddress:10.0.0.64,IPAddress:10.0.0.65

Add iSCSI Virtual Disk to iSCSI clients

To add the iSCSI virtual disk to the iSCSI clients, first make sure that the Microsoft iSCSI Initiator Service has been started on the iSCSI clients. The Microsoft iSCSI Initiator Service is installed on every Windows Server 2016 server, but it’s startuptype has been set to ‘manual’. To set the startuptype to ‘automatic’ and start the service, use the following commands. In this tutorial the iSCSI virtual disk is added to two Hyper-V servers that will be part of a Hyper-V cluster later. The server’s hostnames are ‘hv04.lab01.local’ and ‘hv05.lab01.local’.

To change the service for multiple servers at once, first add the server’s hostnames to a variable that is used for the Invoke-Command cmdlet.

$ServerList = 'hv04.lab01.local','hv05.lab01.local'

Invoke-Command ($ServerList) {Set-Service –Name MSiSCSI –StartupType Automatic; start-service msiscsi}

To add the iSCSI target-server (iSCSI Target Portal) using ip-address 10.0.0.11 to the Microsoft iSCSI Initiator, use the following command.

Invoke-Command ($ServerList) {New-IscsiTargetPortal –TargetPortalAddress 10.0.0.11}

To check if the iSCSI Initiator Service can contact the iSCSI target-server, use the following command.

Invoke-Command ($ServerList) {Get-IscsiTarget}

If the iSCSI Initiator Service can contact the iSCSI target-server, you should see the iSCSI target-server’s IQN (NodeAddress). In this example the NodeAddress is ‘iqn.1991-05.com.microsoft:strg01-target01-target’. The field ‘IsConnected’ has a value ‘False’.

To connect to the iSCSI target-server, use the following command.

Invoke-Command ($ServerList) {$target = Get-IscsiTarget; Connect-IscsiTarget –NodeAddress $target.NodeAddress}

Now the field ‘IsConnected’ shows ‘True’ when you run the following command again.

Invoke-Command ($ServerList) {Get-IscsiTarget}

To show more information about the iSCSI connections, use the following command.

Invoke-Command ($ServerList) {Get-IscsiConnection}

To show information about the iSCSI sessions, use the following command.

Invoke-Command ($ServerList) {Get-IscsiSession}

The information shows that the sessions are not persistent; the field ‘IsPersistent’ has a value ‘False’. After a restart of the iSCSI Initiator Service, the sessions are lost. To make the sessions persistent, use the following command.

Invoke-Command ($ServerList) {Get-IscsiSession | Register-IscsiSession}

To check if the sessions are persistent, use the following command again.

Invoke-Command ($ServerList) {Get-IscsiSession}

Now the disk has been added to the iSCSI Initiator Service of each iSCSI client. To show information about the disk use the following command.

Invoke-Command ($ServerList) {Get-Disk}

This will show information of all disks of the iSCSI client, including the local disks. To show only information about the iSCSI disk, filter out the local disk that has DiskNumer 0.

Invoke-Command ($ServerList) {Get-Disk | where {$_.DiskNumber -eq 1}}

Because the iSCSI virtual disk is connected to two iSCSI clients, you will get the same information twice. The disk is offline (IsOffline is True) and has not been initialized (Guid is empty).

The iSCSI virtual disk is now ready and available to your iSCSI clients. It can be initialized and formatted using NTFS or ReFS. In this tutorial the iSCSI disk is connected to two iSCSI clients. You only need to initialize and format the new disk on one of the two iSCSI clients.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.