准备工作:
主机 | 系统 | 地址 | 软件 |
target主机 | CentOS 6.4 i386 | 192.168.0.22 | iscsi-target-utils |
initiator主机 | CentOS 6.4 i386 | 192.168.0.23 | iscsi-initiator-utils |
initiator主机 | CentOS 6.4 i386 | 192.168.0.24 | iscsi-initiator-utils |
一. 配置target主机
在target主机有两块硬盘,/dev/sda和/dev/sdb,其中sdb作为iSCSI的共享磁盘
1. 安装iSCSI的服务端
[root@target ~]# yum install scsi-target-utils
2. 将tgtd服务设置为开机启动,并将它启动
[root@target ~]# chkconfig tgtd on[root@target ~]# service tgtd startStarting SCSI target daemon: [ OK ]
3. 关于tgtadm命令的简单介绍
[root@target ~]# tgtadm -hUsage: tgtadm [OPTION]Linux SCSI Target Framework Administration Utility, version 1.0.24 --lld--mode target --op new --tid --targetname add a new target with and . must not be zero. --lld --mode target --op delete [--force] --tid delete the specific target with . With force option, the specific target is deleted even if there is an activity. --lld --mode target --op show show all the targets. --lld --mode target --op show --tid show the specific target's parameters. --lld --mode target --op update --tid --name --value change the target parameters of the specific target with . --lld --mode target --op bind --tid --initiator-address --lld --mode target --op bind --tid --initiator-name enable the target to accept the specific initiators. --lld --mode target --op unbind --tid --initiator-address --lld --mode target --op unbind --tid --initiator-name disable the specific permitted initiators. --lld --mode logicalunit --op new --tid --lun \ --backing-store --bstype --bsoflags add a new logical unit with to the specific target with . The logical unit is offered to the initiators. must be block device files (including LVM and RAID devices) or regular files. bstype option is optional. bsoflags supported options are sync and direct (sync:direct for both). --lld --mode logicalunit --op delete --tid --lun delete the specific logical unit with that the target with has. --lld --mode account --op new --user --password add a new account with and . --lld --mode account --op delete --user delete the specific account having . --lld --mode account --op bind --tid --user [--outgoing] add the specific account having to the specific target with . could be or . If you use --outgoing option, the account will be added as an outgoing account. --lld --mode account --op unbind --tid --user delete the specific account having from specific target. --control-port use control port --help display this help and exitReport bugs to .
首先,创建一个target
[root@target ~]# tgtadm -L iscsi -m target -o new -t 1 -T iqn.2014-03.com.flyence:target1
查看该target的相关情况
[root@target ~]# tgtadm -L iscsi -m target -o show
创建一个LU,并将/dev/sdb作为共享磁盘
[root@target ~]# tgtadm -L iscsi -m logicalunit -o new -t 1 -l 1 -b /dev/sdb
允许192.168.0.0/16网段的主机访问
[root@target ~]# tgtadm -L iscsi -m target -o bind -t 1 -I 192.168.0.0/24
创建认证账号,并绑定到target上
[root@target ~]# tgtadm -L iscsi -m account -o new --user flyence --password flyence[root@target ~]# tgtadm -L iscsi -m account -o bind -t 1 --user flyence
该账号作为客户机访问服务端的认证账号
创建认证账号,并以outgoing的形式绑定到target上
[root@target ~]# tgtadm -L iscsi -m account -o new --user evan --password evan[root@target ~]# tgtadm -L iscsi -m account -o bind -t 1 --user evan --outgoing
当客户机向服务端写入前,客户机会用此账号验证服务器的身份。
二. 配置客户端
1. 安装iscsi-initiator-utils
[root@initiator ~]# yum install iscsi-initiator-utils
2. 设置initiator的名字
[root@initiator iscsi]# echo "InitiatorName=`iscsi-iname -p iqn.2014-03.com.flyence`" > /etc/iscsi/initiatorname.iscsi[root@initiator iscsi]# cat initiatorname.iscsiInitiatorName=iqn.2014-03.com.flyence:9e9d0941d1f
3. 在/etc/iscsi/iscsi.conf中配置验证账号信息,启用以下5行,并修改
node.session.auth.authmethod = CHAP node.session.auth.username = flyencenode.session.auth.password = flyence node.session.auth.username_in = evannode.session.auth.password_in = evan
4. 发现服务器的target
[root@initiator iscsi]# iscsiadm -m discovery -t st -p 192.168.0.22:3260Starting iscsid: [ OK ]192.168.0.22:3260,1 iqn.2014-03.com.flyence:target1
5. 登录上述被发现的target
[root@initiator iscsi]# iscsiadm -m node -T iqn.2014-03.com.flyence:target1 -p 192.168.0.22:3260 -lLogging in to [iface: default, target: iqn.2014-03.com.flyence:target1, portal: 192.168.0.22,3260] (multiple)Login to [iface: default, target: iqn.2014-03.com.flyence:target1, portal: 192.168.0.22,3260] successful.
6. 查看当前的磁盘情况
[root@initiator iscsi]# fdisk -l /dev/sd[a-z]Disk /dev/sda: 128.8 GB, 128849018880 bytes255 heads, 63 sectors/track, 15665 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000925b4 Device Boot Start End Blocks Id System/dev/sda1 * 1 26 204800 83 LinuxPartition 1 does not end on cylinder boundary./dev/sda2 26 7675 61440000 8e Linux LVM/dev/sda3 7675 14049 51200000 8e Linux LVM/dev/sda4 14049 15666 12983296 5 Extended/dev/sda5 14049 15666 12982272 8e Linux LVMDisk /dev/sdb: 128.8 GB, 128849018880 bytes255 heads, 63 sectors/track, 15665 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000
多出一块硬盘/dev/sdb,并且sdb还没分区,
7. 将sdb分两个区,并将sdb1格式化为ext4
[root@initiator iscsi]# fdisk /dev/sdb....[root@initiator iscsi]# fdisk -l /dev/sdbDisk /dev/sdb: 128.8 GB, 128849018880 bytes255 heads, 63 sectors/track, 15665 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xc9458e42 Device Boot Start End Blocks Id System/dev/sdb1 1 1306 10490413+ 83 Linux/dev/sdb2 1307 2612 10490445 83 Linux[root@initiator iscsi]# mkfs.ext4 /dev/sdb1
8. 此时,查看target主机的sdb硬盘情况
[root@target ~]# fdisk -l /dev/sdbDisk /dev/sdb: 128.8 GB, 128849018880 bytes255 heads, 63 sectors/track, 15665 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xc9458e42 Device Boot Start End Blocks Id System/dev/sdb1 1 1306 10490413+ 83 Linux/dev/sdb2 1307 2612 10490445 83 Linux
已被分好区
当要开机启动服务时
[root@initiator iscsi]# chkconfig iscsid on
三. 服务端target配置文件的设定
在服务端如果只是在命令行中定义target和LU,在重启时会消失,要使其永久有效,就得写入配置文件/etc/tgt/targets.conf
backing-store /dev/sdb initiator-address 192.168.0.0/16 incominguser flyence flyence outgoinguser evan evan
重启服务器主机,再看看target的配置
[root@target ~]# tgtadm -L iscsi -m target -o showTarget 1: iqn.2014-03.com.flyence:target2 System information: Driver: iscsi State: ready I_T nexus information: I_T nexus: 1 Initiator: iqn.2014-03.com.flyence:9e9d0941d1f Connection: 0 IP Address: 192.168.0.23 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: 128849 MB, Block size: 512 Online: Yes Removable media: No Prevent removal: No Readonly: No Backing store type: rdwr Backing store path: /dev/sdb Backing store flags: Account information: flyence evan (outgoing) ACL information: 192.168.0.0/16
此时,由于已修改了target的名字,则客户机无法直接登录,只能删除原有信息,重新发现和登录
在客户机上,先登出原有的target
[root@initiator iscsi]# iscsiadm -m node -T iqn.2014-03.com.flyence:target1 -p 192.168.0.22:3260 -uLogging out of session [sid: 1, target: iqn.2014-03.com.flyence:target1, portal: 192.168.0.22,3260]Logout of [sid: 1, target: iqn.2014-03.com.flyence:target1, portal: 192.168.0.22,3260] successful.
删除该target及相关文件
[root@initiator iscsi]# iscsiadm -m node -T iqn.2014-03.com.flyence:target1 -p 192.168.0.22:3260 -o delete[root@initiator iscsi]# rm -rf /var/lib/iscsi/send_targets/192.168.0.22,3260/
重启服务
[root@initiator iscsi]# service iscsid restartStopping iscsid:Starting iscsid: [ OK ]
重新发现和登录
[root@initiator iscsi]# iscsiadm -m discovery -t st -p 192.168.0.22:3260192.168.0.22:3260,1 iqn.2014-03.com.flyence:target2[root@initiator iscsi]# iscsiadm -m node -T iqn.2014-03.com.flyence:target2 -p 192.168.0.22:3260 -lLogging in to [iface: default, target: iqn.2014-03.com.flyence:target2, portal: 192.168.0.22,3260] (multiple)Login to [iface: default, target: iqn.2014-03.com.flyence:target2, portal: 192.168.0.22,3260] successful.
查看磁盘
[root@initiator iscsi]# fdisk -l /dev/sdbDisk /dev/sdb: 128.8 GB, 128849018880 bytes255 heads, 63 sectors/track, 15665 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xc9458e42 Device Boot Start End Blocks Id System/dev/sdb1 1 1306 10490413+ 83 Linux/dev/sdb2 1307 2612 10490445 83 Linux