Multipath實現LUN裝置名稱的持久化

season0891發表於2012-09-10
一. 補充知識
 
根據傳輸協議的不同的,網路卡可分為三種,一是乙太網卡,二是FC網路卡,三是iSCSI網路卡。
 
(1)乙太網卡:學名Ethernet Adapter,傳輸協議為IP協議,一般透過光纖線纜或雙絞線與乙太網交換機連線。介面型別分為光口和電口。光口一般都是透過光纖線纜來進行資料傳輸,介面模組一般為SFP(傳輸率2Gb/s)和GBIC(1Gb/s),對應的介面為SC、ST和LC。電口目前常用介面型別為RJ45,用來與雙絞線連線,也有與同軸電纜連線的介面,不過現在已經用的比較少了。 

(2)FC網路卡:一般也叫光纖網路卡,學名Fibre Channel HBA(Hose Bus Adapter: 主機匯流排介面卡)。傳輸協議為光纖通道協議,一般透過光纖線纜與光纖通道交換機連線。介面型別分為光口和電口。光口一般都是透過光纖線纜來進行資料傳輸,介面模組一般為SFP(傳輸率2Gb/s)和GBIC(1Gb/s),對應的介面為SC和LC。電口的介面型別一般為DB9針或HSSDC。
“光纖網路卡”一般是指FC HBA卡,插在上,外接儲存用的光纖交換機;而光口的乙太網卡一般都叫做“光纖乙太網卡”,也是插在伺服器上,不過它外接的是帶光口的乙太網交換機。
 
(3)ISCSI網路卡:Internet Small Computer System Interface, 學名ISCSI HBA,傳輸ISCSI協議,介面型別與乙太網卡相同。 iSCSI(internet SCSI)是一套定義在TCP/IP網路上傳輸SCSI協議的命令。他將SCSI定義的發起方(initiator)和目標方(target)由原來的 SCSI匯流排連線擴充套件到internet上,打破了SCSI定義中的儲存距離的限制。
 
 
FC SAN和IP SAN是目前兩種流行的SAN儲存方案:
(1) SAN裝置在連入系統後,表示為一個或多個target ID,它的邏輯分配單元即為LUN-Logical Unit Number(邏輯單元號)。
(2)IP SAN也稱之為ISCSI(internet Small Computer System Interface)。
 
ISCSI技術的核心是在TCP/IP網路上傳輸SCSI協議,是指用TCP/IP報文、和ISCSI報文封裝SCSI報文,使得SCSI命令和資料可以在普通乙太網路上進行傳輸
 
 
瞭解上面的知識,現在來看一下Multipath。 Multipath除了上面提到的,實現LUN裝置的持久化,它還有另一個功能,支援多路徑輪詢(提高 I/O負載能力)。 即可以透過多個網路卡去訪問Target 裝置。 這樣就提高了I/O的能力。 在生產環境中較多使用multipath實現LUN的持久化和多路徑訪問。
 
       注意, 這裡有個問題,當我們配置了多session訪問儲存的target後,那麼每個網路卡都會生成一個/dev/sd*的裝置。 這個在昨天的裡已經有說明。昨天說的lun裝置隨即對映到可用的/dev/sd* 裝置上,在這裡一個lun 對映還可以到多個/dev/sd* 裝置的問題。所以對LUN 裝置的持久化是非常重要的。
但是每個target 的ID 是唯一的,在多session訪問的條件下,不管一個lun對映到幾個裝置, 在配置Multipath的時候,我用的是target ID。 這樣就保證了target 的唯一性。
 
 
二. 配置
 
2.1 安裝Multipath
檢視相關包:
[root@rac1 ~]# rpm -qa|grep device-mapper
device-mapper-multipath-0.4.7-30.el5
device-mapper-event-1.02.32-1.el5
device-mapper-1.02.32-1.el5
 
如果沒有安裝,從系統的安裝檔案裡找到這集個包:
device-mapper-1.02.32-1.el5.i386.rpm
device-mapper-event-1.02.32-1.el5.i386.rpm
device-mapper-multipath-0.4.7-30.el5.i386.rpm
 
