2009-05-04 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder.git] / util / i386 / pc / grub-install.in
blob1558bc19a99699d6ef0874ab127d86bfbb1ee4c3
1 #! /bin/sh
3 # Install GRUB on your drive.
4 # Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
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.
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.
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/>.
19 # Initialize some variables.
20 transform="@program_transform_name@"
22 prefix=@prefix@
23 exec_prefix=@exec_prefix@
24 sbindir=@sbindir@
25 bindir=@bindir@
26 libdir=@libdir@
27 PACKAGE_NAME=@PACKAGE_NAME@
28 PACKAGE_TARNAME=@PACKAGE_TARNAME@
29 PACKAGE_VERSION=@PACKAGE_VERSION@
30 target_cpu=@target_cpu@
31 platform=@platform@
32 pkglibdir=${libdir}/`echo ${PACKAGE_TARNAME}/${target_cpu}-${platform} | sed ${transform}`
34 grub_setup=${sbindir}/`echo grub-setup | sed ${transform}`
35 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
36 grub_mkimage=${bindir}/`echo grub-mkimage | sed ${transform}`
37 else
38 grub_mkimage=${bindir}/`echo grub-mkelfimage | sed ${transform}`
40 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
41 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
42 rootdir=
43 grub_prefix=`echo /boot/grub | sed ${transform}`
44 modules=
46 install_device=
47 no_floppy=
48 force_lba=
49 recheck=no
50 debug=no
52 # Usage: usage
53 # Print the usage.
54 usage () {
55 cat <<EOF
56 Usage: grub-install [OPTION] install_device
57 Install GRUB on your drive.
59 -h, --help print this message and exit
60 -v, --version print the version information and exit
61 --modules=MODULES pre-load specified modules MODULES
62 --root-directory=DIR install GRUB images under the directory DIR
63 instead of the root directory
64 --grub-setup=FILE use FILE as grub-setup
65 --grub-mkimage=FILE use FILE as grub-mkimage
66 --grub-mkdevicemap=FILE use FILE as grub-mkdevicemap
67 --grub-probe=FILE use FILE as grub-probe
68 --no-floppy do not probe any floppy drive
69 --recheck probe a device map even if it already exists
70 --force install even if problems are detected
72 INSTALL_DEVICE can be a GRUB device name or a system device filename.
74 grub-install copies GRUB images into the DIR/boot directory specified by
75 --root-directory, and uses grub-setup to install grub into the boot
76 sector.
78 Report bugs to <bug-grub@gnu.org>.
79 EOF
82 # Check the arguments.
83 for option in "$@"; do
84 case "$option" in
85 -h | --help)
86 usage
87 exit 0 ;;
88 -v | --version)
89 echo "grub-install (GNU GRUB ${PACKAGE_VERSION})"
90 exit 0 ;;
91 --modules=*)
92 modules=`echo "$option" | sed 's/--modules=//'` ;;
93 --root-directory=*)
94 rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
95 --grub-setup=*)
96 grub_setup=`echo "$option" | sed 's/--grub-setup=//'` ;;
97 --grub-mkimage=*)
98 grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'` ;;
99 --grub-mkdevicemap=*)
100 grub_mkdevicemap=`echo "$option" | sed 's/--grub-mkdevicemap=//'` ;;
101 --grub-probe=*)
102 grub_probe=`echo "$option" | sed 's/--grub-probe=//'` ;;
103 --no-floppy)
104 no_floppy="--no-floppy" ;;
105 --recheck)
106 recheck=yes ;;
107 # This is an undocumented feature...
108 --debug)
109 debug=yes ;;
110 -f | --force)
111 setup_force="--force" ;;
113 echo "Unrecognized option \`$option'" 1>&2
114 usage
115 exit 1
118 if test "x$install_device" != x; then
119 echo "More than one install_devices?" 1>&2
120 usage
121 exit 1
123 install_device="${option}" ;;
124 esac
125 done
127 # for make_system_path_relative_to_its_root()
128 . ${libdir}/grub/grub-mkconfig_lib
130 if test "x$install_device" = x; then
131 echo "install_device not specified." 1>&2
132 usage
133 exit 1
136 # If the debugging feature is enabled, print commands.
137 setup_verbose=
138 if test $debug = yes; then
139 set -x
140 setup_verbose="--verbose"
143 # Initialize these directories here, since ROOTDIR was initialized.
144 case "$host_os" in
145 netbsd* | openbsd*)
146 # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub
147 # instead of /boot/grub.
148 grub_prefix=`echo /grub | sed ${transform}`
149 bootdir=${rootdir}
152 # Use /boot/grub by default.
153 bootdir=${rootdir}/boot
155 esac
157 grubdir=${bootdir}/`echo grub | sed ${transform}`
158 device_map=${grubdir}/device.map
160 grub_probe="${grub_probe} --device-map=${device_map}"
162 # Check if GRUB is installed.
163 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
164 set $grub_setup dummy
165 if test -f "$1"; then
167 else
168 echo "$1: Not found." 1>&2
169 exit 1
173 set $grub_mkimage dummy
174 if test -f "$1"; then
176 else
177 echo "$1: Not found." 1>&2
178 exit 1
181 set $grub_mkdevicemap dummy
182 if test -f "$1"; then
184 else
185 echo "$1: Not found." 1>&2
186 exit 1
189 # Create the GRUB directory if it is not present.
190 test -d "$bootdir" || mkdir "$bootdir" || exit 1
191 test -d "$grubdir" || mkdir "$grubdir" || exit 1
193 # If --recheck is specified, remove the device map, if present.
194 if test $recheck = yes; then
195 rm -f $device_map
198 # Create the device map file if it is not present.
199 if test -f "$device_map"; then
201 else
202 # Create a safe temporary file.
203 test -n "$mklog" && log_file=`$mklog`
205 $grub_mkdevicemap --device-map=$device_map $no_floppy || exit 1
208 # Make sure that there is no duplicated entry.
209 tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \
210 | sort | uniq -d | sed -n 1p`
211 if test -n "$tmp"; then
212 echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2
213 exit 1
216 # Copy the GRUB images to the GRUB directory.
217 for file in ${grubdir}/*.mod ${grubdir}/*.lst ${grubdir}/*.img ${grubdir}/efiemu??.o; do
218 if test -f $file && [ "`basename $file`" != menu.lst ]; then
219 rm -f $file || exit 1
221 done
222 for file in ${pkglibdir}/*.mod ${pkglibdir}/*.lst; do
223 cp -f $file ${grubdir} || exit 1
224 done
225 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
226 for file in ${pkglibdir}/*.img ${pkglibdir}/efiemu??.o; do
227 if test -f $file; then
228 cp -f $file ${grubdir} || exit 1
230 done
233 # Write device to a variable so we don't have to traverse /dev every time.
234 grub_device=`$grub_probe --target=device ${grubdir}`
236 # Create the core image. First, auto-detect the filesystem module.
237 fs_module=`$grub_probe --target=fs --device ${grub_device}`
238 if test "x$fs_module" = x -a "x$modules" = x; then
239 echo "Auto-detection of a filesystem module failed." 1>&2
240 echo "Please specify the module with the option \`--modules' explicitly." 1>&2
241 exit 1
244 # Then the partition map module. In order to support partition-less media,
245 # this command is allowed to fail (--target=fs already grants us that the
246 # filesystem will be accessible).
247 partmap_module=`$grub_probe --target=partmap --device ${grub_device} 2> /dev/null`
249 # Device abstraction module, if any (lvm, raid).
250 devabstraction_module=`$grub_probe --target=abstraction --device ${grub_device}`
252 # The order in this list is critical. Be careful when modifying it.
253 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
254 modules="$modules biosdisk"
255 else
256 modules="$modules ata"
258 modules="$modules $fs_module $partmap_module $devabstraction_module"
260 prefix_drive=
261 if [ "x${devabstraction_module}" = "x" ] ; then
262 if echo "${install_device}" | grep -qx "(.*)" ; then
263 install_drive="${install_device}"
264 else
265 install_drive="`$grub_probe --target=drive --device ${install_device}`"
267 grub_drive="`$grub_probe --target=drive --device ${grub_device}`"
269 # Strip partition number
270 install_drive="`echo ${install_drive} | sed -e s/,[0-9]*[a-z]*//g`"
271 grub_drive="`echo ${grub_drive} | sed -e s/,[0-9]*[a-z]*//g`"
272 if [ "${target_cpu}-${platform}" != "i386-pc" ] ; then
273 # generic method (used on coreboot)
274 uuid="`$grub_probe --target=fs_uuid --device ${grub_device}`"
275 if [ "x${uuid}" = "x" ] ; then
276 echo "UUID needed on this platform, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
277 exit 1
279 prefix_drive="(UUID=${uuid})"
280 modules="$modules fs_uuid"
281 elif [ "x${grub_drive}" != "x${install_drive}" ] ; then
282 uuid="`$grub_probe --target=fs_uuid --device ${grub_device}`"
283 if [ "x${uuid}" = "x" ] ; then
284 echo "You attempted a cross-disk install, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
285 exit 1
287 prefix_drive="(UUID=${uuid})"
288 modules="$modules fs_uuid"
290 else
291 prefix_drive=`$grub_probe --target=drive --device ${grub_device}`
294 relative_grubdir=`make_system_path_relative_to_its_root ${grubdir}` || exit 1
295 if [ "x${relative_grubdir}" = "x" ] ; then
296 relative_grubdir=/
299 if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
300 $grub_mkimage --output=${grubdir}/core.img --prefix=${prefix_drive}${relative_grubdir} $modules || exit 1
302 # Now perform the installation.
303 $grub_setup ${setup_verbose} ${setup_force} --directory=${grubdir} --device-map=${device_map} \
304 ${install_device} || exit 1
305 else
306 $grub_mkimage -d ${pkglibdir} --output=/boot/multiboot.img --prefix=${prefix_drive}${relative_grubdir} $modules || exit 1
309 # Prompt the user to check if the device map is correct.
310 echo "Installation finished. No error reported."
311 echo "This is the contents of the device map $device_map."
312 echo "Check if this is correct or not. If any of the lines is incorrect,"
313 echo "fix it and re-run the script \`grub-install'."
314 echo
316 cat $device_map
318 # Bye.
319 exit 0