centos7 使用 iscsi 网络存储服务

安装

yum -y installtargetcli

开启

[root@server ~]# systemctl enable target.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/target.service to /usr/lib/systemd/system/target.service.
[root@server ~]# systemctl start target.service    

配置服务端

1.进入配置接口

[root@server ~]# targetcli 
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
targetcli shell version 2.1.fb41
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

2.查看帮助信息

    /> 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 wil 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 

4.查看目录

    /> 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]

5.添加硬盘

/> backstores/block create Datastore /dev/sdb 
Created block storage object Datastore using /dev/sdb.

6.建立一个名称 IQN

/> /iscsi create iqn.2017-05.com.v5linux.iscsi.server0   
Created target iqn.2017-05.com.v5linux.iscsi.server0.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
    /> ls
o- / ......................................................................................................................... [...]
  o- backstores .............................................................................................................. [...]
  | o- block .................................................................................................. [Storage Objects: 1]
  | | o- Datastore ..................................................................... [/dev/sdb (20.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.2017-05.com.v5linux.iscsi.server0 ............................................................................. [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]
  o- loopback ......................................................................................................... [Targets: 0]
/> 
  1. 配置监听端口

默认监听的端口是2160,监听地址是任何地址

/> cd /iscsi/iqn.2017-05.com.v5linux.iscsi.server0/
/iscsi/iqn.20...iscsi.server0> ls
o- iqn.2017-05.com.v5linux.iscsi.server0 ................................................................................. [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/iqn.20...iscsi.server0> 

配置提示不能创建网络配置,需要先删除默认的0。0.0.0

/> /iscsi/iqn.2017-05.com.v5linux.iscsi.server0/tpg1/portals create 172.10.100.129 ip_port=3260
Using default IP port 3260
Could not create NetworkPortal in configFS    

进入到/iscsi/iqn.2017-05.com.v5linux.iscsi.server0/tpg1/portals 目录

/iscsi/iqn.20.../tpg1/portals> delete 0.0.0.0 3260  #删除 ip 和端口
Deleted network portal 0.0.0.0:3260
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................................................ [Portals: 0]
/iscsi/iqn.20.../tpg1/portals> /iscsi/iqn.2017-05.com.v5linux.iscsi.server0/tpg1/portals create 172.10.100.129 ip_port=3260
Using default IP port 3260 #添加 ip 和端口
Created network portal 172.10.100.129:3260.
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................................................ [Portals: 1]
  o- 172.10.100.129:3260 ...................................................................................................... [OK]

创建一个 acl

/iscsi/iqn.20.../tpg1/portals> /iscsi/iqn.2017-05.com.v5linux.iscsi.server0/tpg1/acls create iqn.2017-05.com.v5linux.iscsi.desktop
Created Node ACL for iqn.2017-05.com.v5linux.iscsi.desktop          

iqn.2017-05.com.v5linux.iscsi.desktop 表示客户端,这个后面会用到,名称任意

创建一个 lun (target 块设备的逻辑单元)

/iscsi/iqn.20.../tpg1/portals> /iscsi/iqn.2017-05.com.v5linux.iscsi.server0/tpg1/acls create iqn.2017-05.com.v5linux.iscsi.desktop
Created Node ACL for iqn.2017-05.com.v5linux.iscsi.desktop
/iscsi/iqn.20.../tpg1/portals> /iscsi/iqn.2017-05.com.v5linux.iscsi.server0/tpg1/luns create /backstores/block/Datastore 
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2017-05.com.v5linux.iscsi.desktop    

确认配置保存

/iscsi/iqn.20.../tpg1/portals> cd /
/> saveconfig 
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
/>     

再次查看整个目录结构

/> ls
o- / ......................................................................................................................... [...]
  o- backstores .............................................................................................................. [...]
  | o- block .................................................................................................. [Storage Objects: 1]
  | | o- Datastore ....................................................................... [/dev/sdb (20.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.2017-05.com.v5linux.iscsi.server0 ............................................................................. [TPGs: 1]
  |   o- tpg1 ............................................................................................... [no-gen-acls, no-auth]
  |     o- acls .......................................................................................................... [ACLs: 1]
  |     | o- iqn.2017-05.com.v5linux.iscsi.desktop ................................................................ [Mapped LUNs: 1]
  |     |   o- mapped_lun0 ............................................................................. [lun0 block/Datastore (rw)]
  |     o- luns .......................................................................................................... [LUNs: 1]
  |     | o- lun0 ..................................................................................... [block/Datastore (/dev/sdb)]
  |     o- portals .................................................................................................... [Portals: 1]
  |       o- 172.10.100.129:3260 .............................................................................................. [OK]
  o- loopback ...............        

退出

/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json

客户端

1.安装并且启动

[root@client ~]# yum -y install iscsi-initiator-utils
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.zju.edu.cn
Package iscsi-initiator-utils-6.2.0.873-35.el7.x86_64 already installed and latest version
Nothing to do
[root@client ~]# systemctl start iscsid.service

2.配置InitiatorName

[root@client ~]# cat /etc/iscsi/initiatorname.iscsi 
InitiatorName=iqn.1994-05.com.redhat:60b98a424980
[root@client ~]# echo InitiatorName=iqn.2017-05.com.v5linux.iscsi.desktop > /etc/iscsi/initiatorname.iscsi #注意自己的主机名一定要与 server 的 acl 匹配
[root@client ~]# cat /etc/iscsi/initiatorname.iscsi 
InitiatorName=iqn.2017-05.com.v5linux.iscsi.desktop

3.查找服务端

[root@client ~]# iscsiadm -m discovery -t st -p 172.10.100.129
172.10.100.129:3260,1 iqn.2017-05.com.v5linux.iscsi.server0

参数

-m discovery 表示发现查找
-t senbtargets 表示发布的 target (缩写为-t st)

-p 指定 ip 和端口 ip:port

4.重启服务

[root@client ~]# systemctl restart iscsid.service 

5.连接

[root@client ~]# iscsiadm -m node -T iqn.2017-05.com.v5linux.iscsi.server0 -l
Logging in to [iface: default, target: iqn.2017-05.com.v5linux.iscsi.server0, portal: 172.10.100.129,3260] (multiple)
Login to [iface: default, target: iqn.2017-05.com.v5linux.iscsi.server0, portal: 172.10.100.129,3260] successful.

6.查看磁盘

[root@client ~]# fdisk -l

Disk /dev/sdb: 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 /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: 0x0000f054

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/mapper/cl-root: 18.2 GB, 18249416704 bytes, 35643392 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/cl-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/sdc: 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 / 4194304 bytes

排错

1.提示no route

确认防火墙端口是否开启,确认网段是否一致