1. 程式人生 > >iscsi使用教程(中)

iscsi使用教程(中)

groups 操作 isn true pro x86 discovery driver pac

服務端管理命令

### tgtadm 是一個模式化的命令,其使用格式如下:
# tgtadm --lld [driver] --op [operation] --mode [mode] [OPTION]

其中模式(mode)和操作(operation)對應關系如下:

- 模式:
  - 操作:

- target模式:
  - new:創建一個新的目標
  - show:顯示目標
  - delete:刪除目標
  - update:更新模板
  - bind:IP地址控制
  - unbind:解除IP地址控制

- logicalunit模式:
  - new:創建一個新的LUN
  - delete:刪除一個LUN

- account模式(控制發現/登入目標時的客戶端用戶名):
  - new:創建賬戶
  - delete:刪除賬戶
  - bind:綁定賬戶到目標
  - unbind:解除目標的綁定賬戶

用法示例如下:

### 創建一個ID為[id],名稱為[name]的目標
# tgtadm --lld iscsi --op new --mode target --tid [id] --targetname [name]

### 顯示所有或ID為[id]的目標
# tgtadm --lld iscsi --op show --mode target [--tid [id]]

### 向ID為[id]的目標上添加一個新的編號為[lun]的LUN,其後端存儲對應的塊設備路徑為[path](LUN:0是系統預留編號)
# tgtadm --lld iscsi --op new --mode logicalunit --tid [id] --lun [lun] --backing-store [path]

### 刪除ID為[id]的目標
# tgtadm --lld iscsi --op delete --mode target --tid=[id]

### 刪除ID為[id]的目標上編號為[lun]的LUN
# tgtadm -lld iscsi --op delete --mode logicalunit --tid=[id] --lun [lun]

### 定義ID為[id]的目標的訪問控制列表,[address]為允許訪問的客戶端地址
# tgtadm --lld iscsi --op bind --mode target --tid [id] --initiator-address [address]

### 移除ID為[id]的目標的訪問控制列表
# tgtadm --lld iscsi --op unbind --mode target --tid [id] --initiator-address [address]

常用命令示例如下:

### 獲取目標信息
# tgtadm --lld iscsi --op show --mode target

### 創建目標
# tgtadm --lld iscsi --mode target --op new --tid 1 --targetname iqn.2010-10.org.openstack:vol

### 創建LUN
# tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 --bsoflags="sync" --backing-store=/dev/cinder-volumes/vol

### 配置訪問控制
# tgtadm --lld iscsi --op bind --mode target --tid 1 --initiator-address ALL

### 添加CHAP認證:創建賬戶
# tgtadm --lld iscsi --op new --mode account --user fwdssg --password fwdssg

### 添加CHAP認證:綁定目標
# tgtadm --lld iscsi --op bind --mode account --user fwdssg --tid 1

### 使用配置文件完成以上等價操作
# cat /etc/tgt/conf.d/vol.conf
<target iqn.2010-10.org.openstack:vol>
    backing-store /dev/cinder-volumes/vol
    driver iscsi
    incominguser fwdssg fwdssg
    write-cache on
</target>
# tgt-admin --update iqn.2010-10.org.openstack:vol

客戶端管理命令

### iscsiadm是個模式化的工具,其模式可通過-m或--mode選項指定,常見的模式有(如果沒有額外指定其它選項discovery和node會顯示其相關的所有記錄):
###     session:用於顯示所有的活動會話和連接
###     fw:顯示所有的啟動固件值
###     host:顯示所有的iSCSI主機
###     iface:顯示/var/lib/iscsi/ifaces目錄中的所有ifaces設定

### 其用法如下:

# 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 [ -x chap_tbl_idx ] ] | [ -C flashnode [ -A portal_type ] [ -x flashnode_idx ] ] | [ -C stats ] ] [ [ -o operation ] [ -n name ] [ -v value ] ]

# iscsiadm -k priority

常用參數說明如下:

-d, --debug=debug_level         顯示debug信息,級別為0-8
-l, --login                     連接目標
-t, --type=type                 可用的類型有(此選項僅用於discovery模式):sendtargets(可簡寫為st)、slp、fw和isns
-p, --portal=ip[:port]          指定目標服務的IP和端口;
-m, --mode op                   可用的模式有discovery、node、fw、host、iface和session
-T, --targetname=targetname     用於指定目標的名字
-u, --logout                    斷開同目標的連接
-o, --op=OPEARTION              指定針對discoverydb數據庫的操作,可用操作有:new、delete、update、show和nonpersistent
-I, --interface=[iface]         指定執行操作的iSCSI接口,這些接口定義在/var/lib/iscsi/ifaces中

