#!/bin/sh
#
# Insert a list of installed kernels in a grub menu.lst file
# Copyright 2001 Wichert Akkerman
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Modifications:
# David B.Harris
# Jason Thomas
## StartOPTIONS
# name of file menu is stored in
menufile="menu.lst"
# directory's to look for the grub installation and the menu file
grubdirs="/boot/grub /boot/boot/grub"
# Default kernel options, overidden by the kopt statement in the menufile.
kopt="root=/dev/hda1 ro"
# Drive(in GRUB terms) where the kernel is located. If a seperate
# partition, this would be mounted under /boot, overridden by the kopt statement in menufile
groot="(hd0,0)"
# Default options to use in a new menu.lst . This will only be used if menu.lst
# doesn't already exist. Only edit the lines between the two "EOF"s. The others are
# part of the script.
newtemplate=$(tempfile)
cat > "$newtemplate" <&2
exit 1
}
echo -n "Searching for GRUB installation directory ... "
for d in $grubdirs ; do
if [ -d "$d" ] ; then
dir="$d"
break
fi
done
if [ -z "$dir" ] ; then
abort "GRUB is not installed. To install grub, you may use the 'grub-install' command"
else
echo "found: $dir ."
fi
echo -n "Testing for an existing GRUB menu.list file... "
# Test if our menu file exists
if [ -f "$dir/$menufile" ] ; then
menu="$dir/$menufile"
echo "found: $menu ."
else
# if not ask user if they want us to create one
menu="$dir/$menufile"
echo
echo
echo -n "Could not find $menu file. "
echo -n "Would you like one generated for you? "
echo -n "(y/N) "
read answer
case "$answer" in
y* | Y*)
cat "$newtemplate" > $menu
;;
*)
abort "Not creating menu.lst as you wish"
;;
esac
fi
# Extract the kernel options to use
tmp=$(sed -ne 's/# kopt=\(.*\)/\1/p' $menu)
[ -z "$tmp" ] || kopt="$tmp"
# Extract the grub root
tmp=$(sed -ne 's/# groot=\(.*\)/\1/p' $menu)
[ -z "$tmp" ] || groot="$tmp"
# Generate the menu options we want to insert
buffer=$(tempfile)
echo $start >> $buffer
echo "# lines between the DEBIAN KERNELS markers will be modfied by" >> $buffer
echo "# the grub-script except for the default optons below" >> $buffer
echo >> $buffer
echo "# default kernel options" >> $buffer
echo "# kopt=$kopt" >> $buffer
echo "# default grub root device" >> $buffer
echo "# groot=$groot" >> $buffer
echo >> $buffer
for i in $(/bin/ls -1 /boot/vmlinuz-* | sort -r) ; do
name=$(echo $i | sed -e 's/.*vmlinuz-//')
if [ `mount | grep -qs "on /boot"` ] ; then
kernel=$(echo $i | sed -e 's/\/boot//')
else
kernel=$i
fi
echo -e "title\tDebian GNU/Linux, kernel $name" >> $buffer
echo -e "root\t$groot" >> $buffer
echo -e "kernel\t$kernel $kopt" >> $buffer
echo >> $buffer
echo -e "title\tDebian GNU/Linux, kernel $name (recovery mode)" >> $buffer
echo -e "root\t$groot" >> $buffer
echo -e "kernel\t$kernel $kopt single" >> $buffer
echo >> $buffer
done
echo $end >> $buffer
echo -n "Updating $menu ... "
# Insert the new options into the menu
if ! grep -q "^$start" $menu ; then
cat $buffer >> $menu
else
umask 077
sed -e "/^$start/,/^$end/{
/^$start/r $buffer
d
}
" $menu > $menu.new
cat $menu.new > $menu
rm -f $buffer $menu.new
fi
echo -e "done\n"
if [ ! -z $answer ]; then
cat <