Linux上open-iscsi 的安装,配置和使用

关于open-iscsi

open-iscsi是一个实现 RFC3720 iSCSI协议的高性能initiator程序。iSCSI使得访问SAN上的存储不再只能依赖Fibre Channel,也可以通过TCP协议和以太网络。在很多Linux平台都可以方便的下载到open-iscsi包。

有了它就可以在Linux上直接连接远端的block device了,就像使用本地block device一样方便。

 


 

目前是open-iscsi包含两个部分:内核部分和用户空间部分

  • Kernel part(内核)

实现了scsi read/write功能,包含3个内核模块 scsi_transport_iscsi.ko, libiscsi.ko and iscsi_tcp.ko,在安装了open-iscsi的Linux上,可以查看到。

$ sudo lsmod | grep iscsi -i
iscsi_trgt            101664  5 
iscsi_tcp              17580  0 
libiscsi_tcp           21554  1 iscsi_tcp
libiscsi               48004  3 libiscsi_tcp,iscsi_tcp,ib_iser
scsi_transport_iscsi    77478  4 iscsi_tcp,ib_iser,libiscsi
scsi_mod              191405  12 sg,scsi_transport_spi,scsi_dh,scsi_transport_iscsi,libata,mptspi,sd_mod,sr_mod,iscsi_tcp,mptscsih,ib_iser,libiscsi

 

  • User space part(用户空间)

主要完成如:配置管理,iSCSI target的自动发现,登录、登出,错误处理和连接保持等。用户通过 iscsiadm 来管理initiator端与target端的连接。下面是管理工具iscsiadm的使用帮助。

$ sudo iscsiadm --help
iscsiadm -m discoverydb [ -hV ] [ -d debug_level ] [-P printlevel] [ -t type -p ip:port -I ifaceN ... [ -Dl ] ] | [ [ -p ip:port -t type] [ -o operation ] [ -n name ] [ -v value ] [ -lD ] ] 
iscsiadm -m discovery [ -hV ] [ -d debug_level ] [-P printlevel] [ -t type -p ip:port -I ifaceN ... [ -l ] ] | [ [ -p ip:port ] [ -l | -D ] ] 
iscsiadm -m node [ -hV ] [ -d debug_level ] [ -P printlevel ] [ -L all,manual,automatic ] [ -U all,manual,automatic ] [ -S ] [ [ -T targetname -p ip:port -I ifaceN ] [ -l | -u | -R | -s] ] [ [ -o  operation  ] [ -n name ] [ -v value ] ]
iscsiadm -m session [ -hV ] [ -d debug_level ] [ -P  printlevel] [ -r sessionid | sysfsdir [ -R | -u | -s ] [ -o operation ] [ -n name ] [ -v value ] ]
iscsiadm -m iface [ -hV ] [ -d debug_level ] [ -P printlevel ] [ -I ifacename | -H hostno|MAC ] [ [ -o  operation  ] [ -n name ] [ -v value ] ] [ -C ping [ -a ip ] [ -b packetsize ] [ -c count ] [ -i interval ] ]
iscsiadm -m fw [ -d debug_level ] [ -l ]
iscsiadm -m host [ -P printlevel ] [ -H hostno|MAC ] [ [ -C chap [ -o operation ] [ -v chap_tbl_idx ] ] | [ -C flashnode [ -o operation ] [ -A portal_type ] [ -x flashnode_idx ] [ -n name ] [ -v value ] ] ]
iscsiadm -k priority

 

安装open-iscsi

  • Debian/Ubuntu

sudo apt-get install open-iscsi
  • Redhat/Centos/Suse

yum install iscsi-initiator-utils

安装完成后,比较重要的配置文件有两个: /etc/iscsi/iscsid.conf  以及 /etc/iscsi/initiatorname.iscsi 

iscsi的基本配置文件

  • /etc/iscsi/initiatorname.iscsi 

这个文件包含host的initiator IQN,在很多企业级存储上都要用到这个,否则无法访问存储上的资源。

