带你3步快速掌握iSCSI搭建

来源:互联网 发布:labview软件 编辑:程序博客网 时间:2024/06/01 14:57

今天呢,给大家分享一下Linux上iSCSI的搭建,通过文字+图像的解说,不管你是小白还是菜鸟,都能够让你掌握搭建iSCSI的精华所在!

废话不多说,看招:

首先呢,给大家说一下本次教程的环境:系统版本:RedHat Enterprise Linux 6.2

在搭建iSCSI前呢,先把SELinux禁用;关闭防火墙并清空所有规则。并配置好yum源(直接挂载本地光盘,yum源指向本地就行)。

基于RHCS+iSCSI+CLVM实现Web服务的共享存储集群架构 http://www.linuxidc.com/Linux/2013-05/84888.htm

Linux环境iSCSI存储及多路径功能配置 http://www.linuxidc.com/Linux/2013-05/84635.htm

构建基于IP SAN的iSCSI存储系统 http://www.linuxidc.com/Linux/2013-05/84570.htm

iSCSI连接不上解决 http://www.linuxidc.com/Linux/2013-01/78462.htm

Citrix XenServer中安装CentOS 6.0并配置iSCSI服务 http://www.linuxidc.com/Linux/2013-01/78461.htm

CentOS 5.3 使用iSCSI挂载存储磁盘柜 http://www.linuxidc.com/Linux/2011-01/31529.htm

开始搭建

首先要安装一个包:scsi-target-utils-1.0.14-4.el6.x86_64

  1. #yum -y install scsi-target-utils

在这或许会有人抱怨了,邪恶啊,这么长的名字记不住肿么办?不要怕,没事,邪恶教你一招:直接执行命令#yum -y install scsi-target* 也可以,这下简单多了吧。

好了,安装完以后,要启动服务,iSCSI的服务为tgtd,直接启动就行了

  1. #service tgtd start
  2. #chkconfig tgtd on (设为开机启动)

到此,我们安装完毕,简单吧!!!

创建iSCSI

在创建前呢,你要在你的电脑上先分出一个区用来搭建iSCSI存储。邪恶这次是分出一个5G大小的分区来创建iSCSI,给大家附上我的分区图:

大家看到了吧,sda5,大小为5G!

其实,在这里简单的提一下,分区的大小你自己随便定,也可以是一个硬盘,都行!

接下来呢,就是激动人心的时刻,关键来了哦,邪恶所说的精华就是接下来的内容,看招:

执行下面的三条命令:

  1. #tgtadm --lld iscsi --mode target --op new --tid 1 -T iqn.mailtest.com.20140916:disk1
  2. #tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 -b /dev/sda5
  3. #tgtadm --lld iscsi --mode target --op bind --tid 1 -I ALL

 

给大家简单解释一下命令,建议大家在执行前先执行一下

  1. #tgtadm --help

看一下命令的帮助文档。

第一条命令,只需掌握 --tid后面的参数“1”,这个“1”在这里就是个编号,你也可以设为2,不过建议大家从1开始,习惯嘛!然后就是-T后面的参数,-T后面的参数自己随便写,但记住一点就行,需以iqn开头,然后后面的你想怎么起都行,一般都是"iqn.hostname:diskX",hostname就是你的主机名,X代表编号。

第二条命令,需要记住--tid后面的参数必须要和第一条的一样, --lun后面的参数也是代表编号,和第一条命令的--tid类似,-b后面就跟你要搭建iSCSI的分区就行。

第三条命令,更简单,只需注意--tid后面的参数要和第一条、第二条的一样,其实第二条和第三条都是以第一条的为基准的,然后就是-I后面的参数,这里设置为ALL是代表允许所有的IP都可以访问。你也可以设置为个别的IP,具体的可以参考帮助文档。

执行完以后,可以执行

  1. #tgt-admin -s

查看一下自己创建的iSCSI,这里把我自己搭建效果图给大家附上

从图上可以看到,刚才自己创建的iSCSI Target及编号,也可以看到自己创建的lun和编号,最下面可以显示出用的哪个分区创建的。

到此,搭建完毕,是不是很简单!

这在儿,有个要点需记住,那就是开机后,上面3条命令创建的iSCSI会失效,要想开机后还有,那就把上面的3条命令添加到/etc/rc.d/rc.local里面,看图:

这样,以后在重启或关机后再开机就可以保证创建的iSCSI失效了!

或许到这儿,就会有人问了:“邪恶,你为什么不讲一下理论知识?”。

