okay here's another, this one will generate a menu.lst, by parsing an ls
of /boot/vmlinuz-*, it's attached if you feel its useful and should be
added to grub. It's debian specfic so will need a bit of work to make it
usefull all round.


-- 
Jason Thomas                           Phone:  +61 2 6257 7111
System Administrator  -  UID 0         Fax:    +61 2 6257 7311
tSA Consulting Group Pty. Ltd.         Mobile: 0418 29 66 81
1 Hall Street Lyneham ACT 2602         http://www.topic.com.au/
#!/bin/sh
#
# Insert a list of installed kernels in a grub menu.lst file
#   Copyright 2001 Wichert Akkerman <[EMAIL PROTECTED]>
#
# 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 <[EMAIL PROTECTED]>
#       Jason Thomas <[EMAIL PROTECTED]>

## 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" <<EOF
# By default, boot the first entry after five seconds.
default         0
timeout         5

# Pretty colours
color cyan/blue white/blue

#
# examples
#
# title         Windows 95/98/NT/2000
# root          (hd0,0)
# makeactive
# chainloader   +1
#
# title         Linux
# root          (hd0,1)
# kernel        /vmlinuz root=/dev/hda2 ro
#

EOF
## End OPTIONS

# Make sure we use the standard sorting order
LC_COLLATE=C
# Magic markers we use
start="### BEGIN DEBIAN KERNELS"
end="### END DEBIAN KERNELS"

# Abort on errors
set -e

abort() {
        echo -e "\n$@" >&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 <<EOF

Please note that configuration parameters for GRUB are stored in
$menu . You must edit this file in order to set the options
which GRUB passes to the kernel, as well as the drive which GRUB looks in to
for the kernel.

Everything on the line after "kopt=" is passed to the kernel as parameters,
and "groot=" must be set to the partition(in GRUB terms, such as "(hd0,0)")
which GRUB will load the kernel from.

After you have edited $menu , please re-run 'update-grub'.
EOF
fi

PGP signature

Reply via email to