~ubuntu-installer/lupin/hardy

55 by ago
Added init fixes for umountfs to make it skip /host folders (containing
1
#!/bin/bash
2
#
3
# Insert a list of installed kernels in a grub config 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
# Abort on errors
27
set -e
28
29
host_os=`uname -s | tr '[A-Z]' '[a-z]'`
30
31
abort() {
32
	message=$@
33
34
	echo >&2
35
	printf '%s\n' "$message" >&2
36
	echo >&2
37
	exit 1
38
}
39
40
find_grub_dir ()
41
{
42
	echo  -n "Searching for GRUB installation directory ... " >&2
43
44
	for d in $grub_dirs ; do
45
		if [ -d "$d" ] ; then
46
			grub_dir="$d"
47
			break
48
		fi
49
	done
50
	
51
	if [ -z "$grub_dir" ] ; then
52
		abort "No GRUB directory found.
53
 To create a template run 'mkdir /boot/grub' first.
54
 To install grub, install it manually or try the 'grub-install' command.
55
 ### Warning, grub-install is used to change your MBR. ###"
56
	else
57
		echo "found: $grub_dir" >&2
58
	fi
59
60
	echo $grub_dir
61
}
62
63
find_device ()
64
{
65
	mount_point=$1
66
67
	# Autodetect current root device
68
	device=
69
	if [ -f /etc/fstab ] ; then
70
		while read DEV MNT FOO; do
71
			if `echo "$DEV" | grep -q "^#"`; then
72
				continue
73
			fi
74
			if [ "$MNT" = "$mount_point" ]; then
75
				device="$DEV";
76
			fi
77
		done < /etc/fstab
78
	fi
79
80
	if [ -n "$device" ] ; then
81
		case "$device" in
82
			LABEL=* | UUID=*)
83
				device=`readlink -f "$(findfs $device)"`
84
			;;
85
			*)
86
				device=`readlink -f "$device"`
87
			;;
88
		esac
89
	fi
90
91
	echo $device
92
}
93
94
find_root_device ()
95
{
96
	device=$(find_device "/")
97
98
	if [ -z "$device" ]; then
99
		echo "Cannot determine root device.  Assuming /dev/hda1" >&2
100
		echo "This error is probably caused by an invalid /etc/fstab" >&2
101
		device=/dev/hda1
102
	fi
103
104
	echo $device
105
}
106
107
# Usage: convert_raid1 os_device
108
# Checks if os_device is a software raid1.
109
# If so, converts to first physical device in array.
110
convert_raid1 ()
111
{
112
    case $1 in
113
        /dev/md[0-9])
114
            : ;; # Continue
115
        *)
116
            return 1 ;;
117
    esac
118
119
    [ -x /sbin/mdadm ] || return 1
120
121
    # Check that the raid device is raid1
122
    raidlevel=$(mdadm -D -b $1 | grep "^ARRAY" | \
123
            sed "s/^.*level=//" | cut -d" " -f1)
124
    [ "$raidlevel" = "raid1" ] || return 1
125
    
126
    # Take only the first device that makes up the raid
127
    raiddev=$(mdadm -D $1 | grep -A1 "Number" | grep "dev" \
128
                          | sed "s/^.*\(\/dev\/.*\)$/\1/")
129
    [ -n "$raiddev" ] || return 1
130
131
    echo $raiddev
132
    return 0
133
}
134
135
# Usage: convert os_device
136
# Convert an OS device to the corresponding GRUB drive.
137
# This part is OS-specific.
138
convert () {
139
    # First, check if the device file exists.
140
    if test -e "$1"; then
141
		:
142
    else
143
		echo "$1: Not found or not a block device." 1>&2
144
		exit 1
145
    fi
146
147
	host_os=`uname -s | tr '[[:upper:]]' '[[:lower:]]'`
148
149
    # Break the device name into the disk part and the partition part.
150
    case "$host_os" in
151
    linux)
152
		tmp_disk=`echo "$1" | sed -e 's%\([sh]d[[:lower:]]\)[0-9]*$%\1%' \
153
				  -e 's%\(fd[0-9]*\)$%\1%' \
154
				  -e 's%/part[0-9]*$%/disc%' \
155
				  -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
156
		tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[[:lower:]]\([0-9]*\)$%\1%' \
157
				  -e 's%.*/fd[0-9]*$%%' \
158
				  -e 's%.*/floppy/[0-9]*$%%' \
159
				  -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
160
				  -e 's%.*c[0-7]d[0-9]*p*%%'`
161
	;;
162
    gnu)
163
		tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
164
		tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
165
    freebsd|*/kfreebsd)
166
		tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%\1%' \
167
			    | sed 's%r\{0,1\}\(da[0-9]*\).*$%\1%'`
168
		tmp_part=`echo "$1" \
169
	    		| sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
170
       	    	| sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
171
	;;
172
    netbsd|*/knetbsd)
173
		tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
174
	    		| sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
175
		tmp_part=`echo "$1" \
176
	    		| sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
177
	;;
178
    *)
179
		echo "update-grub does not support your OS yet." 1>&2
180
		exit 1 ;;
181
    esac
182
183
    # Get the drive name.
184
    tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
185
			| sed 's%.*\(([hf]d[0-9][a-z0-9,]*)\).*%\1%'`
186
187
    # If not found, print an error message and exit.
188
    if test "x$tmp_drive" = x; then
189
		echo "$1 does not have any corresponding BIOS drive." 1>&2
190
		exit 1
191
    fi
192
193
    if test "x$tmp_part" != x; then
194
		# If a partition is specified, we need to translate it into the
195
		# GRUB's syntax.
196
		case "$host_os" in
197
		linux)
198
	    	  echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
199
		gnu)
200
	    	  if echo $tmp_part | grep "^s" >/dev/null; then
201
				tmp_pc_slice=`echo $tmp_part \
202
		    		| sed "s%s\([0-9]*\)[a-z]*$%\1%"`
203
				tmp_drive=`echo "$tmp_drive" \
204
		    		| sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
205
	    	  fi
206
	    	  if echo $tmp_part | grep "[a-z]$" >/dev/null; then
207
				tmp_bsd_partition=`echo "$tmp_part" \
208
		    		| sed "s%[^a-z]*\([a-z]\)$%\1%"`
209
				tmp_drive=`echo "$tmp_drive" \
210
		    		| sed "s%)%,$tmp_bsd_partition)%"`
211
	    	  fi
212
	    	  echo "$tmp_drive" ;;
213
		freebsd|*/kfreebsd)
214
	    	  if echo $tmp_part | grep "^s" >/dev/null; then
215
				tmp_pc_slice=`echo $tmp_part \
