grub-install.in 31.3 KB
Newer Older
1
2
3
#! /bin/sh

# Install GRUB on your drive.
4
# Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009  Free Software Foundation, Inc.
5
#
6
7
8
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
9
10
# (at your option) any later version.
#
11
12
13
14
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
16
#
# You should have received a copy of the GNU General Public License
17
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
19

# Initialize some variables.
20
21
transform="@program_transform_name@"

22
23
prefix="@prefix@"
exec_prefix="@exec_prefix@"
24
datarootdir="@datarootdir@"
25
26
27
28
sbindir="@sbindir@"
bindir="@bindir@"
libdir="@libdir@"
sysconfdir="@sysconfdir@"
29
30
31
PACKAGE_NAME=@PACKAGE_NAME@
PACKAGE_TARNAME=@PACKAGE_TARNAME@
PACKAGE_VERSION=@PACKAGE_VERSION@
32
33
34
35

export TEXTDOMAIN=@PACKAGE@
export TEXTDOMAINDIR="@localedir@"

36
host_os=@host_os@
37
38
source_dir=
target=
39
datadir="@datadir@"
40
if [ "x$pkgdatadir" = x ]; then
41
    pkgdatadir="${datadir}/@PACKAGE@"
42
fi
43
localedir="@datadir@/locale"
44

45
self="`basename $0`"
46

47
48
49
50
grub_mkimage="${bindir}/`echo grub-mkimage | sed ${transform}`"
grub_probe="${sbindir}/`echo grub-probe | sed ${transform}`"
grub_editenv="${bindir}/`echo grub-editenv | sed ${transform}`"
grub_mkrelpath="${bindir}/`echo grub-mkrelpath | sed ${transform}`"
51
rootdir=
52
bootdir=
53
grubdir="`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`"
54
modules=
55
56
57

install_device=
force_lba=
58
recheck=no
59
debug=no
60
debug_image=
61

62
63
update_nvram=yes

Colin Watson's avatar
Colin Watson committed
64
65
removable=no
efi_quiet=
66

67
# Get GRUB_DISTRIBUTOR.
68
69
if test -f "${sysconfdir}/default/grub" ; then
    . "${sysconfdir}/default/grub"
70
71
fi

72
bootloader_id="$(echo "$GRUB_DISTRIBUTOR" | tr 'A-Z' 'a-z' | cut -d' ' -f1)"
73
74
75
76
if test -z "$bootloader_id"; then
    bootloader_id=grub
fi

77
disk_module=unspecified
78

79
. "${pkgdatadir}/grub-mkconfig_lib"
80

81
82
83
# Usage: usage
# Print the usage.
usage () {
84
85
    # TRANSLATORS: INSTALL_DEVICE isn't an identifier and is the DEVICE you
    # install to.
86
    gettext_printf "Usage: %s [OPTION] [INSTALL_DEVICE]" "$self"
87
88
89
    echo
    gettext "Install GRUB on your drive." ; echo
    echo
90
91
92
    print_option_help "-h, --help" "$(gettext "print this message and exit")"
    print_option_help "-v, --version" "$(gettext "print the version information and exit")"
    print_option_help "--modules=$(gettext "MODULES")" "$(gettext "pre-load specified modules MODULES")"
93
    dirmsg="$(gettext_printf "install GRUB images under the directory DIR/%s instead of the %s directory" "@grubdirname@" "$grubdir")"
94
    print_option_help "--boot-directory=$(gettext "DIR")" "$dirmsg"
95
    # TRANSLATORS: "TARGET" as in "target platform".
96
97
    target_trans="$(gettext "TARGET")"
    # TRANSLATORS: "current" refers to the platform user's currently running on
98
99
100
101
102
103
    print_option_help "--target=$target_trans" "$(gettext "install GRUB for TARGET platform [default=current]")"
    print_option_help "--directory=$(gettext "DIR")" "$(gettext "use GRUB images from DIR. Takes precedence over target")"
    print_option_help "--grub-setup=$(gettext "FILE")" "$(gettext "use FILE as grub-setup")"
    print_option_help "--grub-mkimage=$(gettext "FILE")" "$(gettext "use FILE as grub-mkimage")"
    print_option_help "--grub-mkrelpath=$(gettext "FILE")" "$(gettext "use FILE as grub-mkrelpath")"
    print_option_help "--grub-probe=$(gettext "FILE")" "$(gettext "use FILE as grub-probe")"
104
105
    # TRANSLATORS: "may break" doesn't just mean that option wouldn't have any
    # effect but that it will make the resulting install unbootable from HDD.
106
107
108
109
110
111
112
113
    print_option_help "--allow-floppy" "$(gettext "make the drive also bootable as floppy (default for fdX devices). May break on some BIOSes.")"
    print_option_help "--recheck" "$(gettext "delete device map if it already exists")"
    print_option_help "--force" "$(gettext "install even if problems are detected")"
    print_option_help "--force-file-id" "$(gettext "use identifier file even if UUID is available")"
    print_option_help "--disk-module=$(gettext "MODULE")" "$(gettext "disk module to use (biosdisk or native). This option is only available on BIOS target.")"
    print_option_help "--no-nvram" "$(gettext "don't update the \`boot-device' NVRAM variable. This option is only available on IEEE1275 targets.")"
    print_option_help  "--removable" "$(gettext "the installation device is removable. This option is only available on EFI.")"
    print_option_help  "--bootloader-id=$(gettext "ID")" "$(gettext "the ID of bootloader. This option is only available on EFI.")"
114
    print_option_help "--efi-directory=$(gettext "DIR")" "$(gettext "use DIR as the EFI System Partition root.")"
115
116
117
118
119
120
121
122
echo
gettext "INSTALL_DEVICE must be system device filename.";echo
echo

gettext_printf "%s copies GRUB images into %s, and uses grub-setup
to install grub into the boot sector.\n" "$self" "$grubdir";echo
echo
gettext "Report bugs to <bug-grub@gnu.org>."; echo
123
124
}

