modify for the code review, and remove the mkTmpfstab.sh it is belong to mkinitrd pkg

This commit is contained in:
zorro
2010-09-30 13:52:29 +08:00
parent 729459754d
commit 4df3906e2b
3 changed files with 125 additions and 418 deletions

View File

@@ -1,226 +0,0 @@
#! /bin/bash
############################################################################
# DESCRIPTION: This shell is made for the bug of the mkinitrd-5.1.19.6 from
# Redhat. The bug is that we can get the true raid device name
# by UUID in fstab. So this file try to modify this bug.
#
# SCRIPT NAME: mkTmpfstab.sh
#
# argument: <-f|--from> specify the path of the old fstab
# <-t|--to> specify the path of the temp fstab
#
# return value: 1 false to make the file
# 0 make allright
#
# AUTHOR: ZR_Lang
#
# EMALL: zrlang@linx-info.com
#
# DATE: 2010-09-16
#
# HISTORY:
# REVISOR DATE MODIFICATION
# ZR_Lang 2010-09-16 create
###########################################################################
PATHFROM=''
PATHTO=''
LOGDIR="/var/log/"
LOGFILE="mkTmpfstab.log"
###########################################################################
# Function Name: setArgument()
# Input: $@
# Return: nothing
# Description: set the corresponding argument
###########################################################################
setArgument(){
ARGV_MKFSTAB=($(getopt -l from:,to: -o f:,t: -- $@ 2>>$LOGDIR$LOGFILE))
for((i=0; i<${#ARGV_MKFSTAB[@]}; i++))
do
case ${ARGV_MKFSTAB[$i]} in
--from|-f)
((i++))
PATHFROM=${ARGV_MKFSTAB[$i]}
;;
--to|-t)
((i++))
PATHTO=${ARGV_MKFSTAB[$i]}
;;
--)
break
;;
*)
echo "error argument, please use help message" >>$LOGDIR$LOGFILE
exit 1
;;
esac
done
PATHFROM=`echo $PATHFROM| sed -e s/\'/''/g`
PATHTO=`echo $PATHTO| sed -e s/\'/''/g`
}
###########################################################################
# Function Name: cpTmpfstab()
# Input: $PATHFROM $PATHTO
# Return: nothing
# Description: copy the old fstab to a temp place
###########################################################################
cpTmpfstab(){
local PATHTODIR=`dirname $2`
mkdir -p $PATHTODIR 2>>$LOGDIR$LOGFILE
if [ $? != "0" ];then
echo "can not makedir named $PATHTODIR" >>$LOGDIR$LOGFILE
exit 1
fi
cp $1 $2 2>>$LOGDIR$LOGFILE
if [ $? != "0" ]; then
echo "can not copy $1 to $2" >>$LOGDIR$LOGFILE
exit 1
fi
}
###########################################################################
# Function Name: mdfyTmpFstab()
# Input: $PATHTO
# Return: nothing
# Description: modify the temp fstab, change UUID to the right device name
###########################################################################
mdfyTmpFstab(){
local i
local j
local LASTNAME=''
#replace UUID in the temp fstab
local UUIDFROMFSTAB=$(awk '/^[ \t]*[^#]/ { if ($1 ~ "^[UUID]") { print $1}}' $1)
if [ -n "$UUIDFROMFSTAB" ];then
for i in $UUIDFROMFSTAB
do
local NAMEFROMUUID=`blkid -t$i |cut -f 1 -d :`
if [ ! "${NAMEFROMUUID}" ];then
echo "error UUID, can not find devices by blkid" >>$LOGDIR$LOGFILE
exit 1
fi
if [ `echo $NAMEFROMUUID| wc -w` -lt 2 ];then
LASTNAME=$NAMEFROMUUID
else
for j in $NAMEFROMUUID
do
if [ `echo $j|grep 'md'` ];then
if [ ! $LASTNAME ]; then
LASTNAME=$j
else
echo "too many raid devices be found" >>$LOGDIR$LOGFILE
exit 1
fi
else
if [ `mdadm -Ds -v|grep -w $j` ];then
continue
else
echo "too many partitions be found" >>$LOGDIR$LOGFILE
exit 1
fi
fi
done
fi
sed -i s@$i@$LASTNAME@g $1
LASTNAME=''
done
fi
#replace by-id in the temp fstab
local BYIDFROMFSTAB=$(awk '/^[ \t]*[^#]/ { if ($1 ~ "by-id") { print $1}}' $1)
if [ -n "$BYIDFROMFSTAB" ];then
for i in $BYIDFROMFSTAB
do
local NAMEFROMBYID=`readlink $BYIDFROMFSTAB`
NAMEFROMBYID="/dev/`basename $NAMEFROMBYID`"
sed -i s@$i@$NAMEFROMBYID@g $1
done
fi
}
###########################################################################
# Function Name: checkArgument()
# Input: $PATHFROM $PATHTO
# Return: nothing
# Description: check the arguments
###########################################################################
checkArgument(){
if [ ! -f $1 ];then
echo "Can not find pathfrom $1" >>$LOGDIR$LOGFILE
exit 1
fi
if [ ! $2 ];then
echo "pathto argument is empty!!" >>$LOGDIR$LOGFILE
exit 1
fi
}
###########################################################################
# Function Name: ()
# Input: $PATHFROM $PATHTO
# Return: nothing
# Description: check the arguments
###########################################################################
###########################################################################
# Function Name: usage()
# Input: nothing
# Return: nothing
# Description: print the helped message
###########################################################################
usage(){
echo "Usage: mkraid [-h|--help] <-f|--from oldfstab> <-t|--to tmpfstab>"
echo "This shell is just used to modify a false of mkinitrd, when used UUID to specify a raid device in fstab the mkinitrd always analysis false, not only one devices would be found according to the UUID. So I write this shell for makeinitrd. The shell will take the old fstab(usually is /etc/fstab) to a temp place, then change the UUID to right device name in the temp fstab. The makeinitrd shell should use the temp fstab file to make the initrd and stay the old fstab, after make initrd remove the temp fstab."
echo""
echo "ARGUMENT:
[-h|--help] -- print the helped message
<oldfstab> -- specify the path of the initial fstab.
<tmpfstab> -- specify the temp path of a new fstab, the makeinitrd will use the temp fstab to make initrd."
echo ""
echo "RETURN VALUE:
1 make temp fstab error
0 make allright
the log message will be print in $LOGDIR$LOGFILE"
echo ""
echo "EXAMPLE:
If you want to according /etc/fstab to make a temp fstab file in /tmp named .fstab.tmp, you can write for this:"
echo ""
echo " mkTmpfstab.sh -f /etc/fstab -t /tmp/.fstab.tmp "
echo "The /tmp/.fstab.tmp will be the argument of the option named --fstab of mkinitrd. If you use this shell please remember this."
echo ""
echo ""
echo "Report bugs to <zrlang@linx-info.com>"
}
###########################################################################
# main from here #
###########################################################################
if [ $1 == "--help" -o $1 == "-h" ];then
usage
exit 0
elif [ $# -lt 4 ];then
echo "need more arguments, please look at the helped message"
exit 1
fi
#initial the log file
if [ -d $LOGDIR ];then
mkdir -p $LOGDIR
touch $LOGDIR$LOGFILE
fi
NOWDATE=`date`
echo $NOWDATE >>$LOGDIR$LOGFILE
setArgument $@
checkArgument $PATHFROM $PATHTO
cpTmpfstab $PATHFROM $PATHTO
mdfyTmpFstab $PATHTO
echo "make temp fstab all right" >>$LOGDIR$LOGFILE
# End of file

View File

@@ -30,12 +30,9 @@
# ZR_Lang 2010-08-23 create
###########################################################################
. ./functions
#global value can be modified for some exchange
LOGDIR="/var/log/"
LOGFILE="mkraid.log"
ERRNO="" #error number of return value
RAIDNAME=""
RAIDLEVEL=""
ACTIVELIST=""
@@ -48,74 +45,60 @@ DEVICELIST=""
# Function Name: setArgument()
# Input: $@
# Return: nothing
# Description: set the corresponding argument from the stdin
# Description: set the corresponding argument
###########################################################################
setArgument(){
local i
local ARGV=($(getopt -l name:,level:,active:,spare: -o n:,l:,a:,s: -- $@ 2>>${LOGDIR}${LOGFILE}))
local i
local ARGV=($(getopt -l name:,level:,active:,spare:,help -o n:,l:,a:,s:,h -- $@ 2>>${DEV_LOG}))
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
for((i=0; i<${#ARGV[@]}; i++))
do
case ${ARGV[$i]} in
--name|-n)
((i++))
RAIDNAME=${ARGV[$i]}
;;
--level|-l)
((i++))
RAIDLEVEL=${ARGV[$i]}
;;
--active|-a)
((i++))
ACTIVELIST=${ARGV[$i]}
;;
--spare|-s)
((i++))
SPARELIST=${ARGV[$i]}
;;
--help|-h)
usage
return 0
;;
--)
;;
*)
info "error argument, please use help message and modify it"
return 1
;;
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`
if [ ! "${ACTIVELIST}" ];then
ACTIVENUM=0
else
ACTIVENUM=`echo $ACTIVELIST | tr , ' ' | wc -w`
fi
if [ ! "${SPARELIST}" ]; then
SPARENUM=0
else
SPARENUM=`echo $SPARELIST | tr , ' ' | wc -w`
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`
}
###########################################################################
@@ -126,50 +109,50 @@ setArgument(){
# raid, if error echo the errno to stdout and exit
###########################################################################
testBeforeMkraid(){
local i
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
mdadm -D $RAIDNAME >>${DEV_LOG} 2>>${DEV_LOG}
if [ "$?" == "0" ]; then
info "$RAIDNAME has exist"
return 3
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
if [ "${RAIDLEVEL}" == "0" -o "${RAIDLEVEL}" == "1" ]; then
if [ $ACTIVENUM -lt 2 ]; then
info "RAID$RAIDLEVEL need more devices"
return 2
fi
#test "(5) level0 don't need spare devices"
if [ "${SPARENUM}" != "0" -a "${RAIDLEVEL}" == "0" ];then
info "RAID0 donot need spare devices"
return 5
fi
elif [ "${RAIDLEVEL}" == "5" ]; then
if [ "${ACTIVENUM}" -lt 3 ]; then
info "RAID$RAIDLEVEL need more devices"
return 2
fi
elif [ "${RAIDLEVEL}" == "10" ]; then
if [ "${ACTIVENUM}" -lt 4 ]; then
info "RAID$RAIDLEVEL need more devices"
return 2
fi
else #test "(4) this shell not support this RAID level" error
info "this shell not support $RAIDNAME"
return 4
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
for i in ${DEVICELIST}
do
if mdadm -Ds -v | grep -w $i ; then
info "incorrect active and spare devices name, maybe it has been used"
return 6
fi
done
return 0
}
###########################################################################
@@ -179,45 +162,12 @@ testBeforeMkraid(){
# 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
#test "(7) create or assimble raid device lose"
if [ "${1}" != "0" ];then
info "create or assimble $RAIDNAME lose" >>${LOGDIR}${LOGFILE} 2>>${LOGDIR}${LOGFILE}
return 7
fi
return 0
}
###########################################################################
@@ -227,17 +177,17 @@ errMessage(){
# Description: print the helped message
###########################################################################
usage(){
echo "Usage: mkraid [-h|--help] <-n|--name raidname> <-l|--level raidlevel>
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:
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. "
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:
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
@@ -247,55 +197,38 @@ usage(){
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:
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>"
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}
info "${NOWDATE}"
setArgument $@; erv
testBeforeMkraid $@; erv
setArgument $@
testBeforeMkraid $@
if [ $? != "0" ]; then
errMessage $ERRNO
exit $ERRNO
fi
#make raid active devices and spare devices
if [ $SPARENUM == "0" ]; then
op_x=""
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
op_x="-x$SPARENUM"
fi
#final correct
ERRNO=0
errMessage $ERRNO
echo yes | mdadm -C -q $RAIDNAME -l$RAIDLEVEL -n$ACTIVENUM $op_x $DEVICELIST >>${DEV_LOG} 2>>${DEV_LOG}
testAfterCreate $?; erv
info "make $RAIDNAME allright"
# End of file

View File

@@ -8,7 +8,7 @@ if [ $1 == "--help" -o $1 == "-h" ];then
exit 0
fi
if [ ! $1 ]; then
if [ -n $1 ]; then
echo "error argument, please look the usage by --help or -h"
exit 1
fi
@@ -16,7 +16,7 @@ fi
CONDIR=`dirname $1`
if [ ! -d $CONDIR ]; then
mkdir -p $CONDIR
if [ $? != 0 ]; then
if [ $? -ne 0 ]; then
echo "error argument, please look the usage by --help or -h"
exit 1
fi