安裝很簡單:
Rpm -Uvh device-mapper-*.rpm
 
說明:
(1)device-mapper-multipath
提供multipathd和multipath等工具和multipath.conf等配置檔案。這些工具透過device mapper的ioctr的介面建立和配置multipath裝置(呼叫device-mapper的使用者空間庫。建立的多路徑裝置會在/dev/mapper中)
 
(2)device-mapper
device-mapper包括兩大部分:核心部分和使用者部分。
核心部分由device-mapper核心(multipath.ko)和一些target driver(dm-multipath.ko)構成。dm-mod.ko是實現multipath的基礎,dm-multipath其實是dm的一個target驅動。核心完成裝置的對映,而target根據對映關係和自身特點具體處理從mappered device 下來的i/o。同時,在核心部分,提供了一個介面,使用者透過ioctr可和核心部分通訊,以指導核心驅動的行為,比如如何建立mappered device,這些device的屬性等。
 
使用者空間部分包括device-mapper這個包。其中包括dmsetup工具和一些幫助建立和配置mappered device的庫。這些庫主要抽象,封裝了與ioctr通訊的介面,以便方便建立和配置mappered device。device-mapper-multipath的程式中就需要呼叫這些庫。
 
 
2.2 配置ISCSI多session訪問
 
1個iSCSI Initiator透過多個Session連入同1個iSCSI Target裝置,以便利用多網路卡或iSCSI HBA啟用負載平衡與故障失效切換機制,也可稱作Multiple Sessions per Initiator。
 
2.2.1 斷開ISCSI登入狀態
       [root@rac1 ~]# iscsiadm -m node -T iqn.2006-01.com.san -p 192.168.6.1 -u
Logging out of session [sid: 1, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]
Logout of [sid: 1, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]: successful
 
-u 表示logout,-l 表示login。 具體可以用man iscsiadm 檢視。
 
 
2.2.2新建訪問介面檔案
 
[root@rac1 ~]# iscsiadm -m iface -I iface0 --op=new
New interface iface0 added
[root@rac1 ~]# iscsiadm -m iface -I iface1 --op=new
New interface iface1 added
 
介面檔案儲存在/var/lib/iscsi/ifaces目錄中
 
[root@rac1 ifaces]# cd /var/lib/iscsi/ifaces/
[root@rac1 ifaces]# ls
iface0  iface1
[root@rac1 ifaces]# cat iface0
# BEGIN RECORD 2.0-871
iface.iscsi_ifacename = iface0
iface.transport_name = tcp
# END RECORD
[root@rac1 ifaces]# cat iface1
# BEGIN RECORD 2.0-871
iface.iscsi_ifacename = iface1
iface.transport_name = tcp
# END RECORD
[root@rac1 ifaces]#
 
2.2.3 配置iface
 
[root@rac1 ifaces]# iscsiadm -m iface -I iface0 --op=update -n iface.net_ifacename -v eth0
iface0 updated.
[root@rac1 ifaces]# iscsiadm -m iface -I iface1 --op=update -n iface.net_ifacename -v eth1
iface1 updated.
 
 
2.2.4 確認iface配置
 
[root@rac1 ifaces]# iscsiadm -m iface
default tcp,,,,
iser iser,,,,
iface1 tcp,,,eth1,
iface0 tcp,,,eth0,
 
 
2.2.5 搜尋ISCSI Target
 
[root@rac1 ifaces]# iscsiadm -m discovery -t st -p 192.168.6.1  -I iface0 -I iface1
192.168.6.1:3260,1 iqn.2006-01.com.san
192.168.6.1:3260,1 iqn.2006-01.com.san
 
 
2.2.6 建立與Target的連線
[root@rac1 ifaces]# iscsiadm -m node -l
Logging in to [iface: iface1, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]
Logging in to [iface: iface0, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]
Login to [iface: iface1, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]: successful
Login to [iface: iface0, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]: successful
 
