How to configure iSCSI Target in Linux

Share on Social Media

Learn how to configure an iSCSI Target in Linux with our step-by-step guide. Set up a network storage solution to enhance your server’s storage capabilities efficiently. #centlinux #linux #iscsi

What is iSCSI Target?

iSCSI (Internet Small Computer System Interface) is a protocol used to link data storage devices over an IP network infrastructure. An iSCSI target is often a dedicated network-connected hard disk storage device, but may also be a general-purpose computer. Nearly all modern mainstream server operating systems (such as BSD, Linux, Solaris or Windows Server) can provide iSCSI target functionality, either as a built-in feature or with supplemental software. The computer providing the iSCSI targets is called iSCSI Server.

What is iSCSI Initiator?

An iSCSI initiator is an iSCSI client. An iSCSI initiator typically serves the same purpose to a computer as a SCSI bus adapter would, except that, instead of physically cabling SCSI devices (like hard drives and tape changers), an iSCSI initiator sends SCSI commands over an IP network.

In this post, we will configure iSCSI Target in Linux, and then persistently mount that iSCSI Target on a iSCSI Initiator.

Recommended Online Training: Learn Bash Shell in Linux for Beginners

745772 0021

Read Also: How to Configure an iScsi Target in Openfiler

Linux Server Specification

We have provisioned two CentOS 7 virtual machines with following specifications.

 iSCSI ServeriSCSI Initiator
Operating System:CentOS 7CentOS 7
Hostname:server1.example.comdesktop1.example.com
IP Address:192.168.116.11/24192.168.116.12/24

Configure iSCSI Target

We have an spare hard disk (20 GB) is available. we have created a partition (20GB) in it using fdisk to make it usable by iSCSI Server.

# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
Disk identifier: 0x94c52e45

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux

we need targetcli package to configure an iSCSI Target.

# yum install -y targetcli

Now obtain a targetcli shell.

# targetcli
targetcli shell version 2.1.fb34
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/>

Check the status of current iSCSI Target configurations.

/> 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]
/>

At the targetcli shell, create a Storage Block using following commands.

/> cd backstores/block/
/backstores/block> create block1 /dev/sdb1
Created block storage object block1 using /dev/sdb1.
/backstores/block> ls
o- block .................................................. [Storage Objects: 1]
  o- block1 ....................... [/dev/sdb1 (20.0GiB) write-thru deactivated]

Create a TPG (Target Portal Group).

/backstores/block> cd /iscsi
/iscsi> create iqn.2018-07.com.example.server1:remotedisk1
Created target iqn.2018-07.com.example.server1:remotedisk1.
Created TPG 1.
/iscsi> ls
o- iscsi .......................................................... [Targets: 1]
  o- iqn.2018-07.com.example.server1:remotedisk1 ..................... [TPGs: 1]
    o- tpg1 ............................................. [no-gen-acls, no-auth]
      o- acls ........................................................ [ACLs: 0]
      o- luns ........................................................ [LUNs: 0]
      o- portals .................................................. [Portals: 0]
/iscsi>

Obtain the client iqn (iSCSI Qualified Name) from the client machine and use it to create an ACL, to limit the access to our iSCSI Target by desktop1.example.com machine.

/iscsi> cd /iscsi/iqn.2018-07.com.example.server1:remotedisk1/tpg1/acls
/iscsi/iqn.20...sk1/tpg1/acls> create iqn.1994-05.com.redhat:31532b60ee10
Created Node ACL for iqn.1994-05.com.redhat:31532b60ee10
/iscsi/iqn.20...sk1/tpg1/acls> ls
o- acls .............................................................. [ACLs: 1]
  o- iqn.1994-05.com.redhat:31532b60ee10 ...................... [Mapped LUNs: 1]
    o- mapped_lun0 .................................... [lun0 block/block1 (rw)]
/iscsi/iqn.20...sk1/tpg1/acls>

Create a LUN (Logical Unit Number).

