Wednesday, July 8, 2015

[iSCSI Target and Initiator] Installation and Configuration for Linux

iSCSI Target (Server) 


First we set up the target (server2):

aptitude install iscsitarget

Open /etc/default/iscsitarget...

vi /etc/default/iscsitarget
... and set ISCSITARGET_ENABLE to true:

 ISCSITARGET_ENABLE=true

We can use unused logical volumes, image files, hard drives (e.g. /dev/sdb), hard drive partitions (e.g. /dev/sdb1) or RAID devices (e.g. /dev/md0) for the storage. In this example I will create a logical volume of 20GB named storage_lun1 in the volume group vg0:

lvcreate -L20G -n storage_lun1 vg0

(If you want to use an image file, you can create it as follows:
mkdir /storage dd if=/dev/zero of=/storage/lun1.img bs=1024k count=20000

This creates the image file /storage/lun1.img with a size of 20GB.)

Next we edit /etc/ietd.conf...

vi /etc/ietd.conf

... and comment out everything in that file. At the end we add the following stanza:

[...]
Target iqn.2001-04.com.example:storage.lun1
        IncomingUser someuser secret
        OutgoingUser
        Lun 0 Path=/dev/vg0/storage_lun1,Type=fileio
        Alias LUN1
        #MaxConnections  6

The target name must be a globally unique name, the iSCSI standard defines the "iSCSI Qualified Name" as follows: iqn.yyyy-mm.<reversed domain name>[:identifier]; yyyy-mm is the date at which the domain is valid; the identifier is freely selectable. The IncomingUser line contains a username and a password so that only the initiators (clients) that provide this username and password can log in and use the storage device; if you don't need authentication, don't specify a username and password in the IncomingUser line. In the Lun line, we must specify the full path to the storage device (e.g. /dev/vg0/storage_lun1, /storage/lun1.img, /dev/sdb, etc.).
Now we tell the target that we want to allow connections to the device iqn.2001-04.com.example:storage.lun1 from the IP address 192.168.0.100 (server1.example.com) (comment out the ALL ALL line because that would allow all initiators to connect to all targets)...

vi /etc/initiators.allow

[...]
iqn.2001-04.com.example:storage.lun1 192.168.0.100
#ALL ALL

... and start the target:

/etc/init.d/iscsitarget start 

 

=======================================

 iSCSI Initiator (Client)


SCSI (Internet Small Computer System Interface) is a protocol that allows SCSI commands to be transmitted over a network. Typically iSCSI is implemented in a SAN (Storage Area Network) to allow servers to access a large store of hard drive space. The iSCSI protocol refers to clients as initiators and iSCSI servers as targets.
Ubuntu Server can be configured as both an iSCSI initiator and a target. This guide provides commands and configuration options to setup an iSCSI initiator. It is assumed that you already have an iSCSI target on your local network and have the appropriate rights to connect to it. The instructions for setting up a target vary greatly between hardware providers, so consult your vendor documentation to configure your specific iSCSI target.

iSCSI Initiator Install

To configure Ubuntu Server as an iSCSI initiator install the open-iscsi package. In a terminal enter:
sudo apt-get install open-iscsi

iSCSI Initiator Configuration

Once the open-iscsi package is installed, edit /etc/iscsi/iscsid.conf changing the following:
node.startup = automatic
You can check which targets are available by using the iscsiadm utility. Enter the following in a terminal:
sudo iscsiadm -m discovery -t st -p 192.168.0.10
  • -m: determines the mode that iscsiadm executes in.
  • -t: specifies the type of discovery.
  • -p: option indicates the target IP address.
Change example 192.168.0.10 to the target IP address on your network.
If the target is available you should see output similar to the following:

192.168.0.10:3260,1 iqn.1992-05.com.emc:sl7b92030000520000-2

The iqn number and IP address above will vary depending on your hardware.
You should now be able to connect to the iSCSI target, and depending on your target setup you may have to enter user credentials. Login to the iSCSI node:
sudo iscsiadm -m node --login
Check to make sure that the new disk has been detected using dmesg:
dmesg | grep sd