注意事項:
刪除失效的iscsi 連線:如果因為錯誤配置等原因建立了iscsi 對應,系統不會自動刪除,需手動刪除。
 
如:
[root@rac3 mapper]# iscsiadm -m node
192.168.6.1:3260,1 iqn.2006-01.com.san
192.168.6.1:3260,1 iqn.2006-01.com.san
 
我們看到上面有二個iscsi對應,加入這個對應已經失效,系統不會自動刪除。
 
刪除命名:
iscsiadm -m node -o delete -T iqn.2006-01.com.san -p 192.168.6.1:3260
 
 
2.2.7 檢視連線狀態
 
[root@rac1 ifaces]# netstat -anp |grep 3260
tcp        0      0 192.168.6.5:63327      192.168.6.1:3260     ESTABLISHED 2370/iscsid        
tcp        0      0 192.168.6.6:32380      192.168.6.1:3260     ESTABLISHED 2370/iscsid        
 
 
 
2.3 multipath的預設配置
 
Multipath的配置檔案是:/etc/multipath.conf。該檔案中大部分配置是註釋掉的,可以將他儲存為備用,然後新建一個multipath.conf 檔案,編輯新的配置檔案。
 
[root@rac1 etc]# cp multipath.conf multipath.conf.back
 
 
2.3.1 黑名單過濾
multipath會把所有裝置都加入到黑名單(devnode "*"),也就是禁止使用。所以,我們首先需要取消該設定,把配置檔案修改為類似下面的內容:
 
devnode_blacklist { 
        #devnode "*" 
        devnode "hda" 
        wwid 3600508e000000000dc7200032e08af0b 
}
 
這裡禁止使用hda,也就是光碟機。另外,還使用wwid限制使用本地的sda裝置。
 
可透過下面的命令獲得wwid:
[root@rac1 ~]# /sbin/scsi_id -g -u -s /block/sdf
14f504e46494c450034594d6462472d534745442d6a714841
 
這裡有的一個bug,具體參考:
scsi_id does not return WWID for /dev/sda with aacraid driver
 
 
這裡要有一點要注意的,就是要獲取某個裝置的wwid時,必須先確保用fdisk -l等看到這個裝置。 如果沒有看到,是獲取不到的。 這時候可以嘗試把iscsi initiator 重啟一下,網路卡禁用啟用,最後在與target 連線:iscsiadm -m node -l。
 
當fdsk -l能看到裝置了,我們可以正常獲取裝置的wwid了。
 
[root@rac1 ~]#  iscsiadm -m node -l
Logging in to [iface: iface1, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]
Logging in to [iface: iface0, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]
Login to [iface: iface1, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]: successful
Login to [iface: iface0, target: iqn.2006-01.com.san, portal: 192.168.6.1,3260]: successful
[root@rac1 ~]# fdisk -l
Disk /dev/sdf: 39.7 GB, 39795556352 bytes
64 heads, 32 sectors/track, 37952 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
 
Disk /dev/sdf doesn't contain a valid partition
 
Disk /dev/sdg: 39.7 GB, 39795556352 bytes
64 heads, 32 sectors/track, 37952 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
 
[root@rac1 ~]# /sbin/scsi_id -g -u -s /block/sdf
14f504e46494c450034594d6462472d534745442d6a714841
[root@rac1 ~]# /sbin/scsi_id -g -u -s /block/sdg
14f504e46494c450034594d6462472d534745442d6a714841
 
 
關於:scsi_id
其包含在udev程式包中,可以在multipath.conf中配置該程式來獲取scsi裝置的序號。透過序號,便可以判斷多個路徑對應了同一裝置。這個是多路徑實現的關鍵。scsi_id是透過sg驅動,向裝置傳送EVPD page80或page83 的inquery命令來查詢scsi裝置的標識。但一些裝置並不支援EVPD 的inquery命令,所以他們無法被用來生成multipath裝置。但可以改寫scsi_id,為不能提供scsi裝置標識的裝置虛擬一個識別符號,並輸出到標準輸出。
 