125
argument () {
126
  opt="$1"
127
128
129
  shift

  if test $# -eq 0; then
130
      gettext_printf "%s: option requires an argument -- \`%s'\n" "$0" "$opt" 1>&2
131
132
      exit 1
  fi
133
  echo "$1"
134
135
}

136
allow_floppy=""
137
force_file_id=
138
efidir=
139

140
# Check the arguments.
BVK Chaitanya's avatar
BVK Chaitanya committed
141
142
143
144
145
while test $# -gt 0
do
    option=$1
    shift

146
147
148
149
150
    case "$option" in
    -h | --help)
	usage
	exit 0 ;;
    -v | --version)
151
	echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
152
	exit 0 ;;
BVK Chaitanya's avatar
BVK Chaitanya committed
153
154

    --modules)
155
	modules=`argument $option "$@"`; shift;;
156
157
    --modules=*)
	modules=`echo "$option" | sed 's/--modules=//'` ;;
BVK Chaitanya's avatar
BVK Chaitanya committed
158

159
160
161
    --force-file-id)
	force_file_id=y ;;

162
# Accept and ignore for compatibility
BVK Chaitanya's avatar
BVK Chaitanya committed
163
    --font)
164
        shift;;
165
    --font=*)
166
	;;
BVK Chaitanya's avatar
BVK Chaitanya committed
167

168
# Accept for compatibility
BVK Chaitanya's avatar
BVK Chaitanya committed
169
    --root-directory)
170
	rootdir="`argument $option "$@"`"; shift;;
171
    --root-directory=*)
172
	rootdir="`echo "$option" | sed 's/--root-directory=//'`" ;;
BVK Chaitanya's avatar
BVK Chaitanya committed
173

174
    --boot-directory)
175
	bootdir="`argument $option "$@"`"; shift;;
176
    --boot-directory=*)
177
	bootdir="`echo "$option" | sed 's/--boot-directory=//'`" ;;
178

179
180
181
182
183
    --efi-directory)
	efidir="`argument $option "$@"`"; shift;;
    --efi-directory=*)
	efidir="`echo "$option" | sed 's/--efi-directory=//'`" ;;

184
185
186
187
188
189
190
191
192
193
    --directory | -d)
	source_dir="`argument $option "$@"`"; shift;;
    --directory=*)
	source_dir="`echo "$option" | sed 's/--directory=//'`" ;;

    --target)
	target="`argument $option "$@"`"; shift;;
    --target=*)
	target="`echo "$option" | sed 's/--target=//'`" ;;

BVK Chaitanya's avatar
BVK Chaitanya committed
194
    --grub-setup)
195
	grub_setup="`argument "$option" "$@"`"; shift;;
196
    --grub-setup=*)
197
	grub_setup="`echo "$option" | sed 's/--grub-setup=//'`" ;;
BVK Chaitanya's avatar
BVK Chaitanya committed
198

199
    --bootloader-id)
200
	bootloader_id="`argument $option "$@"`"; shift;;
201
    --bootloader-id=*)
202
	bootloader_id="`echo "$option" | sed 's/--bootloader-id=//'`" ;;
203

BVK Chaitanya's avatar
BVK Chaitanya committed
204
    --grub-mkimage)
205
	grub_mkimage="`argument $option "$@"`"; shift;;
206
    --grub-mkimage=*)
207
	grub_mkimage="`echo "$option" | sed 's/--grub-mkimage=//'`" ;;
BVK Chaitanya's avatar
BVK Chaitanya committed
208

209
    --grub-mkrelpath)
