Chinaunix首页 | 论坛 | 博客
  • 博客访问: 611288
  • 博文数量: 244
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-27 09:53
个人简介

记录学习,记录成长

文章分类

全部博文(244)

我的朋友

分类: LINUX

2015-11-24 21:01:35

环境:
centos 6.5 
iSCSI Target:192.168.85.128+scsi-target-utils

iSCSI Initiator:
192.168.85.144+iscsi-initiator-utils
192.168.85.145+iscsi-initiator-utils

基于tgtadm和iscsiadm配置iSCSI
一.服务器端配置
1. 准备要共享出去的设备,这里使用分区(虽然将分区共享出去,但是iSCSI Initiator识别其为硬盘而不是分区)
[root@localhost ~]# fdisk /dev/sdb
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x14186367

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         305     2449881   83  Linux
/dev/sdb2             306         805     4016250   83  Linux
/dev/sdb3             806        1305     4016250   83  Linux

[root@localhost ~]# fdisk -l /dev/sdb[1-3]

Disk /dev/sdb1: 2508 MB, 2508678144 bytes
255 heads, 63 sectors/track, 304 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

Disk /dev/sdb2: 4112 MB, 4112640000 bytes
255 heads, 63 sectors/track, 500 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

Disk /dev/sdb3: 4112 MB, 4112640000 bytes
255 heads, 63 sectors/track, 500 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/sdb2和/dev/sdb3共享出去;

2.安装SCSI服务端
[root@localhost ~]# yum install -y scsi-target-utils

3.启动服务并查看
[root@localhost ~]# service tgtd start
Starting SCSI target daemon: [  OK  ]

[root@localhost ~]# chkconfig tgtd on

[root@localhost ~]# netstat -tnlp | grep :3260
tcp        0      0 0.0.0.0:3260                0.0.0.0:*                   LISTEN      3206/tgtd           
tcp        0      0 :::3260                     :::*                        LISTEN      3206/tgtd           

4.新建一个target并查看
关于tgtadm和iscsiadm的用法:http://blog.chinaunix.net/uid-30212356-id-5520545.html

[root@localhost ~]# tgtadm -L iscsi -o new -m target -t 1 -T iqn.2015-11.com.a.test

[root@localhost ~]# tgtadm -L iscsi -o show -m target
Target 1: iqn.2015-11.com.a.test
    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: 
    Account information:
    ACL information:
然后必须向target中添加lun并关联存储设备才能被客户端访问使用;

5.创建一个lun
[root@localhost ~]# tgtadm -L iscsi -o new -m logicalunit -t 1 -l 1 -b /dev/sdb2

[root@localhost ~]# tgtadm -L iscsi -o show -m target
Target 1: iqn.2015-11.com.a.test
    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: 4113 MB, Block size: 512 #磁盘大小及块大小
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr #访问类型
            Backing store path: /dev/sdb2 #关联设备
            Backing store flags: 
    Account information: #客户端登录信息
    ACL information: #授权信息

lun0是一个controller,而lun1则是一个磁盘;

6.开放给192.168.85.0/24网段内的主机访问
[root@localhost ~]# tgtadm -L iscsi -o bind -m target -t 1 -I 192.168.85.0/24
[root@localhost ~]# tgtadm -L iscsi -o show -m target
Target 1: iqn.2015-11.com.a.test
    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: 4113 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdb2
            Backing store flags: 
    Account information:
    ACL information:
        192.168.85.0/24

二.客户端配置
1.安装SCSI客户端
[root@node2 ~]# yum install -y iscsi-initiator-utils

2.初始化,为每个initiator命名并放在/etc/iscsi/下
2.1可以自动生成
[root@node2 ~]# echo "InitiatorName=`iscsi-iname -p iqn.2015-11.com.a`" > /etc/iscsi/initiatorname.iscsi

