iscsi服务配置及

 

架设iscsi target服务
服务端软件:scsi-target-utils
客户端:    scsi-initiator-utils

/etc/tgt/targets.conf:主要配置文件,设定要分享的磁盘格式与哪几颗;
/usr/sbin/tgt-admin:在线查询、删除 target 等功能的设定工具;
/usr/sbin/tgt-setup-lun:建立 target 以及设定分享的磁盘与可使用的客户端等工具软件。
/usr/sbin/tgtadm:手动直接管理的管理员工具 (可使用配置文件取代);
/usr/sbin/tgtd:主要提供 iSCSI target 服务的主程序;
/usr/sbin/tgtimg:建置预计分享的映像文件装置的工具 (以映像文件仿真磁盘);

共享出去的设备可以是一个档案、一个没有用到的分区、一个LV(LVM)
共享一个文档target
[root@compute-192-168-2-190 opt]# dd if=/dev/zero of=/opt/cloud.img bs=1 count=0 seek=100G
0+0 records in
0+0 records out
0 bytes (0 B) copied, 8.7639e-05 s, 0.0 kB/s
[root@compute-192-168-2-190 opt]#

一个磁盘分区/dev/sdb5
Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       60801   488384001    5  Extended
/dev/sdb5               1       13055   104864224+  83  Linux
/dev/sdb6           13056       26110   104864256   83  Linux
/dev/sdb7           26111       39165   104864256   83  Linux
/dev/sdb8           39166       60801   173791138+  83  Linux

共享一个LV target(/dev/sdb6)
[root@compute-192-168-2-190 opt]# pvcreate /dev/sdb6
  dev_is_mpath: failed to get device for 8:22
  Physical volume "/dev/sdb6" successfully created
[root@compute-192-168-2-190 opt]# vgcreate lvmpool /dev/sdb6
  Volume group "lvmpool" successfully created
[root@compute-192-168-2-190 opt]# pvcreate /dev/sdb8
  Physical volume "/dev/sdb8" successfully created
[root@compute-192-168-2-190 opt]# vgextend lvmpool /dev/sdb8
  Volume group "lvmpool" successfully extended  
[root@compute-192-168-2-190 opt]# vgdisplay
  --- Volume group ---
  VG Name               lvmpool
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               265.74 GiB
  PE Size               4.00 MiB
  Total PE              68030
  Alloc PE / Size       0 / 0
  Free  PE / Size       68030 / 265.74 GiB
  VG UUID               SaiQH7-cRQ8-LXNE-78yy-4oXS-UcS3-JOej2h  

[root@compute-192-168-2-190 opt]# lvcreate -L 20G -n iscsi lvmpool
  Logical volume "iscsi" created
[root@compute-192-168-2-190 opt]# lvscan
  ACTIVE            '/dev/lvmpool/iscsi' [20.00 GiB] inherit


分享的iscsi target文档命名
以iqn开头表示iscsi qualified name(iscsi合格名称)
iqn.yyy-mm.<reversed domain name>:identifier
iqn.年-月.网域名的反转写法:这个分享target的名称
iqn.2015-03.compute-192-168-2-190:youyun
如同多个磁盘一样,target也可以有多个,每个在同一个target上的磁盘我们可以第一个逻辑单位编号。
我们的iscsi initiator就跟target协调后才取得lun的存取权,这个里我们会在一个target上使用三个磁盘

修改tgt的配置文件/etc/tgt/targets.conf
vim /etc/tgt/targets.conf
# Sample target with one LUN only. Defaults to allow access for all initiators:

<target iqn.2015-03.compute-192-168-2-190:youyun>
    backing-store /dev/sdb5
    backing-store /opt/cloud.img
    backing-store /dev/lvmpool/iscsi
</target>
配置文件中除了backing-store项外还有其他可选配置项:
1.backing-store (虚拟的装置), direct-store (实际的装置):
 设定装置时,如果你的整颗磁盘是全部被拿来当 iSCSI 分享之用,
