NAS:网络附加存储服务器
SAN:存储局域网
让LinuxPC变成一台可通过Web管理的NAS:
FressNAS:http://sourceforge.net/projects/freenas/
SAN提供“磁盘”给主机用,可以格式化,分区等;
NAS提供的是“网络协议的文件系统(NFS、SMB等)
iSCSI:Internet SCSI
iSCSI target:相当于服务器
iSCSI initiator:相当于客户端
储存方式:
1.DAS:直接存储
2.NAS:提供文件系统
3.SAN:提供磁盘
使用到的软件:
#用来将Linux系统仿真成为iSCSI target的功能
[root@Centosszm ~]# yum install scsi-target-utils
#用于挂载iSCSI
[root@Centosszm ~]# yum install iscsi-initiator-utils
主要文件:
/etc/tgt/targets.conf
/usr/sbin/tgt-admin
/usr/sbin/tgt-setup-lun
/usr/sbin/tgtadm
/usr/sbin/tgtd
/usr/sbin/tgtimg
可以使用作的target:
1.文件
2.分区
3.设备
1.建立所需要的磁盘设备。
[root@Centosszm ~]# dd if=/dev/zero of=/srv/iscsi/disk1.img bs=1M count=50
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 5.28837 s, 9.9 MB/s
[root@Centosszm ~]# chcon -Rv -t tgtd_var_lib_t /srv/iscsi/
changing security context of `/srv/iscsi/disk1.img'
changing security context of `/srv/iscsi/'
[root@Centosszm ~]# fdisk /dev/sda
[root@Centosszm ~]# partprobe
#查看已挂载SWAP
[root@Centosszm ~]# swapon -s
Filename Type Size Used Priority
/dev/sda7 partition 2047992 0 -1
[root@Centosszm ~]# pvcreate /dev/sda10
Physical volume "/dev/sda10" successfully created
[root@Centosszm ~]# vgextend vg_szm /dev/sda10
Volume group "vg_szm" successfully extended
[root@Centosszm ~]# lvcreate -L 50M -n iscsi01 vg_szm
Rounding up size to full physical extent 52.00 MiB
Logical volume "iscsi01" created
[root@Centosszm ~]# lvscan
ACTIVE '/dev/vg_szm/LogVol00' [8.81 GiB] inherit
ACTIVE '/dev/vg_szm/iscsi01' [52.00 MiB] inherit
2.规划共享的iSCSI target文件名: iqn(ISCSI Qualifed Name合格名称)
iqn.yyyy-mm.单位网络名的反转写法:这个共享的target名称
3.设置tgt
[root@Centosszm ~]# hostname
Centosszm.test.com
[root@Centosszm ~]# vi /etc/tgt/targets.conf
<target iqn.2013-04.com.Centosszm:szmdisk>
backing-store /srv/iscsi/ disk1.img #整块磁盘用direct-store
#backing-store /dev/sda8
backing-store /dev/vg_szm/ iscsi01 #磁盘2
initiator-address 192.168.179.0/24 #用户地址
incominguser szmuser szmpassword #账号与密码
write-cache off #关闭缓存
</target>
[root@Centosszm ~]# /etc/init.d/tgtd start
Starting SCSI target daemon: [ OK ]
#用的是3260这个端口
[root@Centosszm ~]# netstat -tulnp | grep tgt
tcp 0 0 0.0.0.0:3260 0.0.0.0:* LISTEN 2766/tgtd
tcp 0 0 :::3260 :::* LISTEN 2766/tgtd
[root@Centosszm ~]# tgt-admin --show
Target 1: iqn.2013-04.com.Centosszm:szmdisk
System information:
Driver: iscsi
State: ready
I_T nexus information:
LUN information:
LUN: 0
Type: controller #这个是控制器,不是可用的LUN
SCSI ID: IET 00010000
SCSI SN: beaf10
Size: 0 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: None
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 55 MB #磁盘大小
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: /dev/vg_szm/iscsi01
LUN: 2
Type: disk
SCSI ID: IET 00010002
SCSI SN: beaf12
Size: 52 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: /srv/iscsi/disk1.img
Account information:
szmuser #用户账号
ACL information:
192.168.179.0/24 #可连接IP
#防火墙设置:
[root@Centosszm ~]# iptables -A INPUT -p tcp --dport 3260 -j ACCEPT
[root@Centosszm ~]# /etc/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
iSCSCI initiator的设置:
/etc/iscsi/iscsid.conf:
/sbin/iscsid
/sbin/iscsiadm
/etc/init.d/iscsid
/etc/init.d/iscsi #这个会呼叫上面那个命令
1.客户端配置文件
[root@szm ~]# vim /etc/iscsi/iscsid.conf
node.session.auth.username = szmuser #连接时用到的密码
node.session.auth.password = szmpassword
discovery.sendtargets.auth.username = szmuser
discovery.sendtargets.auth.password = szmpassword #检测时用到的密码
[root@szm ~]# chkconfig iscsid on
[root@szm ~]# chkconfig iscsi on
2.检测192.168.179.7这台target的相关数据
-m discovvery:
-t sendtargets:
-p IP:prot :
[root@szm ~]# iscsiadm -m discovery -t sendtargets -p 192.168.179.7
Starting iscsid: [ OK ]
192.168.179.7:3260,1 iqn.2013-04.com.Centosszm:szmdisk
[root@szm ~]# ll -R /var/lib/iscsi/nodes/
/var/lib/iscsi/nodes/:
total 4
drw-------. 3 root root 4096 Apr 8 23:50 iqn.2013-04.com.Centosszm:szmdisk
/var/lib/iscsi/nodes/iqn.2013-04.com.Centosszm:szmdisk:
total 4
drw-------. 2 root root 4096 Apr 8 23:50 192.168.179.7,3260,1
/var/lib/iscsi/nodes/iqn.2013-04.com.Centosszm:szmdisk/192.168.179.7,3260,1:
total 4
-rw-------. 1 root root 1897 Apr 8 23:50 default
3.开始进行连接iSCSI target
#找出目前本机上面所有检测到的target信息,可能并未登录
[root@szm ~]# iscsiadm -m node
192.168.179.7:3260,1 iqn.2013-04.com.Centosszm:szmdisk
#登录某台target,不要重新启动iSCSI服务
[root@szm ~]# iscsiadm -m node -T iqn.2013-04.com.Centosszm:szmdisk --login
Logging in to [iface: default, target: iqn.2013-04.com.Centosszm:szmdisk, portal: 192.168.179.7,3260] (multiple)
Login to [iface: default, target: iqn.2013-04.com.Centosszm:szmdisk, portal: 192.168.179.7,3260] successful.
[root@szm ~]# fdisk -l
……
Disk /dev/sdc: 54 MB, 54525952 bytes
2 heads, 52 sectors/track, 1024 cylinders
Units = cylinders of 104 * 512 = 53248 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdd: 52 MB, 52428800 bytes
2 heads, 50 sectors/track, 1024 cylinders
Units = cylinders of 100 * 512 = 51200 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
4.更新/删除/添加target数据的方法
iscsiadm -m node -T targetname --logout
iscsiadm -m node -o [delete|new|update] -T targetname
[root@szm ~]# iscsiadm -m node -T iqn.2013-04.com.Centosszm:szmdisk --logout
Logging out of session [sid: 1, target: iqn.2013-04.com.Centosszm:szmdisk, portal: 192.168.179.7,3260]
Logout of [sid: 1, target: iqn.2013-04.com.Centosszm:szmdisk, portal: 192.168.179.7,3260] successful.
没有删除连接:
[root@szm ~]# iscsiadm -m node
192.168.179.7:3260,1 iqn.2013-04.com.Centosszm:szmdisk
[root@szm ~]# iscsiadm -m node -o delete -T iqn.2013-04.com.Centosszm:szmdisk
[root@szm ~]# iscsiadm -m node
iscsiadm: No records found
#例子:
[root@szm ~]# iscsiadm -m discovery -t sendtargets -p 192.168.179.7
192.168.179.7:3260,1 iqn.2013-04.com.Centosszm:szmdisk
[root@szm ~]# iscsiadm -m node
192.168.179.7:3260,1 iqn.2013-04.com.Centosszm:szmdisk
[root@szm ~]# /etc/init.d/iscsi restart
[root@szm ~]# fdisk -l
……
Disk /dev/sdc: 54 MB, 54525952 bytes
2 heads, 52 sectors/track, 1024 cylinders
Units = cylinders of 104 * 512 = 53248 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdd: 52 MB, 52428800 bytes
2 heads, 50 sectors/track, 1024 cylinders
Units = cylinders of 100 * 512 = 51200 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[root@szm ~]# pvcreate /dev/sd{c,d}
Physical volume "/dev/sdc" successfully created
Physical volume "/dev/sdd" successfully created
[root@szm ~]# vgcreate iscsi /dev/sd{c,d}
Volume group "iscsi" successfully created
[root@szm ~]# vgdisplay
--- Volume group ---
VG Name iscsi
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 96.00 MiB
PE Size 4.00 MiB
Total PE 24 #一共24个PE
Alloc PE / Size 0 / 0
Free PE / Size 24 / 96.00 MiB
VG UUID nGlC74-pbo5-SrMO-29jd-xhaM-g5ds-A7lLFL
[root@szm ~]# lvcreate -l 24 -n disk iscsi
Logical volume "disk" created
[root@szm ~]# lvdisplay
--- Logical volume ---
LV Path /dev/iscsi/disk
LV Name disk
VG Name iscsi
LV UUID V8I8UH-9qrh-M671-Gph2-E36Y-jWQ5-vV0MAG
LV Write Access read/write
LV Creation host, time szm, 2013-04-09 13:35:07 +0800
LV Status available
# open 0
LV Size 96.00 MiB
Current LE 24
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
[root@szm ~]# mkfs -t ext4 /dev/iscsi/disk
[root@szm ~]# mkdir -p /data/iscsi
#_netdev:需要网络开机启动完成后才会挂载
[root@szm ~]# vim /etc/fstab
/dev/iscsi/disk /data/iscsi ext4 defaults,_netdev 1 2
[root@szm ~]# mkdir /data/iscsi/testdir
[root@szm ~]# touch /data/iscsi/testdir/testfile
[root@szm ~]# ll /data/iscsi/
total 14
drwx------. 2 root root 12288 Apr 9 13:35 lost+found
drwxr-xr-x. 2 root root 1024 Apr 9 13:39 testdir
服务器端查看:
[root@Centosszm ~]# tgt-admin --show
Target 1: iqn.2013-04.com.Centosszm:szmdisk
System information:
Driver: iscsi
State: ready
I_T nexus information:
I_T nexus: 3
Initiator: iqn.1994-05.com.redhat:84971b47c598 #这个名字可以修改
Connection: 0
IP Address: 192.168.179.146 #连接进来的IP
LUN information:
LUN: 0
Type: controller
SCSI ID: IET 00010000
SCSI SN: beaf10
Size: 0 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: None
LUN: 1
Type: disk
SCSI ID: IET 00010001
SCSI SN: beaf11
Size: 55 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: /dev/vg_szm/iscsi01
LUN: 2
Type: disk
SCSI ID: IET 00010002
SCSI SN: beaf12
Size: 52 MB
Online: Yes
Removable media: No
Backing store type: rdwr
Backing store path: /srv/iscsi/disk1.img
Account information:
szmuser
ACL information:
192.168.179.0/24
#修改名字最好在使用前修改,否则会使得磁盘无法使用。
[root@szm ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.1994-05.com.szm:84971b47c598
[root@szm ~]# /etc/init.d/iscsi restart
Stopping iscsi: [ OK ]
Starting iscsi: [ OK ]
#发生下面的改变了,郁闷,全得LV不能够使用了。
[root@szm ~]# fdisk -l
Disk /dev/sde: 54 MB, 54525952 bytes
2 heads, 52 sectors/track, 1024 cylinders
Units = cylinders of 104 * 512 = 53248 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdf: 52 MB, 52428800 bytes
2 heads, 50 sectors/track, 1024 cylinders
Units = cylinders of 100 * 512 = 51200 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000