准备工作:


主机系统地址软件
target主机CentOS 6.4 i386192.168.0.22iscsi-target-utils
initiator主机CentOS 6.4 i386192.168.0.23iscsi-initiator-utils
initiator主机CentOS 6.4 i386192.168.0.24iscsi-initiator-utils



一. 配置target主机


在target主机有两块硬盘,/dev/sda和/dev/sdb,其中sdb作为iSCSI的共享磁盘


1. 安装iSCSI的服务端

[root@target ~]# yum install scsi-target-utils


2. 将tgtd服务设置为开机启动,并将它启动

[root@target ~]# chkconfig tgtd on
[root@target ~]# service tgtd start
Starting SCSI target daemon:                               [  OK  ]


3. 关于tgtadm命令的简单介绍

[root@target ~]# tgtadm -h
Usage: tgtadm [OPTION]
Linux SCSI Target Framework Administration Utility, version 1.0.24
  --lld <driver> --mode target --op new --tid <id> --targetname <name>
                        add a new target with <id> and <name>. <id> must not be zero.
  --lld <driver> --mode target --op delete [--force] --tid <id>
            delete the specific target with <id>.
            With force option, the specific target is deleted
            even if there is an activity.
  --lld <driver> --mode target --op show
                        show all the targets.
  --lld <driver> --mode target --op show --tid <id>
                        show the specific target's parameters.
  --lld <driver> --mode target --op update --tid <id> --name <param> --value <value>
                        change the target parameters of the specific
                        target with <id>.
  --lld <driver> --mode target --op bind --tid <id> --initiator-address <address>
  --lld <driver> --mode target --op bind --tid <id> --initiator-name <name>
                        enable the target to accept the specific initiators.
  --lld <driver> --mode target --op unbind --tid <id> --initiator-address <address>
  --lld <driver> --mode target --op unbind --tid <id> --initiator-name <name>
                        disable the specific permitted initiators.
  --lld <driver> --mode logicalunit --op new --tid <id> --lun <lun> \
                        --backing-store <path> --bstype <type> --bsoflags <options>
                        add a new logical unit with <lun> to the specific
                        target with <id>. The logical unit is offered
                        to the initiators. <path> must be block device files
                        (including LVM and RAID devices) or regular files.
                        bstype option is optional.
                        bsoflags supported options are sync and direct
                        (sync:direct for both).
  --lld <driver> --mode logicalunit --op delete --tid <id> --lun <lun>
                        delete the specific logical unit with <lun> that
                        the target with <id> has.
  --lld <driver> --mode account --op new --user <name> --password <pass>
                        add a new account with <name> and <pass>.
  --lld <driver> --mode account --op delete --user <name>
                        delete the specific account having <name>.
  --lld <driver> --mode account --op bind --tid <id> --user <name> [--outgoing]
                        add the specific account having <name> to
                        the specific target with <id>.
                        <user> could be <IncomingUser> or <OutgoingUser>.
                        If you use --outgoing option, the account will
                        be added as an outgoing account.
  --lld <driver> --mode account --op unbind --tid <id> --user <name>
                        delete the specific account having <name> from specific
                        target.
  --control-port <port> use control port <port>
  --help                display this help and exit
Report bugs to <stgt@vger.kernel.org>.


首先,创建一个target

[root@target ~]# tgtadm -L iscsi -m target -o new -t 1 -T iqn.2014-03.com.flyence:target1


查看该target的相关情况

[root@target ~]# tgtadm -L iscsi -m target -o show


创建一个LU,并将/dev/sdb作为共享磁盘

[root@target ~]# tgtadm -L iscsi -m logicalunit -o new -t 1 -l 1 -b /dev/sdb


允许192.168.0.0/16网段的主机访问

[root@target ~]# tgtadm -L iscsi -m target -o bind  -t 1 -I 192.168.0.0/24


创建认证账号,并绑定到target上

[root@target ~]# tgtadm -L iscsi -m account -o new --user flyence --password flyence
[root@target ~]# tgtadm -L iscsi -m account -o bind -t 1 --user flyence

该账号作为客户机访问服务端的认证账号


创建认证账号,并以outgoing的形式绑定到target上

[root@target ~]# tgtadm -L iscsi -m account -o new --user evan --password evan
[root@target ~]# tgtadm -L iscsi -m account -o bind -t 1 --user evan --outgoing

当客户机向服务端写入前,客户机会用此账号验证服务器的身份。


二. 配置客户端


1. 安装iscsi-initiator-utils

[root@initiator ~]# yum install iscsi-initiator-utils


2. 设置initiator的名字

[root@initiator iscsi]# echo "InitiatorName=`iscsi-iname -p iqn.2014-03.com.flyence`" > /etc/iscsi/initiatorname.iscsi
[root@initiator iscsi]# cat initiatorname.iscsi
InitiatorName=iqn.2014-03.com.flyence:9e9d0941d1f