常見用法示例:

### 發現目標
# iscsiadm -m discovery -t sendtargets -p 92.0.0.12

### 連接目標
# iscsiadm -m node -T iqn.2010-10.org.openstack:volume-ea0ec392-92dd-4d95-bbc8-277c7f881bbf -p 92.0.0.12 -l

### 刪除目標

# iscsiadm -m node -o delete -T iqn.2010-10.org.openstack:vol -p 92.0.0.14

### 查詢連接會話
# iscsiadm -m session

使用LIO/TCM管理服務端

檢測內核是否支持LIO/TCM

# find /lib/modules/$(uname -r) -iname  *target*
/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/target
/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/target/iscsi/iscsi_target_mod.ko
/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/target/target_core_file.ko
/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/target/target_core_iblock.ko
/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/target/target_core_mod.ko
/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/target/target_core_pscsi.ko

# grep ‘TARGET_CORE\|TCM‘ /boot/config-$(uname -r)
 # CONFIG_TCM_QLA2XXX is not set
CONFIG_TARGET_CORE=m
CONFIG_TCM_IBLOCK=m
CONFIG_TCM_FILEIO=m
CONFIG_TCM_PSCSI=m
 # CONFIG_TCM_USER is not set
CONFIG_TCM_FC=m

安裝管理軟件

### 有了這個就可以不用安裝scsi-target-utils.x86_64了,兩者對應的服務分別為tgtd和target
# yum install targetcli -y

啟動並打印幫助

# targetcli
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb46
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type ‘help‘.

/> help

GENERALITIES
============
This is a shell in which you can create, delete and configure
configuration objects.

The available commands depend on the current path or target
path you want to run a command in: different path have
different sets of available commands, i.e. a path pointing at
an iscsi target will not have the same availaible commands as,
say, a path pointing at a storage object.

The prompt that starts each command line indicates your
current path. Alternatively (useful if the prompt displays
an abbreviated path to save space), you can run the
pwd command to display the complete current path.

Navigating the tree is done using the cd command. Without
any argument, cd will present you with the full objects
tree. Just use arrows to select the destination path, and
enter will get you there. Please try help cd for navigation
tips.

COMMAND SYNTAX
==============
Commands are built using the following syntax:

[TARGET_PATH] COMMAND_NAME [OPTIONS]

The TARGET_PATH indicates the path to run the command from.
If ommited, the command will be run from your current path.

The OPTIONS depend on the command. Please use help
COMMAND to get more information.


AVAILABLE COMMANDS
==================
The following commands are available in the
current path:

  - bookmarks action [bookmark]
  - cd [path]
  - clearconfig [confirm]
  - exit
  - get [group] [parameter...]
  - help [topic]
  - ls [path] [depth]
  - pwd
  - refresh
  - restoreconfig [savefile] [clear_existing]
  - saveconfig [savefile]
  - sessions [action] [sid]
  - set [group] [parameter=value...]
  - status
  - version
/>

創建backstore(在此之前,我們要創建一個路徑為/dev/cinder-volumes/vfwd的lv)

/> 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]
/> cd backstores/block
/backstores/block> create vol-fwd /dev/cinder-volumes/vfwd
Created block storage object vol-fwd using /dev/cinder-volumes/vfwd.

創建iscsi目標

/backstores/block> cd /iscsi
/iscsi> create iqn.2010-10.org.openstack:vol-fwd
Created target iqn.2010-10.org.openstack:vol-fwd.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.

創建LUN

/iscsi> ls
o- iscsi .............................................................................................................. [Targets: 1]
  o- iqn.2010-10.org.openstack:vol-fwd ................................................................................... [TPGs: 1]
    o- tpg1 ................................................................................................. [no-gen-acls, no-auth]
      o- acls ............................................................................................................ [ACLs: 0]
      o- luns ............................................................................................................ [LUNs: 0]
      o- portals ...................................................................................................... [Portals: 1]
        o- 0.0.0.0:3260 ....................................................................................................... [OK]