那么才能够使用 direct-store 。不过,根据网络上的其他文件,
似乎说明这个设定值有点危险的样子。所以,基本上还是建议单纯使用模拟的 backing-store 较佳
2.initiator-address (用户端地址): 如果你想要限制能够使用这个 target 的客户端来源,
才需要填写这个设定值。基本上,不用设定它 (代表所有人都能使用的意思),
因为我们后来会使用 iptables 来规范可以联机的客户端嘛!
3.incominguser (用户账号密码设定): 如果除了来源 IP 的限制之外,
你还想要让使用者输入账密才能使用你的 iSCSI target 的话,那么就加用这个设定项目。
此设定后面接两个参数,分别是账号与密码啰。
4.write-cache [off|on] (是否使用快取): 在预设的情况下,tgtd 会使用快取来增快速度。
不过,这样可能会有遗失数据的风险。所以,如果你的数据比较重要的话,
 或许不要使用快取,直接存取装置会比较妥当一些。
<target iqn.2015-03.compute-192-168-2-190:youyun>
    backing-store /dev/sdb5
    backing-store /opt/cloud.img
    backing-store /dev/lvmpool/iscsi
  initiator-address 192.168.2.0/24 #只允许192.168.2.0/24这个网段的访问
  incominguser  usyouyun psyouyun  #用户名  密码
  write-cache off #关掉缓存
</target>
注:initiator-addrress以及下面的几项要缩进一格(如果正常也不用缩进)
 
启动iscsi target服务查看相关端口
[root@compute-192-168-2-190 ~]# chkconfig tgtd on
[root@compute-192-168-2-190 ~]# netstat -tlunp| grep tgt
tcp        0      0 0.0.0.0:3260                0.0.0.0:*                   LISTEN      27625/tgtd
tcp        0      0 :::3260                     :::*                        LISTEN      27625/tgtd
查看target相关信息:
[root@compute-192-168-2-190 ~]# tgt-admin --show
Target 1: iqn.2015-03.compute-192-168-2-190:youyun
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
    LUN information:
        LUN: 0        #这是个控制器,是不可用的lun
            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: 21475 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/lvmpool/iscsi
            Backing store flags:
        LUN: 2
            Type: disk
            SCSI ID: IET     00010002
            SCSI SN: beaf12
            Size: 107381 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /dev/sdb5
            Backing store flags:
        LUN: 3
            Type: disk
            SCSI ID: IET     00010003
            SCSI SN: beaf13
            Size: 107374 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /opt/cloud.img
            Backing store flags:
    Account information:
        usyouyun
    ACL information:
        192.168.2.0/24
设定防火墙:
iptables必须要打开tgt的端口3260
[root@compute-192-168-2-190 ~]# iptables -I INPUT -p tcp -s 192.168.2.0/24 --dport 3260 -j ACCEPT
[root@compute-192-168-2-190 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@compute-192-168-2-190 ~]# iptables-save | grep 3260
-A INPUT -s 192.168.2.0/24 -p tcp -m tcp --dport 3260 -j ACCEPT

架设客户端iscsi initiator获取target lun的使用权
相关的软件与软件架构
/etc/iscsi/iscsid.conf:主要的配置文件,用来连结到 iSCSI target 的设定;
/sbin/iscsid:启动 iSCSI initiator 的主要服务程序;
/sbin/iscsiadm:用来管理 iSCSI initiator 的主要设定程序;
/etc/init.d/iscsid:让本机模拟成为 iSCSI initiater 的主要服务;
/etc/init.d/iscsi:在本机成为 iSCSI initiator 之后,启动此脚本,让我们可以登入 iSCSI target。
所以 iscsid 先启动后,才能启动这个服务。为了防呆,所以 /etc/init.d/iscsi 已经写了一个启动指令,
 启动 iscsi 前尚未启动 iscsid ,则会先呼叫 iscsid 才继续处理 iscsi

