Linux 部署 iSCSI 服务端
服务端实验环境
iSCSI-server :RHEL8
IP:192.168.121.10
一、服务端安装 target 服务和 targetcli 命令行工具
- 只需要安装 targetcli 命令行工具就行,target 服务会一并安装好
[root@iSCSI-server ~]# yum install -y targetcli
[root@iSCSI-server ~]# systemctl start target
[root@iSCSI-server ~]# systemctl enable target
Created symlink /etc/systemd/system/multi-user.target.wants/target.service → /usr/lib/systemd/system/target.service.
[root@iSCSI-server ~]# systemctl status target
● target.service - Restore LIO kernel target configuration
Loaded: loaded (/usr/lib/systemd/system/target.service; enabled; vendor preset: disabled)
Active: active (exited) since Thu 2020-12-17 00:02:41 CST; 21min ago
Main PID: 45827 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 11340)
Memory: 0B
CGroup: /system.slice/target.service
Dec 17 00:02:41 iSCSI-server systemd[1]: Starting Restore LIO kernel target configuration...
Dec 17 00:02:41 iSCSI-server target[45827]: No saved config file at /etc/target/saveconfig.json, ok, exiting
Dec 17 00:02:41 iSCSI-server systemd[1]: Started Restore LIO kernel target configuration.
- 启动 targetcli
[root@iSCSI-server ~]# targetcli
targetcli shell version 2.1.53
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]
/>
二、添加存储空间
- 以硬盘形式添加20G存储空间(无需分区)
[root@iSCSI-server ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
sr0 11:0 1 7.3G 0 rom
nvme0n1 259:0 0 80G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 79G 0 part
├─rhel-root 253:0 0 50G 0 lvm /
├─rhel-swap 253:1 0 2G 0 lvm [SWAP]
└─rhel-home 253:2 0 27G 0 lvm /home
- 以文件形式添加1G的存储空间
[root@iSCSI-server ~]# dd if=/dev/zero of=/mnt/disk.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.43664 s, 312 MB/s
[root@iSCSI-server ~]# ll -h /mnt/disk.img
-rw-r--r--. 1 root root 1.0G Dec 17 00:36 /mnt/disk.img
三、创建iSCSI磁盘(块设备)
[root@iSCSI-server ~]# targetcli
targetcli shell version 2.1.53
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> ls
o- / ......................................................................................................................... [...]
o- backstores .............................................................................................................. [...]
| o- block .................................................................................................. [Storage Objects: 0] //说明:block 中创建的是以硬盘形式的存储空间
| o- fileio ................................................................................................. [Storage Objects: 0] //说明:fileio 中创建的是以文件形式的存储空间
| o- pscsi .................................................................................................. [Storage Objects: 0]
| o- ramdisk ................................................................................................ [Storage Objects: 0]
o- iscsi ............................................................................................................ [Targets: 0]
o- loopback ......................................................................................................... [Targets: 0]
/> backstores/block create disk_one /dev/sda //disk_one 是磁盘名称,后面接磁盘本地存储路径
Created block storage object disk_one using /dev/sda.
/> backstores/fileio create disk_two /mnt/disk.img //同理,disk_two是第二块磁盘名称
Created fileio disk_two with size 1073741824
/> ls
o- / ......................................................................................................................... [...]
o- backstores .............................................................................................................. [...]
| o- block .................................................................................................. [Storage Objects: 1]
| | o- disk_one ...................................................................... [/dev/sda (20.0GiB) write-thru deactivated]
| | o- alua ................................................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
| o- fileio ................................................................................................. [Storage Objects: 1]
| | o- disk_two .................................................................. [/mnt/disk.img (1.0GiB) write-back deactivated]
| | o- alua ................................................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
| o- pscsi .................................................................................................. [Storage Objects: 0]
| o- ramdisk ................................................................................................ [Storage Objects: 0]
o- iscsi ............................................................................................................ [Targets: 0]
o- loopback ......................................................................................................... [Targets: 0]
四、创建服务端程序(iSCSI目标)
[root@iSCSI-server ~]# targetcli
targetcli shell version 2.1.53
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
/> ls
o- / ......................................................................................................................... [...]
o- backstores .............................................................................................................. [...]
| o- block .................................................................................................. [Storage Objects: 1]
| | o- disk_one ...................................................................... [/dev/sda (20.0GiB) write-thru deactivated]
| | o- alua ................................................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
| o- fileio ................................................................................................. [Storage Objects: 1]
| | o- disk_two .................................................................. [/mnt/disk.img (1.0GiB) write-back deactivated]
| | o- alua ................................................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
| o- pscsi .................................................................................................. [Storage Objects: 0]
| o- ramdisk ................................................................................................ [Storage Objects: 0]
o- iscsi ............................................................................................................ [Targets: 0]
o- loopback ......................................................................................................... [Targets: 0]
/> iscsi/ create iqn.2020-12.linux.iscis.server:server //创建服务的程序,以时间命名
Created target iqn.2020-12.linux.iscis.server:server.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/>
- 查看已经创建的服务的程序(iSCSI目标)
/> ls
o- / ......................................................................................................................... [...]
o- backstores .............................................................................................................. [...]
| o- block .................................................................................................. [Storage Objects: 1]
| | o- disk_one ...................................................................... [/dev/sda (20.0GiB) write-thru deactivated]
| | o- alua ................................................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
| o- fileio ................................................................................................. [Storage Objects: 1]
| | o- disk_two .................................................................. [/mnt/disk.img (1.0GiB) write-back deactivated]
| | o- alua ................................................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
| o- pscsi .................................................................................................. [Storage Objects: 0]
| o- ramdisk ................................................................................................ [Storage Objects: 0]
o- iscsi ............................................................................................................ [Targets: 1]
| o- iqn.2020-12.linux.iscis.server:server ............................................................................. [TPGs: 1] //刚刚创建的ISCSI目标
| 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]
五、创建服务卷(逻辑单元LUN:为了让客户端识别)
/> ls
o- / ......................................................................................................................... [...]
o- backstores .............................................................................................................. [...]
| o- block .................................................................................................. [Storage Objects: 1]
| | o- disk_one ...................................................................... [/dev/sda (20.0GiB) write-thru deactivated]
| | o- alua ................................................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
| o- fileio ................................................................................................. [Storage Objects: 1]
| | o- disk_two .................................................................. [/mnt/disk.img (1.0GiB) write-back deactivated]
| | o- alua ................................................................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp ....................................................................... [ALUA state: Active/optimized]
| o- pscsi .................................................................................................. [Storage Objects: 0]
| o- ramdisk ................................................................................................ [Storage Objects: 0]
o- iscsi ............................................................................................................ [Targets: 1]
| o- iqn.2020-12.linux.iscis.server:server ............................................................................. [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]
/> cd /iscsi/iqn.2020-12.linux.iscis.server:server/tpg1/
/iscsi/iqn.20...r:server/tpg1> luns/ create /backstores/block/disk_one
Created LUN 0.
/iscsi/iqn.20...r:server/tpg1> luns/ create /backstores/fileio/disk_two
Created LUN 1.
/iscsi/iqn.20...r:server/tpg1>
- 查看已经创建端服务卷
/iscsi/iqn.20...r:server/tpg1> ls
o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
o- acls ................................................................................................................ [ACLs: 0]
o- luns ................................................................................................................ [LUNs: 2]
| o- lun0 ......................................................................... [block/disk_one (/dev/sda) (default_tg_pt_gp)] //对应的第一块磁盘
| o- lun1 ................................................................... [fileio/disk_two (/mnt/disk.img) (default_tg_pt_gp)] //对应的第二块磁盘
o- portals .......................................................................................................... [Portals: 1]
o- 0.0.0.0:3260 ........................................................................................................... [OK]
/iscsi/iqn.20...r:server/tpg1>
六、创建客户端连接器(启用CHAP认证)
- 生产客户端连接器
/> cd /iscsi/iqn.2020-12.linux.iscis.server:server/tpg1/
/iscsi/iqn.20...r:server/tpg1> acls/ create iqn.2020-12.linux.iscis.server:client
Created Node ACL for iqn.2020-12.linux.iscis.server:client
Created mapped LUN 1.
Created mapped LUN 0.
/iscsi/iqn.20...r:server/tpg1>
- 查看创建的连接器
/iscsi/iqn.20...r:server/tpg1> ls
o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
o- acls ................................................................................................................ [ACLs: 1]
| o- iqn.2020-12.linux.iscis.server:client ...................................................................... [Mapped LUNs: 2] //客户端连接器
| o- mapped_lun0 .................................................................................... [lun0 block/disk_one (rw)]
| o- mapped_lun1 ................................................................................... [lun1 fileio/disk_two (rw)]
o- luns ................................................................................................................ [LUNs: 2]
| o- lun0 ......................................................................... [block/disk_one (/dev/sda) (default_tg_pt_gp)]
| o- lun1 ................................................................... [fileio/disk_two (/mnt/disk.img) (default_tg_pt_gp)]
o- portals .......................................................................................................... [Portals: 1]
o- 0.0.0.0:3260 ........................................................................................................... [OK]
- 设置CHAP认证
/iscsi/iqn.20...r:server/tpg1> cd acls/iqn.2020-12.linux.iscis.server:client/
/iscsi/iqn.20...server:client> set
attribute auth global parameter group=
/iscsi/iqn.20...server:client> set auth
mutual_password= mutual_userid= password= userid=
/iscsi/iqn.20...server:client> set auth userid=admin
Parameter userid is now 'admin'.
/iscsi/iqn.20...server:client> set auth password=123456
Parameter password is now '123456'.
/iscsi/iqn.20...server:client>
七、重启target服务以及关闭防火墙
[root@iSCSI-server ~]# systemctl restart target
[root@iSCSI-server ~]# systemctl stop firewalld
查询3260端口是否开启
[root@iSCSI-server ~]# ss -anpt |grep 3260
LISTEN 0 256 0.0.0.0:3260 0.0.0.0:*