~ubuntu-branches/ubuntu/hoary/grub/hoary

« back to all changes in this revision

Viewing changes to debian/update-grub

  • Committer: Bazaar Package Importer
  • Author(s): Jason Thomas
  • Date: 2002-02-04 15:35:01 UTC
  • Revision ID: james.westby@ubuntu.com-20020204153501-4dueio3est059vk0
Tags: 0.91-2
added patch from upstream which fixes a typo in the makefile that broke
xfs support, md5crypt, serial support and hercules support

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# Insert a list of installed kernels in a grub menu.lst file
 
4
#   Copyright 2001 Wichert Akkerman <wichert@linux.com>
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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.
 
19
#
 
20
# Contributors:
 
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>
 
25
 
 
26
## StartOPTIONS
 
27
# name of file menu is stored in
 
28
menufile="menu.lst"
 
29
 
 
30
# directory's to look for the grub installation and the menu file
 
31
grubdirs="/boot/grub /boot/boot/grub"
 
32
 
 
33
# Default kernel options, overidden by the kopt statement in the menufile.
 
34
kopt="root=/dev/hda1 ro"
 
35
 
 
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
 
38
groot="(hd0,0)"
 
39
 
 
40
# should grub create the alternative boot options in the menu
 
41
alternative="true"
 
42
 
 
43
# should grub lock the alternative boot options in the menu
 
44
lockalternative="false"
 
45
 
 
46
# options to use with the alternative boot options
 
47
altoptions="(recovery mode) single"
 
48
 
 
49
# controls howmany kernels are listed in the menu.lst,
 
50
# this does not include the alternative kernels
 
51
howmany="all"
 
52
 
 
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
 
55
# part of the script.
 
56
newtemplate=$(tempfile)
 
57
cat > "$newtemplate" <<EOF
 
58
## default num
 
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.
 
61
#
 
62
# You can specify 'saved' instead of a number. In this case, the default entry
 
63
# is the entry saved with the command 'savedefault'.           
 
64
default         0
 
65
 
 
66
## timeout sec
 
67
# Set a timeout, in SEC seconds, before automatically booting the default entry
 
68
# (normally the first entry defined).
 
69
timeout         5
 
70
 
 
71
# Pretty colours
 
72
color cyan/blue white/blue
 
73
 
 
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
 
77
# command 'lock'
 
78
# e.g. password topsecret
 
79
#      password --md5 \$1\$gLhU0/\$aW78kHK1QfV3P2b2znUoe/
 
80
# password topsecret
 
81
 
 
82
#
 
83
# examples
 
84
#
 
85
# title         Windows 95/98/NT/2000
 
86
# root          (hd0,0)
 
87
# makeactive
 
88
# chainloader   +1
 
89
#
 
90
# title         Linux
 
91
# root          (hd0,1)
 
92
# kernel        /vmlinuz root=/dev/hda2 ro
 
93
#
 
94
 
 
95
EOF
 
96
## End OPTIONS
 
97
 
 
98
# Make sure we use the standard sorting order
 
99
LC_COLLATE=C
 
100
# Magic markers we use
 
101
start="### BEGIN AUTOMAGIC KERNELS LIST"
 
102
end="### END DEBIAN AUTOMAGIC KERNELS LIST"
 
103
 
 
104
startopt="## ## Start Default Options ##"
 
105
endopt="## ## End Default Options ##"
 
106
 
 
107
# Abort on errors
 
108
set -e
 
109
 
 
110
abort() {
 
111
        message=$@
 
112
 
 
113
        echo >&2
 
114
        echo "$message" >&2
 
115
        echo >&2
 
116
        exit 1
 
117
}
 
118
 
 
119
# Extract options from menu.lst
 
120
ExtractMenuOpt()
 
121
{
 
122
        opt=$1
 
123
 
 
124
        sed -ne "/^$start\$/,/^$end\$/ {
 
125
                /^$startopt\$/,/^$endopt\$/ {
 
126
                        /^# $opt=/ {
 
127
                                s/^# $opt=\(.*\)\$/\1/
 
128
                                p
 
129
                        }
 
130
                }
 
131
        }" $menu
 
132
}
 
133
 
 
134
GetMenuOpt()
 
135
{
 
136
        opt=$1
 
137
        value=$2
 
138
 
 
139
        tmp=$(ExtractMenuOpt "$opt")
 
140
 
 
141
        [ -z "$tmp" ] || value="$tmp"
 
142
 
 
143
        echo $value
 
144
}
 
145
 
 
146
# Compares two version strings A and B
 
147
# Returns -1 if A<B
 
148
#          0 if A==B
 
