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