3
# Insert a list of installed kernels in a grub menu.lst file
4
# Copyright 2001 Wichert Akkerman <wichert@linux.com>
6
# This file is free software; you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
# General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
# Jason Thomas <jason@debian.org>
22
# David B.Harris <dbarclay10@yahoo.ca>
23
# Marc Haber <mh@zugschlus.de>
24
# Crispin Flowerday <crispin@zeus.com>
27
# name of file menu is stored in
30
# directory's to look for the grub installation and the menu file
31
grubdirs="/boot/grub /boot/boot/grub"
33
# Default kernel options, overidden by the kopt statement in the menufile.
34
kopt="root=/dev/hda1 ro"
36
# Drive(in GRUB terms) where the kernel is located. If a seperate
37
# partition, this would be mounted under /boot, overridden by the kopt statement in menufile
40
# should grub create the alternative boot options in the menu
43
# should grub lock the alternative boot options in the menu
44
lockalternative="false"
46
# options to use with the alternative boot options
47
altoptions="(recovery mode) single"
49
# controls howmany kernels are listed in the menu.lst,
50
# this does not include the alternative kernels
53
# Default options to use in a new menu.lst . This will only be used if menu.lst
54
# doesn't already exist. Only edit the lines between the two "EOF"s. The others are
56
newtemplate=$(tempfile)
57
cat > "$newtemplate" <<EOF
59
# Set the default entry to the entry number NUM. Numbering starts from 0, and
60
# the entry number 0 is the default if the command is not used.
62
# You can specify 'saved' instead of a number. In this case, the default entry
63
# is the entry saved with the command 'savedefault'.
67
# Set a timeout, in SEC seconds, before automatically booting the default entry
68
# (normally the first entry defined).
72
color cyan/blue white/blue
74
## password ['--md5'] passwd
75
# If used in the first section of a menu file, disable all interactive editing
76
# control (menu entry editor and command-line) and entries protected by the
78
# e.g. password topsecret
79
# password --md5 \$1\$gLhU0/\$aW78kHK1QfV3P2b2znUoe/
85
# title Windows 95/98/NT/2000
92
# kernel /vmlinuz root=/dev/hda2 ro
98
# Make sure we use the standard sorting order
100
# Magic markers we use
101
start="### BEGIN AUTOMAGIC KERNELS LIST"
102
end="### END DEBIAN AUTOMAGIC KERNELS LIST"
104
startopt="## ## Start Default Options ##"
105
endopt="## ## End Default Options ##"
119
# Extract options from menu.lst
124
sed -ne "/^$start\$/,/^$end\$/ {
125
/^$startopt\$/,/^$endopt\$/ {
127
s/^# $opt=\(.*\)\$/\1/
139
tmp=$(ExtractMenuOpt "$opt")
141
[ -z "$tmp" ] || value="$tmp"
146
# Compares two version strings A and B
150
# This compares version numbers of the form
151
# 2.4.14-random > 2.4.14-ac10 > 2.4.14 > 2.4.14-pre2 >
152
# 2.4.14-pre1 > 2.4.13-ac99
155
# First split the version number and remove any '.' 's or dashes
156
v1=$(echo $1 | sed -e 's![^0-9]\+! & !g' -e 's![\.\-]!!g')
157
v2=$(echo $2 | sed -e 's![^0-9]\+! & !g' -e 's![\.\-]!!g')
159
# we weight different kernel suffixes here
162
# others are given 99
163
v1=$(echo $v1 | sed -e 's! ac ! 50 !g' -e 's! pre ! -50 !g' -e 's![^-0-9 ]\+!99!g')
164
v2=$(echo $v2 | sed -e 's! ac ! 50 !g' -e 's! pre ! -50 !g' -e 's![^-0-9 ]\+!99!g')
166
result=0; v1finished=0; v2finished=0;
167
while [ $result -eq 0 -a $v1finished -eq 0 -a $v2finished -eq 0 ];
169
if [ "$v1" = "" ]; then
170
v1comp=0; v1finished=1
172
set -- $v1; v1comp=$1; shift; v1=$*
175
if [ "$v2" = "" ]; then
176
v2comp=0; v2finished=1
178
set -- $v2; v2comp=$1; shift; v2=$*
181
if [ $v1comp -gt $v2comp ]; then result=1
182
elif [ $v1comp -lt $v2comp ]; then result=-1
186
# finally return the result
190
# looks in the directory specified for an initrd image with the version specified
193
# strip trailing slashes
194
directory=$(echo $1 | sed -e 's#/*$##')
203
names="initrd initrd.img initrd-lvm"
207
# make sure we haven't already found it
208
if [ -z "$initrdName" ] ; then
209
if [ -f "$directory/$n-$version" ] ; then
210
initrdName="$n-$version"
213
for c in $compressed ; do
214
if [ -f "$directory/$n-$version.$c" ] ; then
215
initrdName="$n-$version.$c"
229
echo -n "Searching for GRUB installation directory ... "
231
for d in $grubdirs ; do
232
if [ -d "$d" ] ; then
238
if [ -z "$dir" ] ; then
239
abort "GRUB is not installed. To install grub, you may use the 'grub-install' command"
244
echo -n "Testing for an existing GRUB menu.list file... "
246
# Test if our menu file exists
247
if [ -f "$dir/$menufile" ] ; then
248
menu="$dir/$menufile"
250
echo "found: $menu ."
252
# if not ask user if they want us to create one
253
menu="$dir/$menufile"
256
echo -n "Could not find $menu file. "
257
echo -n "Would you like one generated for you? "
262
cat "$newtemplate" > $menu
266
abort "Not creating menu.lst as you wish"
271
# Extract the kernel options to use
272
kopt=$(GetMenuOpt "kopt" "$kopt")
274
# Extract the grub root
275
groot=$(GetMenuOpt "groot" "$groot")
277
# Extract the old recovery value
278
alternative=$(GetMenuOpt "recovery" "$alternative")
280
# Extract the alternative value
281
alternative=$(GetMenuOpt "alternative" "$alternative")
283
# Extract the lockalternative value
284
lockalternative=$(GetMenuOpt "lockalternative" "$lockalternative")
286
# Extract the howmany value
287
howmany=$(GetMenuOpt "howmany" "$howmany")
289
# Generate the menu options we want to insert
291
echo $start >> $buffer
292
echo "## lines between the AUTOMAGIC KERNELS LIST markers will be modified" >> $buffer
293
echo "## by the debian update-grub script except for the default optons below" >> $buffer
295
echo "## DO NOT UNCOMMENT THEM, Just edit them to your needs" >> $buffer
297
echo "## ## Start Default Options ##" >> $buffer
299
echo "## default kernel options" >> $buffer
300
echo "## e.g. kopt=root=/dev/hda1 ro" >> $buffer
301
echo "# kopt=$kopt" >> $buffer
304
echo "## default grub root device" >> $buffer
305
echo "## e.g. groot=(hd0,0)" >> $buffer
306
echo "# groot=$groot" >> $buffer
309
echo "## should update-grub create alternative boot options" >> $buffer
310
echo "## e.g. alternative=true" >> $buffer
311
echo "## alternative=false" >> $buffer
312
echo "# alternative=$alternative" >> $buffer
315
echo "## should update-grub lock alternative boot options" >> $buffer
316
echo "## e.g. lockalternative=true" >> $buffer
317
echo "## lockalternative=false" >> $buffer
318
echo "# lockalternative=$lockalternative" >> $buffer
321
echo "## altoption boot targets option" >> $buffer
322
echo "## multiple altoptions lines are allowed" >> $buffer
323
echo "## e.g. altoptions=(extra menu suffix) extra boot options" >> $buffer
324
echo "## altoptions=(recovery mode) single" >> $buffer
326
if ! grep -q "^# altoptions" $menu ; then
327
echo "# altoptions=$altoptions" >> $buffer
329
grep "^# altoptions" $menu >> $buffer
333
echo "## controls how many kernels should be put into the menu.lst" >> $buffer
334
echo "## only counts the first occurence of a kernel, not the" >> $buffer
335
echo "## alternative kernel options" >> $buffer
336
echo "## e.g. howmany=all" >> $buffer
337
echo "## howmany=7" >> $buffer
338
echo "# howmany=$howmany" >> $buffer
341
echo "## ## End Default Options ##" >> $buffer
345
for kern in $(/bin/ls -1vr /boot/vmlinuz-*) ; do
348
for i in $sortedKernels ; do
349
res=$(CompareVersions "$kern" "$i")
350
if [ "$kern" != "" -a $res -gt 0 ] ; then
351
newerKernels="$newerKernels $kern $i"
354
newerKernels="$newerKernels $i"
357
if [ "$kern" != "" ] ; then
358
newerKernels="$newerKernels $kern"
360
sortedKernels="$newerKernels"
364
for kern in $sortedKernels ; do
365
counter=$(($counter + 1))
366
if test ! x"$howmany" = x"all" ; then
367
if [ $counter -gt $howmany ] ; then
371
kernelName=$(basename $kern)
372
kernelVersion=$(echo $kernelName | sed -e 's/vmlinuz-//')
373
initrdName=$(FindInitrdName "/boot" "$kernelVersion")
375
if mount | grep -qs "on /boot " > /dev/null 2>&1 ; then
377
if [ -n "$initrdName" ] ; then
381
kernel=/boot/$kernelName
382
if [ -n "$initrdName" ] ; then
383
initrd=/boot/$initrdName
387
echo "title Debian GNU/Linux, kernel $kernelVersion" >> $buffer
388
echo "root $groot" >> $buffer
389
echo "kernel $kernel $kopt" >> $buffer
390
if [ -n "$initrdName" ]; then
391
echo "initrd $initrd" >> $buffer
393
echo "savedefault" >> $buffer
396
# insert the alternative boot options
397
if test ! x"$alternative" = x"false" ; then
398
# for each altoptions line do this stuff
399
sed -ne 's/# altoptions=\(.*\)/\1/p' $buffer | while read line; do
400
descr=$(echo $line | sed -ne 's/\(([^)]*)\)[[:space:]]\(.*\)/\1/p')
401
suffix=$(echo $line | sed -ne 's/\(([^)]*)\)[[:space:]]\(.*\)/\2/p')
402
echo "title Debian GNU/Linux, kernel $kernelVersion $descr" >> $buffer
403
# lock the alternative options
404
if test x"$lockalternative" = x"true" ; then
405
echo "lock" >> $buffer
407
echo "root $groot" >> $buffer
408
echo "kernel $kernel $kopt $suffix" >> $buffer
409
if [ -n "$initrdName" ]; then
410
echo "initrd $initrd" >> $buffer
412
echo "savedefault" >> $buffer
420
echo -n "Updating $menu ... "
421
# Insert the new options into the menu
422
if ! grep -q "^$start" $menu ; then
426
sed -e "/^$start/,/^$end/{
431
cat $menu.new > $menu
432
rm -f $buffer $menu.new
437
if [ ! -z $answer ]; then
440
Please note that configuration parameters for GRUB are stored in
441
$menu . You must edit this file in order to set the options
442
which GRUB passes to the kernel, as well as the drive which GRUB looks in to
445
Everything on the line after "kopt=" is passed to the kernel as parameters,
446
and "groot=" must be set to the partition(in GRUB terms, such as "(hd0,0)")
447
which GRUB will load the kernel from.
449
After you have edited $menu , please re-run 'update-grub'.