There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to Setup iSCSI Storage Server on Ubuntu 18.04 LTS

iSCSI stands for (Internet Small Computer System Interface) is a Storage Area Network (SAN) protocol that can be used for online storage management. It is an IP-based storage networking standard that provides block-level access to storage devices by carrying SCSI commands over a TCP/IP network.

iSCSI consists of two components iSCSI target and iSCSI initiator. iSCSI target is a service on iSCSI server that offers access to the shared storage and iSCSI initiator is an iSCSI client that connects to the target and access the shared storage.

In this tutorial, we will show you how to setup iSCSI target and iSCSI initiator on Ubuntu 18.04 server.

Requirements

  • A fresh Ubuntu 18.04 server for iSCSI target with 2 GB external HDD.
  • A fresh Ubuntu 18.04 server for iSCSI initiator.
  • A static IP address 192.168.0.103 is configured on iSCSI target and 192.168.0.102 is configured on iSCSI initiator.
  • A root password is configured on both server.

Getting Started

Before starting, you will need to update both servers with the latest version. You can update them by running the following command on both $

apt-get update -y
apt-get upgrade -y

Once both servers are updated restart them to apply the changes.

Install iSCSI Target

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

apt-get install tgt -y

Once TGT has been installed, check the status of TGT by running the following command:

systemctl status tgt

You should see the following output:

? tgt.service - (i)SCSI target daemon
   Loaded: loaded (/lib/systemd/system/tgt.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-11-08 07:40:28 UTC; 27s ago
     Docs: man:tgtd(8)
 Main PID: 2343 (tgtd)
   Status: "Starting event loop..."
    Tasks: 1
   CGroup: /system.slice/tgt.service
           ??2343 /usr/sbin/tgtd -f

Nov 08 07:40:28 ubuntu systemd[1]: Starting (i)SCSI target daemon...
Nov 08 07:40:28 ubuntu tgtd[2343]: tgtd: iser_ib_init(3431) Failed to initialize RDMA; load kernel modules?
Nov 08 07:40:28 ubuntu tgtd[2343]: tgtd: work_timer_start(146) use timer_fd based scheduler
Nov 08 07:40:28 ubuntu tgtd[2343]: tgtd: bs_init(387) use signalfd notification
Nov 08 07:40:28 ubuntu systemd[1]: Started (i)SCSI target daemon.

Once you have done, you can proceed to the next step.

Configure iSCSI Target

Next, you will need to create a LUN (Logical Unit Number) device on your iSCSI server. LUN is a backend storage devices that the initiator will connect to and use later.

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>

Save and close the file when you are finished. Then, restart the TGT service to apply the configuration changes:

systemctl restart tgt

Here's a brief explanation of each parameter:

target : This is the name of the particular target.

backing-store : This option specify the storage disk that will be used by the initiator.

initiator-address : This is the IP address of the initiator.

incominguser : This is the incoming username/password to secure the LUN.

outgoinguser : This is the outgoing username/password for mutual CHAP authentication.

After restarting the TGT service, check the iSCSI target server with the following command:

tgtadm --mode target --op show

You should see that the iSCSI target is being made available:

Target 1: iqn.2019-11.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: 
        LUN: 1
            Type: disk
            SCSI ID: IET     00010001
            SCSI SN: beaf11
            Size: 2146 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            SWP: No
            Thin-provisioning: No
            Backing store type: rdwr
            Backing store path: /dev/sdb1
            Backing store flags: 
    Account information:
        iscsi-user
        iscsi-target (outgoing)
    ACL information:
        192.168.0.102

Install and Configure iSCSI Initiator

Next, you will need to install the iSCSI initiator package on the iSCSI initiator server. You can install it with the following command:

apt-get install open-iscsi -y

Once the installation is completed, run target discovery against our iSCSI target server to find out the shared targets.

iscsiadm -m discovery -t st -p 192.168.0.103

You should see the available target in the following output:

192.168.0.103:3260,1 iqn.2019-11.example.com:lun1

The above command also generates two files with LUN information. You can see them with the following command:

ls -l /etc/iscsi/nodes/iqn.2019-11.example.com\:lun1/192.168.0.103\,3260\,1/ /etc/iscsi/send_targets/192.168.0.103,3260/

You should see the following files:

/etc/iscsi/nodes/iqn.2019-11.example.com:lun1/192.168.0.103,3260,1/:
total 4
-rw------- 1 root root 1840 Nov  8 13:17 default

/etc/iscsi/send_targets/192.168.0.103,3260/:
total 8
lrwxrwxrwx 1 root root  66 Nov  8 13:17 iqn.2019-11.example.com:lun1,192.168.0.103,3260,1,default -> /etc/iscsi/nodes/iqn.2019-11.example.com:lun1/192.168.0.103,3260,1
-rw------- 1 root root 547 Nov  8 13:17 st_config

Next, you will need to edit default file and define the CHAP information that you have configured on iSCSI target to access the iSCSI target from the iSCSI initiator.

nano /etc/iscsi/nodes/iqn.2019-11.example.com\:lun1/192.168.0.103\,3260\,1/default

Add / Change 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

Save and close the file when you are finished. Then, restart the iSCSI initiator service to apply the configuration changes:

systemctl restart open-iscsi

You should see the following output:

 * Unmounting iscsi-backed filesystems                                                                                                  [ OK ] 
 * Disconnecting iSCSI targets                                                                                                                 iscsiadm: No matching sessions found
                                                                                                                                        [ OK ]
 * Stopping iSCSI initiator service                                                                                                     [ OK ] 
 * Starting iSCSI initiator service iscsid                                                                                              [ OK ] 
 * Setting up iSCSI targets                                                                                                                    
Logging in to [iface: default, target: iqn.2019-11.example.com:lun1, portal: 192.168.0.103,3260] (multiple)
Login to [iface: default, target: iqn.2019-11.example.com:lun1, portal: 192.168.0.103,3260] successful.
                                                                                                                                        [ OK ]
 * Mounting network filesystems                                                                                                         [ OK ] 

You can now check the storage disk shared from the iSCSI target with the following command:

lsblk

You should see that the storage disk is now available to the initiator as sdb:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
??sda1   8:1    0  93.1G  0 part /
??sda2   8:2    0     1K  0 part 
??sda5   8:5    0 186.3G  0 part /home
??sda6   8:6    0 181.6G  0 part /Data
??sda7   8:7    0   4.8G  0 part [SWAP]
sdb      8:16   0     2G  0 disk 

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

tgtadm --mode conn --op show --tid 1

You should get the following output:

Session: 1
    Connection: 0
        Initiator: iqn.1993-08.org.debian:01:2e1e2383de41
        IP Address: 192.168.0.102

Next, you will need to create a filesystem on this shared device (sdb) and mount it to make this device usable.

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

fdisk /dev/sdb

You should see the following output:

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x06091fe8.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-4194303, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303):

Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Next, format this partition with the following command:

mkfs.ext4 /dev/sdb1

Next, mount this partition on the /mnt directory with the following command:

mount /dev/sdb1 /mnt

Now, you can check the mounted device with the following command:

df -h

You should see the following output:

Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G  4.0K  1.9G   1% /dev
tmpfs           384M  1.2M  383M   1% /run
/dev/sda1        92G   36G   51G  42% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none            1.9G   54M  1.9G   3% /run/shm
none            100M   48K  100M   1% /run/user
/dev/sda5       184G   96G   79G  55% /home
/dev/sda6       179G   32G  138G  19% /Data
/dev/sdb1       2.0G  3.0M  1.9G   1% /mnt

Congratulations! you have successfully installed the iSCSI target server and connect it from the iSCSI initiator. You can now use this shared iSCSI device as normal attached disk.

Share this page:

3 Comment(s)