iSCSI分为提供存储设备的服务端和使用存储的客户端
-
iSCSI target:存储设备服务端,存放磁盘或RAID的设备。目的在于提供给其他主机使用
-
iSCSI initiator:使用target存储服务的客户端
SAN和NAS
-
SAN(存储局域网):来自局域网内的其他存储设备提供的磁盘,可以进行格式化操作(如iSCSI)
-
NAS(网络文件系统):来自NAS提供的文件系统,只能立即使用,不能进行格式化(如NFS)
一、存储服务端上安装配置iSCSI target
1、在存储服务器上安装scsi-target-utils软件(rhel7为targetcli)
1
|
[root@node1 ~]
# yum install scsi-target-utils
|
2、启动iscsi服务
1
2
3
4
5
6
|
[root@node1 ~]
# /etc/init.d/tgtd start
[root@node1 ~]
# chkconfig tgtd on
# 监听端口为3260
[root@node1 ~]
# netstat -tuanlp | grep tgtd
tcp 0 0 0.0.0.0:3260 0.0.0.0:* LISTEN 1248
/tgtd
tcp 0 0 :::3260 :::* LISTEN 1248
/tgtd
|
3、scsi-target-utils的相关配置文件
1
2
3
4
5
6
|
# /etc/tgt/targets.conf 主配置文件,设置要共享的磁盘格式与块设备等
# /usr/sbin/tgt-admin 查询、删除target等功能的设置工具
# /usr/sbin/tgt-setup-lun 建立target以及设置共享的磁盘与可使用的客户端等的程序
# /usr/sbin/tgtadm iscsi target管理程序(可使用配置文件取代)
# /usr/sbin/tgtd 提供iSCSI target服务的主程序
# /usr/sbin/tgtimg 搭建共享镜像文件设备程序
|
4、建立分区配置iSCSI存储设备
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
==================================================================
# 创建一个容量为1G的普通分区
[root@node1 ~]
# fdisk /dev/sdb
Command (m
for
help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +1G
Command (m
for
help): p
Disk
/dev/sdb
: 5368 MB, 5368709120 bytes
255 heads, 63 sectors
/track
, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x85d4a809
Device Boot Start End Blocks Id System
/dev/sdb1
1 132 1060258+ 83 Linux
Command (m
for
help): w
The partition table has been altered!
Calling ioctl() to re-
read
partition table.
Syncing disks.
[root@node1 ~]
# ll /dev/sdb*
brw-rw---- 1 root disk 8, 16 Sep 1 00:23
/dev/sdb
brw-rw---- 1 root disk 8, 17 Sep 1 00:23
/dev/sdb1
==================================================================
# 创建一个容量为1G LVM分区
Command (m
for
help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-652, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-652, default 652): +1G
Command (m
for
help): t
Partition number (1-4): 2
Hex code (
type
L to list codes): 8e
Changed system
type
of partition 2 to 8e (Linux LVM)
Command (m
for
help): p
Disk
/dev/sdb
: 5368 MB, 5368709120 bytes
255 heads, 63 sectors
/track
, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x85d4a809
Device Boot Start End Blocks Id System
/dev/sdb1
1 132 1060258+ 83 Linux
/dev/sdb2
133 264 1060290 8e Linux LVM
[root@node1 ~]
# ll /dev/sdb
sdb sdb1 sdb2
[root@node1 ~]
# ll /dev/sdb*
brw-rw---- 1 root disk 8, 16 Sep 1 00:27
/dev/sdb
brw-rw---- 1 root disk 8, 17 Sep 1 00:27
/dev/sdb1
brw-rw---- 1 root disk 8, 18 Sep 1 00:27
/dev/sdb2
[root@node1 ~]
# pvcreate /dev/sdb2
Physical volume
"/dev/sdb2"
successfully created
[root@node1 ~]
# vgcreate -s 8M VolGroup00 /dev/sdb2
Volume group
"VolGroup00"
successfully created
[root@node1 ~]
# lvcreate -L 800M -n iscsi00 VolGroup00
Logical volume
"iscsi00"
created
[root@node1 ~]
# lvscan
ACTIVE
'/dev/VolGroup00/iscsi00'
[800.00 MiB] inherit
ACTIVE
'/dev/VolGroup/lv_root'
[8.57 GiB] inherit
ACTIVE
'/dev/VolGroup/lv_swap'
[960.00 MiB] inherit
[root@node1 ~]
# lvdisplay
--- Logical volume ---
LV Path
/dev/VolGroup00/iscsi00
LV Name iscsi00
VG Name VolGroup00
LV UUID 2G9zLo-kWmj-ydMH-luer-wM0i-4bdl-UJwJov
LV Write Access
read
/write
LV Creation host,
time
node1, 2016-09-01 00:31:14 +0800
LV Status available
# open 0
LV Size 800.00 MiB
Current LE 100
Segments 1
Allocation inherit
Read ahead sectors auto
- currently
set
to 256
Block device 253:2
|
5、共享iSCSI target文件名
1
2
|
iqn.YYYY-mm.<域名的反转写法>:共享名
如:iqn.2015-06.org.haungming:viscsidisk
|
6、配置/etc/tgt/targets.conf,设置共享存储设备的路径相关属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# Sample target with one LUN only. Defaults to allow access for all initiators:
#<target iqn.2008-09.com.example:server.target1>
# backing-store /dev/LVM/somedevice
#</target>
<target iqn.2016-09.org.huangming:viscsidisk>
backing-store
/dev/sdb1
backing-store
/dev/VolGroup00/iscsi00
initiator-address 10.0.0.12
initiator-address 10.0.0.0
/24
incominguser viscsiuser viscsipasswd
write-cache off
<
/target
>
# backing-store:虚拟的设备
# initiator-address:允许使用的客户端
# incominguser:配置通过用户和密码访问
# write-cache:不使用缓存,直接访问存储设备
|
7、重启tgtd服务
1
2
3
|
[root@node1 ~]
# /etc/init.d/tgtd restart
Stopping SCSI target daemon: [ OK ]
Starting SCSI target daemon: [ OK ]
|
8、查看iscsi target共享存储的相关信息,使用tgt-admin --show命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
[root@node1 ~]
# tgt-admin --show
Target 1: iqn.2016-09.org.huangming:viscsidisk
System information:
Driver: iscsi
State: ready
I_T nexus information:
LUN information:
LUN: 0
Type: controller
SCSI ID: IET 00010000
SCSI SN: beaf10
Size: 0 MB, Block size: 1
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
Backing store
type
: null
Backing store path: None
Backing store flags:
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 839 MB, Block size: 512
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
Backing store
type
: rdwr
Backing store path:
/dev/VolGroup00/iscsi00
Backing store flags:
LUN: 2
Type: disk
SCSI ID: IET 00010002
SCSI SN: beaf12
Size: 1086 MB, Block size: 512
Online: Yes
Removable media: No
Prevent removal: No
Readonly: No
Backing store
type
: rdwr
Backing store path:
/dev/sdb1
Backing store flags:
Account information:
viscsiuser
ACL information:
10.0.0.12
/24
|
9、防火墙设置
1
|
[root@node1 ~]
# iptables -A INPUT -p tcp -s 10.0.0.12/24 --dport 3260 -j ACCEPT
|
二、客户端iSCSI initiator的配置
1、安装iscsi-initiator-utils
1
|
[root@node1 ~]
# yum install iscsi-initiator-utils
|
2、iscsi-initiator-utils的主要配置文件
1
2
3
4
5
|
# /etc/iscsi/iscsi.conf 主要配置文件,用来连接到iscsi target的设置
# /sbin/iscsid 启动iSCSI initiator的主要服务程序
# /sbin/iscsiadm 用来管理iSCSI initiator的主要设置程序
# /etc/init.d/iscsid 让本机模拟成为iSCSI initiator的主要服务
# /etc/init.d/iscsi iscsi启动脚本
|
3、配置iscsi initiator,修改/etc/iscsi/iscsid.conf文件
1
2
3
4
5
6
|
# 去掉注释,并设置iscsi target提供的验证用户和密码
node.session.auth.username = viscsiuser
node.session.auth.password = viscsipasswd
discovery.sendtargets.auth.username = viscsiuser
discovery.sendtargets.auth.password = viscsipasswd
|
4、检测iscsi target服务端提供的共享存储设备信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[root@node2 ~]
# iscsiadm -m discovery -t sendtargets -p 10.0.0.11:3260
10.0.0.11:3260,1 iqn.2016-09.org.huangming:viscsidisk
# 查看检查结果
[root@node2 ~]
# ll -R /var/lib/iscsi/nodes/
/var/lib/iscsi/nodes/
:
total 4
drw-------. 3 root root 4096 Sep 3 17:46 iqn.2016-09.org.huangming:viscsidisk
/var/lib/iscsi/nodes/iqn
.2016-09.org.huangming:viscsidisk:
total 4
drw-------. 2 root root 4096 Sep 3 17:46 10.0.0.11,3260,1
/var/lib/iscsi/nodes/iqn
.2016-09.org.huangming:viscsidisk
/10
.0.0.11,3260,1:
total 4
-rw-------. 1 root root 2126 Sep 3 17:46 default
# 启动iscsi服务
[root@node2 ~]
# /etc/init.d/iscsi restart
Stopping iscsi: [ OK ]
Starting iscsi: [ OK ]
# 设置iscsi和iscsid开机启动
[root@node2 ~]
# chkconfig iscsi on
[root@node2 ~]
# chkconfig iscsid on
|
5、登陆iscsi target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# 显示target数据
[root@node2 ~]
# iscsiadm -m node
10.0.0.11:3260,1 iqn.2016-09.org.huangming:viscsidisk
# 登陆target
[root@node2 ~]
# iscsiadm -m node -T iqn.2016-09.org.huangming:viscsidisk --login
# 查看获得的iscsi磁盘信息
[root@node2 ~]
# fdisk -l
Disk
/dev/sda
: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors
/track
, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x000ba502
Device Boot Start End Blocks Id System
/dev/sda1
* 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2
64 1306 9972736 8e Linux LVM
Disk
/dev/mapper/VolGroup-lv_root
: 9202 MB, 9202302976 bytes
255 heads, 63 sectors
/track
, 1118 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk
/dev/mapper/VolGroup-lv_swap
: 1006 MB, 1006632960 bytes
255 heads, 63 sectors
/track
, 122 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk
/dev/sdb
: 838 MB, 838860800 bytes
26 heads, 62 sectors
/track
, 1016 cylinders
Units = cylinders of 1612 * 512 = 825344 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk
/dev/sdc
: 1085 MB, 1085704704 bytes
34 heads, 61 sectors
/track
, 1022 cylinders
Units = cylinders of 2074 * 512 = 1061888 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x00000000
|
6、建立分区
1
2
3
4
5
6
7
8
9
10
|
[root@node2 ~]
# fdisk /dev/sdc
[root@node2 ~]
# mkfs.ext4 /dev/sdc1
[root@node2 ~]
# mkdir /viscsidisk
[root@node2 ~]
# mount /dev/sdc1 /viscsidisk/
[root@node2 ~]
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 8.4G 972M 7.0G 13% /
tmpfs tmpfs 491M 0 491M 0%
/dev/shm
/dev/sda1
ext4 477M 51M 401M 12%
/boot
/dev/sdc1
ext4 987M 1.3M 934M 1%
/viscsidisk
|
7、更新、删除、添加target数据的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# --logout 注销target,但是并没有删除/var/lib/iscsi/nodes内的数据
# -o delete 删除后面接的那台target连接信息/var/lib/iscsi/nodes/*
# -o update 更新相关信息
# -o new 增加一个新的target信息
[root@node2 ~]
# iscsiadm -m node
10.0.0.11:3260,1 iqn.2016-09.org.huangming:viscsidisk
[root@node2 ~]
# iscsiadm -m node -T iqn.2016-09.org.huangming:viscsidisk --logout
Logging out of session [sid: 4, target: iqn.2016-09.org.huangming:viscsidisk, portal: 10.0.0.11,3260]
Logout of [sid: 4, target: iqn.2016-09.org.huangming:viscsidisk, portal: 10.0.0.11,3260] successful.
[root@node2 ~]
# ls /viscsidisk/
ls
: reading directory
/viscsidisk/
: Input
/output
error
# delete删除连接
[root@node2 ~]
# iscsiadm -m node -o delete -T iqn.2016-09.org.huangming:viscsidisk
[root@node2 ~]
# iscsiadm -m node
iscsiadm: No records found
[root@node2 ~]
# fdisk -l
Disk
/dev/sda
: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors
/track
, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x000ba502
Device Boot Start End Blocks Id System
/dev/sda1
* 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2
64 1306 9972736 8e Linux LVM
Disk
/dev/mapper/VolGroup-lv_root
: 9202 MB, 9202302976 bytes
255 heads, 63 sectors
/track
, 1118 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk
/dev/mapper/VolGroup-lv_swap
: 1006 MB, 1006632960 bytes
255 heads, 63 sectors
/track
, 122 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical
/physical
): 512 bytes / 512 bytes
I
/O
size (minimum
/optimal
): 512 bytes / 512 bytes
Disk identifier: 0x00000000
|
8、重新检测并添加target数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@node2 ~]
# iscsiadm -m discovery -t sendtargets -p 10.0.0.11
10.0.0.11:3260,1 iqn.2016-09.org.huangming:viscsidisk
[root@node2 ~]
# iscsiadm -m node
10.0.0.11:3260,1 iqn.2016-09.org.huangming:viscsidisk
[root@node2 ~]
# ll /var/lib/iscsi/nodes/
total 4
drw-------. 3 root root 4096 Sep 3 18:24 iqn.2016-09.org.huangming:viscsidisk
# 重新连接
[root@node2 ~]
# iscsiadm -m node -T iqn.2016-09.org.huangming:viscsidisk --login
Logging
in
to [iface: default, target: iqn.2016-09.org.huangming:viscsidisk, portal: 10.0.0.11,3260] (multiple)
Login to [iface: default, target: iqn.2016-09.org.huangming:viscsidisk, portal: 10.0.0.11,3260] successful.
# 删除target连接之后,再重新连接,磁盘中的数据不会丢失
|
本文转自 HMLinux 51CTO博客,原文链接:http://blog.51cto.com/7424593/1845978