邪恶回答你的很简单:一讲理论就讲的多了,就需要花很大的文字去讲解,而且邪恶也不敢保证理论讲的是否正确。邪恶在此顺便给大家说一下,邪恶的原则是通过自己的实践和自己的心得和体会,尽量的写的精简点,然后分享给大家,让菜鸟和小白都能快速掌握,至于理论知识,百度上一大堆,都解释的很详细,一看就明白!

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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):

[plain] view plaincopy
  1. [chen@localhost 桌面]$ yum list|grep scsi  
  2. *Note* Red Hat Network repositories are not listed below. You must run this command as root to access RHN repositories.  
  3. iscsi-initiator-utils.i686             6.2.0.872-10.el6              @rhel-source  
  4. lsscsi.i686                            0.23-2.el6                    rhel-source  
  5. scsi-target-utils.i686                 1.0.4-3.el6                   rhel-source  
  6. [chen@localhost 桌面]$ su - root  
  7. 密码:  
  8. [root@localhost ~]# yum install scsi-target-utils #安装服务器端软件  
  9. Loaded plugins: refresh-packagekit, rhnplugin  
  10. This system is not registered with RHN.  
  11. RHN support will be disabled.  
  12. Setting up Install Process  
  13. Resolving Dependencies  
  14. --> Running transaction check  
  15. ---> Package scsi-target-utils.i686 0:1.0.4-3.el6 set to be updated  
  16. --> Processing Dependency: librdmacm.so.1 for package: scsi-target-utils-1.0.4-3.el6.i686  
  17. --> Processing Dependency: libibverbs.so.1 for package: scsi-target-utils-1.0.4-3.el6.i686  
  18. --> Processing Dependency: perl(Config::General) for package: scsi-target-utils-1.0.4-3.el6.i686  
  19. --> Running transaction check  
  20. ---> Package libibverbs.i686 0:1.1.4-2.el6 set to be updated  
  21. --> Processing Dependency: libibverbs-driver for package: libibverbs-1.1.4-2.el6.i686  
  22. ---> Package librdmacm.i686 0:1.0.10-2.el6 set to be updated  
  23. ---> Package perl-Config-General.noarch 0:2.44-1.el6 set to be updated  
  24. --> Running transaction check  
  25. ---> Package libmlx4.i686 0:1.0.1-7.el6 set to be updated  
  26. --> Finished Dependency Resolution  
  27.   
  28.   
  29. Dependencies Resolved  
  30.   
  31.   
  32. ================================================================================  
  33.  Package                  Arch        Version            Repository        Size  
  34. ================================================================================  
  35. Installing:  
  36.  scsi-target-utils        i686        1.0.4-3.el6        rhel-source      134 k  
  37. Installing for dependencies:  
  38.  libibverbs               i686        1.1.4-2.el6        rhel-source       43 k  
  39.  libmlx4                  i686        1.0.1-7.el6        rhel-source       27 k  
  40.  librdmacm                i686        1.0.10-2.el6       rhel-source       23 k  
  41.  perl-Config-General      noarch      2.44-1.el6         rhel-source       68 k  
  42.   
  43.   
  44. Transaction Summary  
  45. ================================================================================  
  46. Install       5 Package(s)  
  47. Upgrade       0 Package(s)  
  48.   
  49.   
  50. Total download size: 295 k  
  51. Installed size: 672 k  
  52. Is this ok [y/N]: y  
  53. Downloading Packages:  
  54. --------------------------------------------------------------------------------  
  55. Total                                           830 kB/s | 295 kB     00:00       
  56. Running rpm_check_debug  
  57. Running Transaction Test  
  58. Transaction Test Succeeded  
  59. Running Transaction  
  60.   Installing     : perl-Config-General-2.44-1.el6.noarch                    1/5   
  61.   Installing     : libmlx4-1.0.1-7.el6.i686                                 2/5   
  62.   Installing     : libibverbs-1.1.4-2.el6.i686                              3/5   
  63.   Installing     : librdmacm-1.0.10-2.el6.i686                              4/5   
  64.   Installing     : scsi-target-utils-1.0.4-3.el6.i686                       5/5   
  65.   
  66.   
  67. Installed:  
  68.   scsi-target-utils.i686 0:1.0.4-3.el6                                            
  69.   
  70.   
  71. Dependency Installed:  
  72.   libibverbs.i686 0:1.1.4-2.el6     libmlx4.i686 0:1.0.1-7.el6                   
  73.   librdmacm.i686 0:1.0.10-2.el6     perl-Config-General.noarch 0:2.44-1.el6      
  74.   
  75.   
  76. Complete!  
  77. [root@localhost ~]# ls /dev/sda*  
  78. /dev/sda   /dev/sda2  /dev/sda4  /dev/sda6  
  79. /dev/sda1  /dev/sda3  /dev/sda5  /dev/sda7  
  80. [root@localhost ~]# fdisk /dev/sda #注意服务器端要共享的盘必须是“原生”的,不能经过格式化,所以这里先分一个区  
  81.   
  82.   
  83. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to  
  84.          switch off the mode (command 'c') and change display units to  
  85.          sectors (command 'u').  
  86.   
  87.   
  88. Command (m for help): n  
  89. First cylinder (1614-2610, default 1614):   
  90. Using default value 1614  
  91. Last cylinder, +cylinders or +size{K,M,G} (1614-2610, default 2610): +500M#分一个500M的  
  92.   
  93.   
  94. Command (m for help): w  
  95. The partition table has been altered!  
  96.   
  97.   
  98. Calling ioctl() to re-read partition table.  
  99.   
  100.   
  101. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.  
  102. The kernel still uses the old table. The new table will be used at  
  103. the next reboot or after you run partprobe(8) or kpartx(8)  
  104. Syncing disks.  
  105. [root@localhost ~]# partx -a /dev/sda #刷新分区表  
  106. BLKPG: Device or resource busy  
  107. error adding partition 1  
  108. BLKPG: Device or resource busy  
  109. error adding partition 2  
  110. BLKPG: Device or resource busy  
  111. error adding partition 3  
  112. BLKPG: Device or resource busy  
  113. error adding partition 4  
  114. BLKPG: Device or resource busy  
  115. error adding partition 5  
  116. BLKPG: Device or resource busy  
  117. error adding partition 6  
  118. BLKPG: Device or resource busy  
  119. error adding partition 7  
  120. [root@localhost ~]# ls /dev/sda*  
  121. /dev/sda   /dev/sda2  /dev/sda4  /dev/sda6  /dev/sda8#可以看到有sda8了,就共享sda8  
  122. /dev/sda1  /dev/sda3  /dev/sda5  /dev/sda7  
  123. [root@localhost ~]# vim /etc/tgt/targets.conf   
  124. [root@localhost ~]# tail -5 /etc/tgt/targets.conf   
  125. #</target>  
  126. <target chen> 就加上这四行,这里这个分区的名字叫chen,redhat5中命名好像有一定格式,必须以iqn开头  
  127. backing-store /dev/sda8  
  128. initiator-address 192.169.1.100  
  129. </target>  
  130. [root@localhost ~]# /etc/init.d/tgtd start #启动服务,这样服务器端就配置完成了  
  131. 正在启动 SCSI target daemon:                              [确定]  
  132. [root@localhost ~]#   


