raid 控制脚本

#!/usr/bin/env bash

version 0.2

kaka

set -x

export LC_ALL=C
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

usage() {
cat <<EOF
raid.sh: raid config tool
Usage: raid.sh [OPTION...]
-c, --clear Clear raid config
-n, --noneraid No need to create any raid
-f, --first The first raid info: 300GB,2,raid10 or 800GB,0,raid10 - 0 means all of the disks
[config size, num, level(raid0 just support each disk mode)]
-s, --second The second raid info: 2TB,5,raid5 or 2TB,0,raid5 - 0 means all of the rest disks
-t, --third The third raid info
-o, --forth The forth raid info
-i, --fifth The fifth raid info
-w, --writemode The write mode: WB - write back; WT - write throught; default is WB
-a, --cachemode The cache mode: Cached - cache mode; Direct - direct mode; default is Cached
-r, --ramode The cache mode: NORA - No read ahead; RA - read ahead; ADRA - Adaptive read ahead; default is RA
-adp, --adapter The adapter num of the Controller

Help options:
-h, --help Show this help message
EOF
}

[[ $# -lt 1 ]] && usage && exit

ARGS=$(getopt -q -o cnf:s:t:o:i:w:a:r: -l clear,noneraid:,first:,second:,third:,forth:,fifth:,writemode:,cachemode:,ramode:,adapter:,help -- "[email protected]")
[[ $? -ne 0 ]] && echo "Unknown options: $1" && exit 1
eval set -- "${ARGS}"

_writemode="WB"
_cachemode="Cached"
_ramode="RA"
_adapter="a0"

while [[ $# -gt 0 ]]
do
case "$1" in
-c|--clear)
_clr=1
;;
-n|--noraid)
_noraid=1
;;
-f|--first)
_first=$2
shift
;;
-s|--second)
_second=$2
shift
;;
-t|--third)
_third=$2
shift
;;
-o|--forth)
_forth=$2
shift
;;
-i|--fifth)
_fifth=$2
shift
;;
-w|--writemode)
_writemode=$2
shift
;;
-a|--cachemode)
_cachemode=$2
shift
;;
-r|--ramode)
_ramode=$2
shift
;;
-adp|--adapter)
_adapter=$2
shift
;;
-h|--help)
usage
;;
esac
shift
done

#######################################

Get the info of all the disks

#######################################
MEGACLI=/opt/MegaRAID/MegaCli/MegaCli64
ENCLOSURE=$($MEGACLI -EncInfo -aAll | awk ‘/Device ID/ { print $NF }‘ | sed q)
DISKNUM=$($MEGACLI -EncInfo -aAll | awk ‘/Number of Physical Drives/ { print $NF }‘ | sed q)

declare -a disks_cfgsize
declare -a disks_unit
declare -a disks_esid
x=1
for((i=1;i<=$DISKNUM;i++))
do
e_id=$MEGACLI -pdlist -aall |grep "Enclosure Device ID" | head -n $i | tail -n 1 | awk ‘{print $4}‘
s_id=$MEGACLI -pdlist -aall |grep "Slot Number" | head -n $i | tail -n 1 | awk ‘{print $3}‘
disk_cfgsize_raw=$MEGACLI -pdInfo -PhysDrv[$e_id:$s_id] -aALL|grep "Raw Size"|awk ‘{print $3}‘
disk_size_unit=/opt/MegaRAID/MegaCli/MegaCli64 -pdInfo -PhysDrv[$e_id:$s_id] -aALL|grep "Raw Size"|awk ‘{print $4}‘

[ "$disk_size_unit" = "GB" ] && disk_cfgsize=`echo "scale=0; $disk_cfgsize_raw/100*100" | bc`
[ "$disk_size_unit" = "TB" ] && disk_cfgsize=`echo "scale=1; $disk_cfgsize_raw/1" | bc`

disks_cfgsize[$x]=$disk_cfgsize
disks_unit[$x]=$disk_size_unit
disks_esid[$x]=$e_id:$s_id

let x++

done
###################################

Done

###################################

parameter_verify(){
local size=$1
local num=$2
local level=$3

# all can‘t be null
[ -z "$size" -o -z "$num" -o -z "$level" ] && echo "All parameters cann‘t be empty." && return 1

# just support raid0(each disk), raid1, raid10, raid5 so far
[ $level != "raid0" -a $level != "raid5" -a $level != "raid10" -a $level != "raid1" -a $level != "raid50" -a $level != "raid60" ] && echo "Just support raid0, raid1, raid10, raid5, raid50, raid60 so far" && return 2

return 0

}

