Configuring iSCSI storage in Linux

The server 1 will be an iSCSI target, the server 2 - iSCSI client.

Let's see what we have on server 1:

$ cat /proc/partitions 
major minor  #blocks  name

 252        0   20971520 vda
 252        1    1048576 vda1
 252        2   19921920 vda2
   8        0    1048576 sda
  11        0    1048575 sr0
 253        0   17821696 dm-0
 253        1    2097152 dm-1
there is an device with name sda which I want to share through iSCSI.

The utility to do that is targetcli:
# targetcli
targetcli shell version 2.1.53
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]
backstore is the storage that you want to share and iscsi is how you are going to expose the storage on a network.

Create a blockstorage object in the targetcli withthe name block1 using /dev/sda:
/> cd backstores/block 
/backstores/block> 
/backstores/block> create dev=/dev/sda name=block1
Created block storage object block1 using /dev/sda.
We can see it:
/backstores/block> ls
o- block .................................................. [Storage Objects: 1]
  o- block1 ......................... [/dev/sda (1.0GiB) write-thru deactivated]
    o- alua ................................................... [ALUA Groups: 1]
      o- default_tg_pt_gp ....................... [ALUA state: Active/optimized]
Now we can go to iscsi:
/backstores/block> cd /iscsi 
/iscsi> create wwn=iqn.2022-01.com.example:target
Created target iqn.2022-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.
wwn is a world wide name, that is unique IQN name as they call it in iSCSI which has a very structured format.
It always starts with iqn. Next, it's the convention to use the year followed by the month and the inverse DNS name. And behind the colon, there's unique identifier (target).
We can see that it is open and listening on port 3260 on all available IP adresses.

When we type ls, we can see it has created a lot of additional objects:
/iscsi> ls
o- iscsi .......................................................... [Targets: 1]
  o- iqn.2022-01.com.example:target .................................. [TPGs: 1]
    o- tpg1 ............................................. [no-gen-acls, no-auth]
      o- acls ........................................................ [ACLs: 0]
      o- luns ........................................................ [LUNs: 0]
      o- portals .................................................. [Portals: 1]
        o- 0.0.0.0:3260 ................................................... [OK]
Go deeper to tpg1 - target portal group and a portal is a target that it is listening on specific IP address and specific port.
/iscsi> cd iqn.2022-01.com.example:target/tpg1/
/iscsi/iqn.20...e:target/tpg1> ls
o- tpg1 ................................................. [no-gen-acls, no-auth]
  o- acls ............................................................ [ACLs: 0]
  o- luns ............................................................ [LUNs: 0]
  o- portals ...................................................... [Portals: 1]
    o- 0.0.0.0:3260 ....................................................... [OK]
In TPG there are 3 things need to be done: ACL is the IQN name that you are going to use on the iSCSI initialtor. So let's go to the initiator.

So on server 2 - we need to find the iSCSI initiator name, to find it we need iSCSI software to be installed.
# yum install iscsi-initiator-utils
The result, as now we should have the directory /etc/iscsi and inside it we should have the file initiatorname.iscsi.
If you do not have it, generate a new one:
# echo "InitiatorName=$(/sbin/iscsi-iname)" > /etc/iscsi/initiatorname.iscsi
# systemctl restart iscsid
Put this name to the server 1:
/iscsi/iqn.20...e:target/tpg1> cd acls 
/iscsi/iqn.20...get/tpg1/acls> create wwn=iqn.1994-05.com.redhat:9a9a8dfbbd8
Created Node ACL for iqn.1994-05.com.redhat:9a9a8dfbbd8
Continue with LUN:
/iscsi/iqn.20...get/tpg1/acls> cd ../luns 
/iscsi/iqn.20...get/tpg1/luns> create /backstores/block/block1 
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.1994-05.com.redhat:9a9a8dfbbd8
Exit from targetctl and enable target:
systemctl enable target
systemctl status target
systemctl start target
systemctl status target
Now that the iSCSI target is listening we can go to server 2, which is the iSCSI initiator, and we can use the iscsiadm command. Type:
man iscsiadm
and copy some examples from man page and replace the IP:
# iscsiadm  --mode  discoverydb  --type sendtargets --portal 192.168.122.107 --discover
192.168.122.107:3260,1 iqn.2022-01.com.example:target
And there you can see that it has found the iSCSI target IQN and that is what I need in the next command that I'm going to run. Copy it again from the man page. Show /proc/partitions before of this:
# cat /proc/partitions
major minor  #blocks  name

 252        0   20971520 vda
 252        1    1048576 vda1
 252        2   19921920 vda2
  11        0    1048575 sr0
 253        0   17821696 dm-0
 253        1    2097152 dm-1
and login to iSCSI:
iscsiadm --mode node --targetname iqn.2022-01.com.example:target --portal 192.168.122.107:3260 --login
# iscsiadm --mode node --targetname iqn.2022-01.com.example:target --portal 192.168.122.107:3260 --login
Logging in to [iface: default, target: iqn.2022-01.com.example:target, portal: 192.168.122.107,3260]
Login to [iface: default, target: iqn.2022-01.com.example:target, portal: 192.168.122.107,3260] successful.
And there you can see that it is logging in and it was successfull.

The result of /proc/partitions:
# cat /proc/partitions 
major minor  #blocks  name

 252        0   20971520 vda
 252        1    1048576 vda1
 252        2   19921920 vda2
  11        0    1048575 sr0
 253        0   17821696 dm-0
 253        1    2097152 dm-1
   8        0    1048576 sda
We now have a disk with the name sda. And that is the iSCSI disk. And the next time the system will reboot it will automatically get iSCSI disk. and it will behave any local disk:
# lsscsi 
[0:0:0:0]    cd/dvd  QEMU     QEMU DVD-ROM     2.5+  /dev/sr0 
[6:0:0:0]    disk    LIO-ORG  block1           4.0   /dev/sda