210
	grub_mkrelpath="`argument "$option" "$@"`"; shift;;
211
    --grub-mkrelpath=*)
212
	grub_mkrelpath="`echo "$option" | sed 's/--grub-mkrelpath=//'`" ;;
213

214
       # Ignore: for compatibility
BVK Chaitanya's avatar
BVK Chaitanya committed
215
    --grub-mkdevicemap)
216
	shift;;
217
    --grub-mkdevicemap=*)
218
	;;
BVK Chaitanya's avatar
BVK Chaitanya committed
219
220

    --grub-probe)
221
	    grub_probe="`argument "$option" "$@"`"; shift;;
robertmh's avatar
   
robertmh committed
222
    --grub-probe=*)
223
	grub_probe="`echo "$option" | sed 's/--grub-probe=//'`" ;;
BVK Chaitanya's avatar
BVK Chaitanya committed
224

225
    --no-floppy)
226
	;;
227
    --recheck)
228
	recheck=yes ;;
Colin Watson's avatar
Colin Watson committed
229
230
    --removable)
	removable=yes ;;
BVK Chaitanya's avatar
BVK Chaitanya committed
231

232
233
234
    --allow-floppy)
	allow_floppy="--allow-floppy" ;;

BVK Chaitanya's avatar
BVK Chaitanya committed
235
    --disk-module)
236
237
	disk_module="`argument "$option" "$@"`"; shift;
	;;
238
    --disk-module=*)
239
240
        disk_module="`echo "$option" | sed 's/--disk-module=//'`"
	;;
BVK Chaitanya's avatar
BVK Chaitanya committed
241

242
243
244
    --no-nvram)
	update_nvram=no ;;

245
246
247
    # This is an undocumented feature...
    --debug)
	debug=yes ;;
248
    --debug-image)
249
	debug_image="`argument "$option" "$@"`"; shift;;
250
    --debug-image=*)
251
	debug_image="`echo "$option" | sed 's/--debug-image=//'`" ;;
252

253
254
    -f | --force)
        setup_force="--force" ;;
BVK Chaitanya's avatar
BVK Chaitanya committed
255

256
    -*)
257
	gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
258
259
260
261
262
	usage
	exit 1
	;;
    *)
	if test "x$install_device" != x; then
263
	    gettext "More than one install device?" 1>&2
264
	    echo 1>&2
265
266
267
268
269
270
271
	    usage
	    exit 1
	fi
	install_device="${option}" ;;
    esac
done

272
273
274
275
276
277
278
279
280
281
282
283
284
285
if [ x$source_dir = x ]; then
    if [ x$target = x ]; then
	case x"`uname -m`" in
	    x"powerpc"* | x"ppc"*)
		target="powerpc-ieee1275";;
	    x"sparc"*)
		target="sparc64-ieee1275";;
	    x"mips"*"el")
		target="mipsel-loongson";;
	    x"mips"*)
		target="mips-arc";;
	    x"ia64"*)
		target="ia64-efi";;
	    x"x86_64"* | x"amd64"*)
286
287
288
289
290
291
292
293
294
                # On Linux, we need the efivars kernel modules.
		# If no EFI is available this module just does nothing
		# besides a small hello and if we detect efi we'll load it
		# anyway later. So it should be safe to
		# try to load it here.
		case "$host_os" in
		    linux*)
			modprobe -q efivars 2>/dev/null || true ;;
		esac
295
296
		if [ -d /sys/firmware/efi ]; then
		    target="x86_64-efi"
297
298
		else
		    target=i386-pc
299
		fi
300
		;;
301
	    x"i"?"86"*)
302
303
304
305
306
307
308
309
310
                # On Linux, we need the efivars kernel modules.
		# If no EFI is available this module just does nothing
		# besides a small hello and if we detect efi we'll load it
		# anyway later. So it should be safe to
		# try to load it here.
		case "$host_os" in
		    linux*)
			modprobe -q efivars 2>/dev/null || true ;;
		esac
311
312
		if [ -d /sys/firmware/efi ]; then
		    target="i386-efi"
313
		elif [ -e /proc/device-tree ]; then
314
		    target="i386-ieee1275"
315
316
		else
		    target=i386-pc
317
		fi
318
		;;
319
320
321
322
323
324
325
326
	    *)
		gettext "Unable to determine your platform. Use --target." ;
		echo	;;
	esac
    fi
    source_dir="${libdir}/@PACKAGE@/$target"
fi

327
328
if ! [ -d "$source_dir" ]; then
    gettext_printf "%s doesn't exist. Please specify --target or --directory\\n" "source_dir"
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
    exit 1
fi

. "${source_dir}"/modinfo.sh

if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] ; then
    if [ x$disk_module = xunspecified ]; then
	disk_module=biosdisk
    fi