makeraid0(){
echo "Make raid0"
declare -a disks
local disks_txt=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $1}‘
local write_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $1}‘
local cache_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $2}‘
local ra_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $3}‘

local i=1;
for disk in `echo $disks_txt` ; do disks[$i]=$disk; let i=i+1; done

local write="WB"
local cache="Cached"
local ramode="RA"
[ "$write_mode" = "WT" ] && write="WT"
[ "$cache_mode" = "Direct" ] && cache="Direct"
[ "$ra_mode" = "NORA" ] && ramode="NORA"
[ "$ra_mode" = "ADRA" ] && ramode="ADRA"

for((i=1;i<=${#disks[@]};i++))
do
    [ ! -z "${disks[$i]}" ] && $MEGACLI -CfgLdAdd -r0 [${disks[$i]}] $write $cache $ramode -$adapter || return 1
    disks[$i]=""
done

}

makeraid50(){
echo "Make raid50"
declare -a disks
local disks_txt=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $1}‘
local write_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $1}‘
local cache_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $2}‘
local ra_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $3}‘

local i=1;
for disk in `echo $disks_txt` ; do disks[$i]=$disk; let i=i+1; done

local write="WB"
local cache="Cached"
local ramode="RA"
[ "$write_mode" = "WT" ] && write="WT"
[ "$cache_mode" = "Direct" ] && cache="Direct"
[ "$ra_mode" = "NORA" ] && ramode="NORA"
[ "$ra_mode" = "ADRA" ] && ramode="ADRA"

disknum=${#disks[@]}
[ $disknum -lt 6 ] && echo "Disk num cann‘t be less then 6." && return 2

let raid0_num=disknum/2
let raid1_begin=raid0_num+1
_parameter=$(
echo -n "-Array0["
for((i=1;i<=$raid0_num;i++))
do
    echo -n ${disks[$i]},
done | sed ‘s/,$//‘
echo -n "] "
echo -n "-Array1["
for((i=$raid1_begin;i<=$disknum;i++))
do
    echo -n ${disks[$i]},
done | sed ‘s/,$//‘
echo -n "]"
)
$MEGACLI -CfgSpanAdd -r50 $_parameter $write $cache $ramode -a0

}

makeraid10(){
echo "Make raid10"
declare -a disks
local disks_txt=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $1}‘
local write_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $1}‘
local cache_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $2}‘
local ra_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $3}‘

local i=1;
for disk in `echo $disks_txt` ; do disks[$i]=$disk; let i=i+1; done

local write="WB"
local cache="Cached"
local ramode="RA"
[ "$write_mode" = "WT" ] && write="WT"
[ "$cache_mode" = "Direct" ] && cache="Direct"
[ "$ra_mode" = "NORA" ] && ramode="NORA"
[ "$ra_mode" = "ADRA" ] && ramode="ADRA"

disknum=${#disks[@]}
let disknum=disknum/2
_parameter=$(
for((i=1;i<=$disknum;i++))
do
    echo -n "-Array${i}[${disks[((i*2-1))]},${disks[((i*2))]}]"
done)
$MEGACLI -CfgSpanAdd -r10 $_parameter $write $cache $ramode -a0

}

makeraid5(){
echo "Make raid5"
declare -a disks
local disks_txt=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $1}‘
local write_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $1}‘
local cache_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $2}‘
local ra_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $3}‘

local i=1;
for disk in `echo $disks_txt` ; do disks[$i]=$disk; let i=i+1; done

local write="WB"
local cache="Cached"
local ramode="RA"
[ "$write_mode" = "WT" ] && write="WT"
[ "$cache_mode" = "Direct" ] && cache="Direct"
[ "$ra_mode" = "NORA" ] && ramode="NORA"
[ "$ra_mode" = "ADRA" ] && ramode="ADRA"

