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