elif [ "${grub_modinfo_platform}" = "ieee1275" ] || [ "${grub_modinfo_platform}" = "efi" ] ; then
    disk_module=
else
    disk_module=native
fi

if test "x$grub_setup" = x && [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ]; then
345
346
347
    grub_setup="${sbindir}/`echo grub-bios-setup | sed ${transform}`"
fi

348
if test "x$grub_setup" = x && [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ]; then
349
350
351
    grub_setup="${sbindir}/`echo grub-sparc64-setup | sed ${transform}`"
fi

352
353
if test "x$install_device" = x && ([ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] \
    || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ]); then
354
355
    gettext "Install device isn't specified." 1>&2
    echo 1>&2
356
357
358
359
    usage
    exit 1
fi

360
361
362
363
if ! ([ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] \
    || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ] \
    || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "powerpc-ieee1275" ] \
    || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "mips-arc" ]); then
364
365
366
    install_device=
fi

367
# If the debugging feature is enabled, print commands.
368
setup_verbose=
369
if test x"$debug" = xyes; then
370
    set -x
371
    setup_verbose="--verbose"
Colin Watson's avatar
Colin Watson committed
372
    efi_quiet=-q
373
374
fi

375
376
if [ -z "$bootdir" ]; then
    # Default bootdir if bootdir not initialized.
377
    bootdir="/@bootdirname@"
378

379
380
    if [ -n "$rootdir" ] ; then
        # Initialize bootdir if rootdir was initialized.
381
        bootdir="${rootdir}/@bootdirname@"
382
383
    fi
fi
384

385
386
387
grubdir="`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`"
device_map="${grubdir}/device.map"

388

389
# Check if GRUB is installed.
390
if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ] ; then
391
392
393
394
395
396
397
    set $grub_setup dummy
    if test -f "$1"; then
        :
    else
        echo "$1: Not found." 1>&2
        exit 1
    fi
398
399
fi

400
set "$grub_mkimage" dummy
401
402
403
404
405
406
407
if test -f "$1"; then
    :
else
    echo "$1: Not found." 1>&2
    exit 1
fi

408
if [ x"$grub_modinfo_platform" = xefi ]; then
409
    # Find the EFI System Partition.
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
    if test -n "$efidir"; then
	install_device="`"$grub_probe" --target=device --device-map= "${efidir}"`"
    else
	if test -d "${bootdir}/efi"; then
	    install_device="`"$grub_probe" --target=device --device-map= "${bootdir}/efi"`"
            # Is it a mount point?
	    if test "x$install_device" != "x`"$grub_probe" --target=device --device-map= "${bootdir}"`"; then
		efidir="${bootdir}/efi"
	    fi
	elif test -d "${bootdir}/EFI"; then
	    install_device="`"$grub_probe" --target=device --device-map= "${bootdir}/EFI"`"
            # Is it a mount point?
	    if test "x$install_device" != "x`"$grub_probe" --target=device --device-map= "${bootdir}"`"; then
		efidir="${bootdir}/EFI"
	    fi
	elif test -n "$rootdir" && test "x$rootdir" != "x/"; then
426
427
        # The EFI System Partition may have been given directly using
        # --root-directory.
428
	    install_device="`"$grub_probe" --target=device --device-map= "${rootdir}"`"
429
        # Is it a mount point?
430
431
432
	    if test "x$install_device" != "x`"$grub_probe" --target=device --device-map= "${rootdir}/.."`"; then
		efidir="${rootdir}"
	    fi
433
	fi
434
435
436
437
438
439
440

	if test -n "$efidir"; then
	    efi_fs=`"$grub_probe" --target=fs "--device-map=${device_map}" "${efidir}"`
	    if test "x$efi_fs" = xfat; then :; else
		gettext_printf "%s doesn't look like an EFI partition.\n" "${efidir}" 1>&2
		efidir=
	    fi
441
442
	fi
    fi
443

444
445
446
447
448
449
    if test -n "$efidir"; then
        # The EFI specification requires that an EFI System Partition must
        # contain an "EFI" subdirectory, and that OS loaders are stored in
        # subdirectories below EFI.  Vendors are expected to pick names that do
        # not collide with other vendors.  To minimise collisions, we use the
        # name of our distributor if possible.
450
	efi_distributor="$bootloader_id"
451
452
453
454
455
456
	if test $removable = yes; then
      	   # The specification makes stricter requirements of removable
	   # devices, in order that only one image can be automatically loaded
	   # from them.  The image must always reside under /EFI/BOOT, and it
	   # must have a specific file name depending on the architecture.
	    efi_distributor=BOOT
457
	    case "$grub_modinfo_target_cpu" in
458
459
		i386)
		    efi_file=BOOTIA32.EFI ;;
460
		x86_64)
