Files
new_install/operation/configure_bootloader_cnf.sh
2011-02-25 10:20:17 +08:00

234 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
#
# DESCRIPTION: configure configuration file of bootloader
#
# SCRIPT NAME: configure_bootloader_cnf.sh
#
# Input:
# argument:
# <-r|--root> partition mount by root
# [-b|--boot] partition mount by boot (optional,if the boot is not mount by stand alone)
# [-k|--kparameter] kernel parameter
# [-o|--osversion] OS version
#
# Output:
# NULL
# Return value:
# 1 argument error
# 2 bootloader type doesn't specify
# 3 bootloader no implement
# 4 bootloader configuration file doesn't exist
#
#
# AUTHOR: Qin Bo
#
# EMAIL: bqin@linx-info.com
#
# DATE: 2010-09-07
#
# HISTORY:
# REVISOR DATE MODIFICATION
# Qin Bo 2010-09-07 create
#
#
#
source ./functions
usage ()
{
if [ "$1" == "-n" ];then
cmd="info"
ret=0
else
cmd="err"
ret=1
fi
$cmd "
This script will install boot loader for system
Usage: $0 options
options:
[--help] show this message
<-t|--type > boot loader type:grub,etc.
<-r|--root> partition mount by root
[-b|--boot] partition mount by boot (optional,if the boot is not mount by stand alone)
[-k|--kparameter] kernel parameter
[-o|--osversion] OS version
"
return $ret
}
configure_elilo_cnf()
{
local root_dev
root_dev="/dev/$ROOT_PARTITION"
sed -i "s@root=@root=$root_dev@g" $ELILO_CNF
}
configure_grub_cnf ()
{
local root_hd
local part_num
local root
local hd_dev
local devname
local root_dev
if [ ! -e "$GRUBCNF" ];then
err "$GRUBCNF doesn't exist"
return 4
fi
# get the boot directory in which hard disk
if [ -n "$BOOT_PARTITION" ];then
root_hd=${BOOT_PARTITION%%[0-9]*}
part_num=${BOOT_PARTITION##*[a-z]}
let $((part_num-=1))
else
root_hd=${ROOT_PARTITION%%[0-9]*}
part_num=${ROOT_PARTITION##*[a-z]}
let $((part_num-=1))
fi
info "generate $DEVICEMAP"
# generate device.map
grub --batch --no-floppy --device-map="$DEVICEMAP" << EOF 1>>"$DEV_LOG" 2>&1
quit
EOF
hd_dev=$(grep "$root_hd" "$DEVICEMAP" | awk '{print $1}' | sed 's/(//' | sed 's/)//') #e.g. hd_dev=hd0
info "hd_dev: $hd_dev part_num: $part_num"
root="($hd_dev,$part_num)"
info "root: $root"
# use UUID in menu.lst except md,
# because when I use UUID of raid device in menu.lst,
# initrd can't switch root .
devname=$(basename $ROOT_PARTITION)
if [[ "$devname" =~ ^md[0-9]*$ ]];then
root_dev="/dev/$ROOT_PARTITION"
else
by_uuid=$(get_disk_sym $ROOT_PARTITION "by-uuid")
if [ -n "$by_uuid" ];then
root_dev="UUID=$(echo $by_uuid|sed 's/.*\///')"
else
root_dev="/dev/$ROOT_PARTITION"
fi
fi
info "root_dev: $root_dev"
# modify grub.conf.sample from here
# modify root
sed "/^splashimage / s/(hd0,0)/$root/
/^kernel / s/(hd0,0)/$root/" "$GRUBCNF" >"$GRUBMENU"
# modify boot directory
# boot directory mount stand alone
if [ -n "$BOOT_PARTITION" ];then
sed -i "/^kernel / s@/boot@@
/^initrd / s@/boot@@" "$GRUBMENU"
fi
# modify OS version
sed -i "/^title / s/Secure System/Secure Operating System Version $OSVERSION/" "$GRUBMENU"
# modify root partition
sed -i "/^kernel / s@root=/dev/hda1@root=$root_dev@" "$GRUBMENU"
# append kernel parameter
sed -i "s/^kernel .*$/& $KPARAMETER/g" $GRUBMENU
}
main ()
{
if [ $# -eq 0 ];then
usage; erv
fi
tmp=$(getopt -o t:r:b:k:o: --long type:,root:,boot:,kparameter:,osversion:,help -- "$@" 2>>$DEV_LOG)
if [ $? -ne 0 ];then
usage; erv
fi
# set all argument to arg variable
eval set -- "$tmp"
while true ;do
case "$1" in
-t|--type) TYPE="$2"
info "bootloader type: $TYPE"
shift 2
;;
-r|--root) ROOT_PARTITION="$2"
info "root partition: $ROOT_PARTITION"
shift 2
;;
-b|--boot) BOOT_PARTITION="$2"
info "boot partition: $BOOT_PARTITION"
shift 2
;;
-k|--kparameter) KPARAMETER="$2"
info "kernel parameter: $KPARAMETER"
shift 2
;;
-o|--osversion) OSVERSION="$2"
info "version of opterating system: $OSVERSION"
shift 2
;;
--help) usage -n; exit 0 ;;
# shift the last "--", this dash is define by getopt (more information see man getopt)
--) shift; break;;
*) usage; erv ;;
esac
done
if [ -n "$TYPE" ];then
case "$TYPE" in
grub)
if [ -z "$ROOT_PARTITION" ]; then
err "configure grub configuration file root partition can't be empty"
exit 1
fi
configure_grub_cnf
erv
;;
elilo)
if [ -z "$ROOT_PARTITION" ]; then
err "configure elilo configuration file root partition can't be empty"
exit 1
fi
configure_elilo_cnf
erv
;;
*)
err "$TYPE bootloader no implement yet !"
exit 3
;;
esac
else
err "bootloader type doesn't specify"
exit 2
fi
}
TYPE=""
ROOT_PARTITION=""
BOOT_PARTITION=""
KPARAMETER=""
OSVERSION=""
GRUB_DIR="$TARGET/boot/grub"
GRUBMENU="$GRUB_DIR/menu.lst"
GRUBCNF="$GRUB_DIR/grub.conf.sample"
DEVICEMAP="$GRUB_DIR/device.map"
ELILO_CNF="$TARGET/boot/efi/boot/elilo.conf"
# The quotes around '$@' are essential!
# in this way we can accept whitespace as parameter.
main "$@"