[root@yangzhen ~]# yum -y install targetcli
[root@yangzhen ~]# systemctl start target
[root@yangzhen ~]# 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'.
/> 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]
/> cd backstores/fileio
/backstores/fileio> ls
o- fileio ..................................................................................................... [Storage Objects: 0]
/backstores/fileio> create disk01 /home/scsi/disk01.img 20M
Created fileio disk01 with size 20971520
/backstores/fileio> ls
o- fileio ..................................................................................................... [Storage Objects: 1]
o- disk01 ............................................................... [/home/scsi/disk01.img (20.0MiB) write-back deactivated]
o- alua ....................................................................................................... [ALUA Groups: 1]
o- default_tg_pt_gp ........................................................................... [ALUA state: Active/optimized]
/backstores/fileio> cd /iscsi
/iscsi> ls
o- iscsi .............................................................................................................. [Targets: 0]
/iscsi> create iqn.2019-03.com.smartmap:storage.tartget01
Created target iqn.2019-03.com.smartmap:storage.tartget01.
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- iscsi .............................................................................................................. [Targets: 1]
o- iqn.2019-03.com.smartmap:storage.tartget01 .......................................................................... [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> cd iqn.2019-03.com.smartmap:storage.tartget01/tpg1/luns
/iscsi/iqn.20...t01/tpg1/luns> create /backstores/fileio/disk01
Created LUN 0.
/iscsi/iqn.20...t01/tpg1/luns> cd ../acls
/iscsi/iqn.20...t01/tpg1/acls> ls
o- acls .................................................................................................................. [ACLs: 0]
/iscsi/iqn.20...t01/tpg1/acls> create iqn.2019-03.com.smartmap:192.168.152.129
Created Node ACL for iqn.2019-03.com.smartmap:192.168.152.129
Created mapped LUN 0.
/> ls
o- / ......................................................................................................................... [...]
o- backstores .............................................................................................................. [...]
| o- block .................................................................................................. [Storage Objects: 0]
| o- fileio ................................................................................................. [Storage Objects: 1]
| | o- disk01 ............................................................. [/home/scsi/disk01.img (20.0MiB) write-back activated]
| | 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.2019-03.com.smartmap:storage.tartget01 ........................................................................ [TPGs: 1]
| o- tpg1 ............................................................................................... [no-gen-acls, no-auth]
| o- acls .......................................................................................................... [ACLs: 1]
| | o- iqn.2019-03.com.smartmap:192.168.152.129 ............................................................. [Mapped LUNs: 1]
| | o- mapped_lun0 ............................................................................... [lun0 fileio/disk01 (rw)]
| o- luns .......................................................................................................... [LUNs: 1]
| | o- lun0 ....................................................... [fileio/disk01 (/home/scsi/disk01.img) (default_tg_pt_gp)]
| o- portals .................................................................................................... [Portals: 1]
| o- 0.0.0.0:3260 ..................................................................................................... [OK]
o- loopback ......................................................................................................... [Targets: 0]
/> exit
客户端操作
[root@yz ~]# yum install -y iscsi-initiator-utils
[root@yz ~]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2019-03.com.smartmap:192.168.152.129
[root@yz ~]# systemctl start iscsi
[root@yz ~]# iscsiadm -m discovery -t st -p 192.168.152.128
192.168.152.128:3260,1 iqn.2019-03.com.smartmap:storage.tartget01
[root@yz ~]# iscsiadm -m node -T iqn.2019-03.com.smartmap:storage.tartget01 -p 192.168.152.128 -l
Logging in to [iface: default, target: iqn.2019-03.com.smartmap:storage.tartget01, portal: 192.168.152.128,3260] (multiple)
Login to [iface: default, target: iqn.2019-03.com.smartmap:storage.tartget01, portal: 192.168.152.128,3260] successful.
-l 表示连接 iscsi 目标,-u表示断开和iscsi目标的连接
[root@yz ~]# lsscsi
[0:0:0:0] disk VMware, VMware Virtual S 1.0 /dev/sda
[2:0:0:0] cd/dvd NECVMWar VMware IDE CDR10 1.00 /dev/sr0
[3:0:0:0] disk LIO-ORG disk01 4.0 /dev/sdb
[root@yz ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 18G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
sdb 8:16 0 20M 0 disk
sr0 11:0 1 918M 0 rom
参考链接:https://blog.csdn.net/weixin_41078837/article/details/80570976
https://www.cnblogs.com/gispathfinder/p/8833488.html
https://blog.csdn.net/yzy1103203312/article/details/80596019