461
462
463
464
465
466
467
468
469
470
		    efi_file=BOOTX64.EFI ;;
	    # GRUB does not yet support these architectures, but they're defined
	    # by the specification so we include them here to ease future
	    # expansion.
		ia64)
		    efi_file=BOOTIA64.EFI ;;
	    esac
	else
	    # It is convenient for each architecture to have a different
	    # efi_file, so that different versions can be installed in parallel.
471
	    case "$grub_modinfo_target_cpu" in
472
473
		i386)
		    efi_file=grubia32.efi ;;
474
		x86_64)
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
		    efi_file=grubx64.efi ;;
	 # GRUB does not yet support these architectures, but they're defined
 	 # by the specification so we include them here to ease future
	 # expansion.
		ia64)
		    efi_file=grubia64.efi ;;
		*)
		    efi_file=grub.efi ;;
	    esac
	   # TODO: We should also use efibootmgr, if available, to add a Boot
	   # entry for ourselves.
	fi
	efidir="$efidir/EFI/$efi_distributor"
	mkdir -p "$efidir" || exit 1
    else
        # We don't know what's going on.  Fall back to traditional
        # (non-specification-compliant) behaviour.
	efidir="$grubdir"
	efi_distributor=
	efi_file=grub.efi
    fi
fi

498
# Create the GRUB directory if it is not present.
499
mkdir -p "$grubdir" || exit 1
500
mkdir -p "$grubdir/${grub_modinfo_target_cpu}-$grub_modinfo_platform" || exit 1
501

502
503
504
505
506
# If --recheck is specified, remove the device map, if present.
if test $recheck = yes; then
    rm -f "$device_map"
fi

507
508
# Create the device map file if it is not present.
if test -f "$device_map"; then
509
510
511
512
    # Make sure that there is no duplicated entry.
    tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' "$device_map" \
	| sort | uniq -d | sed -n 1p`
    if test -n "$tmp"; then
513
	gettext_printf "The drive %s is defined multiple times in the device map %s\n" "$tmp" "$device_map" 1>&2
514
515
	exit 1
    fi
516
else
517
    device_map=
518
519
520
fi

# Copy the GRUB images to the GRUB directory.
521
for file in "${grubdir}"/*.mod "${grubdir}"/*.lst "${grubdir}"/*.img "${grubdir}"/efiemu??.o "${grubdir}"/${grub_modinfo_target_cpu}-$grub_modinfo_platform/*.mod "${grubdir}"/${grub_modinfo_target_cpu}-$grub_modinfo_platform/*.lst "${grubdir}"/${grub_modinfo_target_cpu}-$grub_modinfo_platform/*.img "${grubdir}"/${grub_modinfo_target_cpu}-$grub_modinfo_platform/efiemu??.o; do
522
523
    if test -f "$file" && [ "`basename $file`" != menu.lst ]; then
	rm -f "$file" || exit 1
524
525
    fi
done
526
527
for file in "${source_dir}"/*.mod "${source_dir}"/*.lst; do
    cp -f "$file" "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform" || exit 1
528
done
529
530
if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ] ; then
    for file in "${source_dir}"/*.img "${source_dir}"/efiemu??.o; do
531
	if test -f "$file"; then
532
	    cp -f "$file" "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform" || exit 1
533
	fi
534
535
    done
fi
536

537
# Copy gettext files
538
539
mkdir -p "${grubdir}"/locale/
for dir in "${localedir}"/*; do
540
541
    if test -f "$dir/LC_MESSAGES/grub.mo"; then
        cp -f "$dir/LC_MESSAGES/grub.mo" "${grubdir}/locale/${dir##*/}.mo"
542
543
544
    fi
done

