天天看點

搭建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 plain copy

  1. [[email protected] 桌面]$ 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. [[email protected] 桌面]$ su - root  
  7. 密碼:  
  8. [[email protected] ~]# 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. Dependencies Resolved  
  28. ================================================================================  
  29.  Package                  Arch        Version            Repository        Size  
  30. ================================================================================  
  31. Installing:  
  32.  scsi-target-utils        i686        1.0.4-3.el6        rhel-source      134 k  
  33. Installing for dependencies:  
  34.  libibverbs               i686        1.1.4-2.el6        rhel-source       43 k  
  35.  libmlx4                  i686        1.0.1-7.el6        rhel-source       27 k  
  36.  librdmacm                i686        1.0.10-2.el6       rhel-source       23 k  
  37.  perl-Config-General      noarch      2.44-1.el6         rhel-source       68 k  
  38. Transaction Summary  
  39. ================================================================================  
  40. Install       5 Package(s)  
  41. Upgrade       0 Package(s)  
  42. Total download size: 295 k  
  43. Installed size: 672 k  
  44. Is this ok [y/N]: y  
  45. Downloading Packages:  
  46. --------------------------------------------------------------------------------  
  47. Total                                           830 kB/s | 295 kB     00:00       
  48. Running rpm_check_debug  
  49. Running Transaction Test  
  50. Transaction Test Succeeded  
  51. Running Transaction  
  52.   Installing     : perl-Config-General-2.44-1.el6.noarch                    1/5   
  53.   Installing     : libmlx4-1.0.1-7.el6.i686                                 2/5   
  54.   Installing     : libibverbs-1.1.4-2.el6.i686                              3/5   
  55.   Installing     : librdmacm-1.0.10-2.el6.i686                              4/5   
  56.   Installing     : scsi-target-utils-1.0.4-3.el6.i686                       5/5   
  57. Installed:  
  58.   scsi-target-utils.i686 0:1.0.4-3.el6                                            
  59. Dependency Installed:  
  60.   libibverbs.i686 0:1.1.4-2.el6     libmlx4.i686 0:1.0.1-7.el6                   
  61.   librdmacm.i686 0:1.0.10-2.el6     perl-Config-General.noarch 0:2.44-1.el6      
  62. Complete!  
  63. [[email protected] ~]# ls /dev/sda*  
  64. /dev/sda   /dev/sda2  /dev/sda4  /dev/sda6  
  65. /dev/sda1  /dev/sda3  /dev/sda5  /dev/sda7  
  66. [[email protected] ~]# fdisk /dev/sda #注意伺服器端要共享的盤必須是“原生”的,不能經過格式化,是以這裡先分一個區  
  67. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to  
  68.          switch off the mode (command 'c') and change display units to  
  69.          sectors (command 'u').  
  70. Command (m for help): n  
  71. First cylinder (1614-2610, default 1614):   
  72. Using default value 1614  
  73. Last cylinder, +cylinders or +size{K,M,G} (1614-2610, default 2610): +500M#分一個500M的  
  74. Command (m for help): w  
  75. The partition table has been altered!  
  76. Calling ioctl() to re-read partition table.  
  77. WARNING: Re-reading the partition table failed with error 16: 裝置或資源忙.  
  78. The kernel still uses the old table. The new table will be used at  
  79. the next reboot or after you run partprobe(8) or kpartx(8)  
  80. Syncing disks.  
  81. [[email protected] ~]# partx -a /dev/sda #重新整理分區表  
  82. BLKPG: Device or resource busy  
  83. error adding partition 1  
  84. BLKPG: Device or resource busy  
  85. error adding partition 2  
  86. BLKPG: Device or resource busy  
  87. error adding partition 3  
  88. BLKPG: Device or resource busy  
  89. error adding partition 4  
  90. BLKPG: Device or resource busy  
  91. error adding partition 5  
  92. BLKPG: Device or resource busy  
  93. error adding partition 6  
  94. BLKPG: Device or resource busy  
  95. error adding partition 7  
  96. [[email protected] ~]# ls /dev/sda*  
  97. /dev/sda   /dev/sda2  /dev/sda4  /dev/sda6  /dev/sda8#可以看到有sda8了,就共享sda8  
  98. /dev/sda1  /dev/sda3  /dev/sda5  /dev/sda7  
  99. [[email protected] ~]# vim /etc/tgt/targets.conf   
  100. [[email protected] ~]# tail -5 /etc/tgt/targets.conf   
  101. #</target>  
  102. <target chen> 就加上這四行,這裡這個分區的名字叫chen,redhat5中命名好像有一定格式,必須以iqn開頭  
  103. backing-store /dev/sda8  
  104. initiator-address 192.169.1.100  
  105. </target>  
  106. [[email protected] ~]# /etc/init.d/tgtd start #啟動服務,這樣伺服器端就配置完成了  
  107. 正在啟動 SCSI target daemon:                              [确定]  
  108. [[email protected] ~]#   

用戶端(ip:192.169.1.100):

[plain]  view plain copy

  1. [[email protected] 桌面]$ 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. [[email protected] 桌面]$ su - root  
  6. 密碼:  
  7. [[email protected] ~]# 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. Dependencies Resolved  
  16. ================================================================================  
  17.  Package                  Arch      Version                Repository      Size  
  18. ================================================================================  
  19. Installing:  
  20.  iscsi-initiator-utils    x86_64    6.2.0.872-21.el6       rhel-source    579 k  
  21. Transaction Summary  
  22. ================================================================================  
  23. Install       1 Package(s)  
  24. Total download size: 579 k  
  25. Installed size: 2.0 M  
  26. Downloading Packages:  
  27. Running rpm_check_debug  
  28. Running Transaction Test  
  29. Transaction Test Succeeded  
  30. Running Transaction  
  31.   Installing : iscsi-initiator-utils-6.2.0.872-21.el6.x86_64                1/1   
  32. duration: 165(ms)  
  33. Installed products updated.  
  34. Installed:  
  35.   iscsi-initiator-utils.x86_64 0:6.2.0.872-21.el6                                 
  36. Complete!  
  37. [[email protected] ~]# chkconfig --list|grep scsi #用戶端服務自動開啟了  
  38. iscsi           0:關閉1:關閉2:關閉3:啟用4:啟用5:啟用6:關閉  
  39. iscsid         0:關閉1:關閉2:關閉3:啟用4:啟用5:啟用6:關閉  
  40. [[email protected] ~]# iscsiadm -m discovery -t st -p 192.169.1.98<span style="white-space:pre">   </span>#發現伺服器端是否有磁盤共享  
  41. 正在啟動 iscsid:                                          [确定]  
  42. 192.169.1.98:3260,1 chen #可以看到有一個名字叫chen的磁盤,說明發現了  
  43. [[email protected] ~]# iscsiadm -m node -T chen -p 192.169.1.98 -l#登陸伺服器端  
  44. Logging in to [iface: default, target: chen, portal: 192.169.1.98,3260]  
  45. Login to [iface: default, target: chen, portal: 192.169.1.98,3260] successful.#登陸成功  
  46. [[email protected] ~]# fdisk -l #檢視一下本地是否多一塊磁盤  
  47. Disk /dev/sda: 21.5 GB, 21474836480 bytes  
  48. 255 heads, 63 sectors/track, 2610 cylinders  
  49. Units = cylinders of 16065 * 512 = 8225280 bytes  
  50. Sector size (logical/physical): 512 bytes / 512 bytes  
  51. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  52. Disk identifier: 0x000ee48e  
  53.    Device Boot      Start         End      Blocks   Id  System  
  54. /dev/sda1   *           1          26      204800   83  Linux  
  55. Partition 1 does not end on cylinder boundary.  
  56. /dev/sda2              26        1332    10485760   83  Linux  
  57. /dev/sda3            1332        1778     3584000   82  Linux swap / Solaris  
  58. /dev/sda4            1779        2610     6683040    5  Extended  
  59. /dev/sda5            1779        1831      425691   83  Linux  
  60. /dev/sda6            1832        1838       56196   83  Linux  
  61. /dev/sda7            1839        1970     1060258+  83  Linux  
  62. Disk /dev/sdb: 534 MB, 534610944 bytes #可以看到這邊多了一塊磁盤sdb,大小約為500M  
  63. 17 heads, 60 sectors/track, 1023 cylinders  
  64. Units = cylinders of 1020 * 512 = 522240 bytes  
  65. Sector size (logical/physical): 512 bytes / 512 bytes  
  66. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  67. Disk identifier: 0x00000000  
  68. Disk /dev/sdb doesn't contain a valid partition table  
  69. [[email protected] ~]# ls /dev/sdb   
  70. /dev/sdb  
  71. [[email protected] ~]# blkid /dev/sda1  
  72. /dev/sda1: UUID="18ed3ae9-6e4f-4674-ac6d-5d763ead42ce" TYPE="ext4"    
  73. [[email protected] ~]# blkid /dev/sdb #沒有uuid資訊,這塊磁盤是“原生”的  
  74. [[email protected] ~]# ls /dev/sdb  
  75. /dev/sdb  
  76. [[email protected] ~]# fdisk /dev/sdb #可以像本地多了一塊新的磁盤一樣使用這塊磁盤了,這裡做一個分區  
  77. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel  
  78. Building a new DOS disklabel with disk identifier 0x945cf8be.  
  79. Changes will remain in memory only, until you decide to write them.  
  80. After that, of course, the previous content won't be recoverable.  
  81. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)  
  82. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to  
  83.          switch off the mode (command 'c') and change display units to  
  84.          sectors (command 'u').  
  85. Command (m for help): n  
  86. Command action  
  87.    e   extended  
  88.    p   primary partition (1-4)  
  89. p  
  90. Partition number (1-4): 1  
  91. First cylinder (1-1023, default 1):   
  92. Using default value 1  
  93. Last cylinder, +cylinders or +size{K,M,G} (1-1023, default 1023): +100M#分一個100M的主分區  
  94. Command (m for help): p  
  95. Disk /dev/sdb: 534 MB, 534610944 bytes  
  96. 17 heads, 60 sectors/track, 1023 cylinders  
  97. Units = cylinders of 1020 * 512 = 522240 bytes  
  98. Sector size (logical/physical): 512 bytes / 512 bytes  
  99. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  100. Disk identifier: 0x945cf8be  
  101.    Device Boot      Start         End      Blocks   Id  System  
  102. /dev/sdb1               1         202      102990   83  Linux  
  103. Command (m for help): w  
  104. The partition table has been altered!  
  105. Calling ioctl() to re-read partition table.  
  106. Syncing disks.  
  107. [[email protected] ~]# partx -a /dev/sdb  
  108. BLKPG: Device or resource busy  
  109. error adding partition 1  
  110. [[email protected] ~]# ls /dev/sdb*  
  111. /dev/sdb  /dev/sdb1  
  112. [[email protected] ~]# mkdir /sdb1 #建立挂載點  
  113. [[email protected] ~]# mkfs.ext4 /dev/sdb1 #格式化sdb1  
  114. mke2fs 1.41.12 (17-May-2010)  
  115. 檔案系統标簽=  
  116. 作業系統:Linux  
  117. 塊大小=1024 (log=0)  
  118. 分塊大小=1024 (log=0)  
  119. Stride=0 blocks, Stripe width=0 blocks  
  120. 25792 inodes, 102988 blocks  
  121. 5149 blocks (5.00%) reserved for the super user  
  122. 第一個資料塊=1  
  123. Maximum filesystem blocks=67371008  
  124. 13 block groups  
  125. 8192 blocks per group, 8192 fragments per group  
  126. 1984 inodes per group  
  127. Superblock backups stored on blocks:   
  128. 8193, 24577, 40961, 57345, 73729  
  129. 正在寫入inode表: 完成                              
  130. Creating journal (4096 blocks): 完成  
  131. Writing superblocks and filesystem accounting information: 完成  
  132. This filesystem will be automatically checked every 25 mounts or  
  133. 180 days, whichever comes first.  Use tune2fs -c or -i to override.  
  134. [[email protected] ~]# mount /dev/sdb1 /sdb1/ #挂載  
  135. [[email protected] ~]# df -hT  
  136. 檔案系統    類型      容量  已用  可用 已用%% 挂載點  
  137. /dev/sda2     ext4    9.9G  3.3G  6.2G  35% /  
  138. tmpfs        tmpfs    497M  420K  497M   1% /dev/shm  
  139. /dev/sda1     ext4    194M   27M  158M  15% /boot  
  140. /dev/sda6     ext4     54M   25M   26M  50% /quota  
  141. /dev/sr0   iso9660    3.4G  3.4G     0 100% /media/RHEL_6.1 x86_64 Disc 1  
  142. /dev/sr0   iso9660    3.4G  3.4G     0 100% /mnt/iso  
  143. /dev/sdb1     ext4     98M  5.6M   87M   6% /sdb1 #可以看到的确挂載上去了,檔案系統類型為ext4  
  144. [[email protected] ~]#   

注意: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這個發現,要先退出,再删除,删除後必須重新發現,才能登入

轉載自http://blog.csdn.net/chen_jp/article/details/7848835

繼續閱讀