$ sudo cat /etc/iscsi/initiatorname.iscsi 
## DO NOT EDIT OR REMOVE THIS FILE!
## If you remove this file, the iSCSI daemon will not start.
## If you change the InitiatorName, existing access control lists
## may reject this initiator.  The InitiatorName must be unique
## for each iSCSI initiator.  Do NOT duplicate iSCSI InitiatorNames.
InitiatorName=iqn.1993-08.org.debian:01:2b22ec4c44d
  • /etc/iscsi/iscsid.conf 

这个文件包含open-iscsi的配置,下面会提到一些个人经常遇到的一些设置

node.startup = [automatic | manual ]

# To request that the iscsi initd scripts startup a session set to "automatic".
# node.startup = automatic
#
# To manually startup the session set to "manual". The default is manual.
node.startup = manual

 

 

如果node.start = automatic,所有login过的session在下次重启的时候都会再次被login。

 

node.conn[0].timeo.login_timeout = <以秒记得超时>

node.conn[0].timeo.login_timeout = 15

上面是单次登录的超时时间

node.session.initial_login_retry_max = <登录重试次数>

node.session.initial_login_retry_max = 8

登录失败后的最大重试次数

 

当然/etc/iscsi/iscsid.conf 的配置绝大多数都可以通过  iscsiadm --mode node --op <command>  来对特定的node/session针对性的配置,后面会提到。

iscsiadm的使用

iscsiadm使用基本分3个步骤:发现(discovery),登录(login),扫描(rescan)

在使用iscsiadm的过程中有两个名词要注意下

  • target portal:  格式一般是 <IP>:<PORT>, 比如  192.168.183.129:3260
  • target IQN: 指服务器端的 iqn- 打头的一串字符 iqn.2001-04.com.example:storage.lun2

portal和IQN可以唯一确定一个storage的target。

 

 

1. Discovery

 

discovery只要知道storage上的target portal就可以发现所有暴露的portal和iqn。

$ sudo iscsiadm -m discovery -t sendtargets -p 192.168.183.129
192.168.183.129:3260,1 iqn.2001-04.com.example:storage.lun2
192.168.183.129:3260,1 iqn.2001-04.com.example:storage.lun1

上面发现 绿色 的就是storage上的所有target portals, 红色 就是 IQN

 

 

2. login

 

在第一步中发现的target都会保存在本地的数据库中,可以用 sudo iscsiadm -m node 看到。

  • 登录全部target

如果你要登录第一步中所有的portal,可以直接用下面的命令一键登录:

$ sudo iscsiadm -m node --login
Logging in to [iface: default, target: iqn.2001-04.com.example:storage.lun2, portal: 192.168.183.129,3260] (multiple)
Logging in to [iface: default, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.183.129,3260] (multiple)
Login to [iface: default, target: iqn.2001-04.com.example:storage.lun2, portal: 192.168.183.129,3260] successful.
Login to [iface: default, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.183.129,3260] successful.
  • 登录特定的target

$ sudo iscsiadm -m node -T iqn.2001-04.com.example:storage.lun1 -p 192.168.183.129:3260 --login
Logging in to [iface: default, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.183.129,3260] (multiple)
Login to [iface: default, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.183.129,3260] successful.

 

  • 查看登录的session

$ sudo iscsiadm -m session
tcp: [3] 192.168.183.129:3260,1 iqn.2001-04.com.example:storage.lun1 (non-flash)

注意,在默认配置下,登录的session,重启是不会再次登录的,如果要想登录的session自动登录,一种方式是,前面提到的设置 node.start = automatic 

  • 设置session自动登录

$ sudo iscsiadm -m node -T iqn.2001-04.com.example:storage.lun2 -p 192.168.183.129:3260 --op update -n node.startup -v automatic

# 查看session的设置 $
sudo iscsiadm -m node -T iqn.2001-04.com.example:storage.lun2 -p 192.168.183.129:3260 | grep node.startup node.startup = automatic

 

3. rescan

 

一旦登录成功,所有分配给当前主机的LUN都可以看到了,但有时,新分配的LUN,LUN的信息发生变化后,主要不能立即看到,这样就需要rescan了  --rescan 。

  • 扫描所有登录的target/session

