Files
new_install/operation/mkraid.sh
2010-09-10 10:19:10 +08:00

302 lines
8.5 KiB
Bash
Executable File

#! /bin/bash
############################################################################
# DESCRIPTION: makeraid for the 'install' of linx, by using command of mdadm
# , this shell only support RAID0 1 5 and 10, others will
# report false. if someone need other level, please modify.
#
# SCRIPT NAME: mkraid.sh
#
# argument:
#
# return value: 7 create or assimble raid device lose
# 6 incorrect active and spare devices name, maybe it has
# been used
# 5 level0 don't need spare devices
# 4 this shell not support this RAID level
# 3 mdX has exist
# 2 this levelY need more devices
# 1 error argument from user
# 0 make raid allright
#
# AUTHOR: ZR_Lang
#
# EMALL: zrlang@linx-info.com
#
# DATE: 2010-08-23
#
# HISTORY:
# REVISOR DATE MODIFICATION
# ZR_Lang 2010-08-23 create
###########################################################################
#global value can be modified for some exchange
LOGDIR="/var/log/"
LOGFILE="mkraid.log"
ERRNO="" #error number of return value
RAIDNAME=""
RAIDLEVEL=""
ACTIVELIST=""
SPARELIST=""
ACTIVENUM=""
SPARENUM=""
DEVICELIST=""
###########################################################################
# Function Name: setArgument()
# Input: $@
# Return: nothing
# Description: set the corresponding argument from the stdin
###########################################################################
setArgument(){
local i
local ARGV=($(getopt -l name:,level:,active:,spare: -o n:,l:,a:,s: -- $@ 2>>${LOGDIR}${LOGFILE}))
for((i=0; i<${#ARGV[@]}; i++))
do
case ${ARGV[$i]} in
--name)
((i++))
RAIDNAME=${ARGV[$i]}
;;
--level)
((i++))
RAIDLEVEL=${ARGV[$i]}
;;
--active)
((i++))
ACTIVELIST=${ARGV[$i]}
;;
--spare)
((i++))
SPARELIST=${ARGV[$i]}
;;
-n)
((i++))
RAIDNAME=${ARGV[$i]}
;;
-l)
((i++))
RAIDLEVEL=${ARGV[$i]}
;;
-a)
((i++))
ACTIVELIST=${ARGV[$i]}
;;
-s)
((i++))
SPARELIST=${ARGV[$i]}
;;
--)
;;
*)
echo "error argument, please use help message and modify it" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
ERRNO="1"
exit $ERRNO
;;
esac
done
if [ ! $ACTIVELIST ];then
ACTIVENUM=0
else
ACTIVENUM=`echo $ACTIVELIST | tr , '\n' | wc -l`
fi
if [ ! $SPARELIST ]; then
SPARENUM=0
SYMBOL=''
else
SYMBOL=','
SPARENUM=`echo $SPARELIST | tr , '\n' | wc -l`
fi
RAIDNAME=`echo ${RAIDNAME}| sed -e s/\'/''/g`
RAIDLEVEL=`echo ${RAIDLEVEL}| sed -e s/\'/''/g`
SPARELIST=`echo ${SPARELIST}|sed -e s/\'/''/g`
ACTIVELIST=`echo ${ACTIVELIST}|sed -e s/\'/''/g`
DEVICELIST=`echo ${ACTIVELIST},${SPARELIST}|sed -e s/','/' '/g`
}
###########################################################################
# Function Name: testBeforeMkraid()
# Input: $@
# Return: 0 for success, 1 for false. if false, the ERRNO has been setted
# Description: test the condition with the order of arguments before make
# raid, if error echo the errno to stdout and exit
###########################################################################
testBeforeMkraid(){
local i
#test the "(3) mdX has exist" error
mdadm -D $RAIDNAME >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
if [ $? == "0" ]; then
ERRNO="3"
return 1
fi
#test "(2) this levelY need more devices" error
if [ $RAIDLEVEL == "0" -o $RAIDLEVEL == "1" ]; then
if [ $ACTIVENUM -lt 2 ]; then
ERRNO="2"
return 1
fi
#test "(5) level0 don't need spare devices"
if [ $SPARENUM != "0" -a $RAIDLEVEL == "0" ];then
ERRNO="5"
return 1
fi
elif [ $RAIDLEVEL == "5" ]; then
if [ $ACTIVENUM -lt 3 ]; then
ERRNO="2"
return 1
fi
elif [ $RAIDLEVEL == "10" ]; then
if [ $ACTIVENUM -lt 4 ]; then
ERRNO="2"
return 1
fi
else #test "(4) this shell not support this RAID level" error
ERRNO="4"
return 1
fi
#test "(6) incorrect active and spare devices name, maybe it has been
# used"
for i in $DEVICELIST
do
if [ mdadm -Ds -v | grep -w $i ];then
ERRNO="6"
return 1
fi
done
return 0
}
###########################################################################
# Function Name: testAfterCreate()
# Input: $? -- the return value after mdadm create or mdadm add
# Return: 0 for success, 1 for false. if false, the ERRNO has been setted
# Description: test the create or add success or lose
###########################################################################
testAfterCreate(){
#test "(7) create or assimble raid device lose"
if [ $1 != "0" ];then
ERRNO="7"
return 1
fi
return 0
}
errMessage(){
case $1 in
7)
echo "create or assimble $RAIDNAME lose" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
6)
echo "incorrect active and spare devices name, maybe it has been used" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
5)
echo "RAID0 donot need spare devices" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
4)
echo "this shell not support $RAIDNAME" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
3)
echo "$RAIDNAME has exist" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
2)
echo "RAID$RAIDLEVEL need more devices" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
1)
echo "error argument to the mkraid from user">>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
0)
echo "make $RAIDNAME allright" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
*)
echo "error returnnum for errMessage function" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
;;
esac
}
###########################################################################
# Function Name: usage()
# Input: nothing
# Return: nothing
# Description: print the helped message
###########################################################################
usage(){
echo "Usage: mkraid [-h|--help] <-n|--name raidname> <-l|--level raidlevel>
<-a|--active activelist> [-s|--spare sparelist]"
echo "According to the arguments,this shell make raid by create mode. Before make it, there is some testing for the arguments, and the ERRNO will be setted to the approprate value. This shell support RAID0 1 5 and 10 at present. The configfile message will output on stdout. The running message will output in ${LOGDIR}${LOGFILE} for debugging."
echo""
echo "ARGUMENT:
<raidname> -- RAID devices name, example \"/dev/mdX\"
<raidlevel> -- RAID level, example \"0, 1, 5, 10\"
<activelist> -- the RAID active member's list, the interval symbol must be ',',example \"/dev/sda1,/dev/sdb1,/dev/sdc1,/dev/hda1 ... \"
<sparelist> -- the RAID spare member's list, this is not must. the interval symbol same as activelist"
echo ""
echo "RETURN VALUE:
7 create or assimble raid device lose
6 incorrect active and spare devices name, maybe it has been used
5 level0 donot need spare devices
4 this shell not support this RAID level
3 the raid name has exist
2 this levelY need more devices
1 error argument to the mkraid from user
0 make raid allright
The ERRNO will be setted to the approprate value and the return value will be return. The raid confgure message will output on stdout."
echo ""
echo "EXAMPLE:
If you want to make raid1 named the /dev/md0 with two active devices named /dev/sda1 /dev/sdb1 and one spare device named /dev/sdc1, you can write for this:"
echo ""
echo " mkraid -n /dev/md0 -l 1 -a /dev/sda1,/dev/sdb1 -s /dev/sdc1"
echo ""
echo "But if you want to make raid0 with spare devices, there will be some mistake and exit with 5. Other mistake please watch by yourself."
echo ""
echo ""
echo "Report bugs to <zrlang@linx-info.com>"
}
###########################################################################
# main start from here #
###########################################################################
if [ $1 == "--help" -o $1 == "-h" ]; then
usage
exit 0
fi
# create the log file
if [ ! -f ${LOGDIR}${LOGFILE} ]; then
mkdir -p ${LOGDIR}
touch ${LOGDIR}${LOGFILE}
fi
NOWDATE=`date`
echo $NOWDATE >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
setArgument $@
testBeforeMkraid $@
if [ $? != "0" ]; then
errMessage $ERRNO
exit $ERRNO
fi
#make raid active devices and spare devices
if [ $SPARENUM == "0" ]; then
op_x=""
else
op_x="-x$SPARENUM"
fi
echo y | mdadm -C -v $RAIDNAME -l$RAIDLEVEL -n$ACTIVENUM $op_x $DEVICELIST >/dev/null 2>>${LOGDIR}${LOGFILE}
testAfterCreate $?
if [ $? != "0" ]; then
errMessage $ERRNO
exit $ERRNO
fi
#final correct
ERRNO=0
errMessage $ERRNO
# End of file