#!/usr/bin/env bash
version 0.2
kaka
set -x
export LC_ALL=C
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
_writemode="WB"
_cachemode="Cached"
_ramode="RA"
_adapter="a0"
#######################################
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
function disks_sort(){
rm -fr /tmp/disks_size.log >/dev/null 2>&1
for i in ${disks_cfgsize[*]};do echo "$i" >> /tmp/disks_size.log;done
num=sort -n /tmp/disks_size.log |uniq -c|wc -l
for i in seq $num
do
number=`sort -n /tmp/disks_size.log |uniq -c|sed -n "$i"p|awk '{print $1}'`
value=`sort -n /tmp/disks_size.log |uniq -c|sed -n "$i"p|awk '{print $2}'`
for j in `seq $number`
do
eval disk_$i[$j]=$value
done
if [ $number -ge 4 ]
then
let num_raid=$number-1
create_raid $value,$num_raid,raid5
create_raid $value,1,raid1
elif [ $number -eq 3 ]
then
let num_raid_3=$number-1
create_raid $value,$num_raid_3,raid1
create_raid $value,1,raid1
elif [ $number -eq 2 ]
then
num_raid_2=2
create_raid $value,$num_raid_2,raid1
else
create_raid $value,1,raid0
fi
done
}
parameter_verify(){
local size=$1
local num=$2
local level=$3
[ -z "$size" -o -z "$num" -o -z "$level" ] && echo "All parameters cann't be empty." && return 1
[ $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 "$@" | awk -F'ENDDISK' '{print $1}'
local write_mode=echo "$@" | awk -F'ENDDISK' '{print $2}' | awk '{print $1}'
local cache_mode=echo "$@" | awk -F'ENDDISK' '{print $2}' | awk '{print $2}'
local ra_mode=echo "$@" | 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
}
makeraid5(){
echo "Make raid5"
declare -a disks
local disks_txt=echo "$@" | awk -F'ENDDISK' '{print $1}'
local write_mode=echo "$@" | awk -F'ENDDISK' '{print $2}' | awk '{print $1}'
local cache_mode=echo "$@" | awk -F'ENDDISK' '{print $2}' | awk '{print $2}'
local ra_mode=echo "$@" | 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 "$@" | awk -F'ENDDISK' '{print $1}'
local write_mode=echo "$@" | awk -F'ENDDISK' '{print $2}' | awk '{print $1}'
local cache_mode=echo "$@" | awk -F'ENDDISK' '{print $2}' | awk '{print $2}'
local ra_mode=echo "$@" | 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
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}'
echo $size,$num,$level
parameter_verify "$size" "$num" "$level" || exit 1
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)"
declare -a raid_disks
x=1
for((i=1;i<=${#disks_cfgsize[@]};i++)); do
[ ${disks_cfgsize[$i]} = "$size" ] && [ "${disks_esid[$i]}" != "" ] && raid_disks[$x]=${disks_esid[$i]} && disks_esid[$i]="" && let x++
[ $x -gt $num ] && break
done
echo "--------",${#raid_disks[@]} ,$num
[ ${#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
}
disks_sort
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