一、简介

SCSI(Small Computer System Interface)是块数据传输协议,在存储行业广泛应用,是存储设备最基本的标准协议。

iSCSI协议是一种利用IP网络来传输潜伏时间短的SCSI 数据块的方法,iSCSI使用以太网协议传送SCSI命令、响应和数据。iSCSI可以用我们已经熟悉和每天都在使用的以太网来构建IP存储局域网。通过这种方法,iSCSI克服了直接连接存储的局限性,使我们可以跨不同服务器共享存储资源,并可以在不停机状态下扩充存储容量。


二、原理

wKioL1NKj5XyuDgtAADY_3P_vVQ845.jpg


iSCSI的工作过程:当iSCSI主机应用程序发出数据读写请求后,操作系统会生成一个相应的SCSI命令,该SCSI命令在iSCSI Initiator层被封装成iSCSI消息包并通过TCP/IP传送到设备侧,设备侧的iSCSI Target层会解开iSCSI消息包,得到SCSI命令的内容,然后传送给SCSI设备执行;设备执行SCSI命令后的响应,在经过设备侧iSCSI Target层时被封装成iSCSI响应PDU,通过TCP/IP网络传送给主机的iSCSI Initiator层,iSCS Initiator会从iSCSI响应PDU里解析出SCSI响应并传送给操作系统,操作系统再响应给应用程序。要实现iSCSI读写,除了使用特定硬设备外,也可透过软件方式,将服务器仿真为iSCSI的发起端(Initiator)或目标端(target),利用既有的处理器与普通的以太网络卡资源实现iSCSI的连接。



三、规划

172.16.1.50
centos 6.4
target
172.16.1.60
centos 6.4
Initiator


四、搭建iscsi目标段

1. 安装iscsi-target软件

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


2. 查看分区

[root@target ~]# lsblk
NAME                         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                           11:0    1  4.1G  0 rom  /mnt
sda                            8:0    0   20G  0 disk
├─sda1                         8:1    0  500M  0 part /boot
└─sda2                         8:2    0 19.5G  0 part
  ├─vg_target-lv_root (dm-0) 253:0    0 17.6G  0 lvm  /
  └─vg_target-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                            8:16   0   10G  0 disk
├─sdb1                         8:17   0    4G  0 part
└─sdb2                         8:18   0    5G  0 part


3. 启动tgtd服务

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


4. 配置

LUN(Logical Unit Number)是逻辑单元号,一般为数字,用来标识存储设备。

IQN(iSCSI Qualified Name)格式:

格式

意义

范例

yyyy-mm

年份-月份

2014-04

reversed domain name

把域名名称反过来写,通常把公司的域名反过来写

com.target

identifier

识别字,通常注明这个存储空间的用途

share-storage


[root@target ~]# tgtadm --lld iscsi --mode target --op new --tid 1 --targetname iqn.2014-04.com.target:share-storage
#新增target device
[root@target ~]# tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 --backing-store /dev/sdb1
#将起初新建的分区加入target device
[root@target ~]# tgtadm --lld iscsi --mode target --op bind --tid 1 --initiator-address 172.16.1.0/24
#设置可以访问存取此target device的initiator节点
[root@target ~]# tgtadm --lld iscsi --mode target --op show
Target 1: iqn.2014-04.com.target:share-storage
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    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: 4302 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdb1
            Backing store flags:
    Account information:
    ACL information:
        172.16.1.0/24

这些配置命令不需要记忆,-h查看一下就知道了


5. 编辑配置文件配置永久生效

[root@target ~]# vim /etc/tgt/targets.conf
<target iqn.2014-04.com.target:share-storage>
    <backing-store /dev/sdb1>
        lun 1
    </backing-store>
    initiator-address 172.16.1.0/24
</target>
#在最后添加这几行

当然,这里还有很多其他参数可以写,如incominguser、outgoinguser、MaxConnections等,具体可以参见配置文件,文件里写得很清楚。


6. 在防火墙上放行3260端口

[root@target ~]# iptables -I INPUT 1 -p tcp -m state --state NEW --dport 3260 -j ACCEPT
[root@target ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]


