~ubuntu-installer/wubi/gutsy

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