您的位置:首頁技術文章
文章詳情頁

利用 UNIX 腳本來管理 DS4000 磁盤陣列系統

瀏覽:23日期:2024-06-13 11:28:15

為什么使用 Unix 腳本來管理 DS4000

作為一個測試工程師,在我的日常工作中我經常要花費大量的時間去完成一些很簡單但是很耗時的工作。例如,一次我想在 DS4000 上創建 200 個磁盤。但是由于 DS4000 沒有能夠批量創建磁盤的功能,我花費了 4 個小時一個一個地去創建。這次的經歷使我有一種想法去創建一個能夠可以批量地創建磁盤的工具。經過我一段時間的研究,我發現 IBM Storage Manager 支持 CLI(Command Line Interface)功能,這就意味著我可以利用 UNIX 系統的腳本功能來創建一個可以被 IBM Storage Manager 使用的腳本。通過在 IBM Storage Manager 運行這個被創建的腳本,可以在無需人機交互的情況下批量的創建磁盤。但是作者并不保證本方法在任何情況下總是可以成功,請讀者在驗證本文的例子時注意做好相應的備份,最好在實驗環境上進行操作。

例子腳本介紹

那么如何創建 IBM Storage Manager 可使用的腳本呢?IBM Storage Manager 有它自己的腳本格式。只有腳本的變量值改變而格式并不改變。所以,UNIX 腳本需要能夠讓用戶能夠定制的輸入自己的所對應的值,如需要創建磁盤的數量,HBA 卡的 WWPNs 值,來生成 IBM Storage Manager 能夠使用的腳本文件。可以設計一個配置文件來讓用戶輸入自己對應的變量值。下面就是個配置文件的例子:LunNumber:30 #30 個 LUNsArrayNumber:1 # 這些 LUNs 創建在 array 1 上LunName:sunx325b # 這些 LUNs 的名字將為 sunx325b_0, sunx325b_1 ...Capacity:0.5GB # 每個 LUNs 的容量是 0.5GBHostGroupName:sunx325b_group # 這個主機組的名字為 sunx325b_groupHostName:sunx325b # 這個主機名為 sunx325bPort0:210000e08b0e934d # 第一個 HBA 卡的 wwpn 值Port1:210000e08b0e6e50 # 第二個 HBA 卡的 wwpn 值HostType:8 # 主機類型

完成了對配置文件的定義以后,Unix 腳本可以把配置文件中用戶定義的值解析出來,并且組織成為 IBM Storage Manager 可以使用的腳本。一個比較好的解析配置文件的工具是 SED 編輯器,這是一個流式的編輯器。SED 編輯器將需要處理的文本作為輸入,經過一系列操作以后,輸出經過修改的文本。SED 編輯器的典型應用就是通過模式的匹配來提取文件中的一部分內容或者是把文件中的多個相同的字符串進行替換。UNIX 腳本可以通過使用 SED 編輯器從配置文件中得到需要的數值,并把這些值添加到能夠被 IBM Storage Manager 使用的腳本中去。

例如,下面展示了如何使用 UNIX 腳本從配置文件中獲取磁盤數量這個值。這個腳本從配置文件獲取 LunNumber 這個值,然后把這個值賦給腳本中的對應變量 LunNumber。sed -n '/LunNumber:/'p $ConfigFile >tmplunNumber=`sed 's/LunNumber://' tmp`

例子腳本的詳細說明

下面的例子是展示如何使用 SED 編輯器生成一個 IBM Storage Manager 可以使用的腳本。這個例子中,腳本從配置文件中獲得了 ArrayNumber,LunName 和 Capacity 的值,用于生成 IBM Storage Manager 使用的生成磁盤的命令,然后把這個命令添加到 IBM Storage Manager 的腳本(crtlun.cmd)文件。x=0while [ $x -lt $lunNumber ]doy=`expr $x % 2`if [ $y -eq 0 ]thenecho "create logicalDrive array=$arrayNumber userLabel="${lunName}_${x}"capacity=$capacity owner=a;" >> crtlun.cmdelseecho "create logicalDrive array=$arrayNumber userLabel="${lunName}_${x}"capacity=$capacity owner=b;" >> crtlun.cmdfix=`expr $x + 1`done

這個 Unix 工具的內容是不是很簡單?但是它卻幫助了我把原來需要幾個小時的工作減少為只需要幾分鐘就可以完成。下面讓我對我的 UNIX 腳本文件的內容做個比較詳細的介紹。這個名字為 ds4kscript.sh 的腳本文件可以運行在 UNIX(AIX,Solaris,Linux 等)上,用于產生一個可以在 IBM Storage Manager 上運行的腳本。它可以用于在 DS4000 系列上創建和刪除 Lun、host group、host、host port,映射 Lun 等功能。