客户端(ip:192.169.1.100):

[plain] view plaincopy
  1. [chen@localhost 桌面]$ yum list|grep scsi  
  2. scsi-target-utils.x86_64                1.0.14-2.el6                @rhel-source  
  3. iscsi-initiator-utils.x86_64            6.2.0.872-21.el6            rhel-source   
  4. lsscsi.x86_64                           0.23-2.el6                  rhel-source   
  5. [chen@localhost 桌面]$ su - root  
  6. 密码:  
  7. [root@localhost ~]# yum install -y iscsi-initiator-utils#先安装客服端  
  8. Loaded plugins: product-id, refresh-packagekit, subscription-manager  
  9. Updating Red Hat repositories.  
  10. Setting up Install Process  
  11. Resolving Dependencies  
  12. --> Running transaction check  
  13. ---> Package iscsi-initiator-utils.x86_64 0:6.2.0.872-21.el6 will be installed  
  14. --> Finished Dependency Resolution  
  15.   
  16.   
  17. Dependencies Resolved  
  18.   
  19.   
  20. ================================================================================  
  21.  Package                  Arch      Version                Repository      Size  
  22. ================================================================================  
  23. Installing:  
  24.  iscsi-initiator-utils    x86_64    6.2.0.872-21.el6       rhel-source    579 k  
  25.   
  26.   
  27. Transaction Summary  
  28. ================================================================================  
  29. Install       1 Package(s)  
  30.   
  31.   
  32. Total download size: 579 k  
  33. Installed size: 2.0 M  
  34. Downloading Packages:  
  35. Running rpm_check_debug  
  36. Running Transaction Test  
  37. Transaction Test Succeeded  
  38. Running Transaction  
  39.   Installing : iscsi-initiator-utils-6.2.0.872-21.el6.x86_64                1/1   
  40. duration: 165(ms)  
  41. Installed products updated.  
  42.   
  43.   
  44. Installed:  
  45.   iscsi-initiator-utils.x86_64 0:6.2.0.872-21.el6                                 
  46.   
  47.   
  48. Complete!  
  49. [root@localhost ~]# chkconfig --list|grep scsi #客户端服务自动开启了  
  50. iscsi           0:关闭1:关闭2:关闭3:启用4:启用5:启用6:关闭  
  51. iscsid         0:关闭1:关闭2:关闭3:启用4:启用5:启用6:关闭  
  52. [root@localhost ~]# iscsiadm -m discovery -t st -p 192.169.1.98<span style="white-space:pre">   </span>#发现服务器端是否有磁盘共享  
  53. 正在启动 iscsid:                                          [确定]  
  54. 192.169.1.98:3260,1 chen #可以看到有一个名字叫chen的磁盘,说明发现了  
  55. [root@localhost ~]# iscsiadm -m node -T chen -p 192.169.1.98 -l#登陆服务器端  
  56. Logging in to [iface: default, target: chen, portal: 192.169.1.98,3260]  
  57. Login to [iface: default, target: chen, portal: 192.169.1.98,3260] successful.#登陆成功  
  58. [root@localhost ~]# fdisk -l #查看一下本地是否多一块磁盘  
  59.   
  60.   
  61. Disk /dev/sda: 21.5 GB, 21474836480 bytes  
  62. 255 heads, 63 sectors/track, 2610 cylinders  
  63. Units = cylinders of 16065 * 512 = 8225280 bytes  
  64. Sector size (logical/physical): 512 bytes / 512 bytes  
  65. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  66. Disk identifier: 0x000ee48e  
  67.   
  68.   
  69.    Device Boot      Start         End      Blocks   Id  System  
  70. /dev/sda1   *           1          26      204800   83  Linux  
  71. Partition 1 does not end on cylinder boundary.  
  72. /dev/sda2              26        1332    10485760   83  Linux  
  73. /dev/sda3            1332        1778     3584000   82  Linux swap / Solaris  
  74. /dev/sda4            1779        2610     6683040    5  Extended  
  75. /dev/sda5            1779        1831      425691   83  Linux  
  76. /dev/sda6            1832        1838       56196   83  Linux  
  77. /dev/sda7            1839        1970     1060258+  83  Linux  
  78.   
  79.   
  80. Disk /dev/sdb: 534 MB, 534610944 bytes #可以看到这边多了一块磁盘sdb,大小约为500M  
  81. 17 heads, 60 sectors/track, 1023 cylinders  
  82. Units = cylinders of 1020 * 512 = 522240 bytes  
  83. Sector size (logical/physical): 512 bytes / 512 bytes  
  84. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  85. Disk identifier: 0x00000000  
  86.   
  87.   
  88. Disk /dev/sdb doesn't contain a valid partition table  
  89. [root@localhost ~]# ls /dev/sdb   
  90. /dev/sdb  
  91. [root@localhost ~]# blkid /dev/sda1  
  92. /dev/sda1: UUID="18ed3ae9-6e4f-4674-ac6d-5d763ead42ce" TYPE="ext4"    
  93. [root@localhost ~]# blkid /dev/sdb #没有uuid信息,这块磁盘是“原生”的  
  94. [root@localhost ~]# ls /dev/sdb  
  95. /dev/sdb  
  96. [root@localhost ~]# fdisk /dev/sdb #可以像本地多了一块新的磁盘一样使用这块磁盘了,这里做一个分区  
  97. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel  
  98. Building a new DOS disklabel with disk identifier 0x945cf8be.  
  99. Changes will remain in memory only, until you decide to write them.  
  100. After that, of course, the previous content won't be recoverable.  
  101.   
  102.   
  103. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)  
  104.   
  105.   
  106. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to  
  107.          switch off the mode (command 'c') and change display units to  
  108.          sectors (command 'u').  
  109.   
  110.   
  111. Command (m for help): n  
  112. Command action  
  113.    e   extended  
  114.    p   primary partition (1-4)  
  115. p  
  116. Partition number (1-4): 1  
  117. First cylinder (1-1023, default 1):   
  118. Using default value 1  
  119. Last cylinder, +cylinders or +size{K,M,G} (1-1023, default 1023): +100M#分一个100M的主分区  
  120.   
  121.   
  122. Command (m for help): p  
  123.   
  124.   
  125. Disk /dev/sdb: 534 MB, 534610944 bytes  
  126. 17 heads, 60 sectors/track, 1023 cylinders  
  127. Units = cylinders of 1020 * 512 = 522240 bytes  
  128. Sector size (logical/physical): 512 bytes / 512 bytes  
  129. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  130. Disk identifier: 0x945cf8be  
  131.   
  132.   
  133.    Device Boot      Start         End      Blocks   Id  System  
  134. /dev/sdb1               1         202      102990   83  Linux  
  135.   
  136.   
  137. Command (m for help): w  
  138. The partition table has been altered!  
  139.   
  140.   
  141. Calling ioctl() to re-read partition table.  
  142. Syncing disks.  
  143. [root@localhost ~]# partx -a /dev/sdb  
  144. BLKPG: Device or resource busy  
  145. error adding partition 1  
  146. [root@localhost ~]# ls /dev/sdb*  
  147. /dev/sdb  /dev/sdb1  
  148. [root@localhost ~]# mkdir /sdb1 #创建挂载点  
  149. [root@localhost ~]# mkfs.ext4 /dev/sdb1 #格式化sdb1  
  150. mke2fs 1.41.12 (17-May-2010)  
  151. 文件系统标签=  
  152. 操作系统:Linux  
  153. 块大小=1024 (log=0)  
  154. 分块大小=1024 (log=0)  
  155. Stride=0 blocks, Stripe width=0 blocks  
  156. 25792 inodes, 102988 blocks  
  157. 5149 blocks (5.00%) reserved for the super user  
  158. 第一个数据块=1  
  159. Maximum filesystem blocks=67371008  
  160. 13 block groups  
  161. 8192 blocks per group, 8192 fragments per group  
  162. 1984 inodes per group  
  163. Superblock backups stored on blocks:   
  164. 8193, 24577, 40961, 57345, 73729  
  165.   
  166.   
  167. 正在写入inode表: 完成                              
  168. Creating journal (4096 blocks): 完成  
  169. Writing superblocks and filesystem accounting information: 完成  
  170.   
  171.   
  172. This filesystem will be automatically checked every 25 mounts or  
  173. 180 days, whichever comes first.  Use tune2fs -c or -i to override.  
  174. [root@localhost ~]# mount /dev/sdb1 /sdb1/ #挂载  
  175. [root@localhost ~]# df -hT  
  176. 文件系统    类型      容量  已用  可用 已用%% 挂载点  
  177. /dev/sda2     ext4    9.9G  3.3G  6.2G  35% /  
  178. tmpfs        tmpfs    497M  420K  497M   1% /dev/shm  
  179. /dev/sda1     ext4    194M   27M  158M  15% /boot  
  180. /dev/sda6     ext4     54M   25M   26M  50% /quota  
  181. /dev/sr0   iso9660    3.4G  3.4G     0 100% /media/RHEL_6.1 x86_64 Disc 1  
  182. /dev/sr0   iso9660    3.4G  3.4G     0 100% /mnt/iso  
  183. /dev/sdb1     ext4     98M  5.6M   87M   6% /sdb1 #可以看到的确挂载上去了,文件系统类型为ext4  
  184. [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这个发现,要先退出,再删除,删除后必须重新发现,才能登录。


在做过iscsi共享服务到客户端时,在客户端不能格式化或是挂载 下面有两个奇葩的方法可以解决但不知其原因是是啥-------仅供参考

第一种:
这块磁盘以前肯定做过raid或者lvm,磁盘清理不干净所致。
我刚好也遇见过这样的问题,已解决。
如下:


#mkfs.ext4 -T Largefile /dev/sdc1
          /dev/sdc1 is apparently in use by the system; will not make a filesystem here!
解决方法:
# /sbin/dmsetup status
ddf1_4c53492020202020100000550000000047114711000032c8: 0 1951170560 linear 
# /sbin/dmsetup remove_all
# /sbin/dmsetup status
No devices found


然后
#mkfs.ext4 -T Largefile /dev/sdc1  就ok了!
这样就格式化成功并能正常挂载使用。。。。。。。

第二种:








0 0
原创粉丝点击