31 July 2011

Configuring iSCSI Initiators in Windows 2008

Since it's been a while since I've written up a Windows related post, I
thought I'd add one on setting up iSCSI initiators from the command line
(cli).  (It also gave me a reason to toy around in PowerShell (PS).)
Microsoft makes iSCSI initiator software available for Windows 2000,
Windows Server 2003, Windows XP, and is included natively for Windows
Server 2008 and Windows Vista.  Our host details for this are:
        HOSTs:          redmond (initiator node), sunspot (target node)
        OSes:           Windows Server 2008 Standard SP2 (redmond)
                        Solaris 10 u8 (10/09 (sunspot))
        PROMPTs:        PS-redmond C:\>   (PS on redmond)
                        cmd-redmond C:\>  (standard cli on redmond)
                        sunspot [0]       (sunspot)
        IP ADDRs:       10.0.136.25 (redmond), 10.0.136.12 (sunspot)
To start, the iSCSI initiator service (MSiSCSI) must be running, though if
this is the the first configuration of iSCSI devices, we'll likely receive
one of the two messages below indicating that it is not:
        PS-redmond C:\> iscsicli listtargets
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        The Microsoft iSCSI initiator service has not been started.
        PS-redmond C:\> iscsicli
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        Error getting node name: The interface is unknown.
Use the folllowing to configure MSiSCSI to auto-start at boot, start it,
and verify that it is running in PS:
        PS-redmond C:\> set-service msiscsi -startuptype automatic
        PS-redmond C:\> start-service msiscsi
        PS-redmond C:\> get-service msiscsi

        Status   Name               DisplayName
        ------   ----               -----------
        Running  msiscsi            Microsoft iSCSI Initiator Service