這個腳本的輸入是配置文件:config。它保存這用戶自定義的配置信息。這個腳本的輸出是一系列的腳本(這個功能在后面會加以介紹):crtlun.cmd, crthost.cmd, maplun.cmd, delmap.cmd, delhost.cmd, dellun.cmd, setup.cmd, destroy.cmd 支持的操作系統:AIX,Solaris 和 Linux

腳本使用說明

編輯 config 文件,設置參數為用戶所需要的值。下面是一個例子,顯示了 config 文件需要包含那些項以及一些說明:LunNumber:30 #30 個 LUNsArrayNumber:1 # 這些 LUNs 創建在 array 1 上LunName:sunx325b # 這些 LUNs 的名字將為 sunx325b_0, sunx325b_1 ...Capacity:0.5GB # 每個 LUNs 的容量是 0.5GBHostGroupName:sunx325b_group # 這個主機組的名字為 sunx325b_groupHostName:sunx325b # 這個主機名為 sunx325bPort0:210000e08b0e934d # 第一個 HBA 卡的 wwpn 值Port1:210000e08b0e6e50 # 第二個 HBA 卡的 wwpn 值HostType:8 # 主機類型

復制 ds4kscript.sh 到和 config 相同的目錄下,執行:chmod +x ds4kscript.sh (使用戶對 ds4kscript.sh 具有執行權限)./ds4kscript.sh ( 執行 ds4kscript.sh)

在 ds4kscript.sh 運行的目錄下,一系列文件將會被創建出來:crtlun.cmd, crthost.cmd, maplun.cmd, delmap.cmd, delhost.cmd, dellun.cmd, setup.cmd, destroy.cmd。下面逐一對這些文件的功能進行描述:

創建 Lun、host group、host、host port,映射 Lun。它包括了 crtlun.cmd, crthost.cmd, maplun.cmd 的所有功能,算是一個功能集合的腳本。

創建 Luns;

創建 host group、host、host port;

映射端口到 host 上;

刪除 Lun、host group、host、host port,映射 Lun。它包括了 delmap.cmd, delhost.cmd, dellun.cmd 的所有功能,算是一個功能集合的腳本;

刪除 Lun 映射

刪除 host group、host、host port

刪除 Lun

復制 setup.cmd 的內容到 IBM Storage Manager 腳本編輯器中,然后運行它?;蛘哂脩粢部梢詥为毜膹椭?crtlun.cmd, crthost.cmd, maplun.cmd 中的內容,來實現單獨的一些功能。這時 Lun 已經在 DS4000 上建立好了,同時也映射到了主機上。

當然,同時可以執行 destroy.cmd 內的內容來進行刪除操作,或者單獨地執行 delmap.cmd, delhost.cmd, dellun.cmd 內的內容來清除剛才執行的創建操作。

