Chinaunix首页 | 论坛 | 博客
  • 博客访问: 133599
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2018-11-12 09:56
文章分类

全部博文(31)

文章存档

2014年(6)

2013年(9)

2008年(7)

2007年(7)

2006年(2)

分类: LINUX

2013-02-27 17:13:09

                             linux下iscsi共享磁盘的实验

一:SCSI target framework (tgt)

1.        这个在Linux中集成了

# rpm -qa|grep scsi-target

scsi-target-utils-0.0-6.20091205snap.el5_4.1

  

2.       启动服务

#/etc/init.d/tgtd start

       设置为开机自动启动

       Chkconfig tgtd on

       确认一下有没有启动起来

       #netstat –na|grep 3260

       # netstat -na|grep 3260

tcp        0   0 0.0.0.0:3260            0.0.0.0:*                   LISTEN     

tcp        0   0 :::3260                     :::*                   LISTEN

   3260scsi target的侦听端口

3.        配置,所有的配置都是通过tgtadm指令进行设置生效的

 

I:创建target

tgtadm --lld iscsi --op new --mode target --tid 1 --targetname iqn.node212-sda12

这个命令没有回显,说明执行成功

用下面的命令看看刚才的命令干了些什么

# tgtadm --lld iscsi --op show --mode target

Target 1: iqn.node212-sda12

    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

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: None

    Account information:

        ACL information:

 

以上命令创建一个target为iqn.node212-sda12,id为1

--op new 代表新建

--op delete 代表删除

--op show 代表显示

--op bind/unbind等等

 

LUN0是系统自动产生的,用来管理 device controller

 

II:向target分配一个物理的或文件的存储空间,再分配一个lun号;

可以向一个target分配一个或多个存储空间,而一个存储空间同时只能分配给一个target

 

现在我准备了一个划分好的分区和一个文件:

使用 dd 指令来创建一个1GB大小的文件

dd if=/dev/zero of=/var/lun4 bs=1M count=1024

  文件名位lun4,路径为/var/下

使用fdisk 划分一个分区/dev/sda12

          接下来向刚才新建的target  iqn.node212-sda12分配储存空间

          tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 -b /dev/sda12

          向tid为1的target里分配中logical unit为1的logical unit,设备为/dev/sda12,lun号必须指定;

          tgtadm --lld iscsi --op show --mode target 查看信息

         #  tgtadm --lld iscsi --op show --mode target

Target 1: iqn.node212-sda12

    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

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: None

        LUN: 1

            Type: disk

            SCSI ID: IET     00010001

            SCSI SN: beaf11

            Size: 5009 MB

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: /dev/sda12

    Account information:

ACL information:

 

从信息可以看到,已经为tid为1的target分配了/dev/sda12的分区,大小为5G,并未它分配了lun号为1(lun0为系统占用,已经不可以使用)

再为这个target分配 /var/lun4,这个是之前用dd创建的设备

tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 2 -b /var/lun4

 

再看:

[root@localhost init.d]#  tgtadm --lld iscsi --op show --mode target

Target 1: iqn.node212-sda12

    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

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: None

        LUN: 1

            Type: disk

            SCSI ID: IET     00010001

            SCSI SN: beaf11

            Size: 5009 MB

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: /dev/sda12

        LUN: 2

            Type: disk

            SCSI ID: IET     00010002

            SCSI SN: beaf12

            Size: 1074 MB

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: /var/lun4

    Account information:

    ACL information:

 

III: 设置访问控制:

为Target分配了存储设备后,需要设置访问控制后才可以使用,我们可以设置Target任意被访问,但是这样不安全;所以我们可以设置基于IP地址授权或基于用户验证的方式来控制对Target的访问

任意被访问:

任意被访问:

tgtadm --lld iscsi --op bind --mode target --tid 1 -I ALL  <-同样不回显为命令成功

tgtadm --lld -iscsi --op show --mode target           ?查看,看有什么

# tgtadm --lld iscsi --op show --mode target

