Setting up iSCSI on Ubuntu

System Requirements

  • A system running Ubuntu 20.04 for iSCSI target with 1 GB external HDD.
  • A system running fresh Ubuntu 20.04 for iSCSI initiator.
  • A static IP address 172.16.0.8 is configured on iSCSI target and 172.16.0.27 is configured on iSCSI initiator.
  • A root password is configured on both servers.

First, you will need to install Target Framework (TGT) package on the iSCSI target server. You can install it with the following command:

root@ironman7:~# apt install tgt
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ibverbs-providers libconfig-general-perl libibverbs1 librdmacm1
Suggested packages:
  tgt-rbd
The following NEW packages will be installed:
  ibverbs-providers libconfig-general-perl libibverbs1 librdmacm1 tgt
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 586 kB of archives.
After this operation, 1944 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 libibverbs1 arm64 28.0-1ubuntu1 [50.7 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 librdmacm1 arm64 28.0-1ubuntu1 [61.2 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 libconfig-general-perl all 2.63-1 [53.9 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 tgt arm64 1:1.0.79-2ubuntu1 [204 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 ibverbs-providers arm64 28.0-1ubuntu1 [217 kB]
Fetched 586 kB in 1s (610 kB/s)           
Selecting previously unselected package libibverbs1:arm64.
(Reading database ... 66786 files and directories currently installed.)
Preparing to unpack .../libibverbs1_28.0-1ubuntu1_arm64.deb ...
Unpacking libibverbs1:arm64 (28.0-1ubuntu1) ...
Selecting previously unselected package librdmacm1:arm64.
Preparing to unpack .../librdmacm1_28.0-1ubuntu1_arm64.deb ...
Unpacking librdmacm1:arm64 (28.0-1ubuntu1) ...
Selecting previously unselected package libconfig-general-perl.
Preparing to unpack .../libconfig-general-perl_2.63-1_all.deb ...
Unpacking libconfig-general-perl (2.63-1) ...
Selecting previously unselected package tgt.
Preparing to unpack .../tgt_1%3a1.0.79-2ubuntu1_arm64.deb ...
Unpacking tgt (1:1.0.79-2ubuntu1) ...
Selecting previously unselected package ibverbs-providers:arm64.
Preparing to unpack .../ibverbs-providers_28.0-1ubuntu1_arm64.deb ...
Unpacking ibverbs-providers:arm64 (28.0-1ubuntu1) ...
Setting up libibverbs1:arm64 (28.0-1ubuntu1) ...
Setting up libconfig-general-perl (2.63-1) ...
Setting up ibverbs-providers:arm64 (28.0-1ubuntu1) ...
Setting up librdmacm1:arm64 (28.0-1ubuntu1) ...
Setting up tgt (1:1.0.79-2ubuntu1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/tgt.service → /lib/systemd/system/tgt.service.
Processing triggers for systemd (245.4-4ubuntu3.4) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

You can do it by creating the configuration file inside the /etc/tgt/conf.d directory:

nano /etc/tgt/conf.d/iscsi.conf

Add the following lines:

<target iqn.2019-11.example.com:lun1>
     # Provided device as an iSCSI target
     backing-store /dev/sdb1
     initiator-address 192.168.0.102
     incominguser iscsi-user password
     outgoinguser iscsi-target secretpass
</target>

Actual

root@ironman7:~# nano /etc/tgt/conf.d/iscsi.conf
root@ironman7:~# cat /etc/tgt/conf.d/iscsi.conf
<target iqn.2019-11.example.com:lun1>
     # Provided device as an iSCSI target
     backing-store /dev/sda
     initiator-address 172.16.0.8
     incominguser iscsi-user ironmanforever
     outgoinguser iscsi-target tonystarklives
</target>
  • The first line defines the name of the LUN.
  • The second line defines the location and name of the storage device on the iSCSI Target server.
  • The third line defines the IP address of the iSCSI initiator.
  • The fourth line defines the incoming username/password.
  • The fifth line defines the username/password that the target will provide to the initiator to allow for mutual CHAP authentication to take place.

Next, restart the iSCSI service to apply the changes:

systemctl restart tgt

You should get the following output:

tgtadm --mode target --op show

You should get the following output:

Target 1: iqn.2020-07.example.com:lun1
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    LUN information:
        LUN: 0
            Type: controller
            SCSI ID: IET     00010000
            SCSI SN: beaf10
            Size: 0 MB, Block size: 1
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            SWP: No
            Thin-provisioning: No
            Backing store type: null
            Backing store path: None
            Backing store flags:
    Account information:
        iscsi-user
        iscsi-target (outgoing)
    ACL information:
	172.16.0.8

ACL Fix
https://stackoverflow.com/questions/18456410/iscsiadm-no-portals-found
root@ironman7:~# tgtadm --lld iscsi --op bind --mode target --tid 1 -I ALL

Install and Configure iSCSI Initiator

Next, go to the iSCSI initiator machine and install iSCSI initiator package with the following command:

apt install open-iscsi -y
root@ironman8:~# apt install open-iscsi
Reading package lists... Done
Building dependency tree
Reading state information... Done
open-iscsi is already the newest version (2.0.874-7.1ubuntu6.1).
open-iscsi set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Once the installation has been completed, discover the iSCSI target server to find out the shared targets using the following command:

iscsiadm -m discovery -t st -p 172.16.0.6
root@ironman8:~# iscsiadm -m discovery -t st -p 172.16.0.6
172.16.0.6:3260,1 iscsi.ironman7.starlabs.us:lun1

Next, you will need to define the name of the LUN device in initiatorname.iscsi file:

vim /etc/iscsi/initiatorname.iscsi

Add your iSCSI Target LUN name as shown below:

InitiatorName=iqn.2020-07.example.com:lun1
InitiatorName=iscsi.ironman7.starlabs.us:lun1

Save and close the file when you are finished.

Next, you will need to define the CHAP information that you have configured on iSCSI target to access the iSCSI target from the iSCSI initiator. The node configuration file will exist in the directory ‘/etc/iscsi/nodes/‘ and will have a directory per LUN available.

You can define it by editing the following file:

vim /etc/iscsi/nodes/iqn.2020-07.example.com\:lun1/192.168.1.10\,3260\,1/default
root@ironman8:~# vim /etc/iscsi/nodes/iscsi.ironman7.starlabs.us\:lun1/172.16.0.6\,3260\,1/default

Add / Modify the following lines:

node.session.auth.authmethod = CHAP
node.session.auth.username = iscsi-user
node.session.auth.password = password
node.session.auth.username_in = iscsi-target
node.session.auth.password_in = secretpass
node.startup = automatic
node.session.auth.authmethod = CHAP
node.session.auth.username = iscsi-user
node.session.auth.password = ironmanforever
node.session.auth.username_in = iscsi-target
node.session.auth.password_in = tonystarklives
node.startup = automatic

Save and close the file then restart the iSCSI initiator service with the following command:

systemctl restart open-iscsi iscsid

You can also verify the status of the service with the following command:

systemctl status open-iscsi
root@ironman8:~# systemctl status open-iscsi
● open-iscsi.service - Login to default iSCSI targets
     Loaded: loaded (/lib/systemd/system/open-iscsi.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2021-02-12 20:58:27 UTC; 8s ago
       Docs: man:iscsiadm(8)
             man:iscsid(8)
    Process: 2615 ExecStart=/sbin/iscsiadm -m node --loginall=automatic (code=exited, status=0/SUCCESS)
    Process: 2633 ExecStart=/lib/open-iscsi/activate-storage.sh (code=exited, status=0/SUCCESS)
   Main PID: 2633 (code=exited, status=0/SUCCESS)

Feb 12 20:58:26 ironman8.starlabs.us systemd[1]: Starting Login to default iSCSI targets...
Feb 12 20:58:27 ironman8.starlabs.us iscsiadm[2615]: Logging in to [iface: default, target: iscsi.ironman7.starlabs.us:lun1, portal: 172.16.0.6,3260] (multiple)
Feb 12 20:58:27 ironman8.starlabs.us iscsiadm[2615]: Login to [iface: default, target: iscsi.ironman7.starlabs.us:lun1, portal: 172.16.0.6,3260] successful.
Feb 12 20:58:27 ironman8.starlabs.us systemd[1]: Finished Login to default iSCSI targets.

You can also verify the iSCSI connection with the following command:

iscsiadm -m session -o show
root@ironman8:~# lsblk
(truncated output)
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  1.8T  0 disk
mmcblk0     179:0    0   30G  0 disk
├─mmcblk0p1 179:1    0  256M  0 part /boot/firmware
└─mmcblk0p2 179:2    0 29.8G  0 part /

First, create a file system on the shared device (sdb) with the following command:

fdisk /dev/sda
mkfs.ext4 /dev/sda1