配置LIO iSCSI Target
现在主流Linux都可以设置iSCSI,如STGT/TGT、LIO Target等。Linux-IO(LIO)Target是当前Linux标准的iSCSI target的开源实现,包含在Linux内核(2.6.38后)中。LIO不发起会话,而是提供LUNs,等待来自SCSI initiator的命令并进行所需的I/O数据传输。更详细的参考:http://en.wikipedia.org/wiki/LIO_Target
设置一个target,包含以下组件:
存储设备:使用Linux作为target,可以使用LVM逻辑卷来作为底层存储设备,也可以使用其他存储设备,如分区、硬盘、稀疏文件等。
LUN ID:每个通过iSCSI target共享的存储设备是当做一个LUN来共享,每个LUN都有一个唯一ID。
Target ID:如果想要授权target连接到特定节点,创建不同的target,每个target有自己的target ID,也称为IQN(Internet Qualifiied Name)。iSCSI客户端使用IQN来进行连接。
Identifier:标识符用来识别特定的iSCSI target。
端口号:target监听的TCP端口,默认是3260。
CentOS7上使用targetcli命令来设置iSCSI target.
# systemctl enable target.service
# systemctl start target.service
targetcli支持不同的后端存储,当创建iSCSI磁盘时,必须指定后端存储类型。targetcli来查看可用的后端存储。
# targetcli
targetcli shell version 2.1.fb34
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> ls
o- / ............................................................................... [...]
o- backstores .................................................................... [...]
| o- block ........................................................ [Storage Objects: 0]
| o- fileio ....................................................... [Storage Objects: 0]
| o- pscsi ........................................................ [Storage Objects: 0]
| o- ramdisk ...................................................... [Storage Objects: 0]
o- iscsi .................................................................. [Targets: 0]
o- loopback ............................................................... [Targets: 0]
添加LVM,使用命令“/backstores/fileio create lun1 /opt/disk1.img 100M”可以添加一个稀疏文件的target。
/> /backstores/block create lun0 /dev/rootvg/image
Created block storage object lun0 using /dev/rootvg/image.
/> ls
o- / ............................................................................... [...]
o- backstores .................................................................... [...]
| o- block ........................................................ [Storage Objects: 1]
| | o- lun0 ........................ [/dev/rootvg/image (5.0GiB) write-thru deactivated]
| o- fileio ....................................................... [Storage Objects: 0]
| o- pscsi ........................................................ [Storage Objects: 0]
| o- ramdisk ...................................................... [Storage Objects: 0]
o- iscsi .................................................................. [Targets: 0]
o- loopback ............................................................... [Targets: 0]
定义target
/> /iscsi create
Created target iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625.
Created TPG 1.
查看创建的TPG
/> ls
o- / ............................................................................... [...]
o- backstores .................................................................... [...]
| o- block ........................................................ [Storage Objects: 1]
| | o- lun0 ........................ [/dev/rootvg/image (5.0GiB) write-thru deactivated]
| o- fileio ....................................................... [Storage Objects: 0]
| o- pscsi ........................................................ [Storage Objects: 0]
| o- ramdisk ...................................................... [Storage Objects: 0]
o- iscsi .................................................................. [Targets: 1]
| o- iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625 ............ [TPGs: 1]
| o- tpg1 ..................................................... [no-gen-acls, no-auth]
| o- acls ................................................................ [ACLs: 0]
| o- luns ................................................................ [LUNs: 0]
| o- portals .......................................................... [Portals: 0]
o- loopback ............................................................... [Targets: 0]
使用默认配置创建portal
/> cd /iscsi/iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625/tpg1/
/iscsi/iqn.20...60959625/tpg1> portals/ create
Using default IP port 3260
Binding to INADDR_ANY (0.0.0.0)
Created network portal 0.0.0.0:3260.
/iscsi/iqn.20...60959625/tpg1>
指定LUN到portal,如果想要对LUN进行访问限制,使用iSCSI initiator的IQN,然后查看当前设置,最终saveconfig保存。
Created network portal 0.0.0.0:3260.
/iscsi/iqn.20...60959625/tpg1> luns/ create /backstores/block/lun0
Created LUN 0.
/iscsi/iqn.20...60959625/tpg1> acls/ create iqn.2014-03.com.example:123456789
Created Node ACL for iqn.2014-03.com.example:123456789
Created mapped LUN 0.
/iscsi/iqn.20...60959625/tpg1> ls
o- tpg1 ........................................................... [no-gen-acls, no-auth]
o- acls ...................................................................... [ACLs: 1]
| o- iqn.2014-03.com.example:123456789 ................................ [Mapped LUNs: 1]
| o- mapped_lun0 .............................................. [lun0 block/lun0 (rw)]
o- luns ...................................................................... [LUNs: 1]
| o- lun0 ............................................. [block/lun0 (/dev/rootvg/image)]
o- portals ................................................................ [Portals: 1]
o- 0.0.0.0:3260 ................................................................. [OK]
/iscsi/iqn.20...60959625/tpg1> cd /
/> ls
o- / ............................................................................... [...]
o- backstores .................................................................... [...]
| o- block ........................................................ [Storage Objects: 1]
| | o- lun0 .......................... [/dev/rootvg/image (5.0GiB) write-thru activated]
| o- fileio ....................................................... [Storage Objects: 0]
| o- pscsi ........................................................ [Storage Objects: 0]
| o- ramdisk ...................................................... [Storage Objects: 0]
o- iscsi .................................................................. [Targets: 1]
| o- iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625 ............ [TPGs: 1]
| o- tpg1 ..................................................... [no-gen-acls, no-auth]
| o- acls ................................................................ [ACLs: 1]
| | o- iqn.2014-03.com.example:123456789 .......................... [Mapped LUNs: 1]
| | o- mapped_lun0 ........................................ [lun0 block/lun0 (rw)]
| o- luns ................................................................ [LUNs: 1]
| | o- lun0 ....................................... [block/lun0 (/dev/rootvg/image)]
| o- portals .......................................................... [Portals: 1]
| o- 0.0.0.0:3260 ........................................................... [OK]
o- loopback ............................................................... [Targets: 0]
/> saveconfig
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
连接到iSCSI SAN
• node:登录到特定的iSCSI target。
• session: 从当前会话获取信息或者建立一个新的会话到已经连接的target。
• iface和host: 允许指定连接到特定target的方式。
当连接到iSCSI target后,会自动创建配置文件,机器重启后,服务器会自动连接到以前的iSCSI连接。在用iscsiadm连接iSCSI target前,需要确保相关module已经加载。
第一步:Discovery模式
使用iscsiadm --mode discovery --type sendtargets --portal xxxx:3260 --discover来发现iSCSI target。
# iscsiadm --mode discovery --type sendtargets --portal 10.0.0.11:3260 --discover
10.0.0.11:3260,1 iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625
上面的命令不仅显示target的名字,并且将iSCSI配置添加到/var/lib/iscsi/send_targets目录下(redhat)。可以使用“-P”(0或1代表debug级别)选项来输出保存的信息。从下面可以看出是使用SENDTARGETS发现类型。
# iscsiadm --mode discoverydb -P 1
SENDTARGETS:
DiscoveryAddress: 10.0.0.11,3260
Target: iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625
Portal: 10.0.0.11:3260,1
Iface Name: default
iSNS:
No targets found.
STATIC:
No targets found.
FIRMWARE:
No targets found.
根据不同的SAN环境,所有的发现类型都可用,iSNS可以设置一个iSNS服务器,它集中注册了iSCSI target。fireware是使用iSCSI硬件适配器可以发现iSCSI target。SLP目前不可用。
第二步:node模式
在执行上一步后,可以知道taget的IQN。下一步登录target来建立连接。node模式是iSCSI术语,表示iSCSI target和特定的portal之间的实际连接。portal是连接到iSCSI target的地址和端口。
# iscsiadm --mode node --targetname iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625 --portal 10.0.0.11:3260 --login
Logging in to [iface: default, target: iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625, portal: 10.0.0.11,3260] (multiple)
Login to [iface: default, target: iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625, portal: 10.0.0.11,3260] successful.
使用lsscsi命令可以验证连接,发现新增了LIO-ORG类型的/dev/sdb。
# lsscsi
[1:0:0:0] cd/dvd NECVMWar VMware IDE CDR10 1.00 /dev/sr0
[2:0:0:0] disk VMware, VMware Virtual S 1.0 /dev/sda
<span style="color:#ff6600;">[3:0:0:0] disk LIO-ORG lun0 4.0 /dev/sdb </span>
在建立连接后,iSCSI initiator会将配置写到$ISCSI_ROOT/nodes目录。default文件包含了连接到iSCSI target的设置。
# ls -lR
.:
total 0
drw-------. 3 root root 29 Nov 7 03:41 iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625
./iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625:
total 0
drw-------. 2 root root 20 Nov 7 03:41 10.0.0.11,3260,1
./iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625/10.0.0.11,3260,1:
total 4
-rw-------. 1 root root 2064 Nov 7 03:41 default
第三步:管理iSCSI连接
查看当前连接信息:
# iscsiadm --mode node -P 1
Target: iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625
Portal: 10.0.0.11:3260,1
Iface Name: default
获取更详细的信息:
# iscsiadm --mode session -P 2
Target: iqn.2003-01.org.linux-iscsi.controller.x8664:sn.bb2460959625 (non-flash)
Current Portal: 10.0.0.11:3260,1
Persistent Portal: 10.0.0.11:3260,1
**********
Interface:
**********
Iface Name: default
Iface Transport: tcp
Iface Initiatorname: iqn.1994-05.com.redhat:c4e61affa28
Iface IPaddress: 10.0.0.31
Iface HWaddress: <empty>
Iface Netdev: <empty>
SID: 1
iSCSI Connection State: LOGGED IN
iSCSI Session State: LOGGED_IN
Internal iscsid Session State: NO CHANGE
*********
Timeouts:
*********
Recovery Timeout: 120
Target Reset Timeout: 30
LUN Reset Timeout: 30
Abort Timeout: 15
*****
CHAP:
*****
username: <empty>
password: ********
username_in: <empty>
password_in: ********
************************
Negotiated iSCSI params:
************************
HeaderDigest: None
DataDigest: None
MaxRecvDataSegmentLength: 262144
MaxXmitDataSegmentLength: 262144
FirstBurstLength: 65536
MaxBurstLength: 262144
ImmediateData: Yes
InitialR2T: Yes
MaxOutstandingR2T: 1
使用iscsi存储
# fdisk -l
Disk /dev/sda: 21.5 GB, 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 label type: dos
Disk identifier: 0x000b68ef
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 41943039 20458496 8e Linux LVM
Disk /dev/mapper/centos_compute-swap: 2147 MB, 2147483648 bytes, 4194304 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/centos_compute-root: 18.8 GB, 18798870528 bytes, 36716544 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
<span style="color:#ff6600;">Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 4194304 bytes</span>