149
#          1 if A>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
 
153
CompareVersions()
 
154
{
 
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')
 
158
 
 
159
        # we weight different kernel suffixes here
 
160
        # ac  - 50
 
161
        # pre - -50
 
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')
 
165
 
 
166
        result=0; v1finished=0; v2finished=0;
 
167
        while [ $result -eq 0 -a $v1finished -eq 0 -a $v2finished -eq 0 ];
 
168
        do
 
169
                if [ "$v1" = "" ]; then
 
170
                        v1comp=0; v1finished=1
 
171
                else
 
172
                        set -- $v1; v1comp=$1; shift; v1=$*
 
173
                fi
 
174
 
 
175
                if [ "$v2" = "" ]; then
 
176
                        v2comp=0; v2finished=1
 
177
                else
 
178
                        set -- $v2; v2comp=$1; shift; v2=$*
 
179
                fi
 
180
 
 
181
                if   [ $v1comp -gt $v2comp ]; then result=1
 
182
                elif [ $v1comp -lt $v2comp ]; then result=-1
 
183
                fi
 
184
        done
 
185
 
 
186
        # finally return the result
 
187
        echo $result
 
188
}
 
189
 
 
190
# looks in the directory specified for an initrd image with the version specified
 
191
FindInitrdName()
 
192
{
 
193
        # strip trailing slashes
 
194
        directory=$(echo $1 | sed -e 's#/*$##')
 
195
        version=$2
 
196
 
 
197
        # initrd
 
198
        # initrd.img
 
199
        # initrd-lvm
 
200
        # .*.gz
 
201
 
 
202
        initrdName=""
 
203
        names="initrd initrd.img initrd-lvm"
 
204
        compressed="gz"
 
205
 
 
206
        for n in $names ; do
 
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"
 
211
                                break
 
212
                        else
 
213
                                for c in $compressed ; do
 
214
                                        if [ -f "$directory/$n-$version.$c" ] ; then
 
215
                                                initrdName="$n-$version.$c"
 
216
                                                break
 
217
                                        fi
 
218
                                done
 
219
                        fi
 
220
                else
 
221
                        break
 
222
                fi
 
223
        done
 
224
 
 
225
        # return the result
 
226
        echo $initrdName
 
227
}
 
228
 
 
229
echo  -n "Searching for GRUB installation directory ... "
 
230
 
 
231
for d in $grubdirs ; do
 
232
        if [ -d "$d" ] ; then
 
233
                dir="$d"
 
234
                break
 
235
        fi
 
236
done
 
237
 
 
238
if [ -z "$dir" ] ; then
 
239
        abort "GRUB is not installed. To install grub, you may use the 'grub-install' command"
 
240
else
 
241
        echo "found: $dir ."
 
242
fi
 
243
 
 
244
echo -n "Testing for an existing GRUB menu.list file... "
 
245
 
 
246
# Test if our menu file exists
 
247
if [ -f "$dir/$menufile" ] ; then
 
248
        menu="$dir/$menufile"
 
249
        unset newtemplate
 
250
        echo "found: $menu ."
 
251
else
 
252
        # if not ask user if they want us to create one
 
253
        menu="$dir/$menufile"
 
254
        echo
 
255
        echo
 
256
        echo -n "Could not find $menu file. "
 
257
        echo -n "Would you like one generated for you? "
 
258
        echo -n "(y/N) "
 
259
        read answer
 
260
        case "$answer" in
 
261
                y* | Y*)
 
262
                cat "$newtemplate" > $menu
 
263
                unset newtemplate
 
264
                ;;
 
265
                *)
 
266
                abort "Not creating menu.lst as you wish"
 
267
                ;;
 
268
        esac
 
269
fi
 
270
 
 
271
# Extract the kernel options to use
 
272
kopt=$(GetMenuOpt "kopt" "$kopt")
 
273
 
 
274
# Extract the grub root
 
275
groot=$(GetMenuOpt "groot" "$groot")
 
276
 
 
277
# Extract the old recovery value
 
278
alternative=$(GetMenuOpt "recovery" "$alternative")
 
279
 
 
280
# Extract the alternative value
 
281
alternative=$(GetMenuOpt "alternative" "$alternative")
 
282
 
 
283
# Extract the lockalternative value
 
284
lockalternative=$(GetMenuOpt "lockalternative" "$lockalternative")
 
285
 
 
286
# Extract the howmany value
 
287
howmany=$(GetMenuOpt "howmany" "$howmany")
 
288
 
 
289
# Generate the menu options we want to insert
 
290
buffer=$(tempfile)
 
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
 
294
echo >> $buffer
 
