]> git.proxmox.com Git - grub2.git/blob - util/grub-install.in
Unify file copying setup across different install scripts. Add
[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 prefix="@prefix@"
21 exec_prefix="@exec_prefix@"
22 datarootdir="@datarootdir@"
23 sbindir="@sbindir@"
24 bindir="@bindir@"
25 libdir="@libdir@"
26 sysconfdir="@sysconfdir@"
27 PACKAGE_NAME=@PACKAGE_NAME@
28 PACKAGE_TARNAME=@PACKAGE_TARNAME@
29 PACKAGE_VERSION=@PACKAGE_VERSION@
30
31 export TEXTDOMAIN=@PACKAGE@
32 export TEXTDOMAINDIR="@localedir@"
33
34 host_os=@host_os@
35 source_dir=
36 target=
37 datadir="@datadir@"
38 if [ "x$pkgdatadir" = x ]; then
39 pkgdatadir="${datadir}/@PACKAGE@"
40 fi
41 localedir="@datadir@/locale"
42
43 self="`basename $0`"
44
45 grub_mkimage="${bindir}/@grub_mkimage@"
46 grub_probe="${sbindir}/@grub_probe@"
47 grub_editenv="${bindir}/@grub_editenv@"
48 grub_mkrelpath="${bindir}/@grub_mkrelpath@"
49 rootdir=
50 bootdir=
51 grubdir="`echo "/@bootdirname@/@grubdirname@" | sed 's,//*,/,g'`"
52 modules=
53
54 install_device=
55 force_lba=
56 recheck=no
57 debug=no
58 debug_image=
59
60 update_nvram=yes
61
62 removable=no
63 efi_quiet=
64
65 # Get GRUB_DISTRIBUTOR.
66 if test -f "${sysconfdir}/default/grub" ; then
67 . "${sysconfdir}/default/grub"
68 fi
69
70 bootloader_id="$(echo "$GRUB_DISTRIBUTOR" | tr 'A-Z' 'a-z' | cut -d' ' -f1)"
71 if test -z "$bootloader_id"; then
72 bootloader_id=grub
73 fi
74
75 disk_module=unspecified
76
77 . "${pkgdatadir}/grub-mkconfig_lib"
78
79 # Usage: usage
80 # Print the usage.
81 usage () {
82 # TRANSLATORS: INSTALL_DEVICE isn't an identifier and is the DEVICE you
83 # install to.
84 gettext_printf "Usage: %s [OPTION] [INSTALL_DEVICE]" "$self"
85 echo
86 gettext "Install GRUB on your drive." ; echo
87 echo
88 print_option_help "-h, --help" "$(gettext "print this message and exit")"
89 print_option_help "-v, --version" "$(gettext "print the version information and exit")"
90 print_option_help "--modules=$(gettext "MODULES")" "$(gettext "pre-load specified modules MODULES")"
91 grub_print_install_files_help
92
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 target_trans="$(gettext "TARGET")"
97 # TRANSLATORS: "current" refers to the platform user's currently running on
98 print_option_help "--target=$target_trans" "$(gettext "install GRUB for TARGET platform [default=current]")"
99 print_option_help "--directory=$(gettext "DIR")" "$(gettext "use GRUB images from DIR. Takes precedence over target")"
100 print_option_help "--grub-setup=$(gettext "FILE")" "$(gettext "use FILE as grub-setup")"
101 print_option_help "--grub-mkimage=$(gettext "FILE")" "$(gettext "use FILE as grub-mkimage")"
102 print_option_help "--grub-mkrelpath=$(gettext "FILE")" "$(gettext "use FILE as grub-mkrelpath")"
103 print_option_help "--grub-probe=$(gettext "FILE")" "$(gettext "use FILE as grub-probe")"
104 # TRANSLATORS: "may break" doesn't just mean that option wouldn't have any
105 # effect but that it will make the resulting install unbootable from HDD.
106 print_option_help "--allow-floppy" "$(gettext "make the drive also bootable as floppy (default for fdX devices). May break on some BIOSes.")"
107 print_option_help "--recheck" "$(gettext "delete device map if it already exists")"
108 print_option_help "--force" "$(gettext "install even if problems are detected")"
109 print_option_help "--force-file-id" "$(gettext "use identifier file even if UUID is available")"
110 print_option_help "--disk-module=$(gettext "MODULE")" "$(gettext "disk module to use (biosdisk or native). This option is only available on BIOS target.")"
111 print_option_help "--no-nvram" "$(gettext "don't update the \`boot-device' NVRAM variable. This option is only available on IEEE1275 targets.")"
112 print_option_help "--removable" "$(gettext "the installation device is removable. This option is only available on EFI.")"
113 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 echo
116 gettext "INSTALL_DEVICE must be system device filename.";echo
117 echo
118
119 gettext_printf "%s copies GRUB images into %s. On some platforms, it
120 may also install GRUB into the boot sector.\n" "$self" "$grubdir";echo
121 echo
122 gettext "Report bugs to <bug-grub@gnu.org>."; echo
123 }
124
125 allow_floppy=""
126 force_file_id=
127 efidir=
128
129 # Check the arguments.
130 while test $# -gt 0
131 do
132 grub_process_install_options "$@"
133 case "$grub_process_install_options_consumed" in
134 1) shift; continue;;
135 2) shift; shift; continue;;
136 esac
137
138 option=$1
139 shift
140
141 case "$option" in
142 -h | --help)
143 usage
144 exit 0 ;;
145 -v | --version)
146 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
147 exit 0 ;;
148
149 --modules)
150 modules=`argument $option "$@"`; shift;;
151 --modules=*)
152 modules=`echo "$option" | sed 's/--modules=//'` ;;
153
154 --force-file-id)
155 force_file_id=y ;;
156
157 # Accept and ignore for compatibility
158 --font)
159 shift;;
160 --font=*)
161 ;;
162
163 # Accept for compatibility
164 --root-directory)
165 rootdir="`argument $option "$@"`"; shift;;
166 --root-directory=*)
167 rootdir="`echo "$option" | sed 's/--root-directory=//'`" ;;
168
169 --boot-directory)
170 bootdir="`argument $option "$@"`"; shift;;
171 --boot-directory=*)
172 bootdir="`echo "$option" | sed 's/--boot-directory=//'`" ;;
173
174 --efi-directory)
175 efidir="`argument $option "$@"`"; shift;;
176 --efi-directory=*)
177 efidir="`echo "$option" | sed 's/--efi-directory=//'`" ;;
178
179 --directory | -d)
180 source_dir="`argument $option "$@"`"; shift;;
181 --directory=*)
182 source_dir="`echo "$option" | sed 's/--directory=//'`" ;;
183
184 --target)
185 target="`argument $option "$@"`"; shift;;
186 --target=*)
187 target="`echo "$option" | sed 's/--target=//'`" ;;
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 # Ignore: for compatibility
210 --grub-mkdevicemap)
211 shift;;
212 --grub-mkdevicemap=*)
213 ;;
214
215 --grub-probe)
216 grub_probe="`argument "$option" "$@"`"; shift;;
217 --grub-probe=*)
218 grub_probe="`echo "$option" | sed 's/--grub-probe=//'`" ;;
219
220 --no-floppy)
221 ;;
222 --recheck)
223 recheck=yes ;;
224 --removable)
225 removable=yes ;;
226
227 --allow-floppy)
228 allow_floppy="--allow-floppy" ;;
229
230 --disk-module)
231 disk_module="`argument "$option" "$@"`"; shift;
232 ;;
233 --disk-module=*)
234 disk_module="`echo "$option" | sed 's/--disk-module=//'`"
235 ;;
236
237 --no-nvram)
238 update_nvram=no ;;
239
240 # This is an undocumented feature...
241 --debug)
242 debug=yes ;;
243 --debug-image)
244 debug_image="`argument "$option" "$@"`"; shift;;
245 --debug-image=*)
246 debug_image="`echo "$option" | sed 's/--debug-image=//'`" ;;
247
248 -f | --force)
249 setup_force="--force" ;;
250
251 -*)
252 gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
253 usage
254 exit 1
255 ;;
256 *)
257 if test "x$install_device" != x; then
258 gettext "More than one install device?" 1>&2
259 echo 1>&2
260 usage
261 exit 1
262 fi
263 install_device="${option}" ;;
264 esac
265 done
266
267 if [ x$source_dir = x ]; then
268 if [ x$target = x ]; then
269 case x"`uname -m`" in
270 x"powerpc"* | x"ppc"*)
271 target="powerpc-ieee1275";;
272 x"sparc"*)
273 target="sparc64-ieee1275";;
274 x"mips"*"el")
275 target="mipsel-loongson";;
276 x"mips"*)
277 target="mips-arc";;
278 x"ia64"*)
279 target="ia64-efi";;
280 x"x86_64"* | x"amd64"*)
281 # On Linux, we need the efivars kernel modules.
282 # If no EFI is available this module just does nothing
283 # besides a small hello and if we detect efi we'll load it
284 # anyway later. So it should be safe to
285 # try to load it here.
286 case "$host_os" in
287 linux*)
288 modprobe -q efivars 2>/dev/null || true ;;
289 esac
290 if [ -d /sys/firmware/efi ]; then
291 target="x86_64-efi"
292 else
293 target=i386-pc
294 fi
295 ;;
296 x"i"?"86"*)
297 # On Linux, we need the efivars kernel modules.
298 # If no EFI is available this module just does nothing
299 # besides a small hello and if we detect efi we'll load it
300 # anyway later. So it should be safe to
301 # try to load it here.
302 case "$host_os" in
303 linux*)
304 modprobe -q efivars 2>/dev/null || true ;;
305 esac
306 if [ -d /sys/firmware/efi ]; then
307 target="i386-efi"
308 elif [ -e /proc/device-tree ]; then
309 target=i386-pc
310 for x in /proc/device-tree/*; do
311 if [ -e "$x" ]; then
312 target="i386-ieee1275"
313 fi
314 done
315 else
316 target=i386-pc
317 fi
318 ;;
319 *)
320 gettext "Unable to determine your platform. Use --target." ;
321 echo ;;
322 esac
323 fi
324 source_dir="${libdir}/@PACKAGE@/$target"
325 fi
326
327 if ! [ -d "$source_dir" ]; then
328 gettext_printf "%s doesn't exist. Please specify --target or --directory\\n" "$source_dir"
329 exit 1
330 fi
331
332 . "${source_dir}"/modinfo.sh
333
334 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] ; then
335 if [ x$disk_module = xunspecified ]; then
336 disk_module=biosdisk
337 fi
338 elif [ "${grub_modinfo_platform}" = "ieee1275" ] || [ "${grub_modinfo_platform}" = "efi" ] || [ "${grub_modinfo_platform}" = "arc" ] ; then
339 disk_module=
340 else
341 disk_module=native
342 fi
343
344 if test "x$grub_setup" = x && [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ]; then
345 grub_setup="${sbindir}/@grub_bios_setup@"
346 fi
347
348 if test "x$grub_setup" = x && [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ]; then
349 grub_setup="${sbindir}/@grub_sparc64_setup@"
350 fi
351
352 if test "x$install_device" = x && ([ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] \
353 || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ]); then
354 gettext "Install device isn't specified." 1>&2
355 echo 1>&2
356 usage
357 exit 1
358 fi
359
360 if ! ([ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] \
361 || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ] \
362 || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "powerpc-ieee1275" ] \
363 || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "mips-arc" ]); then
364 install_device=
365 fi
366
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"
372 efi_quiet=-q
373 fi
374
375 if [ -z "$bootdir" ]; then
376 # Default bootdir if bootdir not initialized.
377 bootdir="/@bootdirname@"
378
379 if [ -n "$rootdir" ] ; then
380 # Initialize bootdir if rootdir was initialized.
381 bootdir="${rootdir}/@bootdirname@"
382 fi
383 fi
384
385 grubdir="`echo "${bootdir}/@grubdirname@" | sed 's,//*,/,g'`"
386 device_map="${grubdir}/device.map"
387
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 set $grub_setup dummy
392 if test -f "$1"; then
393 :
394 else
395 echo "$1: Not found." 1>&2
396 exit 1
397 fi
398 fi
399
400 set "$grub_mkimage" dummy
401 if test -f "$1"; then
402 :
403 else
404 echo "$1: Not found." 1>&2
405 exit 1
406 fi
407
408 if [ x"$grub_modinfo_platform" = xefi ]; then
409 # Find the EFI System Partition.
410 if test -n "$efidir"; then
411 install_device="`"$grub_probe" --target=device --device-map= "${efidir}"`"
412 else
413 if test -d "${bootdir}/efi"; then
414 install_device="`"$grub_probe" --target=device --device-map= "${bootdir}/efi"`"
415 # Is it a mount point?
416 if test "x$install_device" != "x`"$grub_probe" --target=device --device-map= "${bootdir}"`"; then
417 efidir="${bootdir}/efi"
418 fi
419 elif test -d "${bootdir}/EFI"; then
420 install_device="`"$grub_probe" --target=device --device-map= "${bootdir}/EFI"`"
421 # Is it a mount point?
422 if test "x$install_device" != "x`"$grub_probe" --target=device --device-map= "${bootdir}"`"; then
423 efidir="${bootdir}/EFI"
424 fi
425 elif test -n "$rootdir" && test "x$rootdir" != "x/"; then
426 # The EFI System Partition may have been given directly using
427 # --root-directory.
428 install_device="`"$grub_probe" --target=device --device-map= "${rootdir}"`"
429 # Is it a mount point?
430 if test "x$install_device" != "x`"$grub_probe" --target=device --device-map= "${rootdir}/.."`"; then
431 efidir="${rootdir}"
432 fi
433 fi
434
435 if test -n "$efidir"; then
436 efi_fs=`"$grub_probe" --target=fs "--device-map=${device_map}" "${efidir}"`
437 if test "x$efi_fs" = xfat; then :; else
438 gettext_printf "%s doesn't look like an EFI partition.\n" "${efidir}" 1>&2
439 efidir=
440 fi
441 fi
442 fi
443
444 if test -n "$efidir"; then
445 # The EFI specification requires that an EFI System Partition must
446 # contain an "EFI" subdirectory, and that OS loaders are stored in
447 # subdirectories below EFI. Vendors are expected to pick names that do
448 # not collide with other vendors. To minimise collisions, we use the
449 # name of our distributor if possible.
450 efi_distributor="$bootloader_id"
451 if test $removable = yes; then
452 # The specification makes stricter requirements of removable
453 # devices, in order that only one image can be automatically loaded
454 # from them. The image must always reside under /EFI/BOOT, and it
455 # must have a specific file name depending on the architecture.
456 efi_distributor=BOOT
457 case "$grub_modinfo_target_cpu" in
458 i386)
459 efi_file=BOOTIA32.EFI ;;
460 x86_64)
461 efi_file=BOOTX64.EFI ;;
462 # GRUB does not yet support these architectures, but they're defined
463 # by the specification so we include them here to ease future
464 # expansion.
465 ia64)
466 efi_file=BOOTIA64.EFI ;;
467 esac
468 else
469 # It is convenient for each architecture to have a different
470 # efi_file, so that different versions can be installed in parallel.
471 case "$grub_modinfo_target_cpu" in
472 i386)
473 efi_file=grubia32.efi ;;
474 x86_64)
475 efi_file=grubx64.efi ;;
476 # GRUB does not yet support these architectures, but they're defined
477 # by the specification so we include them here to ease future
478 # expansion.
479 ia64)
480 efi_file=grubia64.efi ;;
481 *)
482 efi_file=grub.efi ;;
483 esac
484 fi
485 efidir="$efidir/EFI/$efi_distributor"
486 mkdir -p "$efidir" || exit 1
487 else
488 # We don't know what's going on. Fall back to traditional
489 # (non-specification-compliant) behaviour.
490 efidir="$grubdir"
491 efi_distributor=
492 efi_file=grub.efi
493 fi
494 fi
495
496 # Create the GRUB directory if it is not present.
497 mkdir -p "$grubdir" || exit 1
498 mkdir -p "$grubdir/${grub_modinfo_target_cpu}-$grub_modinfo_platform" || exit 1
499
500 # If --recheck is specified, remove the device map, if present.
501 if test $recheck = yes; then
502 rm -f "$device_map"
503 fi
504
505 # Device map file is optional
506 if test -f "$device_map"; then
507 # Make sure that there is no duplicated entry.
508 tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' "$device_map" \
509 | sort | uniq -d | sed -n 1p`
510 if test -n "$tmp"; then
511 gettext_printf "The drive %s is defined multiple times in the device map %s\n" "$tmp" "$device_map" 1>&2
512 exit 1
513 fi
514 else
515 device_map=
516 fi
517
518 # Copy the GRUB images to the GRUB directory.
519 grub_install_files "${source_dir}" "${grubdir}" "${grub_modinfo_target_cpu}-$grub_modinfo_platform" all
520
521 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ] ; then
522 for file in "${source_dir}"/*.img "${source_dir}"/efiemu??.o; do
523 if test -f "$file"; then
524 cp -f "$file" "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform" || exit 1
525 fi
526 done
527 fi
528
529 if ! is_path_readable_by_grub "${grubdir}"; then
530 gettext_printf "Path \`%s' is not readable by GRUB on boot. Installation is impossible. Aborting.\n" "${grubdir}" 1>&2
531 exit 1
532 fi
533
534 # Write device to a variable so we don't have to traverse /dev every time.
535 grub_device="`"$grub_probe" --device-map="${device_map}" --target=device "${grubdir}"`" || exit 1
536
537 if ! test -f "${grubdir}"/grubenv; then
538 "$grub_editenv" "${grubdir}"/grubenv create
539 fi
540
541 # Create the core image. First, auto-detect the filesystem module.
542 fs_module="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=fs --device `"
543 if test "x$fs_module" = x ; then
544 gettext_printf "Auto-detection of a filesystem of %s failed.\n" "${grub_device}" 1>&2
545 gettext "Try with --recheck." 1>&2
546 echo 1>&2
547 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
548 exit 1
549 fi
550
551 # Then the partition map module. In order to support partition-less media,
552 # this command is allowed to fail (--target=fs already grants us that the
553 # filesystem will be accessible).
554 partmap_module=
555 for x in `echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=partmap --device 2> /dev/null`; do
556 case "$x" in
557 netbsd | openbsd)
558 partmap_module="$partmap_module part_bsd";;
559 "") ;;
560 *)
561 partmap_module="$partmap_module part_$x";;
562 esac
563 done
564
565 # Device abstraction module, if any (lvm, raid).
566 devabstraction_module="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=abstraction --device`"
567
568 if [ "x$disk_module" = xata ]; then
569 disk_module=pata
570 fi
571
572 if [ "x$disk_module" = xnative ]; then
573 disk_module="pata ahci ohci"
574 if [ "x$grub_modinfo_target_cpu" = "xi386" ] || [ "x$grub_modinfo_target_cpu" = "xx86_64" ]; then
575 disk_module="$disk_module uhci"
576 fi
577 disk_module="$disk_module usbms"
578 fi
579
580 # The order in this list is critical. Be careful when modifying it.
581 modules="$modules $disk_module"
582 modules="$modules $fs_module $partmap_module $devabstraction_module"
583
584 relative_grubdir="`"$grub_mkrelpath" "${grubdir}"`" || exit 1
585 if [ "x${relative_grubdir}" = "x" ] ; then
586 relative_grubdir=/
587 fi
588
589 prefix_drive=
590 config_opt_file=
591
592 rm -f "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
593
594 if [ "x${debug_image}" != x ]; then
595 echo "set debug='${debug_image}'" >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
596 config_opt_file="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
597 fi
598
599 if [ "x${devabstraction_module}" = "x" ] ; then
600 if [ x"${install_device}" != x ]; then
601 if echo "${install_device}" | grep -qx "(.*)" ; then
602 install_drive="${install_device}"
603 else
604 install_drive="`"$grub_probe" --device-map="${device_map}" --target=drive --device "${install_device}"`" || exit 1
605 fi
606 install_drive="`echo "${install_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\1/'`"
607 fi
608 grub_drive="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=drive --device`" || exit 1
609
610 # Strip partition number
611 grub_partition="`echo "${grub_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\3/'`"
612 grub_drive="`echo "${grub_drive}" | sed -e 's/^(\(\([^,\\\\]\|\\\\\\\\\|\\\\,\)*\)\(\(,[a-zA-Z0-9]*\)*\))$/\1/'`"
613
614 if [ x"${install_device}" = x ] && [ x"${grub_modinfo_target_cpu}-$grub_modinfo_platform" = x"powerpc-ieee1275" ]; then
615 install_drive="$grub_drive"
616 fi
617
618 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
619 # generic method (used on coreboot and ata mod)
620 uuid=
621 if [ x"$force_file_id" != xy ]; then
622 uuid="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=fs_uuid --device`"
623 fi
624
625 if [ x"$disk_module" != x ] && [ x"$disk_module" != xbiosdisk ]; then
626 hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=baremetal_hints --device`"
627 elif [ x"$grub_modinfo_platform" = xpc ]; then
628 hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=bios_hints --device`"
629 elif [ x"$grub_modinfo_platform" = xefi ]; then
630 hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=efi_hints --device`"
631 elif [ x"$grub_modinfo_platform" = xieee1275 ]; then
632 hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=ieee1275_hints --device`"
633 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
634 hints="`echo "${grub_device}" | xargs "$grub_probe" --device-map="${device_map}" --target=baremetal_hints --device`"
635 else
636 gettext "No hints available for your platform. Expect reduced performance." 1>&2
637 echo 1>&2
638 hints=
639 fi
640 if [ x"$uuid" != x ]; then
641 echo "search.fs_uuid ${uuid} root $hints " >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
642 search_module=search_fs_uuid
643 else
644 mkdir -p "${grubdir}/uuid"
645 file="`mktemp "${grubdir}/uuid/XXXXXXXXXXXXXXXXXXXXXXXXX"`"
646 relfile="`${grub_mkrelpath} "$file"`"
647 echo "search.file '${relfile}' root $hints " >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
648 search_module=search_fs_file
649 fi
650 echo 'set prefix=($root)'"$(echo "${relative_grubdir}" | sed "s,\\([ \"'\\\\]\\),\\\\\\1,g")" >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
651 config_opt_file="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
652 modules="$modules $search_module"
653 else
654 # we need to hardcode the partition number in the core image's prefix.
655 if [ x"$grub_partition" = x ]; then
656 prefix_drive="()"
657 else
658 # Comma is already there
659 prefix_drive="($grub_partition)"
660 fi
661 fi
662 else
663 if [ x$GRUB_CRYPTODISK_ENABLE = xy ]; then
664 for uuid in "`echo "${grub_device}" | xargs "${grub_probe}" --target=cryptodisk_uuid --device`"; do
665 echo "cryptomount -u $uuid" >> "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
666 done
667 config_opt_file="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/load.cfg"
668 fi
669
670 prefix_drive=`"$grub_probe" --device-map="${device_map}" --target=drive --device "${grub_device}"` || exit 1
671 fi
672
673 case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
674 sparc64-ieee1275) mkimage_target=sparc64-ieee1275-raw ;;
675 mipsel-loongson) mkimage_target=mipsel-loongson-elf ;;
676 *) mkimage_target="${grub_modinfo_target_cpu}-${grub_modinfo_platform}" ;;
677 esac
678
679 case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
680 i386-efi | x86_64-efi) imgext=efi ;;
681 mipsel-loongson | i386-coreboot | i386-multiboot | i386-ieee1275 \
682 | powerpc-ieee1275) imgext=elf ;;
683 *) imgext=img ;;
684 esac
685
686 if [ x"$config_opt_file" = x ]; then
687 "$grub_mkimage" -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
688 else
689 "$grub_mkimage" -c "${config_opt_file}" -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
690 fi
691
692 # Backward-compatibility kludges
693 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "mipsel-loongson" ]; then
694 cp "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" "${bootdir}"/grub.elf
695 elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-ieee1275" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "powerpc-ieee1275" ]; then
696 cp "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" "${grubdir}/grub"
697 elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-efi" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "x86_64-efi" ]; then
698
699 if [ x"$config_opt_file" = x ]; then
700 "$grub_mkimage" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/grub.efi" --prefix="" $modules || exit 1
701 else
702 "$grub_mkimage" -c "${config_opt_file}" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/grub.efi" --prefix="" $modules || exit 1
703 fi
704 fi
705
706
707 # Perform the grub_modinfo_platform-dependent install
708 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-pc" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "sparc64-ieee1275" ] ; then
709 # Now perform the installation.
710 "$grub_setup" ${allow_floppy} ${setup_verbose} ${setup_force} --directory="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform" \
711 --device-map="${device_map}" "${install_device}" || exit 1
712 elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-ieee1275" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "powerpc-ieee1275" ]; then
713
714 # If a install device is defined, copy the core.elf to PReP partition.
715 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "powerpc-ieee1275" ] && [ -n "${install_device}" ]; then
716 if [ "$("${grub_probe}" -m "${device_map}" -d "${install_device}" -t msdos_parttype)" != "41" ]; then
717 gettext "The chosen partition is not a PReP partition." 1>&2
718 echo 1>&2
719 exit 1
720 fi
721
722 if [ "$(file -s -b -L "${install_device}" | awk '{ print $1 }')" = ELF ] || [ x$("${grub_probe}" -m "${device_map}" -d "${install_device}" -t zero_check) = xtrue ]; then
723 dd if="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" of="${install_device}" status=noxfer || {
724 gettext "Failed to copy Grub to the PReP partition." 1>&2
725 echo 1>&2
726 exit 1
727 }
728 else
729 gettext "The PReP partition is not empty. If you are sure you want to use it, run dd to clear it:" 1>&2
730 echo 1>&2
731 echo " dd if=/dev/zero of=${install_device}"
732 exit 1
733 fi
734 fi
735
736 if [ x"$update_nvram" = xyes ]; then
737 ofpathname="`which ofpathname`"
738 nvsetenv="`which nvsetenv`"
739 set "$ofpathname" dummy
740 if test -f "$1"; then
741 :
742 else
743 echo "$1: Not found." 1>&2
744 exit 1
745 fi
746 set "$nvsetenv" dummy
747 if test -f "$1"; then
748 :
749 else
750 echo "$1: Not found." 1>&2
751 exit 1
752 fi
753 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" != "powerpc-ieee1275" ] \
754 || [ -z "${install_device}" ]; then
755 # Get the Open Firmware device tree path translation.
756 dev="`echo $grub_device | sed -e 's/\/dev\///' -e 's/[0-9]\+//'`"
757 partno="`echo $grub_device | sed -e 's/.*[^0-9]\([0-9]\+\)$/\1/'`"
758 ofpath="`$ofpathname $dev`" || {
759 # TRANSLATORS: "device tree path" is the name of the device
760 # for IEEE1275
761 gettext_printf "Couldn't find IEEE1275 device tree path for %s.\nYou will have to set \`boot-device' variable manually.\n" "$dev" 1>&2
762 exit 1
763 }
764
765 # Point boot-device at the new grub install
766 boot_device="$ofpath:$partno,"`"$grub_mkrelpath" "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" | sed 's,/,\\\\,g'`
767
768 else
769
770 dev="`echo "${install_device}" | sed -e 's/\/dev\///' -e 's/[0-9]\+//'`"
771 boot_device="`$ofpathname "$dev"`" || {
772 # TRANSLATORS: "device tree path" is the name of the device
773 # for IEEE1275
774 gettext_printf "Couldn't find IEEE1275 device tree path for %s.\nYou will have to set \`boot-device' variable manually.\n" "$dev" 1>&2
775 exit 1
776 }
777 fi
778
779 "$nvsetenv" boot-device "$boot_device" || {
780 # TRANSLATORS: The %s will be replaced by an external program name.
781 gettext_printf "\`%s' failed.\n" "$nvsetenv" 1>&2
782 gettext "You will have to set \`boot-device' variable manually. At the IEEE1275 prompt, type:" 1>&2
783 echo 1>&2
784 echo " setenv boot-device $boot_device" 1>&2
785 exit 1
786 }
787 fi
788 elif [ x"${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = xmips-arc ]; then
789 dvhtool -d "${install_device}" --unix-to-vh "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" grub
790 gettext "You will have to set \`SystemPartition' and \`OSLoader' manually." 1>&2
791 echo 1>&2
792 elif [ x"$grub_modinfo_platform" = xefi ]; then
793 cp "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" "${efidir}/${efi_file}"
794 # For old macs. Suggested by Peter Jones.
795 if [ x$grub_modinfo_target_cpu = xi386 ]; then
796 cp "${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" "${efidir}/boot.efi"
797 fi
798
799 # Try to make this image bootable using the EFI Boot Manager, if available.
800 efibootmgr="`which efibootmgr`"
801 if test "$removable" = no && test -n "$efi_distributor" && \
802 test -n "$efibootmgr"; then
803 # On Linux, we need the efivars kernel modules.
804 case "$host_os" in
805 linux*)
806 modprobe -q efivars 2>/dev/null || true ;;
807 esac
808
809 # Delete old entries from the same distributor.
810 for bootnum in `efibootmgr | grep '^Boot[0-9]' | \
811 fgrep -i " $efi_distributor" | cut -b5-8`; do
812 efibootmgr $efi_quiet -b "$bootnum" -B
813 done
814
815 # Add a new entry for the image we just created. efibootmgr needs to be
816 # given the disk device and partition number separately, so we have to
817 # fiddle about with grub-probe to get hold of this reasonably reliably.
818 # Use fresh device map text to avoid any problems with stale data, since
819 # all we need here is a one-to-one mapping.
820 efidir_drive="$("$grub_probe" --target=drive --device-map= "$efidir")"
821 efidir_disk="$("$grub_probe" --target=disk --device-map= "$efidir")"
822 if test -z "$efidir_drive" || test -z "$efidir_disk"; then
823 gettext_printf "Can't find GRUB drive for %s; unable to create EFI Boot Manager entry.\n" "$efidir" >&2
824 else
825 efidir_part="$(echo "$efidir_drive" | sed 's/^([^,]*,[^0-9]*//; s/[^0-9].*//')"
826 efibootmgr $efi_quiet -c -d "$efidir_disk" -p "$efidir_part" -w \
827 -L "$bootloader_id" -l "\\EFI\\$efi_distributor\\$efi_file"
828 fi
829 fi
830 else
831 gettext "WARNING: no platform-specific install was performed" 1>&2
832 echo 1>&2
833 fi
834
835 gettext "Installation finished. No error reported." 1>&2
836 echo 1>&2
837
838 # Bye.
839 exit 0