3. 在/etc/iscsi/iscsi.conf中配置验证账号信息,启用以下5行,并修改

node.session.auth.authmethod = CHAP
                                                                                                                                                                                                                                                                                                                               
node.session.auth.username = flyence
node.session.auth.password = flyence
                                                                                                                                                                                                                                                                                                                                                     
node.session.auth.username_in = evan
node.session.auth.password_in = evan


4. 发现服务器的target

[root@initiator iscsi]# iscsiadm -m discovery -t st -p 192.168.0.22:3260
Starting iscsid:                                           [  OK  ]
192.168.0.22:3260,1 iqn.2014-03.com.flyence:target1


5. 登录上述被发现的target

[root@initiator iscsi]# iscsiadm  -m node -T iqn.2014-03.com.flyence:target1 -p 192.168.0.22:3260 -l
Logging in to [iface: default, target: iqn.2014-03.com.flyence:target1, portal: 192.168.0.22,3260] (multiple)
Login to [iface: default, target: iqn.2014-03.com.flyence:target1, portal: 192.168.0.22,3260] successful.


6. 查看当前的磁盘情况

[root@initiator iscsi]# fdisk -l /dev/sd[a-z]
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000925b4
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        7675    61440000   8e  Linux LVM
/dev/sda3            7675       14049    51200000   8e  Linux LVM
/dev/sda4           14049       15666    12983296    5  Extended
/dev/sda5           14049       15666    12982272   8e  Linux LVM
Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


多出一块硬盘/dev/sdb,并且sdb还没分区,


7. 将sdb分两个区,并将sdb1格式化为ext4

[root@initiator iscsi]# fdisk /dev/sdb
....
[root@initiator iscsi]# fdisk -l /dev/sdb
Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc9458e42
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2612    10490445   83  Linux
[root@initiator iscsi]# mkfs.ext4 /dev/sdb1


8. 此时,查看target主机的sdb硬盘情况

[root@target ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc9458e42
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2612    10490445   83  Linux

已被分好区


当要开机启动服务时

[root@initiator iscsi]# chkconfig iscsid on


三. 服务端target配置文件的设定


在服务端如果只是在命令行中定义target和LU,在重启时会消失,要使其永久有效,就得写入配置文件/etc/tgt/targets.conf

<target iqn.2014-03.com.flyence:target2>
    backing-store /dev/sdb
    initiator-address 192.168.0.0/16
                                                                                                                                                   
    incominguser flyence flyence
    outgoinguser evan evan
</target>


重启服务器主机,再看看target的配置

[root@target ~]# tgtadm -L iscsi -m target -o show
Target 1: iqn.2014-03.com.flyence:target2
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
        I_T nexus: 1
            Initiator: iqn.2014-03.com.flyence:9e9d0941d1f
            Connection: 0
                IP Address: 192.168.0.23
    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
            Backing store type: null
            Backing store path: None
            Backing store flags:
        LUN: 1
            Type: disk
            SCSI ID: IET     00010001
            SCSI SN: beaf11
            Size: 128849 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdb
            Backing store flags:
    Account information:
        flyence
        evan (outgoing)
    ACL information:
        192.168.0.0/16


此时,由于已修改了target的名字,则客户机无法直接登录,只能删除原有信息,重新发现和登录

在客户机上,先登出原有的target

[root@initiator iscsi]# iscsiadm -m node -T iqn.2014-03.com.flyence:target1 -p 192.168.0.22:3260 -u
Logging out of session [sid: 1, target: iqn.2014-03.com.flyence:target1, portal: 192.168.0.22,3260]
Logout of [sid: 1, target: iqn.2014-03.com.flyence:target1, portal: 192.168.0.22,3260] successful.


删除该target及相关文件

[root@initiator iscsi]# iscsiadm -m node -T iqn.2014-03.com.flyence:target1 -p 192.168.0.22:3260 -o delete
[root@initiator iscsi]# rm -rf /var/lib/iscsi/send_targets/192.168.0.22,3260/

重启服务

[root@initiator iscsi]# service iscsid restart
Stopping iscsid:
Starting iscsid:                                           [  OK  ]


重新发现和登录

[root@initiator iscsi]# iscsiadm -m discovery -t st -p 192.168.0.22:3260
192.168.0.22:3260,1 iqn.2014-03.com.flyence:target2
[root@initiator iscsi]# iscsiadm -m node -T iqn.2014-03.com.flyence:target2 -p 192.168.0.22:3260 -l
Logging in to [iface: default, target: iqn.2014-03.com.flyence:target2, portal: 192.168.0.22,3260] (multiple)
Login to [iface: default, target: iqn.2014-03.com.flyence:target2, portal: 192.168.0.22,3260] successful.


查看磁盘

[root@initiator iscsi]# fdisk -l /dev/sdb
Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc9458e42
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2612    10490445   83  Linux