一、创建 LVM

[root@storage ~]# pvcreate /dev/sdc1

[root@storage ~]# vgcreate vglabstor1 /dev/sdc1
  Volume group "vglabstor1" successfully created

[root@storage ~]# lvcreate -l 100%FREE -n lvlabstor1 vglabstor1
  Logical volume "lvlabstor1" created.

[root@storage ~]# lvscan
  ACTIVE '/dev/cl/swap' [2.00 GiB] inherit
  ACTIVE '/dev/cl/root' [46.50 GiB] inherit
  ACTIVE '/dev/vglabstor1/lvlabstor1' [80.00 GiB] inherit

[root@storage ~]# mkfs.xfs /dev/vglabstor1/lvlabstor1
meta-data=/dev/vglabstor1/lvlabstor1 isize=512 agcount=4, agsize=5242624 blks
         = sectsz=512 attr=2, projid32bit=1
         = crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=20970496, imaxpct=25
         = sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=10239, version=2
         = sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

[root@storage ~]# mkdir /labstor1

[root@storage ~]# mount /dev/vglabstor1/lvlabstor1 /labstor1/

二、安装配置 iscsi target

1.安装 targetcli

[root@storage ~]# uname -r
3.10.0-514.el7.x86_64

[root@storage ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 

[root@storage ~]# yum install targetcli

2.配置 targetcli

[root@storage ~]# targetcli
targetcli shell version 2.1.fb46
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/backstores> cd fileio
/backstores/fileio> create disk01 /labstor1/disk01.img 1G
Created fileio disk01 with size 1073741824
/backstores/fileio> create disk02 /labstor1/disk02.img 30G
Created fileio disk02 with size 32212254720
/backstores/fileio> ls /
o- / ...................................................................................................................... [...]
  o- backstores ........................................................................................................... [...]
  | o- block ............................................................................................... [Storage Objects: 0]
  | o- fileio .............................................................................................. [Storage Objects: 2]
  | | o- disk01 .......................................................... [/labstor1/disk01.img (1.0GiB) write-back deactivated]
  | | | o- alua ................................................................................................ [ALUA Groups: 1]
  | | |   o- default_tg_pt_gp .................................................................... [ALUA state: Active/optimized]
  | | o- disk02 ......................................................... [/labstor1/disk02.img (30.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.2018-05.storage.kvm.com:stor1 .............................................................................. [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]
/backstores/fileio> cd /iscsi/
/iscsi> ls
o- iscsi ........................................................................................................... [Targets: 1]
  o- iqn.2018-05.storage.kvm.com:stor1 ................................................................................ [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.2018-05.storage.kvm.com:stor1/tpg1/luns
/iscsi/iqn.20...or1/tpg1/luns> create /backstores/fileio/disk01
Created LUN 0.
/iscsi/iqn.20...or1/tpg1/luns> create /backstores/fileio/disk02
Created LUN 1.
/iscsi/iqn.20...or1/tpg1/luns> ls
o- luns ............................................................................................................... [LUNs: 2]
  o- lun0 ............................................................. [fileio/disk01 (/labstor1/disk01.img) (default_tg_pt_gp)]
  o- lun1 ............................................................. [fileio/disk02 (/labstor1/disk02.img) (default_tg_pt_gp)]
/iscsi/iqn.20...or1/tpg1/acls> create iqn.1994-05.com.redhat:kvm
Created Node ACL for iqn.1994-05.com.redhat:kvm
Created mapped LUN 1.
Created mapped LUN 0.
/iscsi/iqn.20...or1/tpg1/acls> create iqn.1994-05.com.redhat:node2
Created Node ACL for iqn.1994-05.com.redhat:node2
Created mapped LUN 1.
Created mapped LUN 0.
/iscsi/iqn.20...or1/tpg1/acls> ls
o- acls ............................................................................................................... [ACLs: 2]
  o- iqn.1994-05.com.redhat:kvm ................................................................................ [Mapped LUNs: 2]
  | o- mapped_lun0 .................................................................................... [lun0 fileio/disk01 (rw)]
  | o- mapped_lun1 .................................................................................... [lun1 fileio/disk02 (rw)]
  o- iqn.1994-05.com.redhat:node2 .............................................................................. [Mapped LUNs: 2]
    o- mapped_lun0 .................................................................................... [lun0 fileio/disk01 (rw)]
    o- mapped_lun1 .................................................................................... [lun1 fileio/disk02 (rw)
/> saveconfig 
Configuration saved to /etc/target/saveconfig.json
[root@storage ~]# systemctl start target
[root@storage ~]# systemctl enable target
Created symlink from /etc/systemd/system/multi-user.target.wants/target.service to /usr/lib/systemd/system/target.service.

三、客户端 initiator 连接 target

1.检查软件是否安装及查看 initiatorname 名称

[root@master ~]# rpm -qa |grep iscsi
libiscsi-1.9.0-7.el7.x86_64
libvirt-daemon-driver-storage-iscsi-3.9.0-14.el7_5.2.x86_64
iscsi-initiator-utils-6.2.0.873-35.el7.x86_64
iscsi-initiator-utils-iscsiuio-6.2.0.873-35.el7.x86_64

[root@k-node2 ~]# rpm -qa |grep iscsi
libiscsi-1.9.0-7.el7.x86_64
iscsi-initiator-utils-iscsiuio-6.2.0.874-4.el7.x86_64
iscsi-initiator-utils-6.2.0.874-4.el7.x86_64
libvirt-daemon-driver-storage-iscsi-3.2.0-14.el7_4.9.x86_64

[root@k-node2 ~]# vim /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.1994-05.com.redhat:node2

[root@master ~]# cat /etc/iscsi/initiatorname.iscsi 
InitiatorName=iqn.1994-05.com.redhat:kvm

2.检查是否可以发现

[root@master ~]# iscsiadm --mode discovery --type sendtargets --portal 172.16.216.167
172.16.216.167:3260,1 iqn.2018-05.storage.kvm.com:stor1

3.测试挂载设备

[root@master ~]# iscsiadm -d2 -m node --login     #<--登陆节点
iscsiadm: Max file limits 1024 4096
iscsiadm: libvirt-iface-08137b06: Creating session 1/1
Logging in to [iface: libvirt-iface-08137b06, target: iqn.2018-05.storage.kvm.com:stor1, portal: 172.16.216.167,3260] (multiple)
Login to [iface: libvirt-iface-08137b06, target: iqn.2018-05.storage.kvm.com:stor1, portal: 172.16.216.167,3260] successful.


[root@master ~]# iscsiadm -m node              #<--查看iscsi发现记录
172.16.216.167:3260,1 iqn.2018-05.storage.kvm.com:stor1

4.断开与 target 的连接

[root@master ~]# iscsiadm -d2 -m node --logout    #<--退出节点

四、故障解决

错误:

ImportError: cannot import name ALUATargetPortGroup

解决方法:

yum install python-rtslib -y