如何在rhel6.0/centos6.0安装iscsi服务

安装环境:
Iscsi server: server.example.com  192.168.0.100
Iscsi client: server.example.com  192.168.0.200

在iscsi server上的操作步骤。
a. 安装iscsi server软件包。
(1) 用RPM的方式安装。
[root@server ~]# rpm -ivh /mnt/Packages/scsi-target-utils-1.0.4-3.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:scsi-target-utils      ########################################### [100%]
[root@server ~]#
[root@server ~]# rpm -ivh /mnt/Packages/perl-Config-General-2.44-1.el6.noarch.rpm
Preparing...                ########################################### [100%]
        package perl-Config-General-2.44-1.el6.noarch is already installed

(2) 用yum命令安装。
yum -y install scsi-target-utils

b. 启动服务并且添加到开机自启动。
[root@server ~]# /etc/init.d/tgtd start
Starting SCSI target daemon:                               [  OK  ]
[root@server ~]# chkconfig tgtd on

c. 使用fdisk命令创建一个分区,或者在文件系统上创建一个镜像文件。
(1)创建了一个500M的分区/dev/sda5。你可以根据自己实际情况来创建分区,lv,镜像文件。
[root@server ~]# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x0005c966
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         548     4194304   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             548         679     1048576   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4             679        1044     2937226    5  Extended
/dev/sda5             679         743      519412   83  Linux

(2)用dd命令创建一个/home/data/p_w_picpathfile并且设置下selinux标签(如果你没有开启selinux,可以忽略这步)。
[root@server ~]# mkdir -p /var/lib/tgtd/virtualization
[root@server ~]# dd if=/dev/zero of=/var/lib/tgtd/virtualization/virtp_w_picpath2.img bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 5.2182 s, 100 MB/s
[root@server ~]# restorecon -R /var/lib/tgtd/
[root@server ~]# ll -Z /var/lib/tgtd/virtualization/
-rw-r--r--. root root system_u:object_r:tgtd_var_lib_t:s0 virtp_w_picpath2.img

d. 编辑配置文件/etc/tgt/targets.conf,并设置下iSCSI Qualified Name (IQN).
如果你是自己分区或者lvm,设置成自己的分区和lv即可。
<target iqn.2012-02.com.example:server.target1>
    backing-store /dev/sda5  #share 1
    backing-store /var/lib/tgtd/virtualization/virtp_w_picpath2.img  #share 2
    write-cache off
</target>
重启服务,使修改后的配置生效。
[root@server ~]# /etc/init.d/tgtd restart
Stopping SCSI target daemon:                               [  OK  ]
Starting SCSI target daemon:                               [  OK  ]

e. Iptables的设置,如果你的server开启了防火情,那么你还需要添加一条iptables,具体设置如下。
[root@server ~]# iptables -I INPUT -p tcp -m tcp --dport 3260 -j ACCEPT
[root@server ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@server ~]# service iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]
[root@server ~]# iptables -L -nv
Chain INPUT (policy ACCEPT 16 packets, 1056 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:3260

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 10 packets, 1256 bytes)
 pkts bytes target     prot opt in     out     source               destination

f. 确定iscsi server是否成功。如果现实如下说明配置iscsi server已经OK。
[root@server ~]# tgt-admin --show
Target 1: iqn.2012-02.com.example:server.target1
    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
            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: 532 MB
            Online: Yes
            Removable media: No
            Backing store type: rdwr
            Backing store path: /dev/sda5
        LUN: 2
            Type: disk
            SCSI ID: IET     00010002
            SCSI SN: beaf12
            Size: 524 MB
            Online: Yes
            Removable media: No
            Backing store type: rdwr
            Backing store path: /var/lib/tgtd/virtualization/virtp_w_picpath2.img
    Account information:
    ACL information:
        ALL

在Iscsi client的配置。
a. 安装iscsi客户端安装包。
同样你也可以使用yum来安装。
[root@client ~]# rpm -ivh /mnt/Packages/iscsi-initiator-utils-6.2.0.872-10.el6.x86_64.rpm
warning: /mnt/Packages/iscsi-initiator-utils-6.2.0.872-10.el6.x86_64.rpm: Header V3 RSA                         /SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:iscsi-initiator-utils  ########################################### [100%]

b. 启动服务并且添加到开机启动。
[root@client ~]# /etc/init.d/iscsid start
[root@client ~]# chkconfig iscsid on

c. 发现iscsi server的共享分区。
[root@client ~]# iscsiadm --mode discovery --type sendtargets --portal 192.168.0.100 192.168.0.100:3260,1 iqn.2012-02.com.example:server.target1
Starting iscsid:                                           [  OK  ]
192.168.0.100:3260,1 iqn.2012-02.com.example:server.target1
[root@client ~]#
登陆到iscsi server共享的分区。
[root@client ~]# iscsiadm -m node -T iqn.2012-02.com.example:server.target1 -p 192.168.0.100 -l
Logging in to [iface: default, target: iqn.2012-02.com.example:server.target1, portal: 192.168.0.100,3260]
Login to [iface: default, target: iqn.2012-02.com.example:server.target1, portal: 192.168.0.100,3260] successful.
大功告成,使用fdisk -l命令查看分区。sdb/sdc就是iscsi server共享出来的分区。
[root@client ~]# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x000c3fc4
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         548     4194304   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3             548         574      204800   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
Disk /dev/sdb: 531 MB, 531877888 bytes
17 heads, 60 sectors/track, 1018 cylinders
Units = cylinders of 1020 * 512 = 522240 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdb doesn't contain a valid partition table
Disk /dev/sdc: 524 MB, 524288000 bytes
17 heads, 59 sectors/track, 1020 cylinders
Units = cylinders of 1003 * 512 = 513536 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdc doesn't contain a valid partition table

d. 现在你可以格式化分区,挂载在本地。如果要实现开机自动挂载,要在fstab文件添加记录。
一定要添加_netdev参数,要不你在重启iscsi server后,启动时会报错。
例如:
/dev/sdc    /mnt   ext4    default,_netdev  0 0