Pages

Thursday, March 29, 2012

How to setup and use iSCSI server in Linux


0. iSCSI Terminology
1. Setup iSCSI server in RedHat Enterprise Linux 6 (RHEL 6)
2. Connecting to iSCSI target from Linux initiator
3. Connecting to iSCSI target from Windows initiator


0. iSCSI terminology


Before we start, it's important to know the terms in iSCSI: iSCSI use server-client protocol: the server is referred as the target, and the client is the initiator.


1. Setup iSCSI server in RedHat Enterprise Linux 6 (RHEL 6)

 Obtains tgt iSCSI target package via yum:
yum install scsi-target-utils
 Configures tgt by editing "/etc/tgt/targets.conf". The file has many examples on how to configure disk space or devices for iSCSI.

For example, a system with its OS installed on /dev/sda and an extra hard disk /dev/sdb dedicated to use for iSCSI, and /dev/sdb is partitioned into sdb1 and sdb2 (use fdisk or parted for this), then the target can be configured by adding the following lines:
<target iqn.2012-03.com.rdlab:kelvin.target1>
   backing-store /dev/sdb1   #LUN1
   backing-store /dev/sdb2   #LUN2
</target>
Or if you don't have an extra disk and want the iSCSI initiator share the disk with your OS like what I did, you can do the following create a 10GB block device in Linux:
mkdir /iscsi ; dd if=/dev/zero of=/iscsi/disk1 bs=1M count=10240
And edit /etc/tgt/targets.conf:

<target iqn.2012-03.com.rdlab:kelvin.target1>
   backing-store /iscsi/disk1
</target>
After completed configuration, start the iSCSI server:
chkconfig tgtd on    #skip this if you don't want tgtd start automatically when system boot up.
service tgtd start 
 To check to make sure the iSCSI target is up and running:

tgtadm --mode target --op show
Target 1: iqn.2012-03.com.rdlab:kelvin.target1
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
        I_T nexus: 6
            Initiator: x9dri1
            Connection: 0
                IP Address: 172.31.10.110
    LUN information:
        LUN: 0
            Type: controller
            SCSI ID: IET     00010000
            SCSI SN: beaf10
            Size: 0 MB, Block size: 1
            Online: Yes
            Removable media: No
            Readonly: No
            Backing store type: null
            Backing store path: None
            Backing store flags:
        LUN: 1
            Type: disk
            SCSI ID: IET     00010001
            SCSI SN: beaf11
            Size: 10737 MB, Block size: 512
            Online: Yes
            Removable media: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /iscsi/disk1
            Backing store flags:
    Account information:
    ACL information:
        ALL

2. Connecting to iSCSI target from Linux initiator.

From the client(linux initiator), check if initiator can detect the target:
iscsiadm -m discovery -t sendtargets -p <target IP>  
and it should show the detected target, for my case the output is as below:

172.31.10.8:3260,1 iqn.2012-03.com.rdlab:kelvin.target1
then you can connect to target and use the iSCSI LUN: 
iscsiadm -m node -T iqn.2012-03.com.rdlab:kelvin.target1 -p 172.31.10.8 --login
dmesg | tail -2   #this is to find out which device is associated with the iSCSI LUN, in this case it's /dev/sdc
If the LUN does not contains partition nor has a file system, you will need to use fdisk or parted to create partition and use mkfs to create the file system before mounting. 
fdisk /dev/sdc   #then use "n" to create partition sdc1 and "w" to save and quit.
mkfs.ext3 /dev/sdc1
mount /dev/sdc1 /mnt    #mount to use . 


3. Connecting to iSCSI target from Windows initiator.
Start Windows iSCSI Initiator from Control Panel > Administrative Tools.

In "Target" tab, add target using its IP, then go to "Volume and Devices" and select the LUN and click "Auto Configure". After done the previous, you should see a new drive appears on Control Panel > Administrative Tools > Disk Management, and you should able to format and use the drive.