/iscsi/iqn.20...sk1/tpg1/acls> cd /iscsi/iqn.2018-07.com.example.server1:remotedisk1/tpg1/luns
/iscsi/iqn.20...sk1/tpg1/luns> create /backstores/block/block1
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.1994-05.com.redhat:31532b60ee10
/iscsi/iqn.20...sk1/tpg1/luns> ls
o- luns .............................................................. [LUNs: 1]
  o- lun0 ........................................... [block/block1 (/dev/sdb1)]
/iscsi/iqn.20...sk1/tpg1/luns>

Create a Portal now.

/iscsi/iqn.20...sk1/tpg1/luns> cd /iscsi/iqn.2018-07.com.example.server1:remotedisk1/tpg1/portals
/iscsi/iqn.20.../tpg1/portals> create 192.168.116.11
Using default IP port 3260
Created network portal 192.168.116.11:3260.
/iscsi/iqn.20.../tpg1/portals> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json

Allow the port 3260/tcp through firewall.

# firewall-cmd --permanent --add-port=3260/tcp
success
# firewall-cmd --reload
success

Start and enable the target service.

# systemctl enable target ; systemctl start target
ln -s '/usr/lib/systemd/system/target.service' '/etc/systemd/system/multi-user.target.wants/target.service'

iSCSI Target has been configured successfully.

Configure iSCSI Initiator

Install iscsi-initiator-utils package. It is required to configure a iSCSI Initiator.

# yum install -y iscsi-initiator-utils

We can confirm the iSCSI Initiator’s iqn that, we have used during iSCSI Target configuration to create an ACL.

# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.1994-05.com.redhat:31532b60ee10

Start and enable iscsi service.

# systemctl enable iscsi ; systemctl start iscsi

Discover the available iSCSI Targets at 192.168.116.11.

# iscsiadm -m discovery -t sendtargets -p 192.168.116.11
192.168.116.11:3260,1 iqn.2018-07.com.example.server1:remotedisk1
# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
ââsda1   8:1    0  300M  0 part /boot
ââsda2   8:2    0  1.5G  0 part [SWAP]
ââsda3   8:3    0 18.2G  0 part /
sr0     11:0    1  3.5G  0 rom  /mnt/iso

# systemctl restart iscsi
# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
ââsda1   8:1    0  300M  0 part /boot
ââsda2   8:2    0  1.5G  0 part [SWAP]
ââsda3   8:3    0 18.2G  0 part /
sdb      8:16   0   20G  0 disk
sr0     11:0    1  3.5G  0 rom  /mnt/iso

Our iSCSI Target Disk has been successfully discovered as /dev/sdb. Let’s create a partition and the filesystem.

# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

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
Building a new DOS disklabel with disk identifier 0x08f4940d.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (8192-41940991, default 8192):
Using default value 8192
Last sector, +sectors or +size{K,M,G} (8192-41940991, default 41940991):
Using default value 41940991
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21473787904 bytes, 41940992 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 4194304 bytes
Disk label type: dos
Disk identifier: 0x08f4940d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            8192    41940991    20966400   83  Linux

# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=16, agsize=327600 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=5241600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Now, persistently mount the partition at /mnt/remotedisk1.

# echo "/dev/sdb1 /mnt/remotedisk1 xfs _netdev 0 0" >> /etc/fstab
# mount -a
# mount | grep /mnt/remotedisk1
/dev/sdb1 on /mnt/remotedisk1 type xfs (rw,relatime,seclabel,attr2,inode64,noquota,_netdev)

We have successfully configure iSCSI Target and then persistently mount it at our iSCSI Initiator.

If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.

Final Thoughts

Configuring an iSCSI Target in Linux can significantly enhance your server’s storage capabilities by providing a robust and scalable network storage solution. By following this guide, you can set up and manage your iSCSI Target efficiently.

If you need further assistance or prefer professional help, I offer expert Linux server configuration services on Fiverr. Check out my Fiverr profile to see how I can help you with your server setup and other IT needs.

Leave a Comment