]> git.proxmox.com Git - grub2.git/blob - util/grub-install.in
Put recheck back
[grub2.git] / util / grub-install.in
1 #! /bin/sh
2
3 # 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 # GRUB is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # GRUB is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # 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 transform="@program_transform_name@"
21
22 prefix="@prefix@"
23 exec_prefix="@exec_prefix@"
24 sbindir="@sbindir@"
25 bindir="@bindir@"
26 libdir="@libdir@"
27 sysconfdir="@sysconfdir@"
28 PACKAGE_NAME=@PACKAGE_NAME@
29 PACKAGE_TARNAME=@PACKAGE_TARNAME@
30 PACKAGE_VERSION=@PACKAGE_VERSION@
31 target_cpu=@target_cpu@
32 platform=@platform@
33 host_os=@host_os@
34 datarootdir=@datarootdir@
35 pkglibdir="${libdir}/`echo ${PACKAGE_TARNAME}/${target_cpu}-${platform} | sed ${transform}`"
36 localedir="@datadir@/locale"
37
38 self="`basename $0`"
39
40 grub_setup="${sbindir}/`echo grub-setup | sed ${transform}`"
41 grub_mkimage="${bindir}/`echo grub-mkimage | sed ${transform}`"
42 grub_mkdevicemap="${sbindir}/`echo grub-mkdevicemap | sed ${transform}`"
43 grub_probe="${sbindir}/`echo grub-probe | sed ${transform}`"
44 grub_editenv="${bindir}/`echo grub-editenv | sed ${transform}`"
45 grub_mkrelpath="${bindir}/`echo grub-mkrelpath | sed ${transform}`"
46 rootdir=
47 bootdir=
48 grubdir="`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`"
49 modules=
50
51 install_device=
52 no_floppy=
53 force_lba=
54 recheck=no
55 debug=no
56 debug_image=
57
58 update_nvram=yes
59
60 removable=no
61 efi_quiet=
62
63 # Get GRUB_DISTRIBUTOR.
64 if test -f "${sysconfdir}/default/grub" ; then
65 . "${sysconfdir}/default/grub"
66 fi
67
68 bootloader_id="$(echo "$GRUB_DISTRIBUTOR" | tr 'A-Z' 'a-z' | cut -d' ' -f1)"
69 if test -z "$bootloader_id"; then
70 bootloader_id=grub
71 fi
72
73 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
74 disk_module=biosdisk
75 elif [ "${platform}" = "ieee1275" ] || [ "${platform}" = "efi" ] ; then
76 disk_module=
77 else
78 disk_module=native
79 fi
80
81 # Usage: usage
82 # Print the usage.
83 usage () {
84 if [ "${target_cpu}-${platform}" = "i386-pc" ] \
85 || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ]; then
86 cat <<EOF
87 Usage: $self [OPTION] install_device
88 EOF
89 else
90 cat <<EOF
91 Usage: $self [OPTION] [install_device]
92 EOF
93 fi
94 cat <<EOF
95 Install GRUB on your drive.
96
97 -h, --help print this message and exit
98 -v, --version print the version information and exit
99 --modules=MODULES pre-load specified modules MODULES
100 --boot-directory=DIR install GRUB images under the directory DIR/@grubdirname@
101 instead of the $grubdir directory
102 --grub-setup=FILE use FILE as grub-setup
103 --grub-mkimage=FILE use FILE as grub-mkimage
104 --grub-mkrelpath=FILE use FILE as grub-mkrelpath
105 --grub-mkdevicemap=FILE use FILE as grub-mkdevicemap
106 --grub-probe=FILE use FILE as grub-probe
107 --no-floppy do not probe any floppy drive
108 --allow-floppy Make the drive also bootable as floppy
109 (default for fdX devices). May break on some BIOSes.
110 --recheck delete device map if it already exists
111 --force install even if problems are detected
112 EOF
113 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
114 cat <<EOF
115 --disk-module=MODULE disk module to use (biosdisk or native)
116 EOF
117 fi
118 if [ "${target_cpu}-${platform}" = "i386-ieee1275" ] || [ "${target_cpu}-${platform}" = "powerpc-ieee1275" ] ; then
119 cat <<EOF
120 --no-nvram don't update the boot-device NVRAM variable
121 EOF
122 fi
123 if [ "${platform}" = "efi" ]; then
124 cat <<EOF
125 --removable the installation device is removable
126 --bootloader-id=ID the ID of bootloader.
127 EOF
128 fi
129 cat <<EOF
130
131 INSTALL_DEVICE can be a GRUB device name or a system device filename.
132
133 $self copies GRUB images into $grubdir, and uses grub-setup
134 to install grub into the boot sector.
135
136 Report bugs to <bug-grub@gnu.org>.
137 EOF
138 }
139
140 argument () {
141 opt="$1"
142 shift
143
144 if test $# -eq 0; then
145 echo "$0: option requires an argument -- '$opt'" 1>&2
146 exit 1
147 fi
148 echo "$1"
149 }
150
151 allow_floppy=""
152
153 # Check the arguments.
154 while test $# -gt 0
155 do
156 option=$1
157 shift
158
159 case "$option" in
160 -h | --help)
161 usage
162 exit 0 ;;
163 -v | --version)
164 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
165 exit 0 ;;
166
167 --modules)
168 modules=`argument $option "$@"`; shift;;
169 --modules=*)
170 modules=`echo "$option" | sed 's/--modules=//'` ;;
171
172 # Accept and ignore for compatibility
173 --font)
174 shift;;
175 --font=*)
176 ;;
177
178 # Accept for compatibility
179 --root-directory)
180 rootdir="`argument $option "$@"`"; shift;;
181 --root-directory=*)
182 rootdir="`echo "$option" | sed 's/--root-directory=//'`" ;;
183
184 --boot-directory)
185 bootdir="`argument $option "$@"`"; shift;;
186 --boot-directory=*)
187 bootdir="`echo "$option" | sed 's/--boot-directory=//'`" ;;
188
189 --grub-setup)
190 grub_setup="`argument "$option" "$@"`"; shift;;
191 --grub-setup=*)
192 grub_setup="`echo "$option" | sed 's/--grub-setup=//'`" ;;
193
194 --bootloader-id)
195 bootloader_id="`argument $option "$@"`"; shift;;
196 --bootloader-id=*)
197 bootloader_id="`echo "$option" | sed 's/--bootloader-id=//'`" ;;
198
199 --grub-mkimage)
200 grub_mkimage="`argument $option "$@"`"; shift;;
201 --grub-mkimage=*)
202 grub_mkimage="`echo "$option" | sed 's/--grub-mkimage=//'`" ;;
203
204 --grub-mkrelpath)
205 grub_mkrelpath="`argument "$option" "$@"`"; shift;;
206 --grub-mkrelpath=*)
207 grub_mkrelpath="`echo "$option" | sed 's/--grub-mkrelpath=//'`" ;;
208
209 --grub-mkdevicemap)
210 grub_mkdevicemap="`argument "$option" "$@"`"; shift;;
211 --grub-mkdevicemap=*)
212 grub_mkdevicemap="`echo "$option" | sed 's/--grub-mkdevicemap=//'`" ;;
213
214 --grub-probe)
215 grub_probe="`argument "$option" "$@"`"; shift;;
216 --grub-probe=*)
217 grub_probe="`echo "$option" | sed 's/--grub-probe=//'`" ;;
218
219 --no-floppy)
220 no_floppy="--no-floppy" ;;
221 --recheck)
222 recheck=yes ;;
223 --removable)
224 removable=yes ;;
225
226 --allow-floppy)
227 allow_floppy="--allow-floppy" ;;
228
229 --disk-module)
230 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
231 disk_module="`argument "$option" "$@"`"; shift;
232 fi ;;
233 --disk-module=*)
234 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
235 disk_module="`echo "$option" | sed 's/--disk-module=//'`"
236 fi ;;
237
238 --no-nvram)
239 update_nvram=no ;;
240
241 # This is an undocumented feature...
242 --debug)
243 debug=yes ;;
244 --debug-image)
245 debug_image="`argument "$option" "$@"`"; shift;;
246 --debug-image=*)
247 debug_image="`echo "$option" | sed 's/--debug-image=//'`" ;;
248
249 -f | --force)
250 setup_force="--force" ;;
251
252 -*)
253 echo "Unrecognized option \`$option'" 1>&2
254 usage
255 exit 1
256 ;;
257 *)
258 if test "x$install_device" != x; then
259 echo "More than one install_devices?" 1>&2
260 usage
261 exit 1
262 fi
263 install_device="${option}" ;;
264 esac
265 done
266
267 . ${libdir}/@PACKAGE@/grub-mkconfig_lib
268
269 if test "x$install_device" = x && ([ "${target_cpu}-${platform}" = "i386-pc" ] \
270 || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ]); then
271 echo "install_device not specified." 1>&2
272 usage
273 exit 1
274 fi
275
276 if ! ([ "${target_cpu}-${platform}" = "i386-pc" ] \
277 || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ]); then
278 install_device=
279 fi
280
281 # If the debugging feature is enabled, print commands.
282 setup_verbose=
283 if test x"$debug" = xyes; then
284 set -x
285 setup_verbose="--verbose"
286 efi_quiet=-q
287 fi
288
289 if [ -z "$bootdir" ]; then
290 # Default bootdir if bootdir not initialized.
291 bootdir="/@bootdirname@"
292
293 if [ -n "$rootdir" ] ; then
294 # Initialize bootdir if rootdir was initialized.
295 bootdir="${rootdir}/@bootdirname@"
296 fi
297 fi
298
299 grubdir="`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`"
300 device_map="${grubdir}/device.map"
301
302
303 # Check if GRUB is installed.
304 if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
305 set $grub_setup dummy
306 if test -f "$1"; then
307 :
308 else
309 echo "$1: Not found." 1>&2
310 exit 1
311 fi
312 fi
313
314 set "$grub_mkimage" dummy
315 if test -f "$1"; then
316 :
317 else
318 echo "$1: Not found." 1>&2
319 exit 1
320 fi
321
322 set "$grub_mkdevicemap" dummy
323 if test -f "$1"; then
324 :
325 else
326 echo "$1: Not found." 1>&2
327 exit 1
328 fi
329
330 if [ x"$platform" = xefi ]; then
331 # Find the EFI System Partition.
332 efidir=
333 if test -d "${bootdir}/efi"; then
334 install_device="`"$grub_mkdevicemap" --device-map=/dev/stdout | "$grub_probe" --target=device --device-map=/dev/stdin "${bootdir}/efi"`"
335 # Is it a mount point?
336 if test "x$install_device" != "x`"$grub_mkdevicemap" --device-map=/dev/stdout | "$grub_probe" --target=device --device-map=/dev/stdin "${bootdir}"`"; then
337 efidir="${bootdir}/efi"
338 fi
339 elif test -d "${bootdir}/EFI"; then
340 install_device="`"$grub_mkdevicemap" --device-map=/dev/stdout | "$grub_probe" --target=device --device-map=/dev/stdin "${bootdir}/EFI"`"
341 # Is it a mount point?
342 if test "x$install_device" != "x`"$grub_mkdevicemap" --device-map=/dev/stdout | "$grub_probe" --target=device --device-map=/dev/stdin "${bootdir}"`"; then
343 efidir="${bootdir}/EFI"
344 fi
345 elif test -n "$rootdir" && test "x$rootdir" != "x/"; then
346 # The EFI System Partition may have been given directly using
347 # --root-directory.
348 install_device="`"$grub_mkdevicemap" --device-map=/dev/stdout | "$grub_probe" --target=device --device-map=/dev/stdin "${rootdir}"`"
349 # Is it a mount point?
350 if test "x$install_device" != "x`"$grub_mkdevicemap" --device-map=/dev/stdout | "$grub_probe" --target=device --device-map=/dev/stdin "${rootdir}/.."`"; then
351 efidir="${rootdir}"
352 fi
353 fi
354
355 if test -n "$efidir"; then
356 efi_fs=`"$grub_probe" --target=fs "--device-map=${device_map}" "${efidir}"`
357 if test "x$efi_fs" = xfat; then :; else
358 echo "${efidir} doesn't look like an EFI partition." 1>&2
359 efidir=
360 fi
361 fi
362
363 if test -n "$efidir"; then
364 # The EFI specification requires that an EFI System Partition must
365 # contain an "EFI" subdirectory, and that OS loaders are stored in
366 # subdirectories below EFI. Vendors are expected to pick names that do
367 # not collide with other vendors. To minimise collisions, we use the
368 # name of our distributor if possible.
369 efi_distributor="$bootloader_id"
370 if test $removable = yes; then
371 # The specification makes stricter requirements of removable
372 # devices, in order that only one image can be automatically loaded
373 # from them. The image must always reside under /EFI/BOOT, and it
374 # must have a specific file name depending on the architecture.
375 efi_distributor=BOOT
376 case "$target_cpu" in
377 i386)
378 efi_file=BOOTIA32.EFI ;;
379 x86_64)
380 efi_file=BOOTX64.EFI ;;
381 # GRUB does not yet support these architectures, but they're defined
382 # by the specification so we include them here to ease future
383 # expansion.
384 ia64)
385 efi_file=BOOTIA64.EFI ;;
386 esac
387 else
388 # It is convenient for each architecture to have a different
389 # efi_file, so that different versions can be installed in parallel.
390 case "$target_cpu" in
391 i386)
392 efi_file=grubia32.efi ;;
393 x86_64)
394 efi_file=grubx64.efi ;;
395 # GRUB does not yet support these architectures, but they're defined
396 # by the specification so we include them here to ease future
397 # expansion.
398 ia64)
399 efi_file=grubia64.efi ;;
400 *)
401 efi_file=grub.efi ;;
402 esac
403 # TODO: We should also use efibootmgr, if available, to add a Boot
404 # entry for ourselves.
405 fi
406 efidir="$efidir/EFI/$efi_distributor"
407 mkdir -p "$efidir" || exit 1
408 else
409 # We don't know what's going on. Fall back to traditional
410 # (non-specification-compliant) behaviour.
411 efidir="$grubdir"
412 efi_distributor=
413 efi_file=grub.efi
414 fi
415 fi
416
417 # Create the GRUB directory if it is not present.
418 mkdir -p "$grubdir" || exit 1
419
420 # If --recheck is specified, remove the device map, if present.
421 if test $recheck = yes; then
422 rm -f "$device_map"
423 fi
424
425 # Create the device map file if it is not present.
426 if test -f "$device_map"; then
427 # Make sure that there is no duplicated entry.
428 tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' "$device_map" \
429 | sort | uniq -d | sed -n 1p`
430 if test -n "$tmp"; then
431 echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2
432 exit 1
433 fi
434 else
435 device_map=
436 fi
437
438 # Copy the GRUB images to the GRUB directory.
439 for file in "${grubdir}"/*.mod "${grubdir}"/*.lst "${grubdir}"/*.img "${grubdir}"/efiemu??.o; do
440 if test -f "$file" && [ "`basename $file`" != menu.lst ]; then
441 rm -f "$file" || exit 1
442 fi
443 done
444 for file in "${pkglibdir}"/*.mod "${pkglibdir}"/*.lst; do
445 cp -f "$file" "${grubdir}" || exit 1
446 done
447 if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
448 for file in "${pkglibdir}"/*.img "${pkglibdir}"/efiemu??.o; do
449 if test -f "$file"; then
450 cp -f "$file" "${grubdir}" || exit 1
451 fi
452 done
453 fi
454
455 # Copy gettext files
456 mkdir -p "${grubdir}"/locale/
457 for dir in "${localedir}"/*; do
458 if test -f "$dir/LC_MESSAGES/grub.mo"; then
459 cp -f "$dir/LC_MESSAGES/grub.mo" "${grubdir}/locale/${dir##*/}.mo"
460 fi
461 done
462
463 is_path_readable_by_grub "${grubdir}" || (echo "${grubdir}" not readable 1>&2 ; exit 1)
464
465 # Write device to a variable so we don't have to traverse /dev every time.
466 grub_device="`"$grub_probe" --device-map="${device_map}" --target=device "${grubdir}"`" || exit 1
467
468 if ! test -f "${grubdir}"/grubenv; then
469 "$grub_editenv" "${grubdir}"/grubenv create
470 fi
471
472 # Create the core image. First, auto-detect the filesystem module.
473 fs_module="`"$grub_probe" --device-map="${device_map}" --target=fs --device "${grub_device}"`"
474 if test "x$fs_module" = x ; then
475 echo "Auto-detection of a filesystem of ${grub_device} failed." 1>&2
476 echo "Try with --recheck." 1>&2
477 echo "If the problem persists please report this together with the output of \"$grub_probe --device-map=\"${device_map}\" --target=fs -v ${grubdir}\" to <bug-grub@gnu.org>" 1>&2
478 exit 1
479 fi
480
481 # Then the partition map module. In order to support partition-less media,
482 # this command is allowed to fail (--target=fs already grants us that the
483 # filesystem will be accessible).
484 partmap_module=
485 for x in `"$grub_probe" --device-map="${device_map}" --target=partmap --device "${grub_device}" 2> /dev/null`; do
486 case "$x" in
487 netbsd | openbsd)
488 partmap_module="$partmap_module part_bsd";;
489 "") ;;
490 *)
491 partmap_module="$partmap_module part_$x";;
492 esac
493 done
494
495 # Device abstraction module, if any (lvm, raid).
496 devabstraction_module="`"$grub_probe" --device-map="${device_map}" --target=abstraction --device "${grub_device}"`"
497
498 if [ "x$disk_module" = xata ]; then
499 disk_module=pata
500 fi
501
502 if [ "x$disk_module" = xnative ]; then
503 disk_module="pata ahci ohci"
504 if [ "x$target_cpu" = "xi386" ] || [ "x$target_cpu" = "xx86_64" ]; then
505 disk_module="$disk_module uhci"
506 fi
507 disk_module="$disk_module usbms"
508 fi
509
510 # The order in this list is critical. Be careful when modifying it.
511 modules="$modules $disk_module"
512 modules="$modules $fs_module $partmap_module $devabstraction_module"
513
514 relative_grubdir="`"$grub_mkrelpath" "${grubdir}"`" || exit 1
515 if [ "x${relative_grubdir}" = "x" ] ; then
516 relative_grubdir=/
517 fi
518
519 prefix_drive=
520 config_opt=
521
522 rm -f "${grubdir}/load.cfg"
523
524 if [ "x${debug_image}" != x ]; then
525 echo "set debug='${debug_image}'" >> "${grubdir}/load.cfg"
526 config_opt="-c ${grubdir}/load.cfg "
527 fi
528
529 if [ "x${devabstraction_module}" = "x" ] ; then
530 if [ x"${install_device}" != x ]; then
531 if echo "${install_device}" | grep -qx "(.*)" ; then
532 install_drive="${install_device}"
533 else
534 install_drive="`"$grub_probe" --device-map="${device_map}" --target=drive --device "${install_device}"`" || exit 1
535 fi
536 install_drive="`echo "${install_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\1/'`"
537 fi
538 grub_drive="`"$grub_probe" --device-map="${device_map}" --target=drive --device "${grub_device}"`" || exit 1
539
540 # Strip partition number
541 grub_partition="`echo "${grub_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\3/'`"
542 grub_drive="`echo "${grub_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\1/'`"
543 if ([ "x$disk_module" != x ] && [ "x$disk_module" != xbiosdisk ]) || [ "x${grub_drive}" != "x${install_drive}" ] || ([ "x$platform" != xefi ] && [ "x$platform" != xpc ] && [ x"${platform}" != x"ieee1275" ]); then
544 # generic method (used on coreboot and ata mod)
545 uuid="`"$grub_probe" --device-map="${device_map}" --target=fs_uuid --device "${grub_device}"`"
546 if [ "x${uuid}" = "x" ] ; then
547 if [ "x$platform" != xefi ] && [ "x$platform" != xpc ] && [ x"${platform}" != x"ieee1275" ]; then
548 echo "UUID needed with $platform, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
549 elif [ "$disk_module" = ata ]; then
550 echo "UUID needed with ata mod, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
551 else
552 echo "UUID needed with cross-disk installs, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
553 fi
554
555 exit 1
556 fi
557
558 if [ x"$disk_module" != x ] && [ x"$disk_module" != xbiosdisk ]; then
559 hints="`"$grub_probe" --device-map="${device_map}" --target=baremetal_hints --device "${grub_device}"`"
560 elif [ x"$platform" = xpc ]; then
561 hints="`"$grub_probe" --device-map="${device_map}" --target=bios_hints --device "${grub_device}"`"
562 elif [ x"$platform" = xefi ]; then
563 hints="`"$grub_probe" --device-map="${device_map}" --target=efi_hints --device "${grub_device}"`"
564 elif [ x"$platform" = xieee1275 ]; then
565 hints="`"$grub_probe" --device-map="${device_map}" --target=ieee1275_hints --device "${grub_device}"`"
566 elif [ x"$platform" = xloongson ] || [ x"$platform" = xqemu ] || [ x"$platform" = xcoreboot ] || [ x"$platform" = xmultiboot ] || [ x"$platform" = xqemu-mips ]; then
567 hints="`"$grub_probe" --device-map="${device_map}" --target=baremetal_hints --device "${grub_device}"`"
568 else
569 echo "No hints available for your platform. Expect reduced performance"
570 hints=
571 fi
572 echo "search.fs_uuid ${uuid} root $hints " >> "${grubdir}/load.cfg"
573 echo 'set prefix=($root)'"${relative_grubdir}" >> "${grubdir}/load.cfg"
574 config_opt="-c ${grubdir}/load.cfg "
575 modules="$modules search_fs_uuid"
576 else
577 # we need to hardcode the partition number in the core image's prefix.
578 if [ x"$grub_partition" = x ]; then
579 prefix_drive="()"
580 else
581 # Comma is already there
582 prefix_drive="($grub_partition)"
583 fi
584 fi
585 else
586 if [ x$GRUB_CRYPTODISK_ENABLE = xy ]; then
587 for uuid in "`"${grub_probe}" --device "${grub_device}" --target=cryptodisk_uuid`"; do
588 echo "cryptomount -u $uuid" >> "${grubdir}/load.cfg"
589 done
590 config_opt="-c ${grubdir}/load.cfg "
591 fi
592
593 prefix_drive=`"$grub_probe" --device-map="${device_map}" --target=drive --device "${grub_device}"` || exit 1
594 fi
595
596 case "${target_cpu}-${platform}" in
597 sparc64-ieee1275) mkimage_target=sparc64-ieee1275-raw ;;
598 mipsel-loongson) mkimage_target=mipsel-loongson-elf ;;
599 *) mkimage_target="${target_cpu}-${platform}" ;;
600 esac
601
602 case "${target_cpu}-${platform}" in
603 i386-efi | x86_64-efi) imgext=efi ;;
604 mipsel-loongson | i386-coreboot | i386-multiboot | i386-ieee1275 \
605 | powerpc-ieee1275) imgext=elf ;;
606 *) imgext=img ;;
607 esac
608
609
610 "$grub_mkimage" ${config_opt} -d "${pkglibdir}" -O ${mkimage_target} --output="${grubdir}/core.${imgext}" --prefix="${prefix_drive}${relative_grubdir}" $modules || exit 1
611
612 # Backward-compatibility kludges
613 if [ "${target_cpu}-${platform}" = "mipsel-loongson" ]; then
614 cp "${grubdir}/core.${imgext}" "${bootdir}"/grub.elf
615 elif [ "${target_cpu}-${platform}" = "i386-ieee1275" ] || [ "${target_cpu}-${platform}" = "powerpc-ieee1275" ]; then
616 cp "${grubdir}/core.${imgext}" "${grubdir}/grub"
617 elif [ "${target_cpu}-${platform}" = "i386-efi" ] || [ "${target_cpu}-${platform}" = "x86_64-efi" ]; then
618 "$grub_mkimage" ${config_opt} -d "${pkglibdir}" -O ${mkimage_target} --output="${grubdir}/grub.efi" --prefix="" $modules || exit 1
619 fi
620
621
622 # Perform the platform-dependent install
623 if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
624 # Now perform the installation.
625 "$grub_setup" ${allow_floppy} ${setup_verbose} ${setup_force} --directory="${grubdir}" \
626 --device-map="${device_map}" "${install_device}" || exit 1
627 elif [ "${target_cpu}-${platform}" = "i386-ieee1275" ] || [ "${target_cpu}-${platform}" = "powerpc-ieee1275" ]; then
628 if [ x"$update_nvram" = xyes ]; then
629 ofpathname="`which ofpathname`"
630 nvsetenv="`which nvsetenv`"
631 set "$ofpathname" dummy
632 if test -f "$1"; then
633 :
634 else
635 echo "$1: Not found." 1>&2
636 exit 1
637 fi
638 set "$nvsetenv" dummy
639 if test -f "$1"; then
640 :
641 else
642 echo "$1: Not found." 1>&2
643 exit 1
644 fi
645 # Get the Open Firmware device tree path translation.
646 dev="`echo $grub_device | sed -e 's/\/dev\///' -e 's/[0-9]\+//'`"
647 partno="`echo $grub_device | sed -e 's/.*[^0-9]\([0-9]\+\)$/\1/'`"
648 ofpath="`$ofpathname $dev`" || {
649 echo "Couldn't find Open Firmware device tree path for $dev."
650 echo "You will have to set boot-device manually."
651 exit 1
652 }
653
654 # Point boot-device at the new grub install
655 boot_device="$ofpath:$partno,"`"$grub_mkrelpath" "${grubdir}/core.${imgext}" | sed 's,/,\\\\,g'`
656 "$nvsetenv" boot-device "$boot_device" || {
657 echo "$nvsetenv failed."
658 echo "You will have to set boot-device manually. At the Open Firmware prompt, type:"
659 echo " setenv boot-device $boot_device"
660 exit 1
661 }
662 fi
663 elif [ x"${target_cpu}-${platform}" = xmips-arc ]; then
664 dvhtool -d "${install_device}" --unix-to-vh "{grubdir}/core.${imgext}" grub
665 echo "You will have to set SystemPartition and OSLoader manually."
666 elif [ x"$platform" = xefi ]; then
667 cp "${grubdir}/core.${imgext}" "${efidir}/${efi_file}"
668 # For old macs. Suggested by Peter Jones.
669 if [ x$target_cpu = xi386 ]; then
670 cp "${grubdir}/core.${imgext}" "${efidir}/boot.efi"
671 fi
672
673 # Try to make this image bootable using the EFI Boot Manager, if available.
674 efibootmgr="`which efibootmgr`"
675 if test "$removable" = no && test -n "$efi_distributor" && \
676 test -n "$efibootmgr"; then
677 # On Linux, we need the efivars kernel modules.
678 case "$host_os" in
679 linux*)
680 modprobe -q efivars 2>/dev/null || true ;;
681 esac
682
683 # Delete old entries from the same distributor.
684 for bootnum in `efibootmgr | grep '^Boot[0-9]' | \
685 fgrep -i " $efi_distributor" | cut -b5-8`; do
686 efibootmgr $efi_quiet -b "$bootnum" -B
687 done
688
689 # Add a new entry for the image we just created. efibootmgr needs to be
690 # given the disk device and partition number separately, so we have to
691 # fiddle about with grub-probe to get hold of this reasonably reliably.
692 # Use fresh device map text to avoid any problems with stale data, since
693 # all we need here is a one-to-one mapping.
694 clean_devmap="$($grub_mkdevicemap --device-map=/dev/stdout)"
695 efidir_drive="$(echo "$clean_devmap" | "$grub_probe" --device-map="${device_map}" --target=drive --device-map=/dev/stdin "$efidir")"
696 if test -z "$efidir_drive"; then
697 echo "Can't find GRUB drive for $efidir; unable to create EFI Boot Manager entry." >&2
698 else
699 efidir_disk="$(echo "$clean_devmap" | grep "^$(echo "$efidir_drive" | sed 's/,[^)]*//')" | cut -f2)"
700 efidir_part="$(echo "$efidir_drive" | sed 's/^([^,]*,[^0-9]*//; s/[^0-9].*//')"
701 efibootmgr $efi_quiet -c -d "$efidir_disk" -p "$efidir_part" -w \
702 -L "$bootloader_id" -l "\\EFI\\$efi_distributor\\$efi_file"
703 fi
704 fi
705 fi
706
707 echo "Installation finished. No error reported."
708
709 # Bye.
710 exit 0