關於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