淘先锋技术网

首页 1 2 3 4 5 6 7

iscsi可以将网络中一台机器上的磁盘共享给另一台机器

共享端叫iscsi服务器,连接端叫客户端。

实验的服务器环境为:

redhat6.0 32bit

ip:192.169.1.98

客户端环境:

redhat6.1 64bit

ip:192.169.1.100

服务器端构建

yum install scsi-target-utils #服务器需要安装的软件包
vim /etc/tgt/targets.conf #配置iscsi的配置文件
<target chen>
backing-store /dev/sdb7 
initiator-address 192.169.1.100      #仅给192.169.1.100 做存储,不加这条表示可共所有主机使用。
</target>
service tgtd start
chkconfig tgtd on

客户端端构建:

yum –y install iscsi-initiator-utils #客户端需要安装的软件包
iscsiadm –m discovery –t st–p 192.169.1.98#客户端发现操作
 iscsiadm -m node -T chen -p 192.169.1.98 -l#客户端连接登陆

具体过程如下:

服务器端(ip:192.169.1.98):

[chen@localhost 桌面]$ yum list|grep scsi
*Note* Red Hat Network repositories are not listed below. You must run this command as root to access RHN repositories.
iscsi-initiator-utils.i686             6.2.0.872-10.el6              @rhel-source
lsscsi.i686                            0.23-2.el6                    rhel-source
scsi-target-utils.i686                 1.0.4-3.el6                   rhel-source
[chen@localhost 桌面]$ su - root
密码:
[root@localhost ~]# yum install scsi-target-utils #安装服务器端软件
Loaded plugins: refresh-packagekit, rhnplugin
This system is not registered with RHN.
RHN support will be disabled.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package scsi-target-utils.i686 0:1.0.4-3.el6 set to be updated
--> Processing Dependency: librdmacm.so.1 for package: scsi-target-utils-1.0.4-3.el6.i686
--> Processing Dependency: libibverbs.so.1 for package: scsi-target-utils-1.0.4-3.el6.i686
--> Processing Dependency: perl(Config::General) for package: scsi-target-utils-1.0.4-3.el6.i686
--> Running transaction check
---> Package libibverbs.i686 0:1.1.4-2.el6 set to be updated
--> Processing Dependency: libibverbs-driver for package: libibverbs-1.1.4-2.el6.i686
---> Package librdmacm.i686 0:1.0.10-2.el6 set to be updated
---> Package perl-Config-General.noarch 0:2.44-1.el6 set to be updated
--> Running transaction check
---> Package libmlx4.i686 0:1.0.1-7.el6 set to be updated
--> Finished Dependency Resolution


Dependencies Resolved


================================================================================
 Package                  Arch        Version            Repository        Size
================================================================================
Installing:
 scsi-target-utils        i686        1.0.4-3.el6        rhel-source      134 k
Installing for dependencies:
 libibverbs               i686        1.1.4-2.el6        rhel-source       43 k
 libmlx4                  i686        1.0.1-7.el6        rhel-source       27 k
 librdmacm                i686        1.0.10-2.el6       rhel-source       23 k
 perl-Config-General      noarch      2.44-1.el6         rhel-source       68 k


Transaction Summary
================================================================================
Install       5 Package(s)
Upgrade       0 Package(s)


Total download size: 295 k
Installed size: 672 k
Is this ok [y/N]: y
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           830 kB/s | 295 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : perl-Config-General-2.44-1.el6.noarch                    1/5 
  Installing     : libmlx4-1.0.1-7.el6.i686                                 2/5 
  Installing     : libibverbs-1.1.4-2.el6.i686                              3/5 
  Installing     : librdmacm-1.0.10-2.el6.i686                              4/5 
  Installing     : scsi-target-utils-1.0.4-3.el6.i686                       5/5 


Installed:
  scsi-target-utils.i686 0:1.0.4-3.el6                                          


Dependency Installed:
  libibverbs.i686 0:1.1.4-2.el6     libmlx4.i686 0:1.0.1-7.el6                 
  librdmacm.i686 0:1.0.10-2.el6     perl-Config-General.noarch 0:2.44-1.el6    


Complete!
[root@localhost ~]# ls /dev/sda*
/dev/sda   /dev/sda2  /dev/sda4  /dev/sda6
/dev/sda1  /dev/sda3  /dev/sda5  /dev/sda7
[root@localhost ~]# fdisk /dev/sda #注意服务器端要共享的盘必须是“原生”的,不能经过格式化,所以这里先分一个区


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').


Command (m for help): n
First cylinder (1614-2610, default 1614): 
Using default value 1614
Last cylinder, +cylinders or +size{K,M,G} (1614-2610, default 2610): +500M#分一个500M的


Command (m for help): w
The partition table has been altered!


Calling ioctl() to re-read partition table.


WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partx -a /dev/sda #刷新分区表
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
BLKPG: Device or resource busy
error adding partition 7
[root@localhost ~]# ls /dev/sda*
/dev/sda   /dev/sda2  /dev/sda4  /dev/sda6  /dev/sda8#可以看到有sda8了,就共享sda8
/dev/sda1  /dev/sda3  /dev/sda5  /dev/sda7
[root@localhost ~]# vim /etc/tgt/targets.conf 
[root@localhost ~]# tail -5 /etc/tgt/targets.conf 
#</target>
<target chen> 就加上这四行,这里这个分区的名字叫chen,redhat5中命名好像有一定格式,必须以iqn开头
backing-store /dev/sda8
initiator-address 192.169.1.100
</target>
[root@localhost ~]# /etc/init.d/tgtd start #启动服务,这样服务器端就配置完成了
正在启动 SCSI target daemon:                              [确定]
[root@localhost ~]# 