/iscsi> cd iqn.2010-10.org.openstack:vol-fwd/tpg1/luns
/iscsi/iqn.20...fwd/tpg1/luns> create /backstores/block/vol-fwd 
Created LUN 0.

創建ACL訪問規則

/iscsi/iqn.20...fwd/tpg1/luns> cd ..
/iscsi/iqn.20...:vol-fwd/tpg1> cd acls
/iscsi/iqn.20...fwd/tpg1/acls> create iqn.2010-10.org.openstack:vol-fwd
Created Node ACL for iqn.2010-10.org.openstack:vol-fwd
Created mapped LUN 0.
/iscsi/iqn.20...fwd/tpg1/acls> ls
o- acls .................................................................................................................. [ACLs: 1]
  o- iqn.2010-10.org.openstack:vol-fwd ............................................................................ [Mapped LUNs: 1]
    o- mapped_lun0 ....................................................................................... [lun0 block/vol-fwd (rw)]
/iscsi/iqn.20...fwd/tpg1/acls> cd iqn.2010-10.org.openstack:vol-fwd/
/iscsi/iqn.20...stack:vol-fwd> set auth userid=fwdssg
Parameter userid is now ‘fwdssg‘.
/iscsi/iqn.20...stack:vol-fwd> set auth password=fwdssg
Parameter password is now ‘fwdssg‘.

創建監聽端口用於客戶端連接

### 默認存在一個監聽地址0.0.0.0:3260

/iscsi/iqn.20...stack:vol-fwd> cd /
/> cd iscsi/iqn.2010-10.org.openstack:vol-fwd/tpg1/portals/
/iscsi/iqn.20.../tpg1/portals> create 0.0.0.0 3261
Binding to INADDR_ANY (0.0.0.0)
Created network portal 0.0.0.0:3261.
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................................................ [Portals: 2]
  o- 0.0.0.0:3260 ............................................................................................................. [OK]
  o- 0.0.0.0:3261 ............................................................................................................. [OK]
/iscsi/iqn.20.../tpg1/portals> delete 0.0.0.0 3261
Deleted network portal 0.0.0.0:3261

最後保存配置

/> saveconfig
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json

其他技巧

### 1. 在不同的目錄下下面打印help顯示的指令都是不同的
/iscsi/iqn.20...ode/tpg1/acls> help

AVAILABLE COMMANDS
==================
The following commands are available in the
current path:

  - bookmarks action [bookmark]
  - cd [path]
  - create wwn [add_mapped_luns]
  - delete wwn
  - exit
  - get [group] [parameter...]
  - help [topic]
  - ls [path] [depth]
  - pwd
  - refresh
  - set [group] [parameter=value...]
  - status
  - tag wwn_or_tag new_tag
  - untag wwn_or_tag

### 2. 每個目錄都有屬性值,通過get命令可以獲取目錄下面所有的屬性值,使用set可以設置這些屬性值,使用tab鍵可以自動補齊命令
/iscsi/iqn.20...ys:anode/tpg1> get

AVAILABLE CONFIGURATION GROUPS
==============================
attribute global parameter auth

/iscsi/iqn.20...ys:anode/tpg1> get attribute
ATTRIBUTE CONFIG GROUP
======================
authentication=0
----------------
If set to 1, enforce authentication for this TPG.

/iscsi/iqn.20...ys:anode/tpg1> set attribute authentication=1
Parameter authentication is now ‘1‘.

### 3. 目錄如果有屬性被設置,使用ls命令後,在目錄右邊的[]內可以看到
/iscsi/iqn.20....nocsys:anode> ls
o- iqn.2010-10.org.nocsys:anode .......................................................................................... [TPGs: 1]
  o- tpg1 ......................................................................................... [gen-acls, tpg-auth, 1-way auth]
    o- acls .............................................................................................................. [ACLs: 0]
    o- luns .............................................................................................................. [LUNs: 1]
    | o- lun0 ............................................................ [block/anode (/dev/ceph-volume/anode) (default_tg_pt_gp)]
    o- portals ........................................................................................................ [Portals: 1]
      o- 0.0.0.0:3260 ......................................................................................................... [OK]

參考文檔

鳥哥私房菜
使用Linux的tgtd提供iscsi服務
創建 iscsi target 服務器和 iscsi initiator 客戶端連接
ISCSI網絡存儲
ISCSI Target (簡體中文)
官方手冊

iscsi使用教程(中)