$ sudo dnf install targetcli
$ sudo systemctl start target.service
$ sudo systemctl enable target.service
$ sudo targetcli
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]
o- vhost ............................................................... [Targets: 0]
/> cd backstores/
/backstores> cd block
/backstores/block> create block01 /dev/sdb
Created block storage object block01 using /dev/sdb.
/> cd iscsi
/iscsi> create
Created target iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.
/> cd iscsi/
/iscsi> cd iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8/
/iscsi/iqn.20....319cf16722f8> cd tpg1/luns
/iscsi/iqn.20...2f8/tpg1/luns> create /backstores/block/block01
Created LUN 0.
/iscsi/iqn.20...2f8/tpg1/luns> cd ../portals/
/iscsi/iqn.20.../tpg1/portals> create 192.168.134.12
Using default IP port 3260
Created network portal 192.168.134.12:3260.
/iscsi/iqn.20.../tpg1/portals> cd ../acls
/iscsi/iqn.20...2f8/tpg1/acls> ls
o- acls ..................................................................... [ACLs: 0]
/iscsi/iqn.20...2f8/tpg1/acls> create iqn.1994-05.com.redhat:91a579c4e39
Created Node ACL for iqn.1994-05.com.redhat:91a579c4e39
Created mapped LUN 0.
/> ls
o- / ............................................................................ [...]
o- backstores ................................................................. [...]
| o- block ..................................................... [Storage Objects: 1]
| | o- block01 ............................ [/dev/sdb (40.0GiB) write-thru activated]
| | o- alua ...................................................... [ALUA Groups: 1]
| | o- default_tg_pt_gp .......................... [ALUA state: Active/optimized]
| o- fileio .................................................... [Storage Objects: 0]
| o- pscsi ..................................................... [Storage Objects: 0]
| o- ramdisk ................................................... [Storage Objects: 0]
o- iscsi ............................................................... [Targets: 1]
| o- iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8 ............. [TPGs: 1]
| o- tpg1 .................................................. [no-gen-acls, no-auth]
| o- acls ............................................................. [ACLs: 1]
| | o- iqn.1994-05.com.redhat:91a579c4e39 ...................... [Mapped LUNs: 1]
| | o- mapped_lun0 .................................. [lun0 block/block01 (rw)]
| o- luns ............................................................. [LUNs: 1]
| | o- lun0 ....................... [block/block01 (/dev/sdb) (default_tg_pt_gp)]
| o- portals ....................................................... [Portals: 1]
| o- 192.168.134.12:3260 ................................................. [OK]
o- loopback ............................................................ [Targets: 0]
o- vhost ............................................................... [Targets: 0]
/> saveconfig
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
/> cd iscsi/
/iscsi> cd iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8/tpg1/
/iscsi/iqn.20...f16722f8/tpg1> cd acls/iqn.1994-05.com.redhat:91a579c4e39/
/iscsi/iqn.20...t:91a579c4e39> set auth userid=IncomingUser
/iscsi/iqn.20...t:91a579c4e39> set auth password=SomePassword
/iscsi/iqn.20...t:91a579c4e39> set auth mutual_userid=OutgoingUser (可选,单向认证时无需设置)
/iscsi/iqn.20...t:91a579c4e39> set auth mutual_password=AnotherPassword (可选,单向认证时无需设置)
/iscsi/iqn.20...t:91a579c4e39> get auth
AUTH CONFIG GROUP
=================
mutual_password=AnotherPassword
-------------------------------
The mutual_password auth parameter.
mutual_userid=OutgoingUser
--------------------------
The mutual_userid auth parameter.
password=SomePassword
---------------------
The password auth parameter.
userid=IncomingUser
-------------------
The userid auth parameter.
$ sudo vim /etc/iscsi/iscsid.conf
# 去掉以下几行注释,并根据Target端配置设置用户名和密码
node.session.auth.authmethod = CHAP
node.session.auth.username = IncomingUser
node.session.auth.password = SomePassword
node.session.auth.username_in = OutgoingUser
node.session.auth.password_in = AnotherPassword
$ sudo iscsiadm -m discovery -t sendtargets -p 192.168.134.12
192.168.134.12:3260,1 iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8
$ sudo iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8 -p 192.168.134.12 -l
$ sudo iscsiadm -m session -P 0
tcp: [1] 192.168.134.12:3260,1 iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8 (non-flash)
$ sudo lsblk --scsi
NAME HCTL TYPE VENDOR MODEL REV TRAN
sda 2:0:0:0 disk VMware, VMware Virtual S 1.0 spi
sdb 3:0:0:0 disk LIO-ORG block01 4.0 iscsi
sr0 1:0:0:0 rom NECVMWar VMware IDE CDR10 1.00 ata
$ sudo fdisk -l
Disk /dev/sdb: 40 GiB, 42949672960 bytes, 83886080 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
Target端
$ sudo targetcli sessions
alias: initiator sid: 1 type: Normal session-state: LOGGED_IN
$ sudo iscsiadm -m node -T iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8 -p 192.168.134.12 -u
Logging out of session [sid: 1, target: iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8, portal: 192.168.134.12,3260]
Logout of [sid: 1, target: iqn.2003-01.org.linux-iscsi.target.x8664:sn.319cf16722f8, portal: 192.168.134.12,3260] successful.
$ sudo iscsiadm -m session -P 0
iscsiadm: No active sessions.