[root@node2 ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2015-11.com.a:101547f871

2.2或者为了方便记忆自定义别名
[root@node2 ~]# echo "InitiatorAlias=initiator1" >> /etc/iscsi/initiatorname.iscsi

[root@node2 ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2015-11.com.a:101547f871
InitiatorAlias=initiator1

3.启动服务
[root@node2 ~]# service iscsi start
[root@node2 ~]# service iscsid start
[root@node2 ~]# chkconfig iscsi on
[root@node2 ~]# chkconfig iscsid on

4.查找Target
[root@node2 ~]# iscsiadm -m discovery -t st -p 192.168.85.128
[  OK  ] iscsid: [  OK  ]
192.168.85.128:3260,1 iqn.2015-11.com.a.test

5.此时只是能找到,但还不能使用,所以,接下来要登录并使用;
5.1 先查看此时客户端的分区情况
[root@node2 ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b097b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2611     2064384   82  Linux swap / Solaris

5.2 登录服务器端使用其共享的磁盘(分区)
[root@node2 ~]# iscsiadm -m node -T iqn.2015-11.com.a.test  -p 192.168.85.128 -l
Logging in to [iface: default, target: iqn.2015-11.com.a.test, portal: 192.168.85.128,3260] (multiple)
Login to [iface: default, target: iqn.2015-11.com.a.test, portal: 192.168.85.128,3260] successful.

5.3 此时的分区情况
[root@node2 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b097b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2611     2064384   82  Linux swap / Solaris

Disk /dev/sdb: 4112 MB, 4112640000 bytes
127 heads, 62 sectors/track, 1020 cylinders
Units = cylinders of 7874 * 512 = 4031488 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
如果此时服务器端再将/dev/sdb3共享出去创建为lun 2,那么客户端会再多一个/dev/sdc

5.4 可以看到,服务器端上的/dev/sdb2分区变为了客户端上的一个磁盘,在客户端上还可以对该磁盘进行分区
[root@node2 ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 4112 MB, 4112640000 bytes
127 heads, 62 sectors/track, 1020 cylinders
Units = cylinders of 7874 * 512 = 4031488 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x49ab33aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         800     3149569   83  Linux
/dev/sdb2             801        1020      866140   83  Linux

6. 然后将/dev/sdb1格式化
[root@node2 ~]# mke2fs -t ext4 /dev/sdb1

而此时在服务器端的分区情况则不变:
[root@localhost ~]# fdisk -l
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x14186367

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         305     2449881   83  Linux
/dev/sdb2             306         805     4016250   83  Linux
/dev/sdb3             806        1305     4016250   83  Linux

7. 客户端上挂载使用刚刚格式化的分区
[root@node2 ~]# mount /dev/sdb1 /media/

[root@node2 ~]# cat /media/a.txt 
hello
hi

为了后面的测试,这里要先卸载客户端1上的/media
[root@node2 ~]# umount /media

8.打开另外一个客户端
8.1 安装上iscsi-initiator-utils且启动服务
[root@node1 ~]# yum install -y iscsi-initiator-utils
[root@node1 ~]# service iscsi start
[root@node1 ~]# service iscsid start
[root@node1 ~]# chkconfig iscsi on
[root@node1 ~]# chkconfig iscsid on

8.2 查找Target
[root@node1 ~]# iscsiadm -m discovery -t st -p 192.168.85.128 
[  OK  ] iscsid: [  OK  ]
192.168.85.128:3260,1 iqn.2015-11.com.a.test

8.3 登录节点
[root@node1 ~]# iscsiadm -m node -T iqn.2015-11.com.a.test -p 192.168.85.128 -l
Logging in to [iface: default, target: iqn.2015-11.com.a.test, portal: 192.168.85.128,3260] (multiple)
Login to [iface: default, target: iqn.2015-11.com.a.test, portal: 192.168.85.128,3260] successful.

8.4  查看
[root@node1 ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 4112 MB, 4112640000 bytes
127 heads, 62 sectors/track, 1020 cylinders
Units = cylinders of 7874 * 512 = 4031488 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x49ab33aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         800     3149569   83  Linux
/dev/sdb2             801        1020      866140   83  Linux

[root@node1 ~]# mount /dev/sdb1 /media/

[root@node1 ~]# cat /media/a.txt 
hello
hi
一般,尽量不要多个客户端同时挂载使用,因为,两个客户端都挂载时,只有一个客户端对文件做了操作,另一个客户端是无法立即查看到的,必须先卸载再挂载才能看到变化,同时也是为了防止对文件的同时写操作产生文件系统崩溃情况的发生,最好是/dev/sdb1由客户端1挂载使用,/dev/sdb2由客户端2挂载使用;

9.登出节点
[root@node2 ~]# iscsiadm -m node -T iqn.2015-11.com.a.test -p 192.168.85.128 -u
Logging out of session [sid: 2, target: iqn.2015-11.com.a.test, portal: 192.168.85.128,3260]
Logout of [sid: 2, target: iqn.2015-11.com.a.test, portal: 192.168.85.128,3260] successful.

[root@node2 ~]# fdisk -l 
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b097b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2354    18598912   83  Linux
/dev/sda3            2354        2611     2064384   82  Linux swap / Solaris

三.其他
1.关于开机自动挂载
每当discovery一个可用服务器后,client端会在/var/lib/iscsi/下生成相应的记录,包括登录的服务器,端口号,Target,所以每次主机重启后,客户端都会自动发现这些配置信息从而自动登录;
如果想要开机自动挂载:
1.1客户端可以将自动挂载命令写入/etc/fstab中,或者挂载时使用_netdev作为选项来实现开机自动挂载;

1.2 使用rm -rf /var/lib/iscsi/*命令删除那些配置信息,或者在登出服务器的时候使用防止自动登入
iscsiadm -m node -T iqn.2015-11.com.a.test -p 192.168.85.128 -u                  #先登出节点
iscsiadm -m node -T iqn.2015-11.com.a.test -p 192.168.85.128 -o delete         #再删除record

2.关于解绑定IP
[root@localhost ~]# tgtadm -L iscsi -m target -o show
Target 1: iqn.2015-11.com.a.test
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    .........省略..........
    Account information:
    ACL information:
        192.168.85.0/24          #绑定IP为192.168.85.0/24

解除IP绑定
[root@localhost ~]# tgtadm -L iscsi -m target -o unbind -t 1 -I 192.168.85.0/24

[root@localhost ~]# tgtadm -L iscsi -m target -o show
Target 1: iqn.2015-11.com.a.test
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    ..........省略.........
    Account information:
    ACL information:         #ip已经解除
3.删除指定lun(不要删除lun 0)
[root@localhost ~]# tgtadm -L iscsi -m logicalunit -o delete -t 1 -l 1

[root@localhost ~]# tgtadm -L iscsi -m target -o show
Target 1: iqn.2015-11.com.a.test
    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: 
    Account information:
    ACL information:

12.删除target
[root@localhost ~]# tgtadm -L iscsi -m target -o delete -t 1 
[root@localhost ~]# tgtadm -L iscsi -m target -o show

补充:
四.基于tgt-admin和配置文件/etc/tgt/target.conf来配置iSCSI
1.编辑配置文件
default-driver iscsi #默认驱动为iSCSI

添加target:

     backing-store /dev/sdb3 #关联磁盘
Initiator-address 192.168.85.0/24  #设定授权访问地址


2.重启服务
[root@localhost tgt]# service tgtd reload
Updating SCSI target daemon configuration: [  OK  ]

3.查看
[root@localhost tgt]# tgtadm -L iscsi -m target -o show
Target 1: iqn.2015-11.com.a.exam
    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: 4113 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdb3
            Backing store flags: 
    Account information:
    ACL information:
        192.168.85.0/24
这样既简单又能保证下次开机不再需要用命令再次配置;

阅读(5175) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

yaosongroy2017-04-02 21:15:53

还不错~