545
if test -f "${pkgdatadir}"/themes/starfield/theme.txt; then
Dalet Omega's avatar
Dalet Omega committed
546
547
548
549
    mkdir -p "${grubdir}"/themes/starfield
    cp "${pkgdatadir}"/themes/starfield/* "${grubdir}"/themes/starfield
fi

550
551
552
553
554
if test -f "${pkgdatadir}"/unicode.pf2; then
    mkdir -p "${grubdir}"/fonts
    cp "${pkgdatadir}"/unicode.pf2 "${grubdir}"/fonts
fi

555
556
557
558
if ! is_path_readable_by_grub "${grubdir}"; then
    gettext_printf "Path \`%s' is not readable by GRUB on boot. Installation is impossible. Aborting.\n" "${grubdir}" 1>&2
    exit 1
fi
559

560
# Write device to a variable so we don't have to traverse /dev every time.
561
grub_device="`"$grub_probe" --device-map="${device_map}" --target=device "${grubdir}"`" || exit 1
562

563
564
if ! test -f "${grubdir}"/grubenv; then
    "$grub_editenv" "${grubdir}"/grubenv create
565
566
fi

567
# Create the core image. First, auto-detect the filesystem module.
568
fs_module="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=fs --device `"
569
if test "x$fs_module" = x ; then
570
571
572
573
    gettext_printf "Auto-detection of a filesystem of %s failed.\n" "${grub_device}" 1>&2
    gettext "Try with --recheck." 1>&2
    echo 1>&2
    gettext_printf "If the problem persists please report this together with the output of %s to <%s>" "\"$grub_probe --device-map=\"${device_map}\" --target=fs -v ${grubdir}\"" "bug-grub@gnu.org" 1>&2
574
575
576
    exit 1
fi

robertmh's avatar
   
robertmh committed
577
# Then the partition map module.  In order to support partition-less media,
578
579
# this command is allowed to fail (--target=fs already grants us that the
# filesystem will be accessible).
580
partmap_module=
581
for x in `echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=partmap --device 2> /dev/null`; do
582
583
584
   case "$x" in
       netbsd | openbsd) 
	   partmap_module="$partmap_module part_bsd";;
585
       "") ;;
586
587
588
       *)
	   partmap_module="$partmap_module part_$x";;
   esac
589
done
robertmh's avatar
   
robertmh committed
590

591
# Device abstraction module, if any (lvm, raid).
592
devabstraction_module="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=abstraction --device`"
593

594
595
596
597
598
if [ "x$disk_module" = xata ]; then
    disk_module=pata
fi

if [ "x$disk_module" = xnative ]; then
599
    disk_module="pata ahci ohci"
600
    if [ "x$grub_modinfo_target_cpu" = "xi386" ] || [ "x$grub_modinfo_target_cpu" = "xx86_64" ]; then
601
602
603
	disk_module="$disk_module uhci"
    fi
    disk_module="$disk_module usbms"
604
605
fi

606
# The order in this list is critical.  Be careful when modifying it.
607
modules="$modules $disk_module"
608
modules="$modules $fs_module $partmap_module $devabstraction_module"
609

610
relative_grubdir="`"$grub_mkrelpath" "${grubdir}"`" || exit 1
611
612
613
614
if [ "x${relative_grubdir}" = "x" ] ; then
    relative_grubdir=/
fi

615
prefix_drive=
616
617
config_opt=

618
rm -f "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
619
620

if [ "x${debug_image}" != x ]; then
621
622
    echo "set debug='${debug_image}'" >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
    config_opt="-c ${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg "
623
624
fi

625
if [ "x${devabstraction_module}" = "x" ] ; then
626
627
628
629
    if [ x"${install_device}" != x ]; then
      if echo "${install_device}" | grep -qx "(.*)" ; then
        install_drive="${install_device}"
      else
630
        install_drive="`"$grub_probe" --device-map="${device_map}" --target=drive --device "${install_device}"`" || exit 1
631
      fi
632
      install_drive="`echo "${install_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\1/'`"
633
    fi
634
    grub_drive="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=drive --device`" || exit 1
635
636

    # Strip partition number
637
638
    grub_partition="`echo "${grub_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\3/'`"
    grub_drive="`echo "${grub_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\1/'`"
639
    if ([ "x$disk_module" != x ] && [ "x$disk_module" != xbiosdisk ]) || [ "x${grub_drive}" != "x${install_drive}" ] || ([ "x$grub_modinfo_platform" != xefi ] && [ "x$grub_modinfo_platform" != xpc ] && [ x"${grub_modinfo_platform}" != x"ieee1275" ]); then
640
        # generic method (used on coreboot and ata mod)
641
642
643
644
	uuid=
	if [ x"$force_file_id" != xy ]; then
            uuid="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=fs_uuid --device`"
	fi
645
646

	if [ x"$disk_module" != x ] && [ x"$disk_module" != xbiosdisk ]; then
647
            hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=baremetal_hints --device`"
648
	elif [ x"$grub_modinfo_platform" = xpc ]; then
649
            hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=bios_hints --device`"
650
	elif [ x"$grub_modinfo_platform" = xefi ]; then
651
            hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=efi_hints --device`"
652
	elif [ x"$grub_modinfo_platform" = xieee1275 ]; then
653
            hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=ieee1275_hints --device`"
654
	elif [ x"$grub_modinfo_platform" = xloongson ] || [ x"$grub_modinfo_platform" = xqemu ] || [ x"$grub_modinfo_platform" = xcoreboot ] || [ x"$grub_modinfo_platform" = xmultiboot ] || [ x"$grub_modinfo_platform" = xqemu-mips ]; then
655
            hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=baremetal_hints --device`"
656
	else
657
            gettext "No hints available for your platform. Expect reduced performance." 1>&2
658
	    echo 1>&2
659
660
	    hints=
        fi
661
	if [ x"$uuid" != x ]; then
662
            echo "search.fs_uuid ${uuid} root $hints " >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
663
664
665
666
667
	    search_module=search_fs_uuid
	else
	    mkdir -p "${grubdir}/uuid"
	    file="`mktemp "${grubdir}/uuid/XXXXXXXXXXXXXXXXXXXXXXXXX"`"
	    relfile="`${grub_mkrelpath} "$file"`"
668
            echo "search.file '${relfile}' root $hints " >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
669
670
	    search_module=search_fs_file
	fi
671
672
	echo 'set prefix=($root)'"${relative_grubdir}" >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
	config_opt="-c ${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg "
673
        modules="$modules $search_module"
674
    else
675
        # we need to hardcode the partition number in the core image's prefix.
676
677
678
	if [ x"$grub_partition" = x ]; then
            prefix_drive="()"
	else
679
680
            # Comma is already there
            prefix_drive="($grub_partition)"
681
	fi
682
    fi
683
else
684
    if [ x$GRUB_CRYPTODISK_ENABLE = xy ]; then
685
	for uuid in "`echo "${grub_device}" | xargs "${grub_probe}"  --target=cryptodisk_uuid --device`"; do
686
	    echo "cryptomount -u $uuid" >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
687
	done
688
	config_opt="-c ${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg "
689
690
    fi

691
    prefix_drive=`"$grub_probe" --device-map="${device_map}" --target=drive --device "${grub_device}"` || exit 1
692
693
fi

694
case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
695
    sparc64-ieee1275) mkimage_target=sparc64-ieee1275-raw ;;
696
    mipsel-loongson) mkimage_target=mipsel-loongson-elf ;;
697
    *) mkimage_target="${grub_modinfo_target_cpu}-${grub_modinfo_platform}" ;;
698
699
esac

700
case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
701
    i386-efi | x86_64-efi) imgext=efi ;;
702
    mipsel-loongson | i386-coreboot | i386-multiboot | i386-ieee1275 \
703
	| powerpc-ieee1275) imgext=elf ;;
704
705
706
707
    *) imgext=img ;;
esac


708
"$grub_mkimage" ${config_opt} -d "${source_dir}" -O ${mkimage_target} --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" --prefix="${prefix_drive}${relative_grubdir}" $modules || exit 1
709

710
# Backward-compatibility kludges
711
712
713
714
715
716
if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "mipsel-loongson" ]; then
    cp "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" "${bootdir}"/grub.elf
elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-ieee1275" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "powerpc-ieee1275" ]; then
    cp "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" "${grubdir}/grub"
elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-efi" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "x86_64-efi" ]; then
    "$grub_mkimage" ${config_opt} -d "${source_dir}" -O ${mkimage_target} --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/grub.efi" --prefix="" $modules || exit 1
717
fi
718

719

720
721
# Perform the grub_modinfo_platform-dependent install
if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ] ; then
722
    # Now perform the installation.
723
    "$grub_setup" ${allow_floppy} ${setup_verbose} ${setup_force} --directory="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform" \
724
	--device-map="${device_map}" "${install_device}" || exit 1
725
elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-ieee1275" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "powerpc-ieee1275" ]; then
726
    if [ x"$update_nvram" = xyes ]; then
727
728
	ofpathname="`which ofpathname`"
	nvsetenv="`which nvsetenv`"
729
	set "$ofpathname" dummy
730
731
732
733
734
735
	if test -f "$1"; then
	    :
	else
	    echo "$1: Not found." 1>&2
	    exit 1
	fi
736
	set "$nvsetenv" dummy
737
738
739
740
741
742
	if test -f "$1"; then
	    :
	else
	    echo "$1: Not found." 1>&2
	    exit 1
	fi
743
744
745
746
747
748
	if  [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" != "powerpc-ieee1275" ] \
	    || [ -z "${install_device}" ]; then
         # Get the Open Firmware device tree path translation.
	    dev="`echo $grub_device | sed -e 's/\/dev\///' -e 's/[0-9]\+//'`"
	    partno="`echo $grub_device | sed -e 's/.*[^0-9]\([0-9]\+\)$/\1/'`"
	    ofpath="`$ofpathname $dev`" || {
749
750
	    # TRANSLATORS: "device tree path" is the name of the device
            # for IEEE1275
751
752
753
		gettext_printf "Couldn't find IEEE1275 device tree path for %s.\nYou will have to set \`boot-device' variable manually.\n" "$dev" 1>&2
		exit 1
	    }
754

755
756
            # Point boot-device at the new grub install
	    boot_device="$ofpath:$partno,"`"$grub_mkrelpath" "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" | sed 's,/,\\\\,g'`
757

758
759
        # If a install device is defined, copy the core.elf to PReP partition.
	else
760
            if [ "$("${grub_probe}" -m "${device_map}" -d "${install_device}" -t msdos_parttype)" != "41" ]; then
761
762
		gettext "The chosen partition is not a PReP partition." 1>&2
		echo 1>&2
763
764
765
766
767
768
		exit 1
            fi

            if [ "$(file -s "${install_device}" -b | awk '{ print $1 }')" = ELF ] || [ $(cmp /dev/zero "${install_device}" &>/dev/null) ]; then
            # Change boot device to the harddisk root
		boot_device="$ofpath"
769
		dd if="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" of="${install_device}" status=noxfer || {
770
771
		    gettext "Failed to copy Grub to the PReP partition." 1>&2
		    echo 1>&2
772
773
774
		    exit 1
		}
            else
775
776
		gettext "The PReP partition is not empty. If you are sure you want to use it, run dd to clear it:" 1>&2
		echo 1>&2
777
778
779
		echo "  dd if=/dev/zero of=${install_device}"
		exit 1
            fi
780
781
782
783
784
785
786
	    dev="`echo "${install_device}" | sed -e 's/\/dev\///' -e 's/[0-9]\+//'`"
	    boot_device="`$ofpathname "$dev"`" || {
	    # TRANSLATORS: "device tree path" is the name of the device
            # for IEEE1275
		gettext_printf "Couldn't find IEEE1275 device tree path for %s.\nYou will have to set \`boot-device' variable manually.\n" "$dev" 1>&2
		exit 1
	    }
787
788
	fi

789
	"$nvsetenv" boot-device "$boot_device" || {
790
791
	    # TRANSLATORS: The %s will be replaced by an external program name.
	    gettext_printf "\`%s' failed.\n" "$nvsetenv" 1>&2
792
	    gettext "You will have to set \`boot-device' variable manually.  At the IEEE1275 prompt, type:" 1>&2
793
794
	    echo 1>&2
	    echo "  setenv boot-device $boot_device" 1>&2
795
796
797
	    exit 1
	}
    fi
798
799
elif [ x"${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = xmips-arc ]; then
    dvhtool -d "${install_device}" --unix-to-vh "{grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" grub
800
    gettext "You will have to set \`SystemPartition' and \`OSLoader' manually." 1>&2
801
    echo 1>&2
802
803
elif [ x"$grub_modinfo_platform" = xefi ]; then
    cp "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" "${efidir}/${efi_file}"
804
    # For old macs. Suggested by Peter Jones.
805
806
    if [ x$grub_modinfo_target_cpu = xi386 ]; then
	cp "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" "${efidir}/boot.efi"
807
    fi
808

Colin Watson's avatar
Colin Watson committed
809
    # Try to make this image bootable using the EFI Boot Manager, if available.
810
    efibootmgr="`which efibootmgr`"
Colin Watson's avatar
Colin Watson committed
811
812
813
814
815
816
817
818
819
820
    if test "$removable" = no && test -n "$efi_distributor" && \
	test -n "$efibootmgr"; then
        # On Linux, we need the efivars kernel modules.
	case "$host_os" in
	    linux*)
		modprobe -q efivars 2>/dev/null || true ;;
	esac

        # Delete old entries from the same distributor.
	for bootnum in `efibootmgr | grep '^Boot[0-9]' | \
821
	    fgrep -i " $efi_distributor" | cut -b5-8`; do
Colin Watson's avatar
Colin Watson committed
822
823
824
825
826
827
828
829
	    efibootmgr $efi_quiet -b "$bootnum" -B
	done

        # Add a new entry for the image we just created.  efibootmgr needs to be
        # given the disk device and partition number separately, so we have to
        # fiddle about with grub-probe to get hold of this reasonably reliably.
        # Use fresh device map text to avoid any problems with stale data, since
        # all we need here is a one-to-one mapping.
830
831
832
	efidir_drive="$("$grub_probe" --target=drive --device-map= "$efidir")"
	efidir_disk="$("$grub_probe" --target=disk --device-map= "$efidir")"
	if test -z "$efidir_drive" || test -z "$efidir_disk"; then
833
	    gettext_printf "Can't find GRUB drive for %s; unable to create EFI Boot Manager entry.\n" "$efidir" >&2
Colin Watson's avatar
Colin Watson committed
834
835
836
	else
	    efidir_part="$(echo "$efidir_drive" | sed 's/^([^,]*,[^0-9]*//; s/[^0-9].*//')"
	    efibootmgr $efi_quiet -c -d "$efidir_disk" -p "$efidir_part" -w \
837
		-L "$bootloader_id" -l "\\EFI\\$efi_distributor\\$efi_file"
Colin Watson's avatar
Colin Watson committed
838
839
	fi
    fi
840
else
841
842
    gettext "WARNING: no platform-specific install was performed" 1>&2
    echo 1>&2
843
fi
844

845
846
gettext "Installation finished. No error reported." 1>&2
echo 1>&2
847
848
849

# Bye.
exit 0