下面是 ds4kscript.sh 的詳細內容,請注意其中的注解內容,會幫助您的理解:下面是 ds4kscript.sh 的詳細內容,請注意其中的注解內容,會幫助您的理解:#ConfigFile=./configProgramName=./ds4kscript.shif [ -f $ConfigFile ]thenecho "OK" >> /dev/nullelseecho "$ConfigFile not exist!"exit -1fi# 從配置文件中依次獲取參數值sed -n '/LunNumber:/'p $ConfigFile >tmplunNumber=`sed 's/LunNumber://' tmp`echo lunNumber=$lunNumbersed -n '/ArrayNumber:/'p $ConfigFile >tmparrayNumber=`sed 's/ArrayNumber://' tmp`echo arrayNumber=$arrayNumbersed -n '/LunName:/'p $ConfigFile >tmplunName=`sed 's/LunName://' tmp`echo lunName=$lunNamesed -n '/Capacity:/'p $ConfigFile >tmpcapacity=`sed 's/Capacity://' tmp`echo capacity=$capacitysed -n '/HostGroupName:/'p $ConfigFile >tmphostGroupName=`sed 's/HostGroupName://' tmp`echo hostGroupName=$hostGroupNamesed -n '/HostName:/'p $ConfigFile >tmphostName=`sed 's/HostName://' tmp`echo hostName=$hostNamesed -n '/Port0:/'p $ConfigFile >tmpport0=`sed 's/Port0://' tmp`echo port0=$port0sed -n '/Port1:/'p $ConfigFile >tmpport1=`sed 's/Port1://' tmp`echo port1=$port1sed -n '/HostType:/'p $ConfigFile >tmphostType=`sed 's/HostType://' tmp`echo hostType=$hostType# 創建 IBM storage manager 使用的腳本中創建 Lun 的部分,并保存為 crtlun.cmd。if [ -f crtlun.cmd ]thenrm crtlun.cmdelseecho "OK" >> /dev/nullfix=0while [ $x -lt $lunNumber ]doy=`expr $x % 2`if [ $y -eq 0 ]thenecho "create logicalDrive array=$arrayNumber userLabel="${lunName}_${x}"capacity=$capacity owner=a;" >> crtlun.cmdelseecho "create logicalDrive array=$arrayNumber userLabel="${lunName}_${x}"capacity=$capacity owner=b;" >> crtlun.cmdfix=`expr $x + 1`done# 創建 IBM storage manager 使用的腳本中創建 host group,host 和 host port 的部分,并保存為 crthost.cmd。if [ -f crthost.cmd ]thenrm crthost.cmdelseecho "OK" >> /dev/nullfiecho "create hostGroup userLabel="$hostGroupName";" >>crthost.cmdecho "create host userLabel = "$hostName" hostGroup = "$hostGroupName";" >>crthost.cmdecho "create hostPort identifIEr = "$port0" userLabel = "${hostName}_hba0"host = "$hostName" hostType = $hostType;" >>crthost.cmdecho "create hostPort identifier = "$port1" userLabel = "${hostName}_hba1"host = "$hostName" hostType = $hostType;" >>crthost.cmd# 創建 IBM storage manager 使用的腳本中映射 Lun 的部分,并保存為 maplun.cmd。if [ -f maplun.cmd ]thenrm maplun.cmdelseecho "OK" >> /dev/nullfix=0while [ $x -lt $lunNumber ]doecho "set logicalDrive ["${lunName}_${x}"] logicalUnitNumber=$xhostGroup="$hostGroupName";" >>maplun.cmdx=`expr $x + 1`done# 創建 IBM storage manager 使用的腳本中刪除 Lun 映射的部分,并保存為 delmap.cmd。if [ -f delmap.cmd ]thenrm delmap.cmdelseecho "OK" >> /dev/nullfix=0while [ $x -lt $lunNumber ]doecho "remove logicalDrive ["${lunName}_${x}"] lunMappinghostGroup="$hostGroupName";" >>delmap.cmdx=`expr $x + 1`done# 創建 IBM storage manager 使用的腳本中刪除 Lun 的部分,并保存為 dellun.cmd。if [ -f dellun.cmd ]thenrm dellun.cmdelseecho "OK" >> /dev/nullfix=0while [ $x -lt $lunNumber ]doecho "delete logicalDrive ["${lunName}_${x}"];" >>dellun.cmdx=`expr $x + 1`done# 創建 IBM storage manager 使用的腳本中刪除 host port 的部分,并保存為 delhost.cmd。if [ -f delhost.cmd ]thenrm delhost.cmdelseecho "OK" >> /dev/nullfiecho "delete hostPort ["$port0"];" >>delhost.cmdecho "delete hostPort ["$port1"];" >>delhost.cmdecho "delete host ["$hostName"];" >>delhost.cmdecho "delete hostGroup ["$hostGroupName"];" >>delhost.cmd# 創建 IBM storage manager 使用的 2 個功能集合腳本,并分別保存為 setup.cmd 和 destroy.cmd。if [ -f setup.cmd ]thenrm setup.cmdelseecho "OK" >> /dev/nullficat crtlun.cmd >>setup.cmdcat crthost.cmd >>setup.cmdcat maplun.cmd >>setup.cmdif [ -f destroy.cmd ]thenrm destroy.cmdelseecho "OK" >> /dev/nullficat delmap.cmd >>destroy.cmdcat delhost.cmd >>destroy.cmdcat dellun.cmd >>destroy.cmd

總結

從上面可以看出,整個腳本使用的技術不是很復雜,主要是利用 SED 編輯器通過模式匹配從配置文件中提取出所需要的部分,然后從新組織起來,形成一定的符合 IBM storage manager 使用的格式。其中還利用了一些簡單的正則表達式,這在很多系統管理的腳本中很常見,可見功能的實效性。當然,作為 SED 這個強大的編輯器,功能遠遠不止這些,這里也是希望能通過我的一些實踐和大家分享一些使用腳本的心得。最終的目的還是提高工作的效率,哪一個管理員或是相關的工程師也不希望總是做一些沒有技術含量而且重復性很大的工作,所以腳本以及 SED 編輯器,Awk 等工具都是各位很好的幫手。下面看一下所生成的兩個功能集合腳本的內容,體驗一下成果的滋味。

