环境:server 172.25.0.11 ——iscsi server iqn:iqn.2016-02.com.example:server0
client 172.25.0.10 ——iscsi client iqn:iqn.2016-02.com.example:desktop0
一、iscsi服务端配置

安装targetcli
yum install targetcli -y
找一块磁盘挂载

RHEL7 配置iscsi服务端并实现客户端自动开机挂载_第1张图片

[root@server0 ~]# targetcli   ——进入此命令配置iscsi服务端
Warning: Could not load preferences file /root/.targetcli/prefs.bin.
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]
/> backstores/block create iscsi_store /dev/vdb1——使用刚才分区的磁盘vdb创建iscsi存储块
Created block storage object iscsi_store using /dev/vdb1.
/> ls
o- / ......................................................................................................................... [...]
  o- backstores .............................................................................................................. [...]
  | o- block .................................................................................................. [Storage Objects: 1]
  | | o- iscsi_store ................................................................... [/dev/vdb1 (3.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]
/> iscsi/ create iqn.2016-02.com.example:server0——创建iscsi服务器端iqn
Created target iqn.2016-02.com.example:server0.
Created TPG 1.
/> iscsi/iqn.2016-02.com.example:server0/tpg1/
iscsi/iqn.2016-02.com.example:server0/tpg1/*         iscsi/iqn.2016-02.com.example:server0/tpg1/luns/     
iscsi/iqn.2016-02.com.example:server0/tpg1/acls/     iscsi/iqn.2016-02.com.example:server0/tpg1/portals/  
...path
/> iscsi/iqn.2016-02.com.example:server0/tpg1/
iscsi/iqn.2016-02.com.example:server0/tpg1/*         iscsi/iqn.2016-02.com.example:server0/tpg1/luns/     
iscsi/iqn.2016-02.com.example:server0/tpg1/acls/     iscsi/iqn.2016-02.com.example:server0/tpg1/portals/  
...path
/> iscsi/iqn.2016-02.com.example:server0/tpg1/luns create /backstores/block/iscsi_store ——创建iscsi存储lun
Created LUN 0.
/> iscsi/iqn.2016-02.com.example:server0/tpg1/acls create iqn.2016-02.com.example:desktop0 ——允许客户端iqn访问此服务器iscsi服务
Created Node ACL for iqn.2016-02.com.example:desktop0
Created mapped LUN 0.
/> iscsi/iqn.2016-02.com.example:server0/tpg1/portals create 172.25.0.11 3260——创建服务器端侦听ip和端口
Using default IP port 3260
Created network portal 172.25.0.11:3260.
/> saveconfig ——保存配置
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
[root@server0 ~]# systemctl start target  ——启动iscsi服务
[root@server0 ~]# systemctl enable target
ln -s '/usr/lib/systemd/system/target.service' '/etc/systemd/system/multi-user.target.wants/target.service'
[root@server0 ~]# firewall-cmd --permanent --add-port=3260/tcp——防火墙开启3260端口
success
[root@server0 ~]# firewall-cmd --reload——重启防火墙服务
success
[root@server0 ~]# 

二、iscsi客户端配置

[root@desktop0 ~]# yum install iscsi-initiator-utils -y
Loaded plugins: langpacks
Package iscsi-initiator-utils-6.2.0.873-21.el7.x86_64 already installed and latest version
Nothing to do
[root@desktop0 ~]# vim /etc/iscsi/initiatorname.iscsi ——修改客户端iqn名称
[root@desktop0 ~]# cat !$
cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2016-02.com.example:desktop0
[root@desktop0 ~]# 
[root@desktop0 ~]# systemctl start iscsi iscsid
[root@desktop0 ~]# systemctl enable iscsi iscsid
ln -s '/usr/lib/systemd/system/iscsid.service' '/etc/systemd/system/multi-user.target.wants/iscsid.service'
[root@desktop0 ~]# iscsiadm -m discovery -t st -p server0
172.25.0.11:3260,1 iqn.2016-02.com.example:server0
[root@desktop0 ~]# iscsiadm -m node -T iqn.2016-02.com.example:server0 -p 172.25.0.11 -l
Logging in to [iface: default, target: iqn.2016-02.com.example:server0, portal: 172.25.0.11,3260] (multiple)
Login to [iface: default, target: iqn.2016-02.com.example:server0, portal: 172.25.0.11,3260] successful.
[root@desktop0 ~]# lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda      8:0    0   3G  0 disk 
vda    253:0    0  10G  0 disk 
└─vda1 253:1    0  10G  0 part /
vdb    253:16   0  10G  0 disk 
[root@desktop0 ~]# fdisk /dev/sda——分区挂载的iscsi磁盘
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa975526e.

Command (m for help): p

Disk /dev/sda: 3221 MB, 3221225472 bytes, 6291456 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
Disk label type: dos
Disk identifier: 0xa975526e

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (8192-6291455, default 8192): 
Using default value 8192
Last sector, +sectors or +size{K,M,G} (8192-6291455, default 6291455): 
Using default value 6291455
Partition 1 of type Linux and of size 3 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@desktop0 ~]# fdisk -l
Disk /dev/sda: 3221 MB, 3221225472 bytes, 6291456 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
Disk label type: dos
Disk identifier: 0xa975526e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192     6291455     3141632   83  Linux

[root@desktop0 ~]# mkfs.xfs /dev/sda1——格式化文件类型为xfs
meta-data=/dev/sda1              isize=256    agcount=8, agsize=98176 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=785408, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@desktop0 ~]# mkdir /mnt/data
[root@desktop0 ~]# vim /etc/fstab 
[root@desktop0 ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/sda1: UUID="701f7c03-8f64-4d39-9acc-55a95d66e57d" TYPE="xfs" 
[root@desktop0 ~]# vim /etc/fstab ——使用uuid挂载
[root@desktop0 ~]# cat !$
cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
UUID=701f7c03-8f64-4d39-9acc-55a95d66e57d /mnt/data xfs _netdev 0 0   ——注意使用_netdev
[root@desktop0 ~]# mount -a
[root@desktop0 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      xfs        10G  3.1G  7.0G  31% /
devtmpfs       devtmpfs  906M     0  906M   0% /dev
tmpfs          tmpfs     921M  124K  921M   1% /dev/shm
tmpfs          tmpfs     921M   17M  904M   2% /run
tmpfs          tmpfs     921M     0  921M   0% /sys/fs/cgroup
/dev/sda1      xfs       3.0G   33M  3.0G   2% /mnt/data
[root@desktop0 ~]# 

注意:开机启动自动挂载还需要修改此参数
vim /var/lib/iscsi/nodes/iqn.2016-02.com.example\:server0/172.25.0.11\,3260\,1/default
node.conn[0].startup = automatic——把手动改为自动
结果成功在客户端挂载的iscsi上创建文件
RHEL7 配置iscsi服务端并实现客户端自动开机挂载_第2张图片