iSCSI,即Internet Small Computer System Interface,Internet小型计算机系统接口,又称为IP-SAN,是一种基于因特网及SCSI-3协议下的存储技术。也是一种在TCP/IP上进行数据块传输的标准。iSCSI可以实现在IP网络上运行SCSI协议,使其能够在诸如高速千兆以太网上进行快速的数据存取备份操作。
优点:
接口具有应用范围广、多任务、带宽大、CPU占用率低,以及热插拔等。
工作流程:
iSCSI系统由SCSI适配器发送一个SCSI命令。
命令封装到TCP/IP包中并送入到以太网络。
接收方从TCP/IP包中抽取SCSI命令并执行相关操作。
把返回的SCSI命令和数据封装到TCP/IP包中,将它们发回到发送方。
系统提取出数据或命令,并把它们传回SCSI子系统。
通过iscsi实现存储共享,创建IQN用于识别启动器和目标,然后建立tpg共享存储组,设置相应的访问控制权限,然后在共享存储组添加lun逻辑单元(存储设备),然后建立实现共享portals入口(ip,port)。
概念:
TPG:共享存储组,某个特定iSCSI目标要侦听的接口IP地址和TCP端口的集合。可以将目标配置添加到TPG以协调多个LUN的设置。
ACL:访问权限控制列表,一种使用节点IQN(通常是启动器名称)来验证启动器的访问权限的访问限制。
IQN:iSCSI限定名称,全球唯一名称,用于以强制命名格式来识别启动器和目标。
IQN格式如下:
iqn.YYYY-MM.com.reversed.domain[:optional_string]
iqn:表示此名称使用域为标识符。
YYYY-MM:表示拥有域名的年月时间。
com.reversed.domain:拥有此iSCSI组织的逆向域名
:optional_string:以冒号为前缀的可选字符串,全球唯一,由域所有者分配,其中可包含冒号为分割符的组织边界。
LUN:逻辑单元号,带有编号的块设备,连接到目标且通过目标来使用。可以有一个或多个LUN连接到单个目标,但通常一个目标提供一个LUN。
portals入口:目标或启动器上用于建立的IP地址和端口,默认3260端口。
具体配置:
一、服务器用Linux,客户端用Windows访问
1、准备一个分区
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (50759680-83886079, default 50759680): +10G
Value out of range.
First sector (50759680-83886079, default 50759680):
Using default value 50759680
Last sector, +sectors or +size{K,M,G} (50759680-83886079, default 83886079):
Using default value 83886079
Partition 3 of type Linux and of size 15.8 GiB is set
Command (m for help): p
Disk /dev/sda: 42.9 GB, 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 / 512 bytes
Disk label type: dos
Disk identifier: 0x000b0f8f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 50759679 25174016 8e Linux LVM
/dev/sda3 50759680 83886079 16563200 83 Linux
Command (m for help): w
The partition table has been altered!
[root@localhost ~]# partprobe
2、装包,关闭防火墙和selinux
[root@localhost ~]# yum install targetcli
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
3、启动服务
[root@localhost ~]# systemctl start target
4、创建
[root@localhost ~]# targetcli
targetcli shell version 2.1.fb41
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.
4.1、查看菜单结构
/> 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]
4.2、切进目录,创建LUN
/> cd backstores/block
/backstores/block> ls
o- block ............................................. [Storage Objects: 0]
/backstores/block> create name=lun0 dev=/dev/sda3
Created block storage object lun0 using /dev/sda3.
/backstores/block> ls
o- block ............................................. [Storage Objects: 1]
o- lun0 .................... [/dev/sda3 (15.8GiB) write-thru deactivated]
4.3、创建IQN
/backstores/block> cd /
/> cd iscsi
/iscsi> ls
o- iscsi ..................................................... [Targets: 0]
关闭自动创建portal功能
/iscsi> set group=global auto_add_default_portal=false
Parameter auto_add_default_portal is now 'false'.
创建IQN
/iscsi> create
Created target iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.e531d000cb15.
Created TPG 1.
/iscsi> ls
o- iscsi ..................................................... [Targets: 1]
o- iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.e531d000cb15 [TPGs: 1]
o- tpg1 ........................................ [no-gen-acls, no-auth]
o- acls ................................................... [ACLs: 0]
o- luns ................................................... [LUNs: 0]
o- portals ............................................. [Portals: 0]
/iscsi> cd /
/> ls
o- / ................................................................ [...]
o- backstores ..................................................... [...]
| o- block ......................................... [Storage Objects: 1]
| | o- lun0 ................ [/dev/sda3 (15.8GiB) write-thru deactivated]
| 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.localhost.x8664:sn.1901fd98d2f4 [TPGs: 1]
| o- tpg1 ...................................... [no-gen-acls, no-auth]
| o- acls ................................................. [ACLs: 0]
| o- luns ................................................. [LUNs: 0]
| o- portals ........................................... [Portals: 0]
o- loopback ................................................ [Targets: 0]
4.4、添加target
/> cd iscsi/iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.1901fd98d2f4/tpg1/
/iscsi/iqn.20...fd98d2f4/tpg1> set attribute authentication=0 demo_mode_write_protect=0 generate_node_acls=1 cache_dynamic_acls=1 #属性认证关闭
Parameter demo_mode_write_protect is now '0'. #演示模式写保护关闭
Parameter authentication is now '0'.
Parameter generate_node_acls is now '1'. #生成节点acl
Parameter cache_dynamic_acls is now '1'. #缓存动态acl
4.5、添加LUN
/iscsi/iqn.20...fd98d2f4/tpg1> cd luns
/iscsi/iqn.20...2f4/tpg1/luns> ls
o- luns ......................................................... [LUNs: 0]
/iscsi/iqn.20...2f4/tpg1/luns> create /backstores/block/lun0
Created LUN 0.
/iscsi/iqn.20...2f4/tpg1/luns> ls
o- luns ......................................................... [LUNs: 1]
o- lun0 ........................................ [block/lun0 (/dev/sda3)]
4.6创建portal接口
/iscsi/iqn.20...2f4/tpg1/luns> cd ..
/iscsi/iqn.20...fd98d2f4/tpg1> cd portals
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ................................................... [Portals: 0]
/iscsi/iqn.20.../tpg1/portals> create 192.168.100.160 3260
Using default IP port 3260
Created network portal 192.168.100.160:3260.
/iscsi/iqn.20.../tpg1/portals> cd /
/> ls
o- / ................................................................ [...]
o- backstores ..................................................... [...]
| o- block ......................................... [Storage Objects: 1]
| | o- lun0 .................. [/dev/sda3 (15.8GiB) write-thru activated]
| 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.localhost.x8664:sn.1901fd98d2f4 [TPGs: 1]
| o- tpg1 ......................................... [gen-acls, no-auth]
| o- acls ................................................. [ACLs: 0]
| o- luns ................................................. [LUNs: 1]
| | o- lun0 ................................ [block/lun0 (/dev/sda3)]
| o- portals ........................................... [Portals: 1]
| o- 192.168.100.160:3260 .................................... [OK]
o- loopback ................................................ [Targets: 0]
4.7、退出targetcli
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
4.8、重启服务
[root@localhost ~]# systemctl restart target
打开windows的iSCSI发起程序,根据端口和ip找到目标并连接:
在磁盘管理中打开后会发现该磁盘,格式化之后可以使用了。
二、服务器用Linux,客户端用Linux访问
服务器的配置如上述,现在我们来看一下用Linux做客户端该怎么配置。
1、装包
[root@localhost ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# yum install iscsi-initiator-utils.i686 -y
2、发现目标
[root@localhost ~]# iscsiadm -m discovery -t sendtargets -p 192.168.100.160192.168.100.160:3260,1 iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.1901fd98d2f4
3、挂载磁盘
[root@localhost ~]# iscsiadm -mode node -T iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.1901fd98d2f4 -p 192.168.100.160:3260 -l
Logging in to [iface: default, target: iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.1901fd98d2f4, portal: 192.168.100.160,3260] (multiple)
Login to [iface: default, target: iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.1901fd98d2f4, portal: 192.168.100.160,3260] successful.
4、查看挂载情况
[root@localhost ~]# fdisk -l
Disk /dev/sda: 42.9 GB, 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 / 512 bytes
Disk label type: dos
Disk identifier: 0x000b0f8f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 50759679 25174016 8e Linux LVM
Disk /dev/mapper/rhel-root: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/rhel-swap: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sdb: 17.0 GB, 16960716800 bytes, 33126400 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
Disk label type: dos
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdb1 1 4294967295 2147483647+ ee GPT
[root@localhost ~]# lsscsi
[0:0:0:0] disk VMware, VMware Virtual S 1.0 /dev/sda
[2:0:0:0] cd/dvd NECVMWar VMware SATA CD01 1.00 /dev/sr0
[33:0:0:0] disk LIO-ORG lun0 4.0 /dev/sdb
5、查看分区
[root@localhost ~]# ll /dev/disk/by-path/
total 0
lrwxrwxrwx. 1 root root 9 Apr 16 11:29 ip-192.168.100.160:3260-iscsi-iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.1901fd98d2f4-lun-0 -> ../../sdb
lrwxrwxrwx. 1 root root 10 Apr 16 11:29 ip-192.168.100.160:3260-iscsi-iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.1901fd98d2f4-lun-0-part1 -> ../../sdb1
lrwxrwxrwx. 1 root root 10 Apr 16 11:29 ip-192.168.100.160:3260-iscsi-iqn.2003-01.org.linux-iscsi.localhost.x8664:sn.1901fd98d2f4-lun-0-part2 -> ../../sdb2
lrwxrwxrwx. 1 root root 9 Apr 16 2019 pci-0000:00:10.0-scsi-0:0:0:0 -> ../../sda
lrwxrwxrwx. 1 root root 10 Apr 16 2019 pci-0000:00:10.0-scsi-0:0:0:0-part1 -> ../../sda1
lrwxrwxrwx. 1 root root 10 Apr 16 2019 pci-0000:00:10.0-scsi-0:0:0:0-part2 -> ../../sda2
lrwxrwxrwx. 1 root root 9 Apr 16 2019 pci-0000:02:04.0-ata-2.0 -> ../../sr0
6、查看日志
[root@localhost ~]# grep sdb /var/log/messages
Apr 16 11:29:02 localhost kernel: sd 33:0:0:0: [sdb] 33126400 512-byte logical blocks: (16.9 GB/15.7 GiB)
Apr 16 11:29:02 localhost kernel: sd 33:0:0:0: [sdb] Write Protect is off
Apr 16 11:29:02 localhost kernel: sd 33:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
Apr 16 11:29:02 localhost kernel: sdb: sdb1 sdb2
Apr 16 11:29:02 localhost kernel: sd 33:0:0:0: [sdb] Attached SCSI disk
7、自动挂载
编辑文件,添加
[root@localhost ~]# vim /etc/fstab
UUID=2bcbfdef-f655-4b96-90cd-9b5661995de6 /test xfs defaults,_netdev 0 0
添加磁盘的UUID,通过blkid查看 开机启动关闭防火墙
重启查看效果。
删除:
targetcli iscsi/ delete iqn....