295
echo "## DO NOT UNCOMMENT THEM, Just edit them to your needs" >> $buffer
 
296
echo >> $buffer
 
297
echo "## ## Start Default Options ##" >> $buffer
 
298
 
 
299
echo "## default kernel options" >> $buffer
 
300
echo "## e.g. kopt=root=/dev/hda1 ro" >> $buffer
 
301
echo "# kopt=$kopt" >> $buffer
 
302
echo >> $buffer
 
303
 
 
304
echo "## default grub root device" >> $buffer
 
305
echo "## e.g. groot=(hd0,0)" >> $buffer
 
306
echo "# groot=$groot" >> $buffer
 
307
echo >> $buffer
 
308
 
 
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
 
313
echo >> $buffer
 
314
 
 
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
 
319
echo >> $buffer
 
320
 
 
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
 
325
 
 
326
if ! grep -q "^# altoptions" $menu ; then
 
327
        echo "# altoptions=$altoptions" >> $buffer
 
328
else
 
329
        grep "^# altoptions" $menu >> $buffer
 
330
fi
 
331
echo >> $buffer
 
332
 
 
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
 
339
echo >> $buffer
 
340
 
 
341
echo "## ## End Default Options ##" >> $buffer
 
342
echo >> $buffer
 
343
 
 
344
sortedKernels=""
 
345
for kern in $(/bin/ls -1vr /boot/vmlinuz-*) ; do
 
346
        # found a kernel
 
347
        newerKernels=""
 
348
        for i in $sortedKernels ; do
 
349
                res=$(CompareVersions "$kern" "$i")
 
350
                if [ "$kern" != "" -a $res -gt 0 ] ; then
 
351
                        newerKernels="$newerKernels $kern $i"
 
352
                        kern=""
 
353
                else
 
354
                        newerKernels="$newerKernels $i"
 
355
                fi
 
356
        done
 
357
        if [ "$kern" != "" ] ; then
 
358
                newerKernels="$newerKernels $kern"
 
359
        fi
 
360
        sortedKernels="$newerKernels"
 
361
done
 
362
 
 
363
counter=0
 
364
for kern in $sortedKernels ; do
 
365
        counter=$(($counter + 1))
 
366
        if test ! x"$howmany" = x"all" ; then
 
367
                if [ $counter -gt $howmany ] ; then 
 
368
                        break
 
369
                fi
 
370
        fi
 
371
        kernelName=$(basename $kern)
 
372
        kernelVersion=$(echo $kernelName | sed -e 's/vmlinuz-//')
 
373
        initrdName=$(FindInitrdName "/boot" "$kernelVersion")
 
374
 
 
375
        if mount | grep -qs "on /boot " > /dev/null 2>&1 ; then
 
376
                kernel=/$kernelName
 
377
                if [ -n "$initrdName" ] ; then
 
378
                        initrd=/$initrdName
 
379
                fi
 
380
        else
 
381
                kernel=/boot/$kernelName
 
382
                if [ -n "$initrdName" ] ; then
 
383
                        initrd=/boot/$initrdName
 
384
                fi
 
385
        fi
 
386
 
 
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
 
392
        fi
 
393
        echo "savedefault" >> $buffer
 
394
        echo >> $buffer
 
395
 
 
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
 
406
                        fi
 
407
                        echo "root              $groot" >> $buffer
 
408
                        echo "kernel            $kernel $kopt $suffix"  >> $buffer
 
409
                        if [ -n "$initrdName" ]; then
 
410
                                echo "initrd            $initrd" >> $buffer
 
411
                        fi
 
412
                        echo "savedefault" >> $buffer
 
413
                        echo >> $buffer
 
414
                done
 
415
        fi
 
416
done
 
417
 
 
418
echo $end >> $buffer
 
419
 
 
420
echo -n "Updating $menu ... "
 
421
# Insert the new options into the menu
 
422
if ! grep -q "^$start" $menu ; then
 
423
        cat $buffer >> $menu
 
424
else
 
425
        umask 077
 
426
        sed -e "/^$start/,/^$end/{
 
427
        /^$start/r $buffer
 
428
        d
 
429
        }
 
430
        " $menu > $menu.new
 
431
        cat $menu.new > $menu
 
432
        rm -f $buffer $menu.new
 
433
fi
 
434
echo "done"
 
435
echo
 
436
 
 
437
if [ ! -z $answer ]; then
 
438
cat <<EOF
 
439
 
 
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
 
443
for the kernel.
 
444
 
 
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.
 
448
 
 
449
After you have edited $menu , please re-run 'update-grub'.
 
450
EOF
 
451
fi