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