216
		    		| sed "s%s\([0-9]*\)[a-h]*$%\1%"`
217
				tmp_drive=`echo "$tmp_drive" \
218
		    		| sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
219
	    	  fi
220
	    	  if echo $tmp_part | grep "[a-h]$" >/dev/null; then
221
				tmp_bsd_partition=`echo "$tmp_part" \
222
		    		| sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
223
				tmp_drive=`echo "$tmp_drive" \
224
		    		| sed "s%)%,$tmp_bsd_partition)%"`
225
	    	  fi
226
	    	  echo "$tmp_drive" ;;
227
		netbsd|*/knetbsd)
228
	    	  if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
229
				tmp_bsd_partition=`echo "$tmp_part" \
230
		    		| sed "s%\([a-p]\)$%\1%"`
231
				tmp_drive=`echo "$tmp_drive" \
232
		    		| sed "s%)%,$tmp_bsd_partition)%"`
233
	    	  fi
234
	    	  echo "$tmp_drive" ;;
235
		esac
236
    else
237
		# If no partition is specified, just print the drive name.
238
		echo "$tmp_drive"
239
    fi
240
}
241
242
# Usage: convert_default os_device
243
# Convert an OS device to the corresponding GRUB drive.
244
# Calls OS-specific convert, and returns a default of
245
# (hd0,0) if anything goes wrong
246
convert_default () {
247
	# Check if device is software raid1 array
248
	if tmp_dev=$(convert_raid1 $1 2>/dev/null) ; then
249
		: # Use device returned by convert_raid1
250
	else
251
		tmp_dev=$1
252
	fi
253
254
	if tmp=$(convert $tmp_dev 2>/dev/null) ; then
255
		echo $tmp
256
	else
257
		echo "(hd0,0)"
258
	fi
259
}
260
261
is_removable () {
262
	removabledevice="$(echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' -e 's%\(fd[0-9]*\)$%\1%' -e 's%/part[0-9]*$%/disc%' -e 's%\(c[0-7]d[0-9]*\).*$%\1%' -e 's%^/dev/%%g')"
263
        if [ -e "/sys/block/$removabledevice/removable" ]; then
264
                if [ "$(cat /sys/block/$removabledevice/removable)" != "0" ]; then
265
                        echo "/dev/$removabledevice"
266
                        return
267
                fi
268
        fi
269
	echo ""
270
}
271
272
## Configuration Options
273
# directory's to look for the grub installation and the menu file
274
grub_dirs="/boot/grub /boot/boot/grub"
275
276
# The grub installation directory
277
grub_dir=$(find_grub_dir)
278
279
# Full path to the menu.lst
280
menu_file_basename=menu.lst
281
menu_file=$grub_dir/$menu_file_basename
282
283
# Full path to the default file
284
default_file_basename=default
285
default_file=$grub_dir/$default_file_basename
286
287
# the device for the / filesystem
288
root_device=$(find_root_device)
289
290
# the device for the /boot filesystem
291
boot_device=$(find_device "/boot")
292
293
# Full path to the device.map
294
device_map=$grub_dir/device.map
295
296
# Enable crashdump menu alternative
297
crashdump="0"
298
299
# Default kernel options, overidden by the kopt statement in the menufile.
72 by ago
Replaced algorithm to detect host_device
300
loop_file=$(awk '$2=="/" && $4~"loop" {print $1}' /etc/fstab)
301
host_device=$(awk '"'${loop_file}'"~"^"$2 && $2!="/" {print $1}' /proc/mounts)
302
host_mountpoint=$(awk '$1=="'${host_device}'" {print $2}' /proc/mounts)
303
loop_file=${loop_file#$host_mountpoint}
304
if [ -n "$host_device" ]; then
62 by ago
Reset boot_device to force kernelDir=/boot (otherwise it becomes empty)
305
    boot_device=
55 by ago
Added init fixes for umountfs to make it skip /host folders (containing
306
    root_device="$host_device"
307
    default_kopt="root=$host_device loop=$loop_file ro"
308
else
309
    default_kopt="root=$root_device ro"
310
fi
311
kopt="$default_kopt"
312
313
# Title
314
title=$(lsb_release --short --description 2>/dev/null) || title="Ubuntu"
315
316
# should update-grub remember the default entry
317
updatedefaultentry="false"
318
319
# Drive(in GRUB terms) where the kernel is located. Overridden by the
320
# kopt statement in menufile.
321
# if we don't have a device.map then we can't use the convert function.
322
check_removable=""
323
if test -f "$device_map" ; then
324
	if test -z "$boot_device" ; then
325
		grub_root_device=$(convert_default "$root_device")
326
		check_removable="$(is_removable "$root_device")"
327
	else
328
		grub_root_device=$(convert_default "$boot_device")
329
		check_removable="$(is_removable "$boot_device")"
330
	fi
331
else
332
	grub_root_device="(hd0,0)"
333
fi
334
335
# If the root/boot device is on a removable target, we need to override
336
# the grub_root_device to (hd0,X). This is a requirement since the BIOS
337
# will change device mapping dynamically if we switch boot device.
338
339
if test -n "$check_removable" ; then
340
	grub_root_device="$(echo "$grub_root_device" | sed -e 's/d.*,/d0,/g')"
341
fi
342
343
# should grub create the alternative boot options in the menu
344
	alternative="true"
345
346
# should grub lock the alternative boot options in the menu
347
	lockalternative="false"
348
349
# additional options to use with the default boot option, but not with the
350
# alternatives
351
	defoptions="quiet splash"
352
353
# should grub lock the old kernels
354
	lockold="false"
355
356
# Xen hypervisor options to use with the default Xen boot option
357
	xenhopt=""
358
359
# Xen Linux kernel options to use with the default Xen boot option
360
	xenkopt="console=tty0"
361
362
# options to use with the alternative boot options
363
	altoptions="(recovery mode) single"
364
365
# controls howmany kernels are listed in the config file,
366
# this does not include the alternative kernels
367
	howmany="all"
368
369
# should grub create a memtest86 entry
370
	memtest86="true"
371
372
# should grub add "savedefault" to default boot options
373
	savedefault="false"
374
375
# stores the command line arguments
376
	command_line_arguments=$1
377
378
# does this version of grub support the quiet option?
379
if [ -f ${grub_dir}/installed-version ] && dpkg --compare-versions `cat ${grub_dir}/installed-version` ge 0.97-11ubuntu4; then
380
    supports_quiet=true
381
else
382
    supports_quiet=false
383
fi
384
385
# read user configuration
386
if test -f "/etc/default/grub" ; then
387
    . /etc/default/grub
388
fi
389
390
# Default options to use in a new config file. This will only be used if $menu_file
391
# doesn't already exist. Only edit the lines between the two "EOF"s. The others are
392
# part of the script.
393
newtemplate=$(tempfile)
394
cat > "$newtemplate" <<EOF
395
# $menu_file_basename - See: grub(8), info grub, update-grub(8)
396
#            grub-install(8), grub-floppy(8),
397
#            grub-md5-crypt, /usr/share/doc/grub
398
#            and /usr/share/doc/grub-doc/.
399
400
## default num
401
# Set the default entry to the entry number NUM. Numbering starts from 0, and
402
# the entry number 0 is the default if the command is not used.
403
#
404
# You can specify 'saved' instead of a number. In this case, the default entry
405
# is the entry saved with the command 'savedefault'.
406
# WARNING: If you are using dmraid do not use 'savedefault' or your
407
# array will desync and will not let you boot your system.
408
default		0
409
410
## timeout sec
411
# Set a timeout, in SEC seconds, before automatically booting the default entry
412
# (normally the first entry defined).
413
timeout		3
414
415
## hiddenmenu
416
# Hides the menu by default (press ESC to see the menu)
417
hiddenmenu
418
419
# Pretty colours
420
#color cyan/blue white/blue
421
422
## password ['--md5'] passwd
423
# If used in the first section of a menu file, disable all interactive editing
424
# control (menu entry editor and command-line)  and entries protected by the
425
# command 'lock'
426
# e.g. password topsecret
427
#      password --md5 \$1\$gLhU0/\$aW78kHK1QfV3P2b2znUoe/
428
# password topsecret
429
430
#
431
# examples
432
#
433
# title		Windows 95/98/NT/2000
434
# root		(hd0,0)
435
# makeactive
436
# chainloader	+1
437
#
438
# title		Linux
439
# root		(hd0,1)
440
# kernel	/vmlinuz root=/dev/hda2 ro
441
#
442
443
#
444
# Put static boot stanzas before and/or after AUTOMAGIC KERNEL LIST
445
446
EOF
447
## End Configuration Options
448
449
echo -n "Searching for default file ... " >&2
450
if [ -f "$default_file" ] ; then
451
  echo "found: $default_file" >&2
452
else
453
  echo "Generating $default_file file and setting the default boot entry to 0" >&2
454
  grub-set-default 0
455
fi
456
457
# Make sure we use the standard sorting order
458
LC_COLLATE=C
459
# Magic markers we use
460
start="### BEGIN AUTOMAGIC KERNELS LIST"
461
end="### END DEBIAN AUTOMAGIC KERNELS LIST"
462
463
startopt="## ## Start Default Options ##"
464
endopt="## ## End Default Options ##"
465
466
# Extract options from config file
467
ExtractMenuOpt()
468
{
469
	opt=$1
470
471
	sed -ne "/^$start\$/,/^$end\$/ {
472
		/^$startopt\$/,/^$endopt\$/ {
473
			/^# $opt=/ {
474
				s/^# $opt=\(.*\)\$/\1/
475
				p
476
			}
477
		}
478
	}" $menu
479
}
480
481
GetMenuOpts()
482
{
483
	opt=$1
484
485
	sed -ne "/^$start\$/,/^$end\$/ {
486
		/^$startopt\$/,/^$endopt\$/ {
487
			/^# $opt=/ {
488
				p
489
			}
490
		}
491
	}" $menu
492
}
493
494
ExtractMenuOpts()
495
{
496
	opt=$1
497
498
	GetMenuOpts $opt | sed "s/^# $opt=\(.*\)\$/\1=\"\2\"/"
499
}
500
501
GetMenuOpt()
502
{
503
	opt=$1
504
	value=$2
505
506
	[ -z "$(GetMenuOpts "$opt")" ] || value=$(ExtractMenuOpt "$opt")
507
508
	echo $value
509
}
510
511
# Compares two version strings A and B
512
# Returns -1 if A<B
513
#          0 if A==B
514
#          1 if A>B
515
# This compares version numbers of the form
516
# 2.4.14.2 > 2.4.14
517
# 2.4.14random = 2.4.14-random > 2.4.14-ac10 > 2.4.14 > 2.4.14-pre2 > 
518
# 2.4.14-pre1 > 2.4.13-ac99
519
CompareVersions()
520
{  
521
    	#Changes the line something-x.y.z into somthing-x.y.z.q
522
	#This is to ensure that kernels with a .q is treated as higher than the ones without               
523
        #First a space is put after the version number
524
        v1=$(echo $1 | sed -e 's!^\(.*-\([0-9]\+\.\)\{2,3\}[0-9]\+\)\(.*\)!\1 \3!g')
525
	v2=$(echo $2 | sed -e 's!^\(.*-\([0-9]\+\.\)\{2,3\}[0-9]\+\)\(.*\)!\1 \3!g')
526
	#If the version number only has 3 digits then put in another .0
527
        v1=$(echo $v1 | sed -e 's!^\(.*-\([0-9]\+\.\)\{2\}[0-9]\+\)\( .*\|$\)!\1.0 \3!g')
528
        v2=$(echo $v2 | sed -e 's!^\(.*-\([0-9]\+\.\)\{2\}[0-9]\+\)\( .*\|$\)!\1.0 \3!g')
529
          
530
	# Then split the version number and remove any '.' 's or dashes
531
	v1=$(echo $v1 | sed -e 's![-\.]\+! !g' -e 's!\([0-9]\)\([[:alpha:]]\)!\1 \2!')
532
	v2=$(echo $v2 | sed -e 's![-\.]\+! !g' -e 's!\([0-9]\)\([[:alpha:]]\)!\1 \2!')
533
534
	# we weight different kernel suffixes here
535
	# ac   = 50
536
	# pre  = -50
537
	# rc   = -40
538
	# test = -60
539
	# others are given 99
540
	v1=$(echo $v1 | sed -e 's! k7! 786 !g' -e 's! ac! 50 !g' -e 's! rc! -40 !g' -e 's! pre! -50 !g' -e 's! test! -60 !g' -e 's![^ ]*[^-0-9 ][^ ]*!99!g')
541
542
	v2=$(echo $v2 | sed -e 's! k7! 786 !g' -e 's! ac! 50 !g' -e 's! rc! -40 !g' -e 's! pre! -50 !g' -e 's! test! -60 !g' -e 's![^ ]*[^-0-9 ][^ ]*!99!g')
543
544
	result=0; v1finished=0; v2finished=0;
545
	while [ $result -eq 0 ] && [ $v1finished -eq 0 ] && [ $v2finished -eq 0 ];
546
	do
547
		if [ "$v1" = "" ]; then
548
			v1comp=0; v1finished=1
549
		else
550
			set -- $v1; v1comp=$1; shift; v1=$*
551
		fi
552
553
		if [ "$v2" = "" ]; then
554
			v2comp=0; v2finished=1
555
		else
556
			set -- $v2; v2comp=$1; shift; v2=$*
557
		fi
558
		
559
		set +e
560
		result=`expr $v1comp - $v2comp` 
561
		result=`expr substr $result 1 2`
562
		set -e
563
564
		if   [ $result -gt 0 ]; then result=1
565
		elif [ $result -lt 0 ]; then result=-1	
566
		fi	
567
	done
568
569
	# finally return the result
570
	echo $result
571
}
572
573
# looks in the directory specified for an initrd image with the version specified
574
FindInitrdName()
575
{
576
	# strip trailing slashes
577
	directory=$(echo $1 | sed -e 's#/*$##')
578
	version=$2
579
580
	# initrd
581
	# initrd.img
582
	# initrd-lvm
583
	# .*.gz
584
585
	initrdName=""
586
	names="initrd initrd.img initrd-lvm"
587
	compressed="gz"
588
589
	for n in $names ; do
590
		# make sure we haven't already found it
591
		if [ -z "$initrdName" ] ; then
592
			if [ -f "$directory/$n$version" ] ; then
593
				initrdName="$n$version"
594
				break
595
			else
596
				for c in $compressed ; do
597
					if [ -f "$directory/$n$version.$c" ] ; then
598
						initrdName="$n$version.$c"
599
						break
600
					fi
601
				done
602
			fi
603
		else
604
			break
605
		fi
606
	done
607
608
	# return the result
609
	echo $initrdName
610
}
611
612
FindXenHypervisorVersions ()
613
{
614
	version=$1
615
616
	if [ -f "/var/lib/linux-image-$version/xen-versions" ]; then
617
		ret="$(cat /var/lib/linux-image-$version/xen-versions)"
618
	fi
619
620
	echo $ret
621
}
622
623
get_kernel_opt()
624
{
625
	kernel_version=$1
626
627
	version=$(echo $kernel_version | sed 's/^[^0-9]*//')
628
	version=$(echo $version | sed 's/[-\+\.]/_/g')
629
	if [ -n "$version" ] ; then
630
		while [ -n "$version" ] ; do
631
			currentOpt="$(eval "echo \${kopt_$version}")"
632
			if [ -n "$currentOpt" ] ; then
633
				break
634
			fi
635
636
			oldversion="$version"
637
			version=$(echo $version | sed 's/_\?[^_]*$//')
638
			if [ "$version" = "$oldversion" ] ; then
639
				# Break infinite loop, if the version isn't what we expect
640
				break
641
			fi
642
		done
643
	fi
644
645
	if [ -z "$currentOpt" ] ; then
646
			currentOpt=$kopt
647
	fi
648
649
	echo $currentOpt
650
}
651
652
write_kernel_entry()
653
{
654
	local kernel_version; kernel_version=$1; shift
655
	local recovery_desc; recovery_desc=$1; shift
656
	local lock_alternative; lock_alternative=$1; shift
657
	local grub_root_device; grub_root_device=$1; shift
658
	local kernel; kernel=$1; shift
659
	local kernel_options; kernel_options=$1; shift
660
	local recovery_suffix; recovery_suffix=$1; shift
661
	local initrd; initrd=$1; shift
662
	local savedefault; savedefault=$1; shift
663
	local lockold; lockold=$1; shift
664
	local hypervisor
665
	if [ -n "$1" ]; then
666
		# Hypervisor.
667
		hypervisor=$1; shift
668
		local hypervisor_image; hypervisor_image=$1; shift
669
		local hypervisor_version; hypervisor_version=$1; shift
670
		local hypervisor_options; hypervisor_options=$1; shift
671
	fi
672
673
	echo -n "title		" >> $buffer
674
675
	if [ -n "$hypervisor" ]; then
676
		echo -n "$hypervisor $hypervisor_version / " >> $buffer
677
	fi
678
679
	echo -n "$title" >> $buffer
680
	if [ -n "$kernel_version" ]; then
681
		echo -n ", " >> $buffer
682
		# memtest86 is not strictly a kernel
683
		if ! echo "$kernel_version" | grep -q ^memtest86; then
684
			echo -n "kernel " >> $buffer
685
		fi
686
		echo -n "$kernel_version" >> $buffer
687
	fi
688
	if [ -n "$recovery_desc" ]; then
689
		echo -n " $recovery_desc" >> $buffer
690
	fi
691
	echo >> $buffer
692
693
	# lock the alternative options
694
	if test x"$lock_alternative" = x"true" ; then
695
		echo "lock" >> $buffer
696
	fi
697
	# lock the old entries
698
	if test x"$lockold" = x"true" ; then
699
	echo "lock" >> $buffer
700
	fi
701
69 by ago
Removed ntfs_3g init script since its functionality has been moved upstream
702
	echo "root		$grub_root_device" >> $buffer
703
55 by ago
Added init fixes for umountfs to make it skip /host folders (containing
704
	echo -n "kernel		"  >> $buffer
705
	if [ -n "$hypervisor" ]; then
706
		echo -n "$hypervisor_image" >> $buffer
707
		if [ -n "$hypervisor_options" ]; then
708
			echo -n " $hypervisor_options"  >> $buffer
709
		fi
710
		echo >> $buffer
711
		echo -n "module		"  >> $buffer
712
	fi
69 by ago
Removed ntfs_3g init script since its functionality has been moved upstream
713
	echo -n "$kernel"  >> $buffer
55 by ago
Added init fixes for umountfs to make it skip /host folders (containing
714
	if [ -n "$kernel_options" ]; then
715
		echo -n " $kernel_options"  >> $buffer
716
	fi
717
	if [ -n "$recovery_desc" ]; then
718
		echo -n " $recovery_suffix"  >> $buffer
719
	fi
720
	echo >> $buffer
721
722
	if [ -n "$initrd" ]; then
723
		if [ -n "$hypervisor" ]; then
724
			echo -n "module		" >> $buffer
725
		else
726
			echo -n "initrd		" >> $buffer
727
		fi
69 by ago
Removed ntfs_3g init script since its functionality has been moved upstream
728
		echo "$initrd" >> $buffer
55 by ago
Added init fixes for umountfs to make it skip /host folders (containing
729
	fi
730
731
	if [ ! -n "$recovery_desc" -a x"$supports_quiet" = x"true" ]; then
732
		echo "quiet" >> $buffer
733
	fi
734
735
	if test x"$savedefault" = x"true" ; then
736
		echo "savedefault" >> $buffer
737
	fi
738
	echo >> $buffer
739
}
740
741
convert_kopt_to_uuid()
742
{
743
	local kopt; kopt=$1
744
745
	convert=false
746
	root=$(echo "$kopt" | sed 's/.*root=//;s/ .*//')
747
	case "$root" in
748
		UUID=*|LABEL=*)
749
			;;
750
		/dev/disk/*)
751
			;;
752
		/dev/mapper/*)
753
			;;
754
		/dev/evms/[hs]d[a-z][0-9]*)
755
			convert=:
756
			;;
757
		/dev/evms/*)
758
			;;
759
		/dev/md[0-9]*)
760
			;;
761
		/dev/*)
762
			convert=:
763
			;;
764
	esac
765
	if $convert; then
766
		if [ -L "$DEV" ] && readlink "$DEV" | grep -q "^/dev/mapper/"
767
		then
768
			:
769
		elif [ -b "$root" ]; then
770
			uuid=$(/sbin/vol_id -u "$root" || true)
771
			if [ -n "$uuid" ]; then
772
				kopt=$(echo "$kopt" | sed "s/\(.*root=\)[^ ]*/\1UUID=$uuid/")
773
			fi
774
		fi
775
	fi
776
777
	echo "$kopt"
778
}
779
780
781
echo -n "Testing for an existing GRUB $menu_file_basename file ... " >&2
782
783
# Test if our menu file exists
784
if [ -f "$menu_file" ] ; then
785
	menu="$menu_file"
786
	rm -f $newtemplate
787
	unset newtemplate
788
	echo "found: $menu_file" >&2
789
	cp -f "$menu_file" "$menu_file~"
790
else
791
	# if not ask user if they want us to create one
792
	menu="$menu_file"
793
	echo >&2
794
	echo >&2
795
	echo -n "Could not find $menu_file file. " >&2
796
	if [ "-y" = "$command_line_arguments" ] ; then
797
		echo >&2
798
		echo "Generating $menu_file" >&2
799
		answer=y
800
	else
801
		echo -n "Would you like $menu_file generated for you? " >&2
802
		echo -n "(y/N) " >&2
803
		read answer
804
	fi
805
806
	case "$answer" in
807
		y* | Y*)
808
		cat "$newtemplate" > $menu_file
809
		rm -f $newtemplate
810
		unset newtemplate
811
		;;
812
		*)
813
		abort "Not creating $menu_file as you wish"
814
		;;
815
	esac
816
fi
817
818
# Extract the crashdump var
819
crashdump=$(GetMenuOpt "crashdump" "$crashdump")
820
821
# Extract the kernel options to use
822
kopt=$(GetMenuOpt "kopt" "$kopt")
823
824
# Update the root device to mount-by-UUID
825
kopt=$(convert_kopt_to_uuid "$kopt")
826
827
# Extract options for specific kernels
828
opts="$(ExtractMenuOpts "\(kopt_[[:alnum:]_]\+\)")"
829
test -z "$opts" || eval "$opts"
830
CustomKopts=$(GetMenuOpts "\(kopt_[[:alnum:]_]\+\)" | \
831
	grep -v "^# kopt_2_6=" || true)
832
833
# Set the kernel 2.6 option only for fresh install (but convert it to
834
# mount-by-UUID on upgrade)
835
test -z "$kopt_2_6" && test -z "$(GetMenuOpt "kopt" "")" && \
836
	kopt_2_6="$default_kopt"
837
test -n "$kopt_2_6" && kopt_2_6=$(convert_kopt_to_uuid "$kopt_2_6")
838
839
# Extract the grub root
840
grub_root_device=$(GetMenuOpt "groot" "$grub_root_device")
841
842
# Extract the old recovery value
843
alternative=$(GetMenuOpt "recovery" "$alternative")
844
845
# Extract the alternative value
846
alternative=$(GetMenuOpt "alternative" "$alternative")
847
848
# Extract the lockalternative value
849
lockalternative=$(GetMenuOpt "lockalternative" "$lockalternative")
850
851
# Extract the additional default options
852
# Check nonaltoptions too for compatibility with Ubuntu <= 5.10
853
defoptions=$(GetMenuOpt "nonaltoptions" "$defoptions")
854
defoptions=$(GetMenuOpt "defoptions" "$defoptions")
855
856
# Extract the lockold value
857
lockold=$(GetMenuOpt "lockold" "$lockold")
858
859
# Extract Xen hypervisor options
860
xenhopt=$(GetMenuOpt "xenhopt" "$xenhopt")
861
862
# Extract Xen Linux kernel options
863
xenkopt=$(GetMenuOpt "xenkopt" "$xenkopt")
864
865
# Extract the howmany value
866
howmany=$(GetMenuOpt "howmany" "$howmany")
867
868
# Extract the memtest86 value
869
memtest86=$(GetMenuOpt "memtest86" "$memtest86")
870
871
 
872
# Extract the updatedefaultentry option
873
updatedefaultentry=$(GetMenuOpt "updatedefaultentry" "$updatedefaultentry")
874
875
# If "default saved" is in use, set the default to true
876
grep -q "^default.*saved" $menu && savedefault=true
877
# Extract the savedefault option
878
savedefault=$(GetMenuOpt "savedefault" "$savedefault")
879
880
# Generate the menu options we want to insert
881
buffer=$(tempfile)
882
echo $start >> $buffer
883
echo "## lines between the AUTOMAGIC KERNELS LIST markers will be modified" >> $buffer
884
echo "## by the debian update-grub script except for the default options below" >> $buffer
885
echo >> $buffer
886
echo "## DO NOT UNCOMMENT THEM, Just edit them to your needs" >> $buffer
887
echo >> $buffer
888
echo "## ## Start Default Options ##" >> $buffer
889
890
echo "## default kernel options" >> $buffer
891
echo "## default kernel options for automagic boot options" >> $buffer
892
echo "## If you want special options for specific kernels use kopt_x_y_z" >> $buffer
893
echo "## where x.y.z is kernel version. Minor versions can be omitted." >> $buffer
894
echo "## e.g. kopt=root=/dev/hda1 ro" >> $buffer
895
echo "##      kopt_2_6_8=root=/dev/hdc1 ro" >> $buffer
896
echo "##      kopt_2_6_8_2_686=root=/dev/hdc2 ro" >> $buffer
897
echo "# kopt=$kopt" >> $buffer
898
if [ -n "$kopt_2_6" ] && [ "$kopt" != "$kopt_2_6" ]; then
899
    echo "# kopt_2_6=$kopt_2_6" >> $buffer
900
fi
901
if [ -n "$CustomKopts" ] ; then
902
    echo "$CustomKopts" >> $buffer
903
fi
904
echo >> $buffer
905
906
echo "## Setup crashdump menu entries" >> $buffer
907
echo "## e.g. crashdump=1" >> $buffer
908
echo "# crashdump=$crashdump" >> $buffer
909
echo >> $buffer
910
911
echo "## default grub root device" >> $buffer
912
echo "## e.g. groot=(hd0,0)" >> $buffer
913
echo "# groot=$grub_root_device" >> $buffer
914
echo >> $buffer
915
916
echo "## should update-grub create alternative automagic boot options" >> $buffer
917
echo "## e.g. alternative=true" >> $buffer
918
echo "##      alternative=false" >> $buffer
919
echo "# alternative=$alternative" >> $buffer
920
echo >> $buffer
921
922
echo "## should update-grub lock alternative automagic boot options" >> $buffer
923
echo "## e.g. lockalternative=true" >> $buffer
924
echo "##      lockalternative=false" >> $buffer
925
echo "# lockalternative=$lockalternative" >> $buffer
926
echo >> $buffer
927
928
echo "## additional options to use with the default boot option, but not with the" >> $buffer
929
echo "## alternatives" >> $buffer
930
echo "## e.g. defoptions=vga=791 resume=/dev/hda5" >> $buffer
931
echo "# defoptions=$defoptions" >> $buffer
932
echo >> $buffer
933
934
echo "## should update-grub lock old automagic boot options" >> $buffer
935
echo "## e.g. lockold=false" >> $buffer
936
echo "##      lockold=true" >> $buffer
937
echo "# lockold=$lockold" >> $buffer
938
echo >> $buffer
939
940
echo "## Xen hypervisor options to use with the default Xen boot option" >> $buffer
941
echo "# xenhopt=$xenhopt" >> $buffer
942
echo >> $buffer
943
944
echo "## Xen Linux kernel options to use with the default Xen boot option" >> $buffer
945
echo "# xenkopt=$xenkopt" >> $buffer
946
echo >> $buffer
947
948
echo "## altoption boot targets option" >> $buffer
949
echo "## multiple altoptions lines are allowed" >> $buffer
950
echo "## e.g. altoptions=(extra menu suffix) extra boot options" >> $buffer
951
echo "##      altoptions=(recovery) single" >> $buffer
952
953
if ! grep -q "^# altoptions" $menu ; then
954
	echo "# altoptions=$altoptions" >> $buffer
955
else
956
	grep "^# altoptions" $menu >> $buffer
957
fi
958
echo >> $buffer
959
960
echo "## controls how many kernels should be put into the $menu_file_basename" >> $buffer
961
echo "## only counts the first occurence of a kernel, not the" >> $buffer
962
echo "## alternative kernel options" >> $buffer
963
echo "## e.g. howmany=all" >> $buffer
964
echo "##      howmany=7" >> $buffer
965
echo "# howmany=$howmany" >> $buffer
966
echo >> $buffer
967
968
969
echo "## should update-grub create memtest86 boot option" >> $buffer
970
echo "## e.g. memtest86=true" >> $buffer
971
echo "##      memtest86=false" >> $buffer
972
echo "# memtest86=$memtest86" >> $buffer
973
echo >> $buffer
974
975
echo "## should update-grub adjust the value of the default booted system" >> $buffer
976
echo "## can be true or false" >> $buffer
977
echo "# updatedefaultentry=$updatedefaultentry" >> $buffer
978
echo >> $buffer
979
980
echo "## should update-grub add savedefault to the default options" >> $buffer
981
echo "## can be true or false" >> $buffer
982
echo "# savedefault=$savedefault" >> $buffer
983
echo >> $buffer
984
985
echo "## ## End Default Options ##" >> $buffer
986
echo >> $buffer
987
988
echo -n "Searching for splash image ... " >&2
989
current_splash=`grep '^splashimage=' ${menu_file} || true`
990
splashimage_path="splashimage=${grub_root_device}${grub_dir##${boot_device:+/boot}}/splash.xpm.gz"
991
if [ `sed -e "/^$start/,/^$end/d" $menu_file | grep -c '^splashimage='` != "0" ] ; then
992
       #checks for splashscreen defined outside the autoupdated part
993
       splashimage=$(grep '^splashimage=' ${menu_file})
994
       echo "found: ${splashimage##*=}" >&2
995
       echo >&2  
996
elif [ -f "${grub_dir}/splash.xpm.gz" ]  && [ "$current_splash" = "" ]; then
997
       echo "found: /boot/grub/splash.xpm.gz" >&2
998
       echo "$splashimage_path" >> $buffer
999
       echo >> $buffer
1000
elif [ -f "${grub_dir}/splash.xpm.gz" ]  && [ "$current_splash" = "$splashimage_path" ]; then
1001
       echo "found: /boot/grub/splash.xpm.gz" >&2
1002
       echo "$splashimage_path" >> $buffer
1003
       echo >> $buffer
1004
elif [ "$current_splash" != "" ] && [ "$current_splash" != "$splashimage_path" ]; then
1005
       echo "found but preserving previous setting: $(grep '^splashimage=' ${menu_file})" >&2
1006
       echo "$current_splash" >> $buffer
1007
       echo >> $buffer
1008
else
1009
       echo "none found, skipping ..." >&2
1010
fi
1011
1012
1013
xenKernels=""
1014
for ver in `grep -l CONFIG_XEN_PRIVILEGED_GUEST=y /boot/config* | sed -e s%/boot/config-%%`; do
1015
  # ver is a kernel version
1016
  kern="/boot/vmlinuz-$ver"
1017
  if [ -r $kern ] ; then
1018
       newerKernels=""
1019
       for i in $xenKernels ; do
1020
                res=$(CompareVersions "$kern" "$i")
1021
                if [ "$kern" != "" ] && [ "$res" -gt 0 ] ; then
1022
                        newerKernels="$newerKernels $kern $i"
1023
                        kern=""
1024
                else
1025
                        newerKernels="$newerKernels $i"
1026
                fi
1027
        done
1028
        if [ "$kern" != "" ] ; then
1029
                newerKernels="$newerKernels $kern"
1030
        fi
1031
        xenKernels="$newerKernels"
1032
    fi
1033
done
1034
1035
in_domU=
1036
if [ -e /proc/xen/capabilities ] && ! grep -q "control_d" /proc/xen/capabilities; then
1037
	in_domU=1
1038
fi
1039
1040
sortedKernels=""
1041
for kern in $(/bin/ls -1vr /boot | grep -v "dpkg-*" | grep "^vmlinuz-") ; do
1042
	if `echo "$xenKernels" | grep -q "$kern "` || `echo "$kern" | grep -q "xen"`; then
1043
		is_xen=1
1044
	else
1045
		is_xen=
1046
	fi
1047
1048
	if [ ! "$in_domU" ] && [ "$is_xen" ]; then
1049
	  # skip xen kernels
1050
          continue
1051
        elif [ "$in_domU" ] && ! [ "$is_xen" ]; then
1052
	  # skip non-xen kernels
1053
	  continue
1054
        fi
1055
        kern="/boot/$kern"
1056
	newerKernels=""
1057
	for i in $sortedKernels ; do
1058
	    res=$(CompareVersions "$kern" "$i")
1059
	    if [ "$kern" != "" ] && [ "$res" -gt 0 ] ; then
1060
		newerKernels="$newerKernels $kern $i"
1061
	 	kern=""
1062
	    else
1063
		newerKernels="$newerKernels $i"
1064
	    fi
1065
	done
1066
	if [ "$kern" != "" ] ; then
1067
	    newerKernels="$newerKernels $kern"
1068
	fi
1069
	sortedKernels="$newerKernels"
1070
done
1071
1072
if test -f "/boot/vmlinuz.old" ; then
1073
	sortedKernels="/boot/vmlinuz.old $sortedKernels"
1074
fi
1075
if test -f "/boot/vmlinuz" ; then
1076
	sortedKernels="/boot/vmlinuz $sortedKernels"
1077
fi
1078
1079
hypervisors=""
1080
for hyp in /boot/xen-*.gz; do
1081
    if [ ! -h "$hyp" ] && [ -f "$hyp" ]; then
1082
	hypervisors="$hypervisors `basename "$hyp"`"
1083
    fi
1084
done
1085
1086
# figure out where grub looks for the kernels at boot time
1087
kernel_dir=/boot
1088
if [ -n "$boot_device" ] ; then
1089
	kernel_dir=
1090
fi
1091
1092
#Finding the value the default line
1093
use_grub_set_default="false"
1094
if test "$updatedefaultentry" = "true" ; then
1095
	defaultEntryNumber=$(sed -ne 's/^[[:blank:]]*default[[:blank:]]*\(.*\).*/\1/p' $menu)
1096
1097
	if [ "$defaultEntryNumber" = "saved" ] ; then
1098
	    defaultEntryNumber=$(sed 'q' "$grub_dir/default")
1099
	    use_grub_set_default="true"	   
1100
	fi
1101
	
1102
	if test -n "$defaultEntryNumber"; then	
1103
		defaultEntryNumberPlusOne=$(expr $defaultEntryNumber \+ 1);
1104
		defaultEntry=$(grep "^[[:blank:]]*title" $menu | sed -ne "${defaultEntryNumberPlusOne}p" | sed -ne ";s/^[[:blank:]]*title[[:blank:]]*//p")
1105
		defaultEntry=$(echo $defaultEntry | sed -e "s/[[:blank:]]*$//") # don't trust trailing blanks	
1106
	else
1107
		notChangeDefault="yes"
1108
	fi
1109
else
1110
		notChangeDefault="yes"
1111
fi
1112
1113
## heres where we start writing out the kernel entries
1114
counter=0
1115
1116
# Xen entries first.
1117
for kern in $xenKernels ; do
1118
	if test ! x"$howmany" = x"all" ; then
1119
		if [ $counter -gt $howmany ] ; then
1120
			break
1121
		fi
1122
	fi
1123
1124
	kernelName=$(basename $kern)
1125
	kernelVersion=$(echo $kernelName | sed -e 's/vmlinuz//')
1126
1127
	initrdName=$(FindInitrdName "/boot" "$kernelVersion")
1128
	initrd=""
1129
1130
	kernel=$kernel_dir/$kernelName
1131
	if [ -n "$initrdName" ] ; then
1132
		initrd=$kernel_dir/$initrdName
1133
	fi
1134
1135
	kernelVersion=$(echo $kernelVersion | sed -e 's/^-//')
1136
	currentOpt=$(get_kernel_opt $kernelVersion)
1137
1138
	hypervisorVersions=$(FindXenHypervisorVersions "$kernelVersion")
1139
1140
	found=
1141
	for hypervisorVersion in $hypervisorVersions; do
1142
		hypervisor="$kernel_dir/xen-$hypervisorVersion.gz"
1143
		if [ -e "$hypervisor" ]; then
1144
			found=1
1145
1146
			echo "Found Xen hypervisor $hypervisorVersion,  kernel: $kernel" >&2
1147
1148
			write_kernel_entry "$kernelVersion" '' '' "$grub_root_device" \
1149
			  "$kernel" "$currentOpt $xenkopt" '' "$initrd" "$savedefault" '' \
1150
			  Xen "$hypervisor" "$hypervisorVersion" "$xenhopt"
1151
	        counter=$(($counter + 1))
1152
		fi
1153
	done
1154
1155
	if [ -z $found ]; then
1156
		for hypervisor in $hypervisors; do
1157
			hypVersion=`basename "$hypervisor" .gz | sed s%xen-%%`
1158
		
1159
			echo "Found Xen hypervisor $hypVersion,  kernel: $kernel" >&2
1160
1161
			write_kernel_entry "$kernelVersion" '' '' "$grub_root_device" \
1162
			  "$kernel" "$currentOpt $xenkopt" '' "$initrd" "$savedefault" '' \
1163
			  Xen "$kernel_dir/$hypervisor" "$hypVersion" "$xenhopt"
1164
	        counter=$(($counter + 1))
1165
		done
1166
	fi
1167
done
1168
1169
for kern in $sortedKernels ; do
1170
	counter=$(($counter + 1))
1171
	if test ! x"$howmany" = x"all" ; then
1172
		if [ $counter -gt $howmany ] ; then 
1173
			break
1174
		fi
1175
	fi
1176
	kernelName=$(basename $kern)
1177
	kernelVersion=$(echo $kernelName | sed -e 's/vmlinuz//')
1178
	initrdName=$(FindInitrdName "/boot" "$kernelVersion")
1179
	initrd=""
1180
1181
	kernel=$kernel_dir/$kernelName
1182
	if [ -n "$initrdName" ] ; then
1183
		initrd=$kernel_dir/$initrdName
1184
	fi
1185
1186
	echo "Found kernel: $kernel" >&2
1187
1188
	if [ "$kernelName" = "vmlinuz" ]; then
1189
		if [ -L "/boot/$kernelName" ]; then
1190
			kernelVersion=`readlink -f "/boot/$kernelName"`
1191
			kernelVersion=$(echo $kernelVersion | sed -e 's/.*vmlinuz-//')
1192
			kernelVersion="$kernelVersion Default"
1193
		else
1194
			kernelVersion="Default"
1195
		fi
1196
	fi
1197
	if [ "$kernelName" = "vmlinuz.old" ]; then
1198
		if [ -L "/boot/$kernelName" ]; then
1199
			kernelVersion=`readlink -f "/boot/$kernelName"`
1200
			kernelVersion=$(echo $kernelVersion | sed -e 's/.*vmlinuz-//')
1201
			kernelVersion="$kernelVersion Previous"
1202
		else
1203
			kernelVersion="Previous"
1204
		fi
1205
	fi
1206
	kernelVersion=$(echo $kernelVersion | sed -e 's/^-//')
1207
	
1208
	currentOpt=$(get_kernel_opt $kernelVersion)
1209
1210
	do_lockold=$lockold
1211
	# do not lockold for the first entry
1212
	[ $counter -eq 1 ] && do_lockold=false
1213
1214
	write_kernel_entry "$kernelVersion" "" "" "$grub_root_device" "$kernel" \
1215
		"$currentOpt $defoptions" "" "$initrd" "$savedefault" "$do_lockold"
1216
1217
	# insert the alternative boot options
1218
	if test ! x"$alternative" = x"false" ; then
1219
		# for each altoptions line do this stuff
1220
		sed -ne 's/# altoptions=\(.*\)/\1/p' $buffer | while read line; do
1221
			descr=$(echo $line | sed -ne 's/\(([^)]*)\)[[:space:]]\(.*\)/\1/p')
1222
			suffix=$(echo $line | sed -ne 's/\(([^)]*)\)[[:space:]]\(.*\)/\2/p')
1223
1224
			test x"$lockalternative" = x"true" && do_lockold=false
1225
			write_kernel_entry "$kernelVersion" "$descr" "$lockalternative" \
1226
				"$grub_root_device" "$kernel" "$currentOpt" "$suffix" "$initrd" \
1227
				"false" "$do_lockold"
1228
		done
1229
	fi
1230
1231
	if [ "$crashdump" = "1" ] ; then
1232
		test x"$lockalternative" = x"true" && do_lockold=false
1233
		write_kernel_entry "$kernelVersion" "(crashdump mode)" "$lockalternative" \
1234
			"$grub_root_device" "$kernel" "$currentOpt" "crashkernel=64M@16M" "$initrd" \
1235
			"false" "$do_lockold"
1236
	fi
1237
done
1238
1239
grub2name="/boot/grub/core.img"
1240
if test -f $grub2name ; then
1241
	echo "Found GRUB 2: $grub2name" >&2
1242
	cat >> $buffer << EOF
1243
title		Chainload into GRUB 2
1244
root		$grub_root_device
1245
kernel		$grub2name
1246
EOF
1247
	if test x"$savedefault" = x"true" ; then
1248
		echo "savedefault" >> $buffer
1249
	fi
1250
	echo >> $buffer
1251
fi
1252
    
1253
memtest86names="memtest86 memtest86+"
1254
1255
if test ! x"$memtest86" = x"false" ; then
1256
	for name in $memtest86names ; do
1257
		if test -f "/boot/$name.bin" ; then
1258
			kernelVersion="$name"
1259
			kernel="$kernel_dir/$name.bin"
1260
			currentOpt=
1261
			initrd=
1262
1263
			echo "Found kernel: $kernel" >&2
1264
1265
			write_kernel_entry "$kernelVersion" "" "" "$grub_root_device" \
1266
			"$kernel" "$currentOpt" "" "$initrd" "false" ""
1267
		fi
1268
	done
1269
fi
1270
1271
echo $end >> $buffer
1272
1273
echo -n "Updating $menu ... " >&2
1274
# Insert the new options into the menu
1275
if ! grep -q "^$start" $menu ; then
1276
    cat $buffer >> $menu
1277
    rm -f $buffer
1278
else
1279
    umask 077
1280
    sed -e "/^$start/,/^$end/{
1281
	/^$start/r $buffer
1282
	d
1283
	}
1284
	" $menu > $menu.new
1285
    cat $menu.new > $menu
1286
    rm -f $buffer $menu.new
1287
fi
1288
1289
# Function to update the default value
1290
set_default_value() {
1291
    if [ "$use_grub_set_default" = "true" ] ; then
1292
	grub-set-default $1
1293
    else
1294
	value="$1"
1295
	newmenu=$(tempfile)
1296
	sed -e "s/^[[:blank:]]*default[[:blank:]]*[[:digit:]]*\(.*\)/default         ${value}\1/;b" $menu > $newmenu
1297
	cat $newmenu > $menu
1298
	rm -f $newmenu
1299
	unset newmenu
1300
    fi
1301
}
1302
1303
#Updating the default number
1304
if test -z "$notChangeDefault"; then
1305
	newDefaultNumberPlusOne=$(grep "^[[:blank:]]*title[[:blank:]]*" $menu | grep -n "${defaultEntry}" | cut -f1 -d ":" | sed -ne "1p")
1306
	if test -z "$newDefaultNumberPlusOne"; then
1307
		echo "Previous default entry removed, resetting to 0">&2
1308
		set_default_value "0"
1309
	elif test -z "$defaultEntry"; then
1310
		echo "Value of default value matches no entry, resetting to 0" >&2
1311
		set_default_value "0"
1312
	else
1313
		if test "$newDefaultNumberPlusOne" = "1"; then
1314
			newDefaultNumber="0"
1315
		else
1316
			newDefaultNumber=$(expr $newDefaultNumberPlusOne - 1)
1317
		fi
1318
		echo "Updating the default booting kernel">&2
1319
		set_default_value "$newDefaultNumber"
1320
	fi
1321
fi
1322
1323
echo "done" >&2
1324
echo >&2