Install iscsi target(server) and iscsi initiate(client) with centos 7

Configure iSCSI Target

A storage on a network is called iSCSI Target, a Client which connects to iSCSI Target is called iSCSI Initiator.
Example Environment

1-iSCSI Target: it will provides the disk space that will be accessible over the network to the client. disk01.img and disk02.img
     + disk01.img  share disk image for pc1 to access
     + disk02.img share disk image for  pc2 to access

Install admin tools first
#yum -y install targetcli
create an disk-image under the /iscsi_disks directory and set it as a SCSI device
#mkdir /iscsi_disks 
Once installed we want to start the target service, and enable it so that it automatically starts up on system boot.
systemctl start target
systemctl enable target
enter the admin console
[root@server ~]# targetcli
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb41
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/> ls
o- / .................................... [...]
  o- backstores ......................... [...]
  | o- block ............. [Storage Objects: 0]
  | o- fileio ............ [Storage Objects: 0]
  | o- pscsi ............. [Storage Objects: 0]
  | o- ramdisk ........... [Storage Objects: 0]
  o- iscsi ....................... [Targets: 0]
  o- loopback .................... [Targets: 0]
The targetcli command offers tab completion, so if you get stuck just press tab a couple of times to view available options. These will change depending on what level of the hierarchy you are in, and just like the file system you can always go up with ‘cd ..’.

Creating a Backstore
The first thing listed are the backstores. Backstores provide different ways of storing the data locally that will be exported to an external system. The available options are block, fileio, pscsi and ramdisk. In our example we’ll be demonstrating both block and fileio options as these are quite common.

- A block backstore is simply a Linux block device such as a hard drive like /dev/sdc.
- A fileio backstore is a file on the file system that has been created with a predefined size, generally the performance of a single file is not as good as a block backstore.


/> backstores/fileio create disk01 /iscsi_disks/disk01.img 5G write_back=false
/>backstores/fileio create disk02 /iscsi_disks/disk02.img 6G write_back=false 
write_back=false mean that simply says to not use any caching which will decrease performance but will reduce possible data loss – a better option in a production environment.

If you’re using a block device rather than a file, the command would look like this. In this example we are using the disk /dev/sdc as our backstore
/> backstores/block create name=block dev=/dev/sdc
Created block storage object block using /dev/sdc.
Once complete if you issue the ‘ls’ command again and the backstores should be listed.
/> ls
o- / ...................................................... [...]
  o- backstores ........................................... [...]
  | o- block ............................... [Storage Objects: 1]
  | | o- block ....... [/dev/sdc (1.0GiB) write-thru deactivated]
  | o- fileio .............................. [Storage Objects: 1]
  | | o- disk01 .. [/iscsi_disks/disk01.img (5G) write-thru deactivated]
  | o- pscsi ............................... [Storage Objects: 0]
  | o- ramdisk ............................. [Storage Objects: 0]
  o- iscsi ......................................... [Targets: 0]
  o- loopback ...................................... [Targets: 0]
Create the iSCSI Target and Portal
Next we want to create the actual iSCSI target itself, start by moving into the iSCSI path as shown below. You don’t need to prepend the ‘cd’ command to access it, however that works as well.
/> iscsi/
Once in here, we can create the iSCSI target with a specific IQN (iqn.2016-01.com.example) and iSCSI target name (target).

/iscsi> create iqn.2016-01.com.example:target
Created target iqn.2016-01.com.example:target.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
if we want to create iqn name by default just use command /iscsi> create then enter
As we can see here, a portal has already been created. As of RHEL 7.1 once a target has been set, a default portal will also be configured which will listen on TCP port 3260 on 0.0.0.0.

Create a LUN
Next we want to make a LUN with our previously defined backstore.
First move into the target portal group (TPG) that was just created.
/iscsi> iqn.2016-01.com.example:target/tpg1/
After this we can create the LUN, specifying any backstore that we have previously created. By default a LUN created in this way will have read-write permissions applied. Here we create a LUN for both our fileio and block backstores.
/iscsi/iqn.20...e:target/tpg1> luns/ create /backstores/fileio/disk01.img
Created LUN 0.

/iscsi/iqn.20...e:target/tpg1> luns/ create /backstores/block/block
Created LUN 1.
Create an ACL
Now we need to configure an access control list (ACL) to define the initiators that are actually allowed to connect to the iSCSI target.

To do this, go to the client system that will be our iSCSI initiator and get the contents of the /etc/iscsi/initiatorname.iscsi file – you can edit this file if you want, otherwise the default is fine. This will work as long as what we configure in our ACL on the iSCSI target server is the same as the contents of this file on the iSCSI initiator client.

[root@client ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.1994-05.com.redhat:5bf95f78165
Now that we have the name of our initiator from the client system, we enter the ACL section on the server within the TPG.
/iscsi/iqn.20...e:target/tpg1> acls/
Now create the ACL by specifying the initiator name found on the client system.
/iscsi/iqn.20...get/tpg1/acls> create iqn.1994-05.com.redhat:5bf95f78165 add_mapped_luns=false
add_mapped_luns=false mean that we just create iqn name but not yet map to LUN,default is =True or we don't use add_mapped_luns,it mean map to all lun. we will map to LUN by this:
/iscsi/iqn.20...get/tpg1/acls/iqn.19...78165>create tpg_lun_or_backstore=/backstores/fileio/disk01 mapped_lun=1
That’s all of the configuration required, we can use ‘cd’ to go up to the root of the iSCSI target and then use ‘ls’ to view all of the configuration.
/iscsi/iqn.20...get/tpg1/acls> cd ../..

/iscsi/iqn.20...xample:target> ls
o- iqn.2016-01.com.example:target .......................... [TPGs: 1]
  o- tpg1 ..................................... [no-gen-acls, no-auth]
    o- acls ................................................ [ACLs: 1]
    | o- iqn.1994-05.com.redhat:5bf95f78165 ......... [Mapped LUNs: 2]
    |   o- mapped_lun0 ................... [lun0 fileio/testfile (rw)]
    |   o- mapped_lun1 ....................... [lun1 block/block (rw)]
    o- luns ................................................ [LUNs: 2]
    | o- lun0 ........................ [fileio/disk01 (/iscsi_disks/disk01.img)]
    | o- lun1 ............................... [block/block (/dev/sdc)]
    o- portals .......................................... [Portals: 1]
      o- 0.0.0.0:3260 ........................................... [OK]
Saving Changes
To save the configuration, simply exit and this will write everything to the /etc/target/saveconfig.json file as shown below.
/iscsi/iqn.20...xample:target> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
iSCSI Firewall RulesOnce complete the target server should now be listening on TCP port 3260 as shown below.
[root@server ~]# netstat -antup | grep 3260
tcp        0      0 0.0.0.0:3260            0.0.0.0:*               LISTEN      -
Optional: set UserID for authentication

go to acls/iqn_name>
/iscsi/tpt1/acls/iqn.20..434>
set auth userid=username 

Parameter userid is now 'username'.
/iscsi/tpt1/acls/iqn.20..434>set auth password=password 

Parameter password is now 'password'.
Configure iSCSI Initiator (client)
Trouble shooting:

-if iscsi client can't connect mount to iscsi server we can delete iqn-name in ACLS and then create iqn-name again.
================================
https://www.rootusers.com/how-to-configure-an-iscsi-target-and-initiator-in-linux/