[Linux] iSCSI target & initiator安裝筆記

2014-02-26 CentOS

此篇介紹將使用 Linux OS 外接Stroage,並用 iSCSI 的方式打給各主機使用。

 
一般的 Storage都是以TB為單位,常見的有NAS、SAN,在一般的2U Stroage中大多是採用1394、SCSI、光纖等等實際介面連接,然後再用一台Storage Server share出去。
 
 
以下是此篇的架構,iSCSI(internet SCSI)是採TCP的方式連接,只要可以透過TCP連接到的都可以使用。
 

iSCSI Server主要分為兩類
 
1. iSCSI target:作為 Storage Server 端,主要提供其他主機使用「硬碟」
2. iSCSI initiator:作為需要「硬碟」的 client user。
 
 
 
 
首先先把外接Storage給mount起來
 
#查看一下開機時有沒有抓到Storage
shell# dmesg
 
#再檢查抓到的硬碟資訊是否正確
shell# fdisk -l
 
 
#看到硬碟在 /dev/sdb,並且有 1.75TB,I/O區塊為512bytes(大小的設定取決於你放置的檔案類型,影響R/W的快慢),由於這顆Storage不需要分割,所以直接跳到格式化的部分
#格式化選擇較多支援的ext3
shell# mkfs -t ext3 /dev/sdb
 
#然後試試看能不能mount,掛載到事先建立好的/volume
shell# mount /dev/sdb /volume
 
#檢查有沒有掛載上去
shell# df
#掛載硬碟
#使用UUID掛載,先檢查Storage的UUID
shell# blkid /dev/sdb
/dev/sdb: UUID=”dc2b34cc-7382-477f-bcb5-10c4831e89bf” TYPE=”ext3″
#到/etc/fstab用剛剛查到的UUID把硬碟掛起來
shell# vim /etc/fstab
#重開機檢查一下有沒有自動把 Storage掛起來。
 
 
 
 
 
iSCSI target
 
#開始建立client Server所需要的硬碟檔,400GB需要一段時間建立
shell# dd if=/dev/zero of=/volume/disk1.img bs=1M count=409600
 
#準備target套件
shell# yum install scsi-target-utils
 
#再來看到主要設定檔,基本上不需要太複雜的設定
shell# vim /etc/tgt/targets.conf
<target iqn.2014-02-25.eric.com.tw:disk1>     #註1
       backing-store /volume/disk1.img             #也可用direct-store指定實體硬碟
       initiator-address 192.168.1.1                 #限制可使用這個target的來源IP
       incominguser account password1234       #限制client user使用這組帳密連線,註2
       write-cache off                                   #若開啟快取,恐會造成資料毀損
</target>
或是可以 include到指定設定檔
 
 
#啟動服務,並查看是否有Listen 3260
shell# service tgtd start
shell# chkconfig tgtd on
shell# netstat -tlunp | grep tgt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address     Foreign Address State     PID/Program name
tcp           0         0   0.0.0.0:3260       0.0.0.0:*        LISTEN        26944/tgtd
tcp           0         0          :::3260          :::*            LISTEN        26944/tgtd
 
註1: target檔名定義,iqn.yyyy-mm-dd.<reversed domain name>:identifier
註2: 密碼的長度最短12bit,最長16bit
 
#管理指令tgt-admin
shell# tgt-admin -show
 
Target 1: iqn.2014-02-25.eric.com.tw:disk1
    System information:
        Driver: iscsi
        State: ready
    I_T nexus information:
        I_T nexus: 1
            Initiator: iqn.1991-05.com.microsoft:vs9      #client user的initiator name
            Connection: 1
                IP Address: 192.168.1.1
    LUN information:
        LUN: 0                                         #LUN 0 是控制器,一個target內一定會有一個控制器
            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                                         #LUN 1 就是我們剛剛建立的Storage
            Type: disk
            SCSI ID: IET     00010001
            SCSI SN: beaf11
            Size: 429497 MB, Block size: 512
            Online: Yes
            Removable media: No
            Prevent removal: No
            Readonly: No
            Backing store type: rdwr
            Backing store path: /volume/disk1.img
            Backing store flags:
    Account information:
        account
    ACL information:
        192.168.1.1
當target的服務有在 on 時,需要新增一個target,但是同時線上又有initiator服務在跑,這時候千萬不可以偷懶直接將Service重起或 reload,有可能會造成其他連線中斷。

這時候該怎麼辦呢?其實target有動態新增的功能,使用動態指令讓設定馬上生效,不過事後也是要把設定檔補上唷!否則重開機設定都消失了。

動態新增target

#請注意tid不得重複,可以tgt-admin -show查看
shell# tgtadm --lld iscsi --mode target --op new --tid 1 --targetname iqn.2014-02-25.eric.com.tw:disk2

#新增disk在Lun 1,同樣的不能重複唷!  Lun 0 是控制器
shell# tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 --backing-store /volume/disk2.img

#限制IP加進去
shell# tgtadm --lld iscsi --mode target --op bind --tid 1 --initiator-address 192.168.1.1

#帳密也加進去
shell# tgtadm --lld iscsi --mode account --op new --user test --password testpassword

#將target跟帳號mapping
shell# tgtadm --lld iscsi --mode account --op bind --tid 1 --user test

 

當然有新增,也會有移除

動態移除target

找到要移除的iSCSI: tgt-admin -show按以下規則依序刪除

#把帳號跟target卸離
shell# tgtadm --lld iscsi --mode account --op unbind --tid 1 --user test

#刪除帳號
shell# tgtadm --lld iscsi --mode account --op delete --user test

#刪除限制IP
shell# tgtadm --lld iscsi --mode target --op unbind --tid 1 --initiator-address 192.168.1.1

#刪除掛載Storage
shell# tgtadm --lld iscsi --mode logicalunit --op delete --tid 1 --lun 1 --backing-store /volume/disk2.img

#刪除target tid 1
shell# tgtadm --lld iscsi --mode target --op delete --tid 1

 

#檢查是否都移除乾淨 tgt-admin -show

※要先將線上的initiator給卸除掛載,使其狀態不是readey,否則過程中會出現錯誤!

iSCSI initiator

這篇的範例initiator是使用Windows掛載的,在Server2003需要手動下載,而Server2008已經內建在「系統管理工具」→「iSCSI啟動器」

1. 在目標打上target ServerName,並按下快速連線

2. 自動探索到target Server,狀態應是「未連線」

3. 按下連線並打勾CHAP,打上target帳密,連線成功。這邊帳密要是輸入顯示錯誤,有可能是因為密碼的長度有問題。

4. 打開磁碟管理,找到剛剛一顆新的硬碟,並且狀態為離線,將其上線並格式化即可使用

檢查硬碟資訊可以看到這顆硬碟是SCSI Disk,target tid 0,LUN 1



參考資料
鳥哥 http://linux.vbird.org/linux_server/0460iscsi.php

給 Mr. 沙先生一點建議

彙整

分類

展開全部 | 收合全部

License

訂閱 Mr. 沙先生 的文章

輸入你的 email 用於訂閱