iSCSI技术在工作形式上分为服务端(target)与客户端(initiator)。
iSCSI服务端即用于存放硬盘存储资源的服务器,它作为前面创建的RAID磁盘阵列的存储端,能够为用户提供可用的存储资。
iSCSI客户端则是用户使用的软件,用于访问远程服务端的存储资源。
能够完成 iSCSI target/initiator 设定的项目非常多,这里使用的是targetcli项目。
iSCSI Target(服务器端)配置
安装:
# yum -y install targetcli
启动target服务:
# systemctl start target
设置为开机自启动服务:
# systemctl enable target
查看防火墙状态:
# systemctl status firewalld
- 禁用防火墙:
# systemctl stop firewalld
# systemctl disable firewalld
- 或者是开放3260端口:
# firewall-cmd --permanent --add-port=3260/tcp
# firewall-cmd --reload
开始配置ISCSI,执行 targetcli 命令后就能看到交互式的配置界面了。在该界面中可以使用很多Linux命令,比如利用ls查看目录参数的结构,使用cd切换到不同的目录中。/backstores/block是iSCSI服务端配置共享设备的位置:
# 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'.
/>
有两种方式可以配置共享资源:
- 使用镜像文件
在执行 targetcli 命令创建的一个40G的镜像:
# dd if=/dev/sda of=/home/ye/iscsi/shareddata.img bs=1024k count=40960
然后可以在相应目录下创建一个名为shareddata 的 fileio backstore:
> /backstores/fileio/ create shareddata /home/ye/iscsi/shareddata.img 40G
- 使用磁盘分区或者逻辑卷
这种方法通常来说有更好的性能
/> /backstores/block/ create xendata /dev/sdb
然后配置ISCSITarget命名,即创建 IQN (Iscsi Qualified Name) :
/> iscsi/ create iqn.2018-06.com.xenserver:xenserver1
进入新创建的目录:
/iscsi/iqn.20...er:xenserver1> cd tpg1/
/iscsi/iqn.20...nserver1/tpg1> ls
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]
在目录tpg1下定义了三个object:
- acls (access control lists: restrict access to resources)
- luns (logical unit number: define exported resources)
- portals (define ways to reach the exported resources; consist in pairs of IP addresses and ports)
创建lun(target块设备的逻辑单元):
- 如果是Fileio backstore
/iscsi/iqn.20...nserver1/tpg1> luns/ create /backstores/fileio/shareddata
- 如果是Block backstore
/iscsi/iqn.20...ample:t1/tpg1> luns/ create /backstores/block/xendata
创建ACL允许ISCSI客户机连接:
/iscsi/iqn.20...nserver1/tpg1> acls/ create iqn.2018-06.com.xenserver:client
创建用户和密码:
/iscsi/iqn.20...nserver1/tpg1> cd acls
/iscsi/iqn.20...er1/tpg1/acls> cd iqn.2018-06.com.xenserver:client/
/iscsi/iqn.20...server:client> ls
o- iqn.2018-06.com.xenserver:client ............................................................................... [Mapped LUNs: 1]
o- mapped_lun0 ..................................................................................... [lun0 fileio/shareddata (rw)]
/iscsi/iqn.20...server:client> set auth userid=ye
Parameter userid is now 'ye'.
/iscsi/iqn.20...server:client> set auth password=123456
Parameter password is now '123456'.
iSCSI Initiator(客户端)配置
安装:
# yum -y install iscsi-initiator-utils
注:Ubuntu下比较方便好用的initiator是open iscsi:
apt-get install -y open-iscsi
启动iscsi服务:
# systemctl start iscsi
设置为开机自启动服务:
# systemctl enable iscsi
配置ISCSIInitiator名称(此处InitiatorName须与服务端配置的ACL允许ISCSI客户机连接的名称一致):
# vi /etc/iscsi/initiatorname.iscsi
修改为:
InitiatorName=iqn.2018-06.com.xenserver:client
修改ISCSIInitiator配置文件:
/etc/iscsi/iscsid.conf
node.session.auth.authmethod = CHAP
node.session.auth.username = ye
node.session.auth.password = 123456
查找ISCSI设备:
# iscsiadm --mode discovery --type sendtargets --portal 172.20.110.22
连接ISCSI设备:
# iscsiadm --mode node --targetname iqn.2018-06.com.xenserver:xenserver1 --portal 172.20.110.22 --login
查看ISCSI设备状态:
# lsblk --scsi
NAME HCTL TYPE VENDOR MODEL REV TRAN
sda 0:0:0:0 disk ATA ST500DM002-1BD14 KC65 sata
sdb 4:0:0:0 disk LIO-ORG shareddata 4.0 iscsi
sr0 1:0:0:0 rom HL-DT-ST DVDRAM GTA0N LC00 sata
使用命令fdisk -l即可看到新添加的ISCSI设备。
创建文件系统
格式分区:
# mkfs.ext4 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=2048 blocks
2621440 inodes, 10485760 blocks
524288 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2157969408
320 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
提取给磁盘的UUID :
# blkid | grep "/dev/sdb"
/dev/sdb: UUID="cb6d172d-1db0-4881-942e-9e88d20a074e" TYPE="ext4"
# echo "UUID=cb6d172d-1db0-4881-942e-9e88d20a074e" >> /etc/fstab
添加挂载点:
# vi /etc/fstab
UUID=cb6d172d-1db0-4881-942e-9e88d20a074e /mnt/xenserver ext4 _netdev 0 0
其中, /mnt/xenserver是挂载路径, ext4代表文件系统 ,_netdev代表该挂载的磁盘分区为网络磁盘分区。
挂载:
# mount /mnt/xenserver
最后设置开机自动连接ISCSI设备:
# iscsiadm --mode node --targetname iqn.2018-06.com.xenserver:xenserver1 --portal 172.20.110.22 -o update -n node.startup -v automatic
参考文献:
[1]. https://www.certdepot.net/rhel7-configure-iscsi-target-initiator-persistently/
[2].