iscsid.conf 里面大概只要设定好登入 target 时的帐密即可,
其他的 target 搜寻、设定、取得的方法都直接使用 iscsiadm 这个指令来完成。
由于 iscsiadm 侦测到的结果会直接写入 /var/lib/iscsi/nodes/ 当中,
因此只要启动 /etc/init.d/iscsi 就能够在下次开机时,自动的连结到正确的 target

initiator架设操作

如果target端设置了,账户和密码则要在client端修改相关的配置项
[root@compute-192-168-2-190 ~]# vim /etc/iscsi/iscsid.conf
node.session.auth.username = usyouyun
node.session.auth.password = psyouyun

discovery.sendtargets.auth.username = usyouyun
discovery.sendtargets.auth.password = psyouyun
[root@compute-192-168-2-190 ~]# chkconfig iscsid on
[root@compute-192-168-2-190 ~]# chkconfig iscsi on
由于我们尚未与 target 联机,所以 iscsi 并无法让我们顺利启动的!因此上面只要 chkconfig 即可,
不需要启动他。 要开始来侦测 target 与写入系统信息啰。全部使用 iscsiadm 这个指令就可以完成所有动作了。

侦测192.168.2.190这部target的相关数据
这里假设不知道target名字
iscsiadm -m discovery -t sendtargets -p IP:port
选项与参数:
-m discovery   :使用侦测的方式进行 iscsiadmin 指令功能;
-t sendtargets :透过 iscsi 的协议,侦测后面的设备所拥有的 target 数据
-p IP:port     :就是那部 iscsi 设备的 IP 与埠口,不写埠口预设是 3260 啰!
example:
[root@compute-192-168-2-190 ~]# iscsiadm -m discovery -t sendtargets -p 192.168.2.190:3260
Starting iscsid:                                           [  OK  ]
192.168.2.190:3260,1 iqn.2015-03.compute-192-168-2-190:youyun
[root@compute-192-168-2-190 ~]# ll -R /var/lib/iscsi/nodes/
/var/lib/iscsi/nodes/:
total 4
drw-------. 3 root root 4096 Mar 20 23:58 iqn.2015-03.compute-192-168-2-190:youyun

/var/lib/iscsi/nodes/iqn.2015-03.compute-192-168-2-190:youyun:
total 4
drw-------. 2 root root 4096 Mar 20 23:58 192.168.2.190,3260,1

/var/lib/iscsi/nodes/iqn.2015-03.compute-192-168-2-190:youyun/192.168.2.190,3260,1:
total 4
-rw-------. 1 root root 1903 Mar 20 23:58 default
装载(登陆)target
因为我们的 initiator 可能会连接多部的 target 设备,因此,我们得先要瞧瞧目前系统上面侦测到的 target 有几部,
然后再找到我们要的那部 target 来进行登入的作业。不过,如果你想要将所有侦测到的 target 全部都登入的话, 那么整个步骤可以再简化:
根据前一个步骤侦测到的资料,装载(登陆)全部的 target(只要重启iscsi即可他会自动登陆/var/lib/iscsi/nodes/下的所有侦测到的target)
[root@compute-192-168-2-190 ~]# service iscsi restart
Stopping iscsi:                                            [  OK  ]
Starting iscsi:                                            [  OK  ]
# 将系统里面全部的 target 通通以 /var/lib/iscs/nodes/ 内的设定登入
# 上面的特殊字体比较需要注意啦!你只要做到这里即可,所有target已经登陆了,底下的瞧瞧就好
[root@compute-192-168-2-190 ~]# fdisk -l | grep 'Disk /dev/s'
Disk /dev/sda: 500.1 GB, 500107862016 bytes
Disk /dev/sdb: 500.1 GB, 500107862016 bytes
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
Disk /dev/sdd: 107.4 GB, 107380965888 bytes
Disk /dev/sde: 107.4 GB, 107374182400 bytes
显示目前系统上所有的target信息
[root@compute-192-168-2-190 ~]# iscsiadm -m node
192.168.2.190:3260,1 iqn.2015-03.compute-192-168-2-190:youyun
选项与参数:
-m node:找出目前本机上面所有侦测到的 target 信息,可能并未登入