客户端(ip:192.169.1.100):

[chen@localhost 桌面]$ yum list|grep scsi
scsi-target-utils.x86_64                1.0.14-2.el6                @rhel-source
iscsi-initiator-utils.x86_64            6.2.0.872-21.el6            rhel-source 
lsscsi.x86_64                           0.23-2.el6                  rhel-source 
[chen@localhost 桌面]$ su - root
密码:
[root@localhost ~]# yum install -y iscsi-initiator-utils#先安装客服端
Loaded plugins: product-id, refresh-packagekit, subscription-manager
Updating Red Hat repositories.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package iscsi-initiator-utils.x86_64 0:6.2.0.872-21.el6 will be installed
--> Finished Dependency Resolution


Dependencies Resolved


================================================================================
 Package                  Arch      Version                Repository      Size
================================================================================
Installing:
 iscsi-initiator-utils    x86_64    6.2.0.872-21.el6       rhel-source    579 k


Transaction Summary
================================================================================
Install       1 Package(s)


Total download size: 579 k
Installed size: 2.0 M
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : iscsi-initiator-utils-6.2.0.872-21.el6.x86_64                1/1 
duration: 165(ms)
Installed products updated.


Installed:
  iscsi-initiator-utils.x86_64 0:6.2.0.872-21.el6                               


Complete!
[root@localhost ~]# chkconfig --list|grep scsi #客户端服务自动开启了
iscsi           0:关闭1:关闭2:关闭3:启用4:启用5:启用6:关闭
iscsid         0:关闭1:关闭2:关闭3:启用4:启用5:启用6:关闭
[root@localhost ~]# iscsiadm -m discovery -t st -p 192.169.1.98	#发现服务器端是否有磁盘共享
正在启动 iscsid:                                          [确定]
192.169.1.98:3260,1 chen #可以看到有一个名字叫chen的磁盘,说明发现了
[root@localhost ~]# iscsiadm -m node -T chen -p 192.169.1.98 -l#登陆服务器端
Logging in to [iface: default, target: chen, portal: 192.169.1.98,3260]
Login to [iface: default, target: chen, portal: 192.169.1.98,3260] successful.#登陆成功
[root@localhost ~]# fdisk -l #查看一下本地是否多一块磁盘


Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000ee48e


   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        1332    10485760   83  Linux
/dev/sda3            1332        1778     3584000   82  Linux swap / Solaris
/dev/sda4            1779        2610     6683040    5  Extended
/dev/sda5            1779        1831      425691   83  Linux
/dev/sda6            1832        1838       56196   83  Linux
/dev/sda7            1839        1970     1060258+  83  Linux


Disk /dev/sdb: 534 MB, 534610944 bytes #可以看到这边多了一块磁盘sdb,大小约为500M
17 heads, 60 sectors/track, 1023 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
[root@localhost ~]# ls /dev/sdb 
/dev/sdb
[root@localhost ~]# blkid /dev/sda1
/dev/sda1: UUID="18ed3ae9-6e4f-4674-ac6d-5d763ead42ce" TYPE="ext4"  
[root@localhost ~]# blkid /dev/sdb #没有uuid信息,这块磁盘是“原生”的
[root@localhost ~]# ls /dev/sdb
/dev/sdb
[root@localhost ~]# fdisk /dev/sdb #可以像本地多了一块新的磁盘一样使用这块磁盘了,这里做一个分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x945cf8be.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').


Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1023, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1023, default 1023): +100M#分一个100M的主分区


Command (m for help): p


Disk /dev/sdb: 534 MB, 534610944 bytes
17 heads, 60 sectors/track, 1023 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: 0x945cf8be


   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         202      102990   83  Linux


Command (m for help): w
The partition table has been altered!


Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
[root@localhost ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1
[root@localhost ~]# mkdir /sdb1 #创建挂载点
[root@localhost ~]# mkfs.ext4 /dev/sdb1 #格式化sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25792 inodes, 102988 blocks
5149 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1984 inodes per group
Superblock backups stored on blocks: 
8193, 24577, 40961, 57345, 73729


正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成


This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mount /dev/sdb1 /sdb1/ #挂载
[root@localhost ~]# df -hT
文件系统    类型      容量  已用  可用 已用%% 挂载点
/dev/sda2     ext4    9.9G  3.3G  6.2G  35% /
tmpfs        tmpfs    497M  420K  497M   1% /dev/shm
/dev/sda1     ext4    194M   27M  158M  15% /boot
/dev/sda6     ext4     54M   25M   26M  50% /quota
/dev/sr0   iso9660    3.4G  3.4G     0 100% /media/RHEL_6.1 x86_64 Disc 1
/dev/sr0   iso9660    3.4G  3.4G     0 100% /mnt/iso
/dev/sdb1     ext4     98M  5.6M   87M   6% /sdb1 #可以看到的确挂载上去了,文件系统类型为ext4
[root@localhost ~]# 

注意:iscsi服务有时候服务重启会出现问题,若修改配置文件,先stop,然后start

/etc/init.d/tgtd stop

/etc/init.d/tgtd start

在客户端

iscsiadm --mode node -T chen -p 192.169.1.98:3260 -u 退出登录

iscsiadm --mode node -o delete -T chen -p 192.169.1.98:3260删除chen这个发现,要先退出,再删除,删除后必须重新发现,才能登录。