How to automate the creation of iSCSI luns on Windows Server 2012 using PowerShell

This is a two part series in which I’ll first show you how to automate the creation of iSCSI luns on Windows Server 2012 with PowerShell. In Part 2, we mount the luns as ESXi vmfs datastores using PowerCLI.

I was recently testing how the Altaro VM Backup software handles an ESXi host with over a 100+ datastores. The test case required iSCSI type datastores, so I needed a quick way to create the iSCSI luns and respective mappings. I was in no way going down the “create them manually” route, so as is often the case, PowerShell saved the day.

For this exercise I’ll be be using Windows Server 2012 set as an iSCSI server with an external JBOD mounted as a local drive. The process of creating an iSCSI lun consists of 3 steps;

  1. Create an iSCSI Virtual Disk.
  2. Create an iSCSI target.
  3. Configure the target.

These steps can be carried out as a single task using the “File and Storage Services” from Server Manager. To do so, just select “New iSCSI Virtual Disk …” from the tasks list, upper right hand corner.

Creating an iSCSI Virtual Disk in Windows Server 2012
Creating an iSCSI Virtual Disk in Windows Server 2012
As hinted at already, carrying out the task manually a hundred times over wasn’t an option I even considered. Hence, I used PowerShell to automate the 3 step task above mentioned. The number 110 is just arbitrary – use something much smaller if you intend to test this out since you’ll need to clean afterwards (which process we can script as well).

The script is as follows;

1.. Import-Module -Name iSCSITarget
2.. 1..110 | %{
3.. $i=”{0:D3}” -f $_
4.. $lun=”x:\iSCSIDisks\iSCSIDisk-“+$i+”.vhd”
5.. $targ=”iSCSITarget-“+$i
6.. Write-Host “Creating iSCSI lun no. “$i
7.. New-IscsiVirtualDisk -Path $lun -Size 1.5GB | Out-Null
8.. New-IscsiServerTarget -TargetName $targ -InitiatorId IPAddress:192.168.11.90, IPAddress:192.168.11.91, IPAddress:192.168.10.199 | Out-Null
9.. Add-IscsiVirtualDiskTargetMapping -TargetName $targ -Path $lun | Out-Null}

The code is mostly self-explanatory but I’ll go through each line nevertheless;

1. Import the iSCSITarget modules so we can use the iSCSI cmdlets.
2. PowerShell shorthand for a foreach loop, the range being 1 to 110.
3. Initialize var $i using the format operator. Each value $i takes is padded to 3 digits {001,002,…, 110} to keep things tidy.
4. $lun is initialized to the path of the .vhd (virtual disk) currently being created. It’s best to specify an absolute path.
5. $targ is initialized to hold the name of the respective iSCSI target.
6. Output to screen where we’re at!
7. Create an iSCSI virtual disk of size 1.5GB. Eg. x:\iscsidisks\iscsidisk-002.vhd.
8. Create an iSCSI target. We need to define the initiators’ IP addresses meaning those resources which can access the iSCSI luns. In this case I specified 3 IP addresses, 2 of which pertain to ESXi hosts and a 3rd assigned to a server on which the backup software software is installed.
9. Finally we create a mapping between the iSCSI virtual disk and the iSCSI target.

Note: The out-null used in lines 7-9 suppresses the output of each command to screen.

The PowerShell version used on this particular Windows Server 2012 is as follows;

PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
3     0     -1    -1 

I’m running the script using Windows PowerShell ISE. I modified the script to create 5 iSCSI virtual disks instead of 110 for demonstration purposes.

Windows PowerShell IDE

Using the aforementioned “File and Storage Services” we then make sure that everything has been created and configured appropriately.

Let's make sure the iSCSI Virtual Disks have been created

We also need to check that the iSCSI targets have also been created

... and finally check if the initiators have been set

This concludes Part 1 of this series. In an upcoming Part 2, I’ll show you how to automate the creation of datastores within a vSphere environment using PowerCLI (a PowerShell interface for VMware).

I hope you found the information in this article useful. See you next time.

PS: I’ve included a video outlining the above process and the complete PowerShell script which includes a function to delete the luns created – use with care!


Script:

#Jason – Script to quickly create iSCSI Luns on storage server

Import-Module -Name iSCSITarget
$max=5

function CreateLuns{
1..$max | %{
$i=”{0:D3}” -f $_
$lun=”X:\tempiscsi\tempiscsi-“+$i+”.vhd”
$targ=”ESX5-“+$i
New-IscsiVirtualDisk -Path $lun -Size 1.5GB | Out-Null
New-IscsiServerTarget -TargetName $targ -InitiatorId IPAddress:192.168.11.90,IPAddress:192.168.11.91,IPAddress:192.168.10.199 | Out-Null
Add-IscsiVirtualDiskTargetMapping -TargetName $targ -Path $lun | Out-Null
Write-Host “Creating lun … “$targ
}
}

function DeleteLuns{
1..$max | %{
$i=”{0:D3}” -f $_
$targ=”ESX5-“+$i
Remove-IscsiServerTarget -TargetName $targ | out-null
Write-Host “Deleting lun … “$targ
}
#Disconnect Luns
(get-iscsiservertarget | where-object {$_.LunMappings -ilike ‘TargetName:ESX5-*’}) | Remove-IscsiVirtualDiskTargetMapping | out-null
#Delete iSCSI targets
(Get-IscsiVirtualDisk | where-object {$_.Path -like ‘x:\tempiscsi\tempiscsi-*’}) | Remove-IscsiVirtualDisk | out-null
}

CreateLuns
#DeleteLuns

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s