只登陆指定的target
 iscsiadm -m node -T target名称 --login
选项与参数:
-T target名称:仅使用后面接的那部 target ,target 名称可用上个指令查到!
--login      :就是登入
[root@compute-192-168-2-190 ~]# iscsiadm -m node -T iqn.2015-03.compute-192-168-2-190:youyun --login
Logging in to [iface: default, target: iqn.2015-03.compute-192-168-2-190:youyun, portal: 192.168.2.190,3260] (multiple)
Login to [iface: default, target: iqn.2015-03.compute-192-168-2-190:youyun, portal: 192.168.2.190,3260] successful.

当实际的iscsi被移除时,在你的client中还是存在它的信息的,这是就需要你手动删除已经不存在的target了步骤1.退出该target 2.删除target
#iscsiadm -m node -T targetname --logout
退出  --logout
[root@compute-192-168-2-190 ~]# iscsiadm -m node -T iqn.2015-03.compute-192-168-2-190:youyun --logout
Logging out of session [sid: 1, target: iqn.2015-03.compute-192-168-2-190:youyun, portal: 192.168.2.190,3260]
Logout of [sid: 1, target: iqn.2015-03.compute-192-168-2-190:youyun, portal: 192.168.2.190,3260] successful.
注:这里虽然已经退出了,但是client和target的连接还是存在的,只是看不到target提供的设备而已
[root@compute-192-168-2-190 ~]# iscsiadm -m node
192.168.2.190:3260,1 iqn.2015-03.compute-192-168-2-190:youyun

#iscsiadm -m node -o [delete|new|update] -T targetname
选项与参数:
--logout :就是注销 target,但是并没有删除 /var/lib/iscsi/nodes/ 内的数据
-o delete:删除后面接的那部 target 链接信息 (/var/lib/iscsi/nodes/*)
-o update:更新相关的信息
-o new   :增加一个新的 target 信息。
[root@compute-192-168-2-190 ~]# iscsiadm -m node -o delete -T iqn.2015-03.compute-192-168-2-190:youyun
[root@compute-192-168-2-190 ~]# service iscsi restart
Stopping iscsi:                                            [  OK  ]
[root@compute-192-168-2-190 ~]# iscsiadm -m node
iscsiadm: No records found

要想恢复就要重新侦测一下iscsi target,然后从新登陆(装载)

查看是谁在使用我们的target
在target服务器上执行
tgt-admin --show
[root@compute-192-168-2-190 ~]# tgt-admin --show
Target 1: iqn.2015-03.compute-192-168-2-190:youyun
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
        I_T nexus: 3
            Initiator: iqn.1994-05.com.redhat:4a02ab4ecc #注意initiator的名字是redhat,这里可以不用管它,对服务和使用是没有影响的
                                                          #如果想修改的话,到initiator节点上修改/etc/iscsi/initiatorname.iscsi文件即可。
            Connection: 0
                IP Address: 192.168.2.190      #192.168.2.190 在使用target
        I_T nexus: 4
            Initiator: iqn.1994-05.com.redhat:284ce7ee36f6  
            Connection: 0
                IP Address: 192.168.2.191      #192.168.2.191 在使用target
    LUN information:
        LUN: 0
            Type: controller

省略。。。。
错误:
[root@compute-192-168-2-190 ~]# tgt-admin --show
tgtadm: can't send the request to the tgt daemon, Transport endpoint is not connected

 

方法:kill tgtd进程重启tgtd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值