Target 1: iqn.node212-sda12

    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

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: None

        LUN: 1

            Type: disk

            SCSI ID: IET     00010001

            SCSI SN: beaf11

            Size: 5009 MB

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: /dev/sda12

        LUN: 2

            Type: disk

            SCSI ID: IET     00010002

            SCSI SN: beaf12

            Size: 1074 MB

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: /var/lun4

    Account information:

    ACL information:

        ALL

把任意访问的授权给去掉

tgtadm --lld iscsi --op unbind --mode target --tid 1 -I ALL

设置基于ip地址的授权

tgtadm --lld iscsi --op bind --mode target --tid 1 -I 192.168.2.23

再授权一个ip:

tgtadm --lld iscsi --op bind --mode target --tid 1 -I 192.168.2.33

授权一段IP

tgtadm --lld iscsi --op bind --mode target --tid 1 -I 192.168.4.0/24

查看一下:

tgtadm --lld iscsi --op show --mode target 

# tgtadm --lld iscsi --op show --mode target

Target 1: iqn.node212-sda12

    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

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: None

        LUN: 1

            Type: disk

            SCSI ID: IET     00010001

            SCSI SN: beaf11

            Size: 5009 MB

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: /dev/sda12

        LUN: 2

            Type: disk

            SCSI ID: IET     00010002

            SCSI SN: beaf12

            Size: 1074 MB

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: /var/lun4

    Account information:

    ACL information:

        192.168.2.23

        192.168.5.116

        192.168.4.0/24

 

客户端连接:window7下

控制面板à系统和安全—>管理工具—>ISCSI发起程序

 


填入服务器的IP地址,点“快速连接”

点完成,再点确定。

从磁盘管理器中可以看到,客户端看到的是两个磁盘

 

IV:删除操作:

从Target中移除lun

tgtadm --lld iscsi --op delete --mode logicalunit --tid 1 --lun 2

#  tgtadm --lld iscsi --op show --mode target

Target 1: iqn.node212-sda12

    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

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: None

        LUN: 1

            Type: disk

            SCSI ID: IET     00010001

            SCSI SN: beaf11

            Size: 5009 MB

            Online: Yes

            Removable media: No

            Backing store type: rdwr

            Backing store path: /dev/sda12

    Account information:

    ACL information:

        192.168.2.23

        192.168.5.116

        192.168.4.0/24

lun 2已经没了

删除target:



#tgtadm --lld iscsi --op delete --mode target --tid 1

不回显,命令成功;直接就把target给删除了,根本没提示你里面是否包含LUN,如果有客户端连接,客户端必须登出(客户端运行iscsiadm -m node -T iqn.node212 -p 192.168.7.212 –u)

用tgtadm --lld iscsi --op show --mode target看已经什么都没了。

 

 

LINUX客户端设置:

发现网络中可用的target

iscsiadm -m discovery -t sendtargets -p 192.168.7.212

#iscsiadm -m discovery -t sendtargets -p 192.168.7.212

192.168.7.212:3260,1 iqn.node212

也可以写成iscsiadm -m discovery -t sendtargets -p 192.168.7.212:3260,不写端口默认找3260

登入可用的target会话

# iscsiadm -m node -T iqn.node212 -p 192.168.7.212:3260 -l

Logging in to [iface: default, target: iqn.node212, portal: 192.168.7.212,3260]

Login to [iface: default, target: iqn.node212, portal: 192.168.7.212,3260]: successful

 

表示成功,fdisk –l查看,会看到这个设备。

 

查看target信息:

Iscsiadm –m node –P N 查看节点信息(N=0,1)

Iscsiadm –m session –P N 查看iscsi的会话信息(N=1-3)

Iscsiadm –m discovery –P N 查看discovery信息(N=0,1)

 

登出一个target会话


Logging out of session [sid: 1, target: iqn.node212, portal: 192.168.7.212,3260]

Logout of [sid: 1, target: iqn.node212, portal: 192.168.7.212,3260]: successful

 

 

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