For the Initiator (client)
Prerequisites
- Be sure to enable the enable the
MSiSCSI
service and set its startup type to Automatic.12PS> Start-Service MSiSCSIPS> Set-Service MSiSCSI -StartupType Automatic - Ensure the iSCSI initiator outgoing & incoming rules are allowed on the firewall.
123456789101112131415# Use the Get-NetFirewallServiceFilter to filter out firewall rules belonging to the MSiSCSI server and display just those rulesPS> Get-NetFirewallServiceFilter -Service msiscsi | Get-NetFirewallRule | Select DisplayGroup,DisplayName,EnabledDisplayGroup DisplayName Enabled------------ ----------- -------iSCSI Service iSCSI Service (TCP-In) FalseiSCSI Service iSCSI Service (TCP-Out) FalsePS> Get-NetFirewallServiceFilter -Service msiscsi | Get-NetFirewallRule | Set-NetFirewallRule -Enabled TruePS> Get-NetFirewallServiceFilter -Service msiscsi | Get-NetFirewallRule | Select DisplayGroup,DisplayName,EnabledDisplayGroup DisplayName Enabled------------ ----------- -------iSCSI Service iSCSI Service (TCP-In) TrueiSCSI Service iSCSI Service (TCP-Out) True
Connecting to a Target
To connect to a target you have to connect to a target portal first. The portal lets you enumerate the targets available.
1 2 3 4 5 6 7 8 9 10 |
PS> New-IscsiTargetPortal -TargetPortalAddress WIN-DATA03 InitiatorInstanceName : InitiatorPortalAddress : IsDataDigest : False IsHeaderDigest : False TargetPortalAddress : WIN-DATA03 TargetPortalPortNumber : 3260 PSComputerName : |
After connecting, list the targets thus:
1 2 3 4 5 |
PS> Get-IscsiTarget IsConnected NodeAddress PSComputerName ----------- ----------- -------------- False iqn.1991-05.com.microsoft:win-data03... |
Do this for each portal you are aware of:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
PS> New-IscsiTargetPortal -TargetPortalAddress WIN-DATA01 InitiatorInstanceName : InitiatorPortalAddress : IsDataDigest : False IsHeaderDigest : False TargetPortalAddress : WIN-DATA01 TargetPortalPortNumber : 3260 PSComputerName : PS> Get-IscsiTarget | ft -AutoSize IsConnected NodeAddress PSComputerName ----------- ----------- -------------- False iqn.1991-05.com.microsoft:win-data03-lun-target False iqn.1991-05.com.microsoft:win-data01-lun-target |
Note that Get-IscsiTarget
now lists targets from all the portals.
To connect to a specific target, use the Connect-IscsiTarget
cmdlet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
PS> Connect-IscsiTarget -NodeAddress iqn.1991-05.com.microsoft:win-data01-lun-target AuthenticationType : NONE InitiatorInstanceName : ROOT\ISCSIPRT\0000_0 InitiatorNodeAddress : iqn.1991-05.com.microsoft:win-data02.rakhesh.local InitiatorPortalAddress : 0.0.0.0 InitiatorSideIdentifier : 400001370000 IsConnected : True IsDataDigest : False IsDiscovered : False IsHeaderDigest : False IsPersistent : False NumberOfConnections : 1 SessionIdentifier : ffffe0000310d020-4000013700000003 TargetNodeAddress : iqn.1991-05.com.microsoft:win-data01-lun-target TargetSideIdentifier : 0300 PSComputerName : |
Viewing the disks
To view the disks available, use the Get-Disk
cmdlet.
1 2 3 4 5 6 7 8 |
PS> get-disk Number Friendly Name OperationalStatus Total Size Partition Style ------ ------------- ----------------- ---------- --------------- 1 VMware, VMware Virtual S SCSI Disk De... Online 60 GB MBR 0 VMware, VMware Virtual S SCSI Disk De... Online 60 GB MBR 2 MSFT Virtual HD SCSI Disk Device Offline 60 GB MBR 3 MSFT Virtual HD SCSI Disk Device Offline 20 GB RAW |
Numbers 2 & 3 are the iSCSI disks.
As far as the OS is concerned these are regular disks. To use them: 1) Change their status to online, 2) Initialize the disk, and 3) Partition & Format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
PS> Set-Disk -Number 3 -IsOffline $false PS> Initialize-Disk -Number 3 -PartitionStyle MBR PS> New-Partition -DiskNumber 3 -Size 10GB -DriveLetter G Disk Number: 3 PartitionNumber DriveLetter Offset Size Type --------------- ----------- ------ ---- ---- 1 G 1048576 10 GB Logical PS> Format-Volume -FileSystem NTFS -DriveLetter G Confirm Are you sure you want to perform this action? Warning, all data on the volume will be lost! [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): DriveLetter FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size ----------- --------------- ---------- --------- ------------ ------------- ---- G NTFS Fixed Healthy 9.91 GB 10 GB |
If you have many disks (physical or via iSCSI) you can also use the Get-IscsiConnection
and Get-IsciSession
to select just the iSCSI connections you want, and filter these to the Get-Disk
cmdlet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
PS> Get-IscsiConnection ConnectionIdentifier : ffffe0000310d020-2 InitiatorAddress : 0.0.0.0 InitiatorPortNumber : 33987 TargetAddress : 10.50.0.25 TargetPortNumber : 3260 PSComputerName : PS> Get-IscsiSession AuthenticationType : NONE InitiatorInstanceName : ROOT\ISCSIPRT\0000_0 InitiatorNodeAddress : iqn.1991-05.com.microsoft:win-data02.rakhesh.local InitiatorPortalAddress : 0.0.0.0 InitiatorSideIdentifier : 400001370000 IsConnected : True IsDataDigest : False IsDiscovered : False IsHeaderDigest : False IsPersistent : False NumberOfConnections : 1 SessionIdentifier : ffffe0000310d020-4000013700000003 TargetNodeAddress : iqn.1991-05.com.microsoft:win-data01-lun-target TargetSideIdentifier : 0300 PSComputerName : # There's just one session above, but suppose there were many I could note the TargetSideIdentifier and use that to filter the one I want PS C:\Users\administrator> Get-IscsiSession -TargetSideIdentifier 0300 | get-disk Number Friendly Name OperationalStatus Total Size Partition Style ------ ------------- ----------------- ---------- --------------- 2 MSFT Virtual HD SCSI Disk Device Online 60 GB MBR 3 MSFT Virtual HD SCSI Disk Device Online 20 GB MBR |
Making a session persistent across reboots
By default iSCSI connections are not persistent across reboots (note the IsPersistent : False
above). To make a connection persistent there area two options:
- When connecting to the iSCSI target the first time use the
-IsPersistent
switch.123456789101112131415161718PS> Connect-IscsiTarget -NodeAddress iqn.1991-05.com.microsoft:win-data01-lun-target -IsPersistent $trueAuthenticationType : NONEInitiatorInstanceName : ROOT\ISCSIPRT\0000_0InitiatorNodeAddress : iqn.1991-05.com.microsoft:win-data02.rakhesh.localInitiatorPortalAddress : 0.0.0.0InitiatorSideIdentifier : 400001370000IsConnected : TrueIsDataDigest : FalseIsDiscovered : FalseIsHeaderDigest : FalseIsPersistent : TrueNumberOfConnections : 1SessionIdentifier : ffffe0000310d020-4000013700000003TargetNodeAddress : iqn.1991-05.com.microsoft:win-data01-lun-targetTargetSideIdentifier : 0300PSComputerName : - If you want to make an existing session persistent, pipe it to the
Register-IcsiSession
cmdlet.1PS> Get-IscsiSession -TargetSideIdentifier 0300 | Register-IscsiSession
To make a session non-persistent, pipe it to the Unregister-IscsiSession
cmdlet.
1 |
PS> Get-IscsiSession -TargetSideIdentifier 0300 | Unregister-IscsiSession |
Disconnecting a Target
If you would like to disconnect a session use the Disconnect-IscsiTarget
cmdlet.
1 2 3 4 5 6 7 8 9 |
PS> Disconnect-IscsiTarget -NodeAddress iqn.1991-05.com.microsoft:win-data01-lun-target Confirm Are you sure you want to perform this action? Performing operation '' on Target ''. [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): # confirm that it's removed PS> Get-IscsiConnection |
For the Target (server)
- Create a Target.
1234567891011121314151617181920212223PS> New-IscsiServerTarget -InitiatorIds @('IQN:iqn.1998-01.com.vmware:esxi1-0ef00195','IQN:iqn.1998-01.com.vmware:esxi2-7e8def61') -TargetName LUN1ChapUserName :ClusterGroupName :ComputerName : WIN-DATA02.rakhesh.localDescription :EnableChap : FalseEnableReverseChap : FalseEnforceIdleTimeoutDetection : TrueFirstBurstLength : 65536IdleDuration : 00:00:00InitiatorIds : {Iqn:iqn.1998-01.com.vmware:esxi1-0ef00195, Iqn:iqn.1998-01.com.vmware:esxi2-7e8def61}LastLogin :LunMappings : {}MaxBurstLength : 262144MaxReceiveDataSegmentLength : 65536ReceiveBufferCount : 10ReverseChapUserName :Sessions : {}Status : NotConnectedTargetIqn : iqn.1991-05.com.microsoft:win-data02-lun1-targetTargetName : LUN1
When creating the target it is very important to specify the initiator IDs. The cmdlet doesn’t prompt for these if you don’t mention (unlike the UI which doesn’t go ahead unless initiator IDs are specified). If the initiator IDs are missing no one can see this target.
Initiator IDs can be specified via IQNs, IP addresses, IPv6 addresses, DNS name, and MAC addresses. Notice the ‘IPN:xxx’ bit above? Replace IQN with IPAddress, IPv6Address, DNSName, and MACAddress if you are specifying initiator IDs using these, followed by the address or name. For instance:
1PS> New-IscsiServerTarget -InitiatorIds DnsName:win-data04.rakhesh.local,DnsName:win-data02.rakhesh.local,DnsName:win-data03.rakhesh.local -TargetName LUN1If there’s only one initiator ID it can be specified as it is. If there multiple, separate them with commas. There’s no need to put them inside @(…) as above – that’s just to make it explicit to the reader that the input is an array. Separating by commas will have the same effect irrespective of the @(…) notation.
It’s also worth pointing out that if multiple initiators are allowed to access a target, they will all be able to read/write to the target, but expect corruption. There are exceptions of course.
- Create an iSCSI virtual disk which will back the LUN presented by the target.
1234567891011121314151617PS> New-IscsiVirtualDisk -Path E:\WIN-DATA02-LUN.vhdx -SizeBytes 30GBClusterGroupName :ComputerName : WIN-DATA02.rakhesh.localDescription :DiskType : DynamicHostVolumeId : {16C30835-9A18-11E3-80C0-000C2940DF92}LocalMountDeviceId :OriginalPath :ParentPath :Path : E:\WIN-DATA02-LUN01.vhdxSerialNumber : E4A112E4-5382-4691-83C9-CEF3FF7CEC59Size : 32212254720SnapshotIds :Status : NotConnectedVirtualDiskIndex : 2115133286
- Create multiple virtual disks if needed.
- Map these virtual disks to the target.
123456789101112131415161718192021222324252627PS> Add-IscsiVirtualDiskTargetMapping -TargetName LUN1 -Path E:\WIN-DATA02-LUN.vhdx,E:\WIN-DATA02-LUN02.vhdxPS> Get-IscsiServerTargetChapUserName :ClusterGroupName :ComputerName : WIN-DATA01.rakhesh.localDescription :EnableChap : FalseEnableReverseChap : FalseEnforceIdleTimeoutDetection : TrueFirstBurstLength : 65536IdleDuration : 00:00:00InitiatorIds : {DnsName:win-data04.rakhesh.local, DnsName:win-data02.rakhesh.local,DnsName:win-data03.rakhesh.local}LastLogin : 2/24/2014 10:28:42 AMLunMappings : {TargetName:LUN;VHD:"E:\WIN-DATA01-LUN.vhdx";LUN:0,TargetName:LUN;VHD:"E:\WIN-DATA01-LUN2.vhdx";LUN:1}MaxBurstLength : 262144MaxReceiveDataSegmentLength : 65536ReceiveBufferCount : 10ReverseChapUserName :Sessions : {}Status : NotConnectedTargetIqn : iqn.1991-05.com.microsoft:win-data01-lun-targetTargetName : LUN
And that’s it. Initiators can now connect to the target. In the example above, since the target has two LUNs, initiators will see two disks after connecting.