配置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>