setup.cmd

create logicalDrive array=1 userLabel="sunx325b_0" capacity=0.5GB owner=a;create logicalDrive array=1 userLabel="sunx325b_1" capacity=0.5GB owner=b;create logicalDrive array=1 userLabel="sunx325b_2" capacity=0.5GB owner=a;create logicalDrive array=1 userLabel="sunx325b_3" capacity=0.5GB owner=b;create logicalDrive array=1 userLabel="sunx325b_4" capacity=0.5GB owner=a;....create logicalDrive array=1 userLabel="sunx325b_26" capacity=0.5GB owner=a;create logicalDrive array=1 userLabel="sunx325b_27" capacity=0.5GB owner=b;create logicalDrive array=1 userLabel="sunx325b_28" capacity=0.5GB owner=a;create logicalDrive array=1 userLabel="sunx325b_29" capacity=0.5GB owner=b;create hostGroup userLabel="sunx325b_group";create host userLabel = "sunx325b" hostGroup = "sunx325b_group";create hostPort identifIEr = "210000e08b0e934d" userLabel = "sunx325b_hba0"host = "sunx325b" hostType = 8;create hostPort identifier = "210000e08b0e6e50" userLabel = "sunx325b_hba1"host = "sunx325b" hostType = 8;set logicalDrive ["sunx325b_0"] logicalUnitNumber=0 hostGroup="sunx325b_group";set logicalDrive ["sunx325b_1"] logicalUnitNumber=1 hostGroup="sunx325b_group";set logicalDrive ["sunx325b_2"] logicalUnitNumber=2 hostGroup="sunx325b_group";set logicalDrive ["sunx325b_3"] logicalUnitNumber=3 hostGroup="sunx325b_group";set logicalDrive ["sunx325b_4"] logicalUnitNumber=4 hostGroup="sunx325b_group";....set logicalDrive ["sunx325b_25"] logicalUnitNumber=25 hostGroup="sunx325b_group";set logicalDrive ["sunx325b_26"] logicalUnitNumber=26 hostGroup="sunx325b_group";set logicalDrive ["sunx325b_27"] logicalUnitNumber=27 hostGroup="sunx325b_group";set logicalDrive ["sunx325b_28"] logicalUnitNumber=28 hostGroup="sunx325b_group";set logicalDrive ["sunx325b_29"] logicalUnitNumber=29 hostGroup="sunx325b_group";

destroy.cmd

remove logicalDrive ["sunx325b_0"] lunMapping hostGroup="sunx325b_group";remove logicalDrive ["sunx325b_1"] lunMapping hostGroup="sunx325b_group";remove logicalDrive ["sunx325b_2"] lunMapping hostGroup="sunx325b_group";remove logicalDrive ["sunx325b_3"] lunMapping hostGroup="sunx325b_group";remove logicalDrive ["sunx325b_4"] lunMapping hostGroup="sunx325b_group";....remove logicalDrive ["sunx325b_25"] lunMapping hostGroup="sunx325b_group";remove logicalDrive ["sunx325b_26"] lunMapping hostGroup="sunx325b_group";remove logicalDrive ["sunx325b_27"] lunMapping hostGroup="sunx325b_group";remove logicalDrive ["sunx325b_28"] lunMapping hostGroup="sunx325b_group";remove logicalDrive ["sunx325b_29"] lunMapping hostGroup="sunx325b_group";delete hostPort ["210000e08b0e934d"];delete hostPort ["210000e08b0e6e50"];delete host ["sunx325b"];delete hostGroup ["sunx325b_group"];delete logicalDrive ["sunx325b_0"];delete logicalDrive ["sunx325b_1"];delete logicalDrive ["sunx325b_2"];delete logicalDrive ["sunx325b_3"];delete logicalDrive ["sunx325b_4"];delete logicalDrive ["sunx325b_5"];....delete logicalDrive ["sunx325b_25"];delete logicalDrive ["sunx325b_26"];delete logicalDrive ["sunx325b_27"];delete logicalDrive ["sunx325b_28"];delete logicalDrive ["sunx325b_29"];

標簽: Unix系統
国产综合久久一区二区三区