multipath程式在建立multipath裝置時,會呼叫scsi_id,從其標準輸出中獲得該裝置的scsi id。在改寫時,需要修改scsi_id程式的返回值為0。因為在multipath程式中,會檢查該直來確定scsi id是否已經成功得到。
 
 
2.3.2 編輯預設規則
不同的device-mapper-multipath或作業系統發行版,其預設的規則都有點不同,以紅旗 DC 5.0 SP2 for x86_64為例,其path_grouping_policy預設為failover,也就是主備的方式。HDS支援多路徑負載均衡,EMC CX300等只支援Failover。預設允許friendly_name,否則使用裝置的wwid作為持久化名稱. 我們修改預設的規則:
 
defaults {
       udev_dir                /dev
       path_grouping_policy    multibus
       failback                immediate
       no_path_retry           fail
       user_friendly_name      yes
}
 
 
2.3.3 配置multipath.conf 檔案
 
我們透過多session來訪問target。 這樣有2個裝置/dev/sdf 和 /dev/sdg。 其實他們都對應一個target。 透過上面的查詢知道,他們的wwid是一樣的。 我們就用這個wwid,把他們配置到一起,當成一個裝置。在該檔案最後新增如下內容。
 
[root@rac1 ~]#cat /etc/multipath.conf|more
multipaths {
        multipath {
wwid    14f504e46494c450034594d6462472d534745442d6a714841
alias                   rac-share
          path_grouping_policy    multibus
          path_checker            readsector0
          path_selector           "round-robin 0"
          failback                manual
          rr_weight               priorities
          no_path_retry           5
                  }
        multipath {
                wwid    1494554000000000000000000010000005c3900000d000000
                alias   voting1
        }
        multipath {
                wwid    149455400000000000000000001000000843900000d000000
                alias   voting2
        }
        multipath {
                wwid    149455400000000000000000001000000ae3900000d000000
                alias   voting3
        }
        multipath {
                wwid    149455400000000000000000001000000d03900000d000000
                alias   ocr1
                 }
         }
 
一個target 對應一個multipath. 如果有多個target 就寫多個multipath 選項。
 
 
2.3.4 重啟multipathd 服務,驗證配置
 
[root@rac1 dev]# service multipathd restart
Device /dev/sda1 not found
Command failed
Stopping multipathd daemon: [  OK  ]
Starting multipathd daemon: [  OK  ]
 
 
進入/dev/mapper 目錄進行驗證:
root@rac3 mapper]# ls -lrt /dev/mapper/*
crw------- 1 root root  10, 62 Nov  1 05:51 /dev/mapper/control
brw-rw---- 1 root disk 253,  0 Nov  1 06:20 /dev/mapper/rac-share
 
 
用 multipath -ll 命令檢視到兩條活躍路徑,他們之間互為A/A 關係。斷掉其中一根線路,那麼系統自動切換到另外一條。
[root@rac3 mapper]# multipath -ll
rac-share (14f504e46494c450034594d6462472d534745442d6a714841) dm-0 OPNFILER,VIRTUAL-DISK
[size=37G][features=1 queue_if_no_path][hwhandler=0][rw]
/_ round-robin 0 [prio=2][active]
 /_ 2:0:0:0 sdf 8:80  [active][ready]
 /_ 3:0:0:0 sdg 8:96  [active][ready]
 
 
2.3.5 將multipathd服務設定成開機自啟動
[root@rac3 mapper]# chkconfig multipathd on
 
 
2.3.6 分割槽或者建立PV
看到了我們剛才配置的多路徑的目錄。 但是這裡有一個問題。 因為這個是裝置建立的,還不能掛載。我們需要對個這個裝置進行分割槽或者建立為PV.
 
注意,用fdisk分割槽並儲存後,必須重新整理multipath的對映表,以便其建立分割槽對應的裝置符.
 
(1)分割槽
 
[root@rac3 mapper]# fdisk /dev/mapper/rac-share
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
 