_parameter=$(
for((i=1;i<=${#disks[@]};i++))
do
    echo -n ${disks[$i]},
done | sed ‘s/,$//‘)
$MEGACLI -CfgLdAdd -r5 [$_parameter] $write $cache $ramode -a0

}

makeraid1(){
echo "Make raid1"
declare -a disks
local disks_txt=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $1}‘
local write_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $1}‘
local cache_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $2}‘
local ra_mode=echo "[email protected]" | awk -F‘ENDDISK‘ ‘{print $2}‘ | awk ‘{print $3}‘

local i=1;
for disk in `echo $disks_txt` ; do disks[$i]=$disk; let i=i+1; done

local write="WB"
local cache="Cached"
local ramode="RA"
[ "$write_mode" = "WT" ] && write="WT"
[ "$cache_mode" = "Direct" ] && cache="Direct"
[ "$ra_mode" = "NORA" ] && ramode="NORA"
[ "$ra_mode" = "ADRA" ] && ramode="ADRA"

_parameter=$(
for((i=1;i<=${#disks[@]};i++))
do
    echo -n ${disks[$i]},
done | sed ‘s/,$//‘)
$MEGACLI -CfgLdAdd -r1 [$_parameter] $write $cache $ramode -a0

}

clearcfg() {
echo "clear"
$MEGACLI -CfgClr -aALL

make all disks good

for((i=1;i<=${#disks_cfgsize[@]};i++)); do
    [ "${disks_esid[$i]}" != "" ] && $MEGACLI -PDMakeGood -PhysDrv[${disks_esid[$i]}] -force -a0 1>/dev/null 2>/dev/null
done
sleep 2s

}

diskinit() {
$MEGACLI -LDinit -start -L0 -a0
}

[[ "$_clr" -eq 1 ]] && clearcfg

[[ $_noraid -eq 1 ]] && echo "no need to create any raid." && exit 0

create_raid(){
local size=echo $1 | awk -F‘,‘ ‘{print $1}‘
local num=echo $1 | awk -F‘,‘ ‘{print $2}‘
local level=echo $1 | awk -F‘,‘ ‘{print $3}‘

# verify the paramters
parameter_verify "$size" "$num" "$level" || exit 1

# When num is zero, all disks to be one raid and exit
if [ $num -eq 0 ] ; then
    echo "All disks will be one raid group."

    declare -a disks_esid_notnull
    for((i=1;i<=${#disks_cfgsize[@]};i++)); do
        [ "${disks_esid[$i]}" != "" ] && disks_esid_notnull[$x]=${disks_esid[$i]} && disks_esid[$i]=""  && let x++
    done

    make${level} ${disks_esid_notnull[@]} "ENDDISK" "$_writemode" "$_cachemode" "$_ramode"
    if [ $? -ne 0 ] ;then echo "Make $level failed"; exit 1; fi

    diskinit
    exit 0
else
    echo "Create $level($num disks with size $size)"

    # find the disks with the specified config size
    declare -a raid_disks
    x=1
    for((i=1;i<=${#disks_cfgsize[@]};i++)); do
        [ ${disks_cfgsize[$i]}${disks_unit[$i]} = "$size" ] && [ "${disks_esid[$i]}" != "" ] && raid_disks[$x]=${disks_esid[$i]} && disks_esid[$i]=""  && let x++
        [ $x -gt $num ] && break
    done

    [ ${#raid_disks[@]} -ne $num ] && echo "No enought disks with config size $size to create the RAID." && exit 1

    make${level} ${raid_disks[@]} "ENDDISK" "$_writemode" "$_cachemode" "$_ramode"
    if [ $? -ne 0 ] ;then echo "Make $level failed"; exit 2; fi
fi

}

if [ -n "$_first" ] ; then
create_raid "$_first"
fi

if [ -n "$_second" ] ; then
create_raid "$_second"
fi

if [ -n "$_third" ] ; then
create_raid "$_third"
fi

if [ -n "$_forth" ] ; then
create_raid "$_forth"
fi

if [ -n "$_fifth" ] ; then
create_raid "$_fifth"
fi

if [ $MEGACLI -pdlist -aall |grep Adapter |wc -l -eq 2 ]
then
num=$MEGACLI -pdlist -aall |grep "Enclosure Device ID"|wc -l
id=$MEGACLI -pdlist -aall |grep "Enclosure Device ID" |awk ‘{print $4}‘|sed -n 1p
id_2=$MEGACLI -pdlist -aall |grep "Enclosure Device ID" |awk ‘{print $4}‘|sed -n "$num"p
disk_num=$MEGACLI -pdlist -aall |grep "Enclosure Device ID" |awk ‘{print $4}‘|wc -l
disk_bnum=$MEGACLI -pdlist -aall |grep -w "Coerced Size" |grep -v "Non"|wc -l
disk_b=$MEGACLI -pdlist -aall |grep -w "Coerced Size" |grep -v "Non"|sed -n "$disk_bnum"p|awk ‘{print $3}‘
create_raid $disk_b $disk_num raid0
fi
diskinit
exit 0

原文地址:https://blog.51cto.com/12768454/2479156

时间: 2024-11-01 13:09:28

raid 控制脚本的相关文章

Centos 6下软raid操作脚本

#!/bin/sh main () { clear echo '          ------------------------------------------------------          ' echo '          1.Create a software raid array          ' echo '          2.View raid array sync status           ' echo '          3.View rai

利用BashShell编写Nginx服务控制脚本

使用源码包安装的Nginx没办法使用"service nginx start"或"/etc/init.d/nginx start"进行操作和控制,所以写了以下的服务控制脚本. 可以使用的选项有: start  启动 stop  停止 reload 重载 restart  重启 status   状态 test 检查配置文件 1.创建脚本文件并添加执行权限 # touch /etc/init.d/nginx # chmod +x /etc/init.d/nginx 2

linux下nginx启动停止重启控制脚本

这是控制nginx服务的脚本文件,包括控制nginx的启动.重启.停止.平滑重启.对配置文件的额检查. [[email protected] ~]# cat nginx.sh #!/bin/env bash # description:nginx server   ###必须加描述 # nginx - this script is used to control nginx service # processname nginx # chkconfig: - 85 15 # edit by su

戴尔PowerEdge RAID控制卡使用示例(PERC H710P为例)

Dell PERC使用示例列表(H710p) 特别说明,本文相关RAID的操作,仅供网友在测试环境里学习和理解戴尔PowerEdge服务器RAID控制卡的功能和使用方法.切勿直接在生产服务器上做相关实验,这可能有误操作并造成数据丢失的风险! 示例演示环境:PowerEdge R620 + H710p Raid控制卡  + 9 x 300G 10k SAS 硬盘 PERC卡的基本使用技巧 PERC卡RAID配置信息的初始化 : 戴尔PowerEdge服务器RAID控制卡的配置,可以使用戴尔提供的多

运维经验分享(七)-- Linux Shell之ChatterServer服务控制脚本第三次优化

运维经验分享作为一个专题,目前共7篇文章 <运维经验分享(一)-- Linux Shell之ChatterServer服务控制脚本> <运维经验分享(二)-- Linux Shell之ChatterServer服务控制脚本二次优化> <运维经验分享(三)-- 解决Ubuntu下crontab不能正确执行Shell脚本的问题(一)> <运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析> <运维经验分享(五)-- 改进的java进程管

运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析

运维经验分享作为一个专题,目前共7篇文章 <运维经验分享(一)-- Linux Shell之ChatterServer服务控制脚本> <运维经验分享(二)-- Linux Shell之ChatterServer服务控制脚本二次优化> <运维经验分享(三)-- 解决Ubuntu下crontab不能正确执行Shell脚本的问题(一)> <运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析> <运维经验分享(五)-- 改进的java进程管

运维经验分享(五)-- 改进的java进程管理的服务控制脚本

运维经验分享作为一个专题,目前共7篇文章 <运维经验分享(一)-- Linux Shell之ChatterServer服务控制脚本> <运维经验分享(二)-- Linux Shell之ChatterServer服务控制脚本二次优化> <运维经验分享(三)-- 解决Ubuntu下crontab不能正确执行Shell脚本的问题(一)> <运维经验分享(四)--关于 java进程管理的服务控制脚本编程思路分析> <运维经验分享(五)-- 改进的java进程管

Linux Shell之ChatterServer服务控制脚本二次优化

最近开发人员提出新的需求,想将ChatterServer服务控制脚本放到ChatterServer根目录下的bin目录下,并向一次性可以部署多个实例,因此这一个需求引发了多个改变: 1.不再使用service进行控制,仍然保留status.start.stop和restart功能 2.BASEDIR不再使用绝对路径而要使用相对路径,例如BASEDIR=.. 3.SERVICEPORT要从ChatterServer根目录下的conf目录中的文件中读取 4.日志目录logs将不再手动创建,控制脚本需

Hadoop中的控制脚本

1.提出问题 在上篇博文中,提到了为什么要配置ssh免密码登录,说是Hadoop控制脚本依赖SSH来执行针对整个集群的操作,那么Hadoop中控制脚本都是什么东西呢?具体是如何通过SSH来针对整个集群的操作?网上完全分布模式下Hadoop的搭建很多,可是看完后,真的了解吗?为什么要配置Hadoop下conf目录下的masters文件和slaves文件,masters文件里面主要记录的是什么东西,slaves文件中又记录的是什么东西,masters文件和slaves文件都有什么作用?好,我看到过一