CentOS ISCSI storage solution

CentOS iscsi target server :

[root@localhost ~]# yum install targetcli
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: centos.ustc.edu.cn
base | 3.6 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
(1/2): extras/7/x86_64/primary_db | 150 kB 00:00
(2/2): updates/7/x86_64/primary_db | 3.6 MB 00:00
Resolving Dependencies
–> Running transaction check
—> Package targetcli.noarch 0:2.1.fb46-4.el7_5 will be updated
—> Package targetcli.noarch 0:2.1.fb46-6.el7_5 will be an update
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Updating:
targetcli noarch 2.1.fb46-6.el7_5 updates 67 k

Transaction Summary
================================================================================
Upgrade 1 Package

Total download size: 67 k
Is this ok [y/d/N]: y
Downloading packages:
Not downloading deltainfo for updates, MD is 370 k and rpms are 67 k
targetcli-2.1.fb46-6.el7_5.noarch.rpm | 67 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : targetcli-2.1.fb46-6.el7_5.noarch 1/2
Cleanup : targetcli-2.1.fb46-4.el7_5.noarch 2/2
Verifying : targetcli-2.1.fb46-6.el7_5.noarch 1/2
Verifying : targetcli-2.1.fb46-4.el7_5.noarch 2/2

Updated:
targetcli.noarch 0:2.1.fb46-6.el7_5

Complete!
[root@localhost ~]# mkdir /iscsi_disks

[root@localhost ~]# 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’.

/> cd backstores/fileio

/backstores/fileio> create disk01 /iscsi_disks/disk01.img 2G
Created fileio disk01 with size 2147483648
/backstores/fileio> cd /iscsi
/iscsi> create iqn.2018-07.daimongu.com:storage01
Created target iqn.2018-07.daimongu.com:storage01.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/iscsi> cd iqn.2018-07.daimongu.com:storage01/tpg1/luns 

/iscsi/iqn.20…e01/tpg1/luns> create /backstores/fileio/disk01
Created LUN 0.

/iscsi/iqn.20…e01/tpg1/luns> cd ../acls
/iscsi/iqn.20…e01/tpg1/acls> create iqn.2018-07.daimongu.com:storage01
Created Node ACL for iqn.2018-07.daimongu.com:storage01
Created mapped LUN 0.

/iscsi/iqn.20…e01/tpg1/acls> cd iqn.2018-07.daimongu.com:storage01/
/iscsi/iqn.20…:storage01> set auth userid=username
Parameter userid is now ‘username’.
/iscsi/iqn.20...:storage01> set auth password=password123456
Parameter password is now ‘password123456’.
/iscsi/iqn.20…:storage01> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup/.
Configuration saved to /etc/target/saveconfig.json
[root@localhost ~]# ss -napt | grep 3260
LISTEN 0 256 *:3260 *:*

[root@localhost ~]# systemctl start target
[root@localhost ~]# systemctl enable target
Created symlink from /etc/systemd/system/multi-user.target.wants/target.service to /usr/lib/systemd/system/target.service.
[root@localhost ~]# firewall-cmd  –add-service=iscsi-target  –permanent

success
[root@localhost ~]# firewall-cmd  –reload
success

Linux iscsi initiator client :

yum y install iscsiinitiatorutils
gedit /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2018-07.daimongu.com:storage01
gedit /etc/iscsi/iscsid.conf
//取消以下注释:
node.session.auth.authmethod = CHAP
node.session.auth.username = username
node.session.auth.password = password123456
//重启服务
systemctl restart iscsid
systemctl restart iscsi
//发现target
iscsiadm m discovery t sendtargets p 172.17.200.137
iscsiadm m node o show
//登录target
iscsiadm m node login
//确认连接状态
iscsiadm m session o show
//确认分区状况
cat /proc/partitions
//新建分区
fdisk /dev/sdb 
//格式化分区
mkfs.ext4 /dev/sdb1
//挂载分区
mkdir /data
mount /dev/sdb1 /data
/etc/fstab中添加
/dev/sdb1       /data       ext4     _netdev   0   0
命令tune2fs l /dev/sdb可以查到文件系统的UUID
/etc/fstab中添加  
UUID=xxxxxxx  /data ext4   _netdev 0  0  替换掉原来的那行

 

[root@localhost data]# iscsiadm -m session -o show
tcp: [5] 172.17.200.137:3260,1 iqn.2018-07.daimongu.com:storage01 

[root@localhost data]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 40G 5.7G 34G 15% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 25k 2.0G 1% /dev/shm
tmpfs tmpfs 2.0G 9.6M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda2 xfs 1.1G 181M 884M 17% /boot
/dev/mapper/rhel-home xfs 20G 40M 20G 1% /home
/dev/sda1 vfat 210M 11M 200M 5% /boot/efi
tmpfs tmpfs 386M 41k 386M 1% /run/user/0
/dev/sdb ext4 2.1G 6.3M 2.0G 1% /data