$ sudo iscsiadm -m session --rescan
Rescanning session [sid: 3, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.183.129,3260]

也可以

  $ sudo iscsiadm -m node --rescan
  Rescanning session [sid: 3, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.183.129,3260]

 

  • 扫描单个session

可以通过session id

$ sudo iscsiadm -m session
tcp: [3] 192.168.183.129:3260,1 iqn.2001-04.com.example:storage.lun1 (non-flash)
tcp: [4] 192.168.183.129:3260,1 iqn.2001-04.com.example:storage.lun2 (non-flash)

上面的 [3], [4]都是session id,直接使用即可:

$ sudo iscsiadm -m session --sid 3 --rescan
Rescanning session [sid: 3, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.183.129,3260]

试了下,不可以通过target和iqn登录,不知道是bug还是参数问题:

$ sudo iscsiadm -m node -T iqn.2001-04.com.example:storage.lun1 -p 192.168.183.129:3260 --rescan
Rescanning session [sid: 3, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.183.129,3260]
iscsiadm: invalid error code 65280
iscsiadm: Could not execute operation on all sessions: (null)

 

4. 找到disk

1. iscsi连接的disk可以通过查询路径  /dev/disk/by-path/ 下的链接

# ls -l /dev/disk/by-path/
total 0
lrwxrwxrwx 1 root root  9 Jun  3 03:01 ip-192.168.183.129:3260-iscsi-iqn.2001-04.com.example:storage.lun1-lun-1 -> ../../sdc
lrwxrwxrwx 1 root root  9 Jun  3 03:08 ip-192.168.183.129:3260-iscsi-iqn.2001-04.com.example:storage.lun2-lun-2 -> ../../sdd
lrwxrwxrwx 1 root root  9 Jun  3 01:26 pci-0000:00:10.0-scsi-0:0:0:0 -> ../../sda
lrwxrwxrwx 1 root root 10 Jun  3 01:26 pci-0000:00:10.0-scsi-0:0:0:0-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 Jun  3 01:26 pci-0000:00:10.0-scsi-0:0:0:0-part2 -> ../../sda2
lrwxrwxrwx 1 root root 10 Jun  3 01:26 pci-0000:00:10.0-scsi-0:0:0:0-part5 -> ../../sda5
lrwxrwxrwx 1 root root  9 Jun  3 01:26 pci-0000:00:10.0-scsi-0:0:1:0 -> ../../sdb

可以看到,在我的测试中,连接的两个target分别暴露了一个LUN给host,lun1 和 lun2,对应的host上的两个device是 /dev/sdc /dev/sdd

 

2. 也可以通过 sudo fdisk -l  查看disk:

$ sudo fdisk -l

Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xcaf795e0

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 80383999 80381952 38.3G 83 Linux
/dev/sda2       80386046 83884031  3497986  1.7G  5 Extended
/dev/sda5       80386048 83884031  3497984  1.7G 82 Linux swap / Solaris

Disk /dev/sdc: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/149455400000000003592265eae69d00a6e8560cd2833744e: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sdd: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/149455400000000005277c9d1f32625c43968336de76fc205: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

NOTE: /dev/mapper/*** 是我安装了multipath-tools后生成的device

如果disk很多的话,还是推荐第一个方法来确定disk路径,否则搞错了,数据丢失可是来不及哭爹喊娘的哦:)

 

总结:

总的来说,iscsi的使用还是比较方便,就是一旦长期不用,后面使用的话,一般都只能复制,粘贴了,枯燥不说,还容易出错。

由于我日常的工作中经常跟open-iscsi、multipath、fc等等打交道,经常觉得:要发现一个新的device步骤非常繁琐。

于是我自己决定开发一个方便的tool/API,简化跟stoage打交道的步骤。项目地址是:https://github.com/peter-wangxu/goock

欢迎大家使用和提供宝贵的意见和建议。

 

参考资料


 

https://github.com/open-iscsi/open-iscsi

https://tools.ietf.org/html/rfc7143

 

posted @ 2017-06-03 16:06  孤独的居士  阅读(15664)  评论(0编辑  收藏  举报