iSCSI:
 internet SCSI(iSCSI):iSCSI主要是通过TCP/IP 的技术,将存储设备通过iSCSI target(iSCSI标的)功能,做成可以提供磁盘的服务器端,再通过iSCSI initiator(iSCSI 初始化用户)功能,做成能够挂载使用iSCSI target的客户端,如此便能通过iSCSI协议来进行磁盘的应用了。

 iSCSI 是IETF制定的一项标准,它是一个供硬件设备使用的可以在IP协议的上层运行的SCSI指令集,这种指令集和
 用于将SCSI数据块映射为以太网数据包,以此来实现在IP网络上运行SCSI协议,使其能够在诸如高速千兆以太网上进行路由选择.

从根本上说,iSCSI是一种基于IP Storage理论的新型存储技术,该技术将广泛使用的SCSI接口技术与IP网络技术相结合,可以在IP
网络上构建SAN。简单的说,iSCSI就是在IP网络上运行SCSI协议的一种网络存储技术。


 iSCSI这个架构主要将存储设备与使用的主机分为俩个部分,分别是:

 	iSCSI target:就是存储设备端,存放磁盘RAID的设备,目前也能够将Linux主机仿真成iSCSI target,目的在于提供其他主机使用的磁盘。

 	iSCSI initiator:就是能够使用target的客户端,通常是服务器,想要连接到iSCSI target的服务器,也必须要安装iSCSI initiator的相关功能后才能使用iSCSI target提供的磁盘。


 iSCSI的优势:
 	iSCSI沿用TCP/IP协议,而TCP/IP是在网络方面最通用、最成熟的协议,且IP网络的基础建非常完善,同时SCSI技术是被磁盘和磁带等设备
 	广泛采用的存储标准,这俩点是iSCSI的建设费用和维护成本非常低廉。

 	iSCSI支持一般的以太网交换机而不是特殊的光线通道交换机,从而减少了异构网络带来的风险。
 	iSCSI是通过IP封包传输存储命令,因此可以在整个Internet上传输数据,没有距离的限制。




iSCSI target(iSCSI标的)设置:(也就是服务端)
	所需程序:RHEL7使用targetcli,targetd(RHEL6使用:scsi-target-utils(tgt) )

	启动targetcli
	systemctl start target


iSCSI target(iSCSI标的)的实际设置:
	iSCSI就是通过一个网络接口,将既有的磁盘共享出去。
	那么那些类型的磁盘可以共享呢,这包括:
		使用dd命令所建立的大型文件可供仿真为磁盘(无须预先格式化)
		使用单一分区(partition)共享为磁盘
		使用单一完整的磁盘(无须预先分区)
		使用磁盘整列共享(其实与单一磁盘相同方式)
		使用软件磁盘阵列(software RAID)共享成单一磁盘
		使用LVM的LV设备共享为磁盘

ISCSI服务端配置:

ISCSI配置范例:(使用硬盘做存储端)
iSCSI target(iSCSI标的)的配置:(服务器端ip:192.168.1.118)

	1.创建2个4GB的分区/dev/sdb1,/dev/sdb1 

	2. yum install target* -y 

	3. systemctl start target ; systemctl enable target

	targetcli是用于管理iSCSI服务端存储的资源的专用配置命令,它能够提供类似于fdsik命令的交互式配置功能,
	将iSCSI共享资源的配置内容抽象成"目录"的形式,我们只需将各类配置信息填入到相应的"目录"中既可。
	/backstores/block是iSCSI服务端配置共享设备的位置。


	4.进入target子系统:
		[root@localhost ~]# targetcli  
		Warning: Could not load preferences file /root/.targetcli/prefs.bin.
		targetcli shell version 2.1.fb37
		Copyright 2011-2013 by Datera, Inc and others.
		For help on commands, type 'help'.

		/> 


	5.  创建block1,block2,分别对应/dev/sdb1,/dev/sdb2

		[root@localhost ~]# targetcli
		Warning: Could not load preferences file /root/.targetcli/prefs.bin.
		targetcli shell version 2.1.fb37
		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]
		/> /backstores/block create block1 /dev/sdb1
		Created block storage object block1 using /dev/sdb1.
		/> ls
		o- / ......................................................................................................................... [...]
		  o- backstores .............................................................................................................. [...]
		  | o- block .................................................................................................. [Storage Objects: 1]
		  | | o- block1 ........................................................................ [/dev/sdb1 (3.7GiB) write-thru deactivated]
		  | o- fileio ................................................................................................. [Storage Objects: 0]
		  | o- pscsi .................................................................................................. [Storage Objects: 0]
		  | o- ramdisk ................................................................................................ [Storage Objects: 0]
		  o- iscsi ............................................................................................................ [Targets: 0]
		  o- loopback ......................................................................................................... [Targets: 0]
		/> /backstores/block create block2 /dev/sdb2
		Created block storage object block2 using /dev/sdb2.
		/> ls
		o- / ......................................................................................................................... [...]
		  o- backstores .............................................................................................................. [...]
		  | o- block .................................................................................................. [Storage Objects: 2]
		  | | o- block1 ........................................................................ [/dev/sdb1 (3.7GiB) write-thru deactivated]
		  | | o- block2 ........................................................................ [/dev/sdb2 (3.7GiB) write-thru deactivated]
		  | o- fileio ................................................................................................. [Storage Objects: 0]
		  | o- pscsi .................................................................................................. [Storage Objects: 0]
		  | o- ramdisk ................................................................................................ [Storage Objects: 0]
		  o- iscsi ............................................................................................................ [Targets: 0]
		  o- loopback ......................................................................................................... [Targets: 0]
		/> 


   6. 创建target iqn.2019-04.com.test.www:disk
   	    /> /iscsi create iqn.2019-04.com.test.www:disk
		Created target iqn.2019-04.com.test.www:disk.
		Created TPG 1.
		Global pref auto_add_default_portal=true
		Created default portal listening on all IPs (0.0.0.0), port 3260.
		/> 
		/> ls
		o- / ......................................................................................................................... [...]
		  o- backstores .............................................................................................................. [...]
		  | o- block .................................................................................................. [Storage Objects: 2]
		  | | o- block1 ........................................................................ [/dev/sdb1 (3.7GiB) write-thru deactivated]
		  | | o- block2 ........................................................................ [/dev/sdb2 (3.7GiB) write-thru deactivated]
		  | o- fileio ................................................................................................. [Storage Objects: 0]
		  | o- pscsi .................................................................................................. [Storage Objects: 0]
		  | o- ramdisk ................................................................................................ [Storage Objects: 0]
		  o- iscsi ............................................................................................................ [Targets: 1]
		  | o- iqn.2019-04.com.test.www:disk ..................................................................................... [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]
		/> 




	7.配置target iqn.2019-04.com.test.www:disk
	(iSCSI协议是通过客户端名称进行验证的,也就是说用户在访问存储共享资源时不需要输入密码,只要iSCSI客户端的名称与服务端中
	设置的访问控制列表中某一项名称条目既可,因此需要在iSCSI服务端的配置文件中写入一串能够验证用户信息的名称。acls参数目录用于存放能够访问iSCSI服务端共享存储资源的客户端名称)

	/> cd /iscsi/iqn.2019-04.com.test.www:disk/tpg1/
	/iscsi/iqn.20...www:disk/tpg1> ls
	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/iqn.20...www:disk/tpg1> acls/ create iqn.2019-04.com.test.www:xx  (创建acls)
	Created Node ACL for iqn.2019-04.com.test.www:xx
	/iscsi/iqn.20...www:disk/tpg1> ls
	o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
	  o- acls ................................................................................................................ [ACLs: 1]
	  | o- iqn.2019-04.com.test.www:xx ................................................................................ [Mapped LUNs: 0]
	  o- luns ................................................................................................................ [LUNs: 0]
	  o- portals .......................................................................................................... [Portals: 1]
	    o- 0.0.0.0:3260 ........................................................................................................... [OK]
	/iscsi/iqn.20...www:disk/tpg1> 



	/iscsi/iqn.20...www:disk/tpg1> ls
	o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
	  o- acls ................................................................................................................ [ACLs: 1]
	  | o- iqn.2019-04.com.test.www:xx ................................................................................ [Mapped LUNs: 0]
	  o- luns ................................................................................................................ [LUNs: 0]
	  o- portals .......................................................................................................... [Portals: 1]
	    o- 0.0.0.0:3260 ........................................................................................................... [OK]
	/iscsi/iqn.20...www:disk/tpg1> luns/ create /backstores/block/block1
	Created LUN 0.
	Created LUN 0->0 mapping in node ACL iqn.2019-04.com.test.www:xx
	/iscsi/iqn.20...www:disk/tpg1> luns/ create /backstores/block/block2       (使block与lun关联)
	Created LUN 1.
	Created LUN 1->1 mapping in node ACL iqn.2019-04.com.test.www:xx
	/iscsi/iqn.20...www:disk/tpg1> 

	/iscsi/iqn.20...www:disk/tpg1> ls
	o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
	  o- acls ................................................................................................................ [ACLs: 1]
	  | o- iqn.2019-04.com.test.www:xx ................................................................................ [Mapped LUNs: 2]
	  |   o- mapped_lun0 ...................................................................................... [lun0 block/block1 (rw)]
	  |   o- mapped_lun1 ...................................................................................... [lun1 block/block2 (rw)]
	  o- luns ................................................................................................................ [LUNs: 2]
	  | o- lun0 ............................................................................................. [block/block1 (/dev/sdb1)]
	  | o- lun1 ............................................................................................. [block/block2 (/dev/sdb2)]
	  o- portals .......................................................................................................... [Portals: 1]
	    o- 0.0.0.0:3260 ........................................................................................................... [OK]





	/iscsi/iqn.20...www:disk/tpg1> ls
	o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
	  o- acls ................................................................................................................ [ACLs: 1]
	  | o- iqn.2019-04.com.test.www:xx ................................................................................ [Mapped LUNs: 2]
	  |   o- mapped_lun0 ...................................................................................... [lun0 block/block1 (rw)]
	  |   o- mapped_lun1 ...................................................................................... [lun1 block/block2 (rw)]
	  o- luns ................................................................................................................ [LUNs: 2]
	  | o- lun0 ............................................................................................. [block/block1 (/dev/sdb1)]
	  | o- lun1 ............................................................................................. [block/block2 (/dev/sdb2)]
	  o- portals .......................................................................................................... [Portals: 1]
	    o- 0.0.0.0:3260 ........................................................................................................... [OK]
	/iscsi/iqn.20...www:disk/tpg1> portals/ delete 0.0.0.0 3260       (删除默认的监听地址)
	Deleted network portal 0.0.0.0:3260
	/iscsi/iqn.20...www:disk/tpg1> portals/ create 192.168.1.118 3260   (创建新的监听地址)
	Using default IP port 3260
	Created network portal 192.168.1.118:3260.
	/iscsi/iqn.20...www:disk/tpg1> ls
	o- tpg1 ..................................................................................................... [no-gen-acls, no-auth]
	  o- acls ................................................................................................................ [ACLs: 1]
	  | o- iqn.2019-04.com.test.www:xx ................................................................................ [Mapped LUNs: 2]
	  |   o- mapped_lun0 ...................................................................................... [lun0 block/block1 (rw)]
	  |   o- mapped_lun1 ...................................................................................... [lun1 block/block2 (rw)]
	  o- luns ................................................................................................................ [LUNs: 2]
	  | o- lun0 ............................................................................................. [block/block1 (/dev/sdb1)]
	  | o- lun1 ............................................................................................. [block/block2 (/dev/sdb2)]
	  o- portals .......................................................................................................... [Portals: 1]
	    o- 192.168.1.118:3260 ..................................................................................................... [OK]
	/iscsi/iqn.20...www:disk/tpg1> 

	/iscsi/iqn.20...www:disk/tpg1> exit   (退出target子系统)
	Global pref auto_save_on_exit=true
	Last 10 configs saved in /etc/target/backup.
	Configuration saved to /etc/target/saveconfig.json    (刚才做到配置的保持位置,修改此配置文件的相关值,重启target服务后,既可修改target的配置)
	[root@localhost ~]# 

