#!/bin/bash # # DESCRIPTION: install bootloader for OS # # SCRIPT NAME: install_bootloader.sh # # Input: # [--help] show this message # <-t|--type> boot loader type:grub,etc. # Output: # NULL # Return value: # 1 argument error # 2 bootloader type doesn't specify # 3 bootloader no implement # 127 bootloader utils not found # # AUTHOR: Qin Bo # # EMAIL: bqin@linx-info.com # # DATE: 2010-09-02 # # HISTORY: # REVISOR DATE MODIFICATION # Qin Bo 2010-09-02 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. " return $ret } install_grub () { # generate device.map grub --batch --no-floppy --device-map="$DEVICEMAP" << EOF 1>>"$DEV_LOG" 2>&1 quit EOF # in order to let bios to load grub correct, # we install grub to first hard disk by default, # so even though OS install in secondary hard disk, # the OS still can be loaded from first hard disk. f_hd=$(grep "(hd0)" "$DEVICEMAP"|awk '{print $NF}') grub-install --no-floppy --root-directory=$TARGET $f_hd 1>>$DEV_LOG 2>&1 } # This should probably be rewritten using udevadm or similar. # from di grub-install device_to_disk () { if [ "$(echo $1 |grep '/dev/mapper')"x != x ];then if [ "$(echo $1 |grep [0-9]p[0-9]$)"x != x ];then echo ${1%p*} else echo "$1" |sed 's/\(.*[a-z]\)\(.*\)/\1/' fi else echo "$1" | \ sed 's:\(/dev/\(cciss\|ida\|rs\)/c[0-9]d[0-9][0-9]*\|/dev/mmcblk[0-9]\|/dev/\(ad\|da\)[0-9]\+\|/dev/[hs]d[0-9]\+\|/dev/[a-z]\+\).*:\1:' fi } install_grub2() { mount -obind /dev $TARGET/dev mount -obind /proc $TARGET/proc mount -obind /sys $TARGET/sys boot_dev=$(chroot $TARGET grub-probe -t device /boot) disk=$(device_to_disk $boot_dev) chroot $TARGET update-grub > /dev/null 2>&1 chroot $TARGET grub-install $disk --force > /dev/null 2>&1 chroot $TARGET update-grub > /dev/null 2>&1 } main () { if [ $# -eq 0 ];then usage; erv fi tmp=$(getopt -o t: --long type:,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" 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) install_grub erv ;; grub2) install_grub2 erv ;; elilo) echo "install elilo"; sed -i '/tmppoint/d' $TARGET/etc/fstab umount /tmp/tmppoint rm -rf /tmp/tmppoint ;; *) err "$type bootloader no implement yet !" exit 3 ;; esac else err "bootloader type doesn't specify" exit 2 fi } GRUB_DIR="$TARGET/boot/grub" DEVICEMAP="$GRUB_DIR/device.map" main "$@"