To do the same via the standard cli, use the following:
        cmd-redmond C:\>sc config msiscsi start= auto
        [SC] ChangeServiceConfig SUCCESS

        cmd-redmond C:\>sc start msiscsi

        SERVICE_NAME: msiscsi
                TYPE               : 20  WIN32_SHARE_PROCESS
                STATE              : 2  START_PENDING
                                        (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
                WIN32_EXIT_CODE    : 0  (0x0)
                SERVICE_EXIT_CODE  : 0  (0x0)
                CHECKPOINT         : 0x0
                WAIT_HINT          : 0x7d0
                PID                : 1028
                FLAGS              :

        cmd-redmond C:\>sc query msiscsi

        SERVICE_NAME: msiscsi
                TYPE               : 20  WIN32_SHARE_PROCESS
                STATE              : 4  RUNNING
                                        (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
                WIN32_EXIT_CODE    : 0  (0x0)
                SERVICE_EXIT_CODE  : 0  (0x0)
                CHECKPOINT         : 0x0
                WAIT_HINT          : 0x0

        c:\Users\Administrator>
If you have Windows Firewall enabled, update it to enable the
preconfigured iSCSI service rule:
        PS-redmond C:\> netsh advfirewall firewall set rule "iSCSI Service (TCP-Out)" new enable=yes

        Updated 1 rule(s).
        Ok.
Now we have MSiSCSI running and our firewall allows the initiator to
establish connections to our target host.  Since don't have any target
hosts configured, 'iscsicli listtargetportals' below returns nothing:
        PS-redmond C:\> iscsicli listtargetportals
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        The operation completed successfully.
Because I previously configured the target host to only allow known
intitiators to connect, we need to retrieve the iSCSI Node Name (INN)
from 'iscsicli':
        PS-redmond C:\> iscsicli
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        [iqn.1991-05.com.microsoft:redmond] Enter command or ^C to exit
        PS-redmond C:\>
As an aside, if you need to change the INN, the first "nodename" command
below will modify it (must be something unique) and the second "nodename"
command resets it back to default.  (I used the default INN for the
purpose of this write up.):
                PS C:\Users\Administrator> iscsicli nodename iqn.2011-07.com.example.redmond
                Microsoft iSCSI Initiator Version 6.0 Build 6000

                The operation completed successfully.
                PS C:\Users\Administrator> iscsicli
                Microsoft iSCSI Initiator Version 6.0 Build 6000

                [iqn.2011-07.com.example.redmond] Enter command or ^C to exit
                PS C:\Users\Administrator> iscsicli nodename *
                Microsoft iSCSI Initiator Version 6.0 Build 6000

                The operation completed successfully.
Included here for completeness of the overall process, "redmond's" INN
is added on "sunspot" as a local initiator, then further added it to the
ACL of targets "sunbeam0" and "sunbeam1".  (Configuration on the target
host will likely differ with other target host OSes and software.):
        sunspot [0] /usr/sbin/iscsitadm create initiator -n \
          iqn.1991-05.com.microsoft:redmond redmond
        sunspot [0] /usr/sbin/iscsitadm modify target -l redmond sunbeam0
        sunspot [0] /usr/sbin/iscsitadm modify target -l redmond sunbeam1
Back on "redmond", we can now add "sunspot" as a target host (portal),
verify it, and list the targets "sunspot" is presenting:
        PS-redmond C:\> iscsicli addtargetportal 10.0.136.12 3260
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        The operation completed successfully.
        PS-redmond C:\> iscsicli listtargetportals
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        Total of 1 portals are persisted:

            Address and Socket   : 10.0.136.12 3260
            Symbolic Name        :
            Initiator Name       :
            Port Number          : <Any Port>
        ++Security Flags        : 0x0
        ++Version              : 0
        ++Information Specified: 0x0
        ++Login Flags          : 0x0

        The operation completed successfully.
        PS-redmond C:\> iscsicli listtargets
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        Targets List: 
            iqn.1986-03.com.sun:02:6abaa85a-bd22-4c4a-daa5-e568f0971673.sunbeam1
            iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
        The operation completed successfully.
If nothing returns from "listtargets", try "refreshtargetportal" to
repoll the target host and subsequently run "listtargets":
                PS C:\Users\Administrator> iscsicli refreshtargetportal 10.0.136.12 3260
                Microsoft iSCSI Initiator Version 6.0 Build 6000

                The operation completed successfully.
At this point, while we are attached to the target host, we still need to
actually log into the particular targets that we want to use.  One reason
for this is to allow us to establish any authentication methods,
multipathing, etc.  Since this is a simple setup (clean connections
without auth, etc), using "qlogintarget" for initial login will use
defaults for most parameters:
        PS-redmond C:\> iscsicli qlogintarget \
          iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        Session Id is 0xffffffff83c098d4-0x4000013700000005
        Connection Id is 0xffffffff83c098d4-0x4
        The operation completed successfully.

        PS-redmond C:\> iscsicli persistentlogintarget \
          iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0 \
          T * * * * * * * * * * * * * * * 0
        Microsoft iSCSI Initiator Version 6.0 Build 6000 

        The operation completed successfully.
After initial login above, using "persistenlogintarget" we've set the
connection to be re-established upon system reboot and allow the LUNs
presented by the target to be used by the system (T).  The additional
fields to "persistentlogintarget" accept the default values (see note 1).
Below, we see the target session is established via method "SendTargets"
(established earlier by way of using "addtargetportal") and review the
output related to our existing iSCSI session.  The second "sessionlist"
output includes details regarding the specific LUNs presented to us via
this target:
        PS-redmond C:\> iscsicli targetinfo \
          iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
        Microsoft iSCSI Initiator Version 6.0 Build 6000

            Discovery Mechanisms :
        ++++ "SendTargets:*10.0.136.12 0003260 Root\ISCSIPRT\0000_0 "

        The operation completed successfully.
        PS-redmond C:\> iscsicli sessionlist /?
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        Total of 1 sessions

        Session Id             : ffffffff83c098d4-4000013700000005
        Initiator Node Name    : iqn.1991-05.com.microsoft:redmond
        Target Node Name       : (null)
        Target Name            : iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
        ISID                   : 40 00 01 37 00 00
        TSID                   : 05 00
        Number Connections     : 1

            Connections:

                Connection Id     : ffffffff83c098d4-4
                Initiator Portal  : 0.0.0.0/2752
                Target Portal     : 10.0.136.12/3260
                CID               : 01 00

        The operation completed successfully.
        PS-redmond C:\> iscsicli sessionlist
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        Total of 1 sessions

        Session Id             : ffffffff83c098d4-4000013700000005
        Initiator Node Name    : iqn.1991-05.com.microsoft:redmond
        Target Node Name       : (null)
        Target Name            : iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
        ISID                   : 40 00 01 37 00 00
        TSID                   : 05 00
        Number Connections     : 1

            Connections:

                Connection Id     : ffffffff83c098d4-4
                Initiator Portal  : 0.0.0.0/2752
                Target Portal     : 10.0.136.12/3260
                CID               : 01 00

            Devices:
                Device Type            : Disk
                Device Number          : 1
                Storage Device Type    : 7
                Partition Number       : 0
                Friendly Name          : SUN SOLARIS SCSI Disk Device
                Device Description     : Disk drive
                Reported Mappings      : Port 3, Bus 0, Target Id 0, LUN 0
                Location               : Bus Number 0, Target Id 0, LUN 0
                Initiator Name         : Root\ISCSIPRT\0000_0
                Target Name            : iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
                Device Interface Name  : \\?\scsi#disk&ven_sun&prod_solaris#1&1c121344&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
                Legacy Device Name     : \\.\PhysicalDrive1
                Device Instance        : 0x82c

                Device Type            : Disk
                Device Number          : 2
                Storage Device Type    : 7
                Partition Number       : 0
                Friendly Name          : SUN SOLARIS SCSI Disk Device
                Device Description     : Disk drive
                Reported Mappings      : Port 3, Bus 0, Target Id 0, LUN 1
                Location               : Bus Number 0, Target Id 0, LUN 1
                Initiator Name         : Root\ISCSIPRT\0000_0
                Target Name            : iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
                Device Interface Name  : \\?\scsi#disk&ven_sun&prod_solaris#1&1c121344&0&000001#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
                Legacy Device Name     : \\.\PhysicalDrive2
                Device Instance        : 0x898

                Device Type            : Disk
                Device Number          : 3
                Storage Device Type    : 7
                Partition Number       : 0
                Friendly Name          : SUN SOLARIS SCSI Disk Device
                Device Description     : Disk drive
                Reported Mappings      : Port 3, Bus 0, Target Id 0, LUN 2
                Location               : Bus Number 0, Target Id 0, LUN 2
                Initiator Name         : Root\ISCSIPRT\0000_0
                Target Name            : iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
                Device Interface Name  : \\?\scsi#disk&ven_sun&prod_solaris#1&1c121344&0&000002#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
                Legacy Device Name     : \\.\PhysicalDrive3
                Device Instance        : 0x904

        The operation completed successfully.
After reviewing the target mappings below, we set the devices to
persist across sessions (bindpersistentdevices).  This means that when
service MSiSCSI starts, it will wait for these devices to be available
(or a timeout occurs) before completing its startup.  Following this,
we verify the persistent devices:
        PS-redmond C:\> iscsicli reporttargetmappings
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        Total of 1 mappings returned
            Session Id             : ffffffff83c098d4-4000013700000005
            Target Name            : iqn.1986-03.com.sun:02:7dcf7ffc-5126-6d65-8a25-e0d52fbf97f8.sunbeam0
            Initiator              : Root\ISCSIPRT\0000_0
            Initiator Scsi Device  : \\.\Scsi3:
            Initiator Bus          : 0
            Initiator Target Id    : 0
                Target Lun: 0x0 <--> OS Lun: 0x0
                Target Lun: 0x100 <--> OS Lun: 0x1
                Target Lun: 0x200 <--> OS Lun: 0x2

        The operation completed successfully.
        PS-redmond C:\> iscsicli bindpersistentdevices
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        The operation completed successfully.
        PS-redmond C:\> iscsicli reportpersistentdevices
        Microsoft iSCSI Initiator Version 6.0 Build 6000

        Persistent Volumes
        "\\?\scsi#disk&ven_sun&prod_solaris#1&1c121344&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}"
        "\\?\scsi#disk&ven_sun&prod_solaris#1&1c121344&0&000001#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}"
        "\\?\scsi#disk&ven_sun&prod_solaris#1&1c121344&0&000002#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}"

        The operation completed successfully.
At this point, the new LUNs are exposed and available to the OS for usage.
We could turn now to "Computer Management > Storage > Disk Management" to
administrate the new LUNs, but that would mean leaving the cli.  Instead,
we'll continue by setting up a basic disk from the cli.  The following
'diskpart' session lists our new LUNs, repartitions one of them, and
assigns the new volume a drive letter (T):
        PS-redmond C:\> diskpart

        Microsoft DiskPart version 6.0.6002
        Copyright (C) 1999-2007 Microsoft Corporation.
        On computer: REDMOND

        DISKPART> list disk

          Disk ###  Status      Size     Free     Dyn  Gpt
          --------  ----------  -------  -------  ---  ---
          Disk 0    Online        14 GB      0 B
          Disk 1    Online       100 MB      0 B
          Disk 2    Online       100 MB      0 B
          Disk 3    Online       100 MB      0 B

        DISKPART> select disk 1

        Disk 1 is now the selected disk.

        DISKPART> list partition

          Partition ###  Type              Size     Offset
          -------------  ----------------  -------  -------
          Partition 1    Primary             98 MB  1024 KB

        DISKPART> select partition 1

        Partition 1 is now the selected partition.

        DISKPART> delete partition

        DiskPart successfully deleted the selected partition.

        DISKPART> create partition primary

        DiskPart succeeded in creating the specified partition.

        DISKPART> list partition

          Partition ###  Type              Size     Offset
          -------------  ----------------  -------  -------
        * Partition 1    Primary            100 MB    64 KB

        DISKPART> list volume

          Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
          ----------  ---  -----------  -----  ----------  -------  ---------  --------
          Volume 0     D   iso02        CDFS   CD-ROM        39 MB  Healthy
          Volume 1     C                NTFS   Partition     14 GB  Healthy    System
        * Volume 2                      RAW    Partition    100 MB  Healthy

        DISKPART> assign letter=t

        DiskPart successfully assigned the drive letter or mount point.

        DISKPART> exit

        Leaving DiskPart...
        PS-redmond C:\>
We can now format the "drive T:" to NTFS, give it a label of "testdrv",
and review the updates with "list volume" from 'diskpart':
        PS-redmond C:\> format t: /fs:ntfs /v:testdrv
        The type of the file system is RAW.
        The new file system is NTFS.

        WARNING, ALL DATA ON NON-REMOVABLE DISK
        DRIVE T: WILL BE LOST!
        Proceed with Format (Y/N)? y
        Formatting 99M
        Creating file system structures.
        Format complete.
            102268 KB total disk space.  
            100016 KB are available.
        PS-redmond C:\> diskpart

        Microsoft DiskPart version 6.0.6002  
        Copyright (C) 1999-2007 Microsoft Corporation.
        On computer: REDMOND

        DISKPART> list volume

          Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
          ----------  ---  -----------  -----  ----------  -------  ---------  --------
          Volume 0     D   iso02        CDFS   CD-ROM        39 MB  Healthy
          Volume 1     C                NTFS   Partition     14 GB  Healthy    System
          Volume 2     T   testdrv      NTFS   Partition    100 MB  Healthy

        DISKPART> exit

        Leaving DiskPart...
        PS-redmond C:\> 
The following simply creates a test file at "T:\testfile", validating
its existence and content:
        PS-redmond C:\> echo "test file to new volume T:" >> T:\testfile
        PS-redmond C:\> dir t:\

            Directory: t:\ 

        Mode                LastWriteTime     Length Name
        ----                -------------     ------ ----
        -a---         7/31/2011  11:09 PM         58 testfile

        PS-redmond C:\> type t:\testfile
        test file to new volume T:
        PS-redmond C:\>
Finally, on "redmond", a 'netstat' displays our current established
session to "sunspot" while on "sunspot", we can see brief IO statistics
relating to target "sunbeam0":
        PS-redmond C:\> netstat -na | select-string 3260 

          TCP    10.0.136.25:49152      10.0.136.12:3260       ESTABLISHED

        PS-redmond C:\>

        sunspot [0] iscsitadm show stats
                                operations    bandwidth
        device                 read  write   read  write
        --------------------  -----  -----  -----  -----
        sunbeam1                  0      0     0K     0K
        sunbeam0                425     1K   2.9M   110M



Note 1: Attributes for "persistentlogintarget", "logintarget", etc:

        iscsicli persistentlogintarget target_iqn T * * * * * * * * * * * * * * * 0
                                                  0 1 2 3 4 5 6 7 8 9 a b c d e f g

        0:      ReportToPNP
        1:      TargetPortalAddress
        2:      TargetPortalSocket
        3:      InitiatorInstance
        4:      Port Number
        5:      Security Flags
        6:      Login Flags
        7:      Header Digest
        8:      Data Digest
        9       Max Connections
        a:      DefaultTime2Wait
        b:      DefaultTime2Retain
        c:      Username
        d:      Password
        e:      AuthType
        f:      Key
        g:      Mapping Count


see also:

    Microsoft iSCSI Software Initiator Version 2.08
    Microsoft iSCSI Initiator User Guide
    Configuring iSCSI Initiators in Solaris
    Configuring iSCSI Initiators in Linux
    Configuring iSCSI Targets in Solaris