Wednesday, July 3, 2019

Configure iscsi quickly on CentOS 7

If you have a Network Attached Storage (NAS), the best way to connect it to other servers is using the iSCSI method. Most companies use a NAS to store files and have employees connect to it directly to share files. But that method is not suitable to connect to other servers. This can be done when disk space on the a server runs low. Some setup on the NAS is needed to setup iSCSI on the NAS. Basically, an iSCSI LUN is created and then connected to a newly created or existing iSCSI target (aka iSCSI server). Refer to the NAS's configuration manual for detail.
Before we configure a Linux server to connect to a NAS over iSCSI, some information is needed first. This will help us when it is needed in the setup process.
First note the IP address of the NAS. Second, well need to know the iSCSI Target Name. This identifies the disk assigned to be used. If everything has ben setup properly, an ISCSI LUN would have been created and assigned to an ISCSI Target. Finally, the mount point for the disk needs to be known, too. This is the directory where the applications on the Linux server writes files to.

First install the necessary software components. At the command line

sudo yum install iscsi-initiator-utils

This will install the iSCSI initiator aka client files. Edit the file /etc/iscsi/initiatorname.iscsi and
add the line below if it isn't already there

InitiatorName=iqn.2014-08.com.example:client

This is the name of the initiator or client. In the example above, it is  'iqn.2014-08.com.example:client' 

Note: if an access control list (ACL) was set in the NAS, set ACL on target (iSCSI server) to match this client name. This will allow the client to connect to the iSCSI server.
Now start the iSCSI system

# systemctl start iscsi

Next, discover the targets available on the NAS. In the example below the NAS IP address is 192.168.1.25

# iscsiadm -m discovery -t st -p 192.168.1.25
192.168.1.25:3260,0 iqn.2000-01.com.synology:backupecc
192.168.1.25:3260,0 iqn.2000-01.com.synology:diskstation.target-1.d87a5fcd9b
192.168.1.25:3260,0 iqn.2000-01.com.synology:diskstation.target-11.d87a5fcd9

Look at the list and see whether the iSCSI target assigned is in the list. If it is, it's time to connect to it.  Set the iSCSI server to start automatically every time the server starts. Then restart it.

# systemctl enable iscsid.service
# systemctl restart iscsid.service

Add the target to our initiator (or connect our client to the target/ iSCSI server).The format of the command is

iscsiadm -m node -T <discovered target iqd> -p <ip of target> -l

So the execute the command using the information gathered

# iscsiadm -m node -T iqn.2000-01.com.synology:diskstation.target-11.d87a5fcd9b -p 192.168.1.25 -l
Logging in to [iface: default, target: iqn.2000-01.com.synology:diskstation.target-11.d87a5fcd9b, portal: 192.168.1.25,3260] (multiple)
Login to [iface: default, target: iqn.2000-01.com.synology:diskstation.target-11.d87a5fcd9b, portal: 192.168.1.25,3260] successful.

Look for the device name created from the command above.

# lsblk --scsi
NAME HCTL       TYPE VENDOR   MODEL             REV TRAN
sda  2:0:0:0    disk SYNOLOGY iSCSI Storage    3.1  iscsi
sr0  0:0:1:0    rom  QEMU     QEMU DVD-ROM     1.5. ata


In the above example, the device name is /dev/sda .

Confirm that it is not connected in READONLY mode. The RO column should be 0

# lsblk | egrep "NAME|sda"
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  500G  0 disk


Connecting to the iSCSI target is like opening the server case and installing and connecting a new hard disk. Since the disk is connected, the disk has to be prepared. First, format the disk

# mkfs.ext4 /dev/sda
<output truncated>
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done


or a better option would be

# mkfs.xfs /dev/sda
<output truncated>
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


Mount it to the mount point created or the directory where the system will store the files to the iSCSI disk to. In this example the mount point is /mnt/disk

# mount /dev/sda /mnt/disk

If everything is successful thus far, make it permanent by doing the following
1. Find UUID
2. insert into /etc/fstab

Finding the  UUID
# blkid /dev/sdb
/dev/sda: UUID="3d86fad7-1896-4538-acae-e78269b3a6a7" TYPE="ext4"

Make note of the UUID value. Edit /etc/fstab

# vi /etc/fstab

add the following line

UUID=3d86fad7-1896-4538-acae-e78269b3a6a7 /mnt              ext4           _netdev         0 0
                               

The format of the line is
<UUID>  <mount point> <what filesystem was created on it (ext4/xfs)> _netdev <this option waits for the network to start before connecting to the iSCSI disk> 0 0
The next time the Linux server restarts, it will connect to the new iSCI disk automatically. Test the disk anyway.

# mount /mnt/disk
# touch /mnt/disk/TestFile

The command above creates empty file called TestFile. If there are no errors in creating the file, the setup is complete. For more information, execute this command

# iscsiadm -m session -P 3

References
https://www.itzgeek.com/how-tos/linux/centos-how-tos/configure-iscsi-target-initiator-on-centos-7-rhel7.html
https://www2.linuxacademy.com/howtoguides/21208-how-to-share-your-storage-with-iscsi/
https://www.certdepot.net/rhel7-configure-iscsi-target-initiator-persistently/
https://www.thegeekdiary.com/complete-guide-to-configuring-iscsi-in-centos-rhel-7/