六、linux发起端配置

1. 安装并启动软件

[root@client ~]# yum install iscsi-initiator-utils -y
[root@client ~]# service iscsi start
Starting iscsi:                                            [  OK  ]
[root@client ~]# chkconfig iscsi on


2. 发现设备并登录

[root@client ~]# iscsiadm -m discovery -t sendtargets -p 172.16.1.50:3260                    
172.16.1.50:3260,1 iqn.2014-04.com.target:share-storage
[root@client ~]# iscsiadm -m node -T iqn.2014-04.com.target:share-storage -p 172.16.1.50:3260 -l
Logging in to [iface: default, target: iqn.2014-04.com.target:share-storage, portal: 172.16.1.50,3260] (multiple)
Login to [iface: default, target: iqn.2014-04.com.target:share-storage, portal: 172.16.1.50,3260] successful.


3. 查看设备

[root@client ~]# lsblk
NAME                         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                           11:0    1  4.1G  0 rom  /mnt
sda                            8:0    0   20G  0 disk
├─sda1                         8:1    0  500M  0 part /boot
└─sda2                         8:2    0 19.5G  0 part
  ├─vg_client-lv_root (dm-0) 253:0    0 17.6G  0 lvm  /
  └─vg_client-lv_swap (dm-1) 253:1    0    2G  0 lvm  [SWAP]
sdb                            8:16   0    4G  0 disk
└─sdb1                         8:17   0    1G  0 part   #iscsi设备,我已经分区并格式化了


4. 自动挂载这个磁盘

[root@client ~]# tune2fs -l /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          4b8cbee1-9e6f-4939-9049-1d18ca61d397
[root@client ~]# vim /etc/fstab
UUID=4b8cbee1-9e6f-4939-9049-1d18ca61d397       /backup         ext4    defaults,_netdev        0 0
#_netdev是针对iscsi设备的特殊mount选项,此挂载选项指示将在网络启动后挂载该卷,在关闭网络前卸载该卷。


5. 删除该iscsi设备

[root@client ~]# iscsiadm -m node -T iqn.2014-04.com.target:share-storage -p 172.16.1.50:3260 -u
Logging out of session [sid: 2, target: iqn.2014-04.com.target:share-storage, portal: 172.16.1.50,3260]
Logout of [sid: 2, target: iqn.2014-04.com.target:share-storage, portal: 172.16.1.50,3260] successful.
[root@client ~]# iscsiadm -m node -o delete -T iqn.2014-04.com.target:share-storage -p 172.16.1.50:3260


6. 进行读写测试

[root@client ~]# cd /backup/
[root@client backup]# ls
ds  dsaf  lost+found
[root@client backup]# dd if=/dev/zero of=/backup/dd bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.896341 s, 117 MB/s
[root@client backup]# ls
dd  ds  dsaf  lost+found


七、使用CHAP认证

1. 目标端

[root@target ~]# vim /etc/tgt/targets.conf
<target iqn.2014-04.com.target:share-storage>
    <backing-store /dev/sdb1>
        lun 1
    </backing-store>
    initiator-address 172.16.1.0/24
    incominguser lion passwd        #发起端登录的用户名和密码
    outgoinguser sven 12345         #连接发起端的用户名和密码
</target>

配置完成重启服务即可,再查看一下。

[root@target ~]# tgtadm --lld iscsi --mode target --op show
Target 1: iqn.2014-04.com.target:share-storage
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
        I_T nexus: 1
            Initiator: iqn.2014-04.com.target:share-storage
            Connection: 0
                IP Address: 172.16.1.60
    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: 4302 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdb1
            Backing store flags:
    Account information:
        lion
        sven (outgoing)
    ACL information:
        172.16.1.0/24


2. 发起端配置

[root@client ~]# vim /etc/iscsi/iscsid.conf
node.session.auth.authmethod = CHAP
node.session.auth.username = lion
node.session.auth.password = passwd
node.session.auth.username_in = sven
node.session.auth.password_in = 12345

配置完成重启服务。

这样就实现了基于用户名和密码的认证了。