[    4.322384] sd 2:0:0:0: Attached scsi generic sg1 type 0
[    4.322797] sd 2:0:0:0: [sda] 41943040 512-byte logical blocks: (21.4 GB/20.0 GiB)
[    4.322843] sd 2:0:0:0: [sda] Write Protect is off
[    4.322846] sd 2:0:0:0: [sda] Mode Sense: 03 00 00 00
[    4.322896] sd 2:0:0:0: [sda] Cache data unavailable
[    4.322899] sd 2:0:0:0: [sda] Assuming drive cache: write through
[    4.323230] sd 2:0:0:0: [sda] Cache data unavailable
[    4.323233] sd 2:0:0:0: [sda] Assuming drive cache: write through
[    4.325312]  sda: sda1 sda2 < sda5 >
[    4.325729] sd 2:0:0:0: [sda] Cache data unavailable
[    4.325732] sd 2:0:0:0: [sda] Assuming drive cache: write through
[    4.325735] sd 2:0:0:0: [sda] Attached SCSI disk
[ 2486.941805] sd 4:0:0:3: Attached scsi generic sg3 type 0
[ 2486.952093] sd 4:0:0:3: [sdb] 1126400000 512-byte logical blocks: (576 GB/537 GiB)
[ 2486.954195] sd 4:0:0:3: [sdb] Write Protect is off
[ 2486.954200] sd 4:0:0:3: [sdb] Mode Sense: 8f 00 00 08
[ 2486.954692] sd 4:0:0:3: [sdb] Write cache: disabled, read cache: enabled, doesn't
 support DPO or FUA
[ 2486.960577]  sdb: sdb1
[ 2486.964862] sd 4:0:0:3: [sdb] Attached SCSI disk

In the output above sdb is the new iSCSI disk. Remember this is just an example; the output you see on your screen will vary.
Next, create a partition, format the file system, and mount the new iSCSI disk. In a terminal enter:
sudo fdisk /dev/sdb
n
p
enter
w
The above commands are from inside the fdisk utility; see man fdisk for more detailed instructions. Also, the cfdisk utility is sometimes more user friendly.
Now format the file system and mount it to /srv as an example:
sudo mkfs.ext4 /dev/sdb1
sudo mount /dev/sdb1 /srv
Finally, add an entry to /etc/fstab to mount the iSCSI drive during boot:
/dev/sdb1       /srv        ext4    defaults,auto,_netdev 0 0
It is a good idea to make sure everything is working as expected by rebooting the server.

OR

Install Open-iSCSI Initiator

Type the following command at a shell prompt:
$ sudo apt-get install open-iscsi

Open-iSCSI default configuration

You need to soft-link (path fix) few two files to autologin work i.e. fix file paths for iscsiadm, enter:
ln -s /etc/{iscsid.conf,initiatorname.iscsi} /etc/iscsi/
Default configuration file could be located at /etc/iscsi/iscsid.conf or ~/.iscsid.conf. Open /etc/iscsi/iscsid.conf file:
# vi /etc/iscsi/iscsid.conf
Set node.session.auth.username, node.session.auth.password and other parameter as follows:
node.startup = automatic
node.session.auth.username = MY-ISCSI-USER
node.session.auth.password = MY-ISCSI-PASSWORD
discovery.sendtargets.auth.username = MY-ISCSI-USER
discovery.sendtargets.auth.password = MY-ISCSI-PASSWORD
node.session.timeo.replacement_timeout = 120
node.conn[0].timeo.login_timeout = 15
node.conn[0].timeo.logout_timeout = 15
node.conn[0].timeo.noop_out_interval = 10
node.conn[0].timeo.noop_out_timeout = 15
node.session.iscsi.InitialR2T = No
node.session.iscsi.ImmediateData = Yes
node.session.iscsi.FirstBurstLength = 262144
node.session.iscsi.MaxBurstLength = 16776192
node.conn[0].iscsi.MaxRecvDataSegmentLength = 65536

Save and close the file. Restart open-iscsi service:
# /etc/init.d/open-iscsi restart
Now you need to run a discovery against the iscsi target host:
# iscsiadm -m discovery -t sendtargets -p ISCSI-SERVER-IP-ADDRESS
If 192.168.1.60 is iSCSI server IP address, enter:
# iscsiadm -m discovery -t sendtargets -p 192.168.1.60
OR
# iscsiadm --mode discovery --type sendtargets --portal 192.168.1.60
Note down the record id (such as iqn.2001-05.com.doe:test) found by the discovery. You need the same for login. Login, must use a node record id found by the discovery:
# iscsiadm --mode node --targetname iqn.2001-05.com.doe:test --portal 192.168.1.60:3260 --login
Finally restart the service again:
# /etc/init.d/open-iscsi restart

Format iSCSI Volume

Now you should see an additional drive on the system such as /dev/sdc. Use /var/log/messages file to find out device name:
# tail -f /var/log/messages
If your device name is /dev/sdc, enter the following command to create a partition:
# fdisk /dev/sdc
Next format partition:
# mkfs.ext3 /dev/sdc1
Mount file system:
# mkdir /iscsi
# mount /dev/sdc1 /iscsi

No comments: