Configure ISCSI Target & Initiator In Linux



                        
     
    Installing iSCSI Target
Open terminal and use yum command to search for the package name which need to get install for iscsi target.
# yum search iscsi
 
Sample Output
========================== N/S matched: iscsi =======================
iscsi-initiator-utils.x86_64 : iSCSI daemon and utility programs
iscsi-initiator-utils-devel.x86_64 : Development files for iscsi-initiator-utils
lsscsi.x86_64 : List SCSI devices (or hosts) and associated information
scsi-target-utils.x86_64 : The SCSI target daemon and utility programs

We got the search result as above, choose the Target package and install to play around.

# yum install scsi-target-utils -y

List the installed package to know the default config, service, and man page location.
# rpm -ql scsi-target-utils.x86_64


Let’s start the iSCSI Service, and check the status of Service up and running, iSCSI service named as tgtd.

# /etc/init.d/tgtd start
# /etc/init.d/tgtd status

Now we need to configure it to start Automatically while system start-up.
# chkconfig tgtd on

Next, verify that the run level configured correctly for the tgtd service.

# chkconfig --list tgtd

Let’s use tgtadm to list what targets and LUNS we currently got configured in our Server.

# tgtadm --mode target --op show

The tgtd installed up and running, but there is no Output from the above command because we have not yet defined the LUNs in Target Server. For manual page, Run ‘man‘ command.

Finally we need to add iptables rules for iSCSI if there is iptables deployed in your target Server. First, find the Port number of iscsi target using following netstat command, The target always listens on TCP port 3260.

# netstat -tulnp | grep tgtd
Next add the following rules to allow iptables to Broadcast the iSCSI target discovery.

# iptables -A INPUT -i eth0 -p tcp --dport 860 -m state --state NEW,ESTABLISHED -j ACCEPT
# iptables -A INPUT -i eth0 -p tcp --dport 3260 -m state --state NEW,ESTABLISHED -j ACCEPT


Note: Rule may vary according to your Default CHAIN Policy. Then save the Iptables and restart the iptables.

# iptables-save
# /etc/init.d/iptables restart

Creating LUNs using LVM in iSCSI Target Server

First of all list the blocks by lsblk command.

Now let’s Partition the drive using fdisk command as shown below.
# fdisk  /dev/sda

Choose n to create a New Partition.

.
Command (m for help): n
Choose p to create a Primary partition

Command action
e   extended
p   primary partition (1-4)

Give a Partition number which we need to create.
Partition number (1-4): 1

As here, we are going to setup a LVM drive. So, we need to use the default settings to use full size of Drive.

First sector (2048-37748735, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-37748735, default 37748735):
Using default value 37748735


Choose the type of partition, Here we need to setup a LVM so use 8e. Use l option to see the list of type.

Command (m for help): t


Choose which partition want to change the type.

Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

After changing the type, check the changes by print (p) option to list the partition table.



Command (m for help): p
Disk /dev/sda: 19.3 GB, 19327352832 bytes
255 heads, 63 sectors/track, 2349 cylinders, total 37748736 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9fae99c8
Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    37748735    18873344   8e  Linux LVM

Write the changes using w to exit from fdisk utility, Restart the system to make changes.

 

              Creating Logical Volumes for LUNs

Now here, we going to create Physical volume using using ‘pvcreate’ command.
# pvcreate /dev/sda1

Create a Volume group with name of iSCSI to identify the group.
# vgcreate vg_iscsi /dev/sda1

Here I’m defining 4 Logical Volumes, if so there will be 4 LUNs in our iSCSI Target server.
# lvcreate -L 4G -n lv_iscsi vg_iscsi
# lvcreate -L 4G -n lv_iscsi-1 vg_iscsi
# lvcreate -L 4G -n lv_iscsi-2 vg_iscsi
# lvcreate -L 4G -n lv_iscsi-3 vg_iscsi

List the Physical volume, Volume group, logical volumes to confirm.
# pvs && vgs && lvs
# lvs

                      Define LUNs in Target Server

We have created Logical Volumes and ready to use with LUN, here we to define the LUNs in target configuration, if so only it will be available for client machines (Initiators).
Open and edit Targer configuration file located at ‘/etc/tgt/targets.conf’ with your choice of editor.
# vim /etc/tgt/targets.conf
Append the following volume definition in target conf file. Save and close the file.
1.     ISCSI qualified name (iqn.2014-07.com.tecmint:tgt1).
2.      Use whatever as your wish.
3.      Identify using target, 1st target in this Server.
4.     LVM Shared for particular LUN.

<target iqn.2017-07.com.openpath:tgt1>
backing-store /dev/vg_iscsi/lv_iscsi
</target>


Next, reload the configuration by starting tgd service as shown below.

# /etc/init.d/tgtd reload

Next verify the available LUNs using the following command.
# tgtadm --mode target --op show

                              Initiator Client Setup
1.     In Client side, we need to install the package ‘iSCSI-initiator-utils

# yum install iscsi-initiator-utils.x86_64

3. After installing the package, we need to discover the share from Target server. The client side commands little hard to remember, so we can use man page to get the list of commands which required to run.


# man iscsiadm

4. Press SHIFT+G to Navigate to the Bottom of the man page and scroll little up to get the login example commands. We need to replace our Target servers IP address in below command Discover the Target.

# iscsiadm --mode discoverydb --type sendtargets --portal 192.168.0.200 --discover
192.168.0.200:3260,1 iqn.2017-07.com.openpath:tgt1
192.168.0.200 will be the server i.e target iscsi ip.
5. Here we got the iSCSI (iqn) qualified name from above command execution.
192.168.0.200:3260,1 iqn.2017-07.com.openpath:tgt1

# /etc/init.d/iscsi stop
# /etc/init.d/iscsi restart

After restarting service list the newly added block by lsblk command and mount on a desire mount point.

Comments