iSCSI initiator(客户端)配置:

	1.yum install iscsi* -y


	iscsiadm是用于管理,查询,插入,更新或删除iSCSI数据库配置文件的命令行工具,用户需要先使用这个工具
	扫描发现远程iSCSI服务端,然后查看找到的服务器上有哪些可用的共享存储资源。

	2.  发现操作
		[root@rhel002 ~]# iscsiadm -m discovery -t st -p 192.168.1.118
		192.168.1.118:3260,1 iqn.2019-04.com.test.www:xx                 #服务端iscsi ACLS名称
		[root@rhel002 ~]# 

			# -m discovery: 扫描并发现可用的存储资源
			# -t st参数为执行扫描操作的类型
			# -p xxx iSCSI服务端的IP地址



	3.编辑/etc/iscsi/initiatorname.iscsi 文件,修改InitiatorName值

	InitiatorName=iqn.2019-04.com.test.www:xx       #注意此处应该设置的值为ISCSI服务端的 acls值

	4.重启服务
		[root@rhel002 ~]# systemctl restart iscsid
		[root@rhel002 ~]# systemctl enable iscsid

	5.登录服务端:

	[root@rhel002 ~]# iscsiadm -m node -T iqn.2019-04.com.test.www:disk -p 192.168.1.118 -l
	Logging in to [iface: default, target: iqn.2019-04.com.test.www:disk, portal: 192.168.1.118,3260] (multiple)
	Login to [iface: default, target: iqn.2019-04.com.test.www:disk, portal: 192.168.1.118,3260] successful.
	[root@rhel002 ~]# 

			# -m node :将客户端所在主机作为一台节点服务器
			# -T xxxx :指定要使用的存储资源
			# -l:进行登录验证

	6.

	[root@rhel002 ~]# lsblk
	NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
	sda             8:0    0   20G  0 disk 
	├─sda1          8:1    0  500M  0 part /boot
	└─sda2          8:2    0 19.5G  0 part 
	  ├─rhel-swap 253:0    0    2G  0 lvm  [SWAP]
	  └─rhel-root 253:1    0 17.5G  0 lvm  /
	sdb             8:16   0  3.7G  0 disk 
	sdc             8:32   0  3.7G  0 disk 
	sr0            11:0    1  3.6G  0 rom  /mnt/cdrom
	[root@rhel002 ~]# 


	这里的sdb,sdc 就是服务端的分区,对应服务端的luno,lun1
	sdb,sdc可以当做本地的分区来使用,再对sdb,sdc进行分区格式化后,需要永久挂载时需注意,
	/etc/fstab中相应的配置行的挂载参数需写成defaults,_netdev,范例如下;

	/dev/sdb1              /mnt/netdisk   xfs          defaults,_netdev 0 0

	如缺少_netdev值,则重启时,系统将无法成功启动

	可以使用 lsscsi命令查看SCSI控制器设备的信息:

	[root@rhel002 ~]# lsscsi
	[2:0:0:0]    disk    VMware,  VMware Virtual S 1.0   /dev/sda 
	[4:0:0:0]    cd/dvd  NECVMWar VMware SATA CD01 1.00  /dev/sr0 
	[34:0:0:0]   disk    LIO-ORG  block1           4.0   /dev/sdb 
	[34:0:0:1]   disk    LIO-ORG  block2           4.0   /dev/sdc 
	[root@rhel002 ~]# 


		iscsiadm -m session [-P #] 命令可以查看与服务端的连接属性,#取值范围为0-3,表示显示信息的详细程度,不带-P 选项的话默认为-P 0

	[root@rhel002 ~]# iscsiadm -m session 
	tcp: [2] 192.168.1.118:3260,1 iqn.2019-04.com.test.www:disk (non-flash)
	[root@rhel002 ~]# iscsiadm -m session -P 1
	Target: iqn.2019-04.com.test.www:disk (non-flash)
		Current Portal: 192.168.1.118:3260,1
		Persistent Portal: 192.168.1.118:3260,1
			**********
			Interface:
			**********
			Iface Name: default
			Iface Transport: tcp
			Iface Initiatorname: iqn.2019-04.com.test.www:xx
			Iface IPaddress: 192.168.1.117
			Iface HWaddress: <empty>
			Iface Netdev: <empty>
			SID: 2
			iSCSI Connection State: LOGGED IN
			iSCSI Session State: LOGGED_IN
			Internal iscsid Session State: NO CHANGE
	[root@rhel002 ~]# 


	7.如何临时断开与服务端的指定连接 :(重启客户端后还会连接)
		iscsiadm -m node -T iqn.2019-04.com.test.www:disk -p 192.168.1.118 -u

			# -u :卸载


		[root@rhel002 ~]# lsblk
		NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
		sda             8:0    0   20G  0 disk 
		├─sda1          8:1    0  500M  0 part /boot
		└─sda2          8:2    0 19.5G  0 part 
		  ├─rhel-swap 253:0    0    2G  0 lvm  [SWAP]
		  └─rhel-root 253:1    0 17.5G  0 lvm  /
		sr0            11:0    1  3.6G  0 rom  /mnt/cdrom
		[root@rhel002 ~]# 


	8.删除与服务端的指定连接:(彻底与服务端断开连接,重启也不会再连接服务端)
		iscsiadm -m node -T iqn.2019-04.com.test.www:disk -p 192.168.1.118 -o delete

		(删除连接后如想再连接服务端,需先重新发现一下(iscsiadm -m discovery -t st -p 192.168.1.118),然后再重新登录服务端)




	9.如何断开与服务端的所有连接:
		iscsiadm -m node -u ALL 

	10.如何删除与服务端的所有连接:
		iscsiadm -m node -o delete