The number of cylinders for this disk is set to 4838.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-4838, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-4838, default 4838):
Using default value 4838
 
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 22: Invalid argument.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
 
[root@rac3 mapper]# multipath -F   --清除多路徑裝置快取
[root@rac3 mapper]# multipath -v3  --重新載入
[root@rac3 mapper]# fdisk -l
……
Disk /dev/sdf: 39.7 GB, 39795556352 bytes
255 heads, 63 sectors/track, 4838 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdf1               1        4838    38861203+  83  Linux
 
Disk /dev/sdg: 39.7 GB, 39795556352 bytes
255 heads, 63 sectors/track, 4838 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdg1               1        4838    38861203+  83  Linux
 
Disk /dev/dm-0: 39.7 GB, 39795556352 bytes
255 heads, 63 sectors/track, 4838 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
     Device Boot      Start         End      Blocks   Id  System
/dev/dm-0p1               1        4838    38861203+  83  Linux
 
分割槽之後使用fdisk -l 命令檢視磁碟會看到磁碟下面已經有分割槽的資訊了。 這時磁碟就可以mount掛載及使用了。
 
 
(2) 建立PV
 
要配置LVM,可以按以下步驟進行:
1.   建立和初始化物理卷(Physical Volume),透過pvcreate建立pv,即pv階段;
2.   新增物理捲到卷組(Volume Group),使用vgcreate加入多個pv成為vg,即vg階段;
3.  在卷組上建立邏輯卷(logical volume),使用lvcreate劃分vg,成為一個或多個lv,即lv階段;
 
常用命令:
# pvcreate /dev/md0      #建立PV
# pvscan
# vgcreate LVM1 /dev/md0  #建立VG
# vgdisplay  LVM1  
# lvcreate -L 1.5TB -n data1 LVM1     #建立LV
# lvcreate -L 325GB -n data2 LVM1   #建立LV
# lvscan                   #檢視LV資訊
# pvscan                  #再次檢視PV資訊
# vgdisplay LVM1       #再次檢視VG資訊
 
 
掛載命令:
#mount /dev/LVM1/data1 /data1
#mount /dev/LVM1/data2 /data2
 
設定開機自動掛載:
編輯/etc/fstab
/dev/LVM1/data1         /data1                  ext3    defaults        2 2
/dev/LVM1/data2         /data2                  ext3    defaults        2 2
 
 
示例:
 
[root@rac3 mapper]# pvcreate /dev/mapper/rac-share
  Physical volume "/dev/mapper/rac-share" successfully created
[root@rac3 mapper]# vgcreate vg0 /dev/mapper/rac-share
  Volume group "vg0" successfully created
[root@rac3 mapper]# lvcreate -L 10M -n lv1 vg0
  Rounding up size to full physical extent 12.00 MB
  Logical volume "lv1" created
[root@rac3 mapper]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg0/lv1
  VG Name                vg0
  LV UUID                XkbDyS-btpZ-fIFA-MvBH-d4kl-hibU-RhuKu1
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                12.00 MB
  Current LE             3
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
[root@rac3 mapper]# mkfs.ext3 /dev/mapper/vg0-lv1   -- 格式化
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
3072 inodes, 12288 blocks
614 blocks (5.00%) reserved for the super user
First block=1
Maximum filesystem blocks=12582912
2 block groups
8192 blocks per group, 8192 fragments per group
1536 inodes per group
Superblock backups stored on blocks:
        8193
 
Writing inode tables: done                           
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
 
注意:
有部分multipath版本存在與lvm相容的問題。當使用device-mapper裝置建立lvm完成,重啟後,雖然lvm仍存在,但/dev/mapper下的裝置丟失。可以參考:
 
解決方法:
/etc/lvm/lvm.conf檔案中加入:
types=["device-mapper", 1]
come from:http://space.itpub.net/?uid-22531473-action-viewspace-itemid-742482
 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-743049/,如需轉載,請註明出處,否則將追究法律責任。

相關文章