open-iscsi是一個實現 RFC3720 iSCSI協議的高性能initiator程序。iSCSI使得訪問SAN上的存儲再也不只能依賴Fibre Channel,也能夠經過TCP協議和以太網絡。在不少Linux平臺均可以方便的下載到open-iscsi包。html
有了它就能夠在Linux上直接鏈接遠端的block device了,就像使用本地block device同樣方便。node
目前是open-iscsi包含兩個部分:內核部分和用戶空間部分git
實現了scsi read/write功能,包含3個內核模塊 scsi_transport_iscsi.ko, libiscsi.ko and iscsi_tcp.ko,在安裝了open-iscsi的Linux上,能夠查看到。github
$ 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
主要完成如:配置管理,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
sudo apt-get install open-iscsi
yum install iscsi-initiator-utils
安裝完成後,比較重要的配置文件有兩個: /etc/iscsi/iscsid.conf 以及 /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
這個文件包含open-iscsi的配置,下面會提到一些我的常常遇到的一些設置session
# 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。app
node.conn[0].timeo.login_timeout = 15
上面是單次登陸的超時時間tcp
node.session.initial_login_retry_max = 8
登陸失敗後的最大重試次數
固然/etc/iscsi/iscsid.conf 的配置絕大多數均可以經過 iscsiadm --mode node --op <command> 來對特定的node/session針對性的配置,後面會提到。
iscsiadm使用基本分3個步驟:發現(discovery),登陸(login),掃描(rescan)
在使用iscsiadm的過程當中有兩個名詞要注意下
portal和IQN能夠惟一肯定一個storage的target。
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
在第一步中發現的target都會保存在本地的數據庫中,能夠用 sudo iscsiadm -m node 看到。
若是你要登陸第一步中全部的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.
$ 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.
$ 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
$ 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
一旦登陸成功,全部分配給當前主機的LUN均可以看到了,但有時,新分配的LUN,LUN的信息發生變化後,主要不能當即看到,這樣就須要rescan了 --rescan 。
$ 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 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)
# 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
$ 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