3 # Install GRUB on your drive.
4 # Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
6 # This file is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # Initialize some variables.
22 exec_prefix
=@exec_prefix@
29 host_vendor
=@host_vendor@
30 pkglibdir
=${libdir}/${PACKAGE}/${host_cpu}-${host_vendor}
32 grub_shell
=${sbindir}/grub
33 grub_set_default
=${sbindir}/grub-set-default
34 log_file
=/tmp
/grub-install.log.$$
35 img_file
=/tmp
/grub-install.img.$$
37 grub_prefix
=/boot
/grub
45 # look for secure tempfile creation wrappers on this platform
46 if test -x /bin
/tempfile
; then
47 mklog
="/bin/tempfile --prefix=grub"
48 mkimg
="/bin/tempfile --prefix=grub"
49 elif test -x /bin
/mktemp
; then
50 mklog
="/bin/mktemp /tmp/grub-install.log.XXXXXX"
51 mkimg
="/bin/mktemp /tmp/grub-install.img.XXXXXX"
61 Usage: grub-install [OPTION] install_device
62 Install GRUB on your drive.
64 -h, --help print this message and exit
65 -v, --version print the version information and exit
66 --root-directory=DIR install GRUB images under the directory DIR
67 instead of the root directory
68 --grub-shell=FILE use FILE as the grub shell
69 --no-floppy do not probe any floppy drive
70 --force-lba force GRUB to use LBA mode even for a buggy
72 --recheck probe a device map even if it already exists
74 INSTALL_DEVICE can be a GRUB device name or a system device filename.
76 grub-install copies GRUB images into the DIR/boot directory specfied by
77 --root-directory, and uses the grub shell to install grub into the boot
80 Report bugs to <bug-grub@gnu.org>.
84 # Usage: convert os_device
85 # Convert an OS device to the corresponding GRUB drive.
86 # This part is OS-specific.
88 # First, check if the device file exists.
92 echo "$1: Not found or not a block device." 1>&2
96 # Break the device name into the disk part and the partition part.
99 tmp_disk
=`echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
100 -e 's%\(d[0-9]*\)p[0-9]*$%\1%' \
101 -e 's%\(fd[0-9]*\)$%\1%' \
102 -e 's%/part[0-9]*$%/disc%' \
103 -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
104 tmp_part
=`echo "$1" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
105 -e 's%.*d[0-9]*p%%' \
106 -e 's%.*/fd[0-9]*$%%' \
107 -e 's%.*/floppy/[0-9]*$%%' \
108 -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
109 -e 's%.*c[0-7]d[0-9]*p%%'`
112 tmp_disk
=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
113 tmp_part
=`echo "$1" | sed "s%$tmp_disk%%"` ;;
114 freebsd
* | kfreebsd
*-gnu)
115 tmp_disk
=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%r\1%' \
116 | sed 's%r\{0,1\}\(da[0-9]*\).*$%r\1%'`
117 tmp_part
=`echo "$1" \
118 | sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
119 | sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
121 netbsd
* | knetbsd
*-gnu)
122 tmp_disk
=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
123 | sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
124 tmp_part
=`echo "$1" \
125 | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
128 echo "grub-install does not support your OS yet." 1>&2
132 # Get the drive name.
133 tmp_drive
=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
134 | sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%'`
136 # If not found, print an error message and exit.
137 if test "x$tmp_drive" = x
; then
138 echo "$1 does not have any corresponding BIOS drive." 1>&2
142 if test "x$tmp_part" != x
; then
143 # If a partition is specified, we need to translate it into the
147 echo "$tmp_drive" |
sed "s%)$%,`expr $tmp_part - 1`)%" ;;
149 if echo $tmp_part |
grep "^s" >/dev
/null
; then
150 tmp_pc_slice
=`echo $tmp_part \
151 | sed "s%s\([0-9]*\)[a-g]*$%\1%"`
152 tmp_drive
=`echo "$tmp_drive" \
153 | sed "s%)%,\`expr "$tmp_pc_slice" - 1\
`)%"`
155 if echo $tmp_part |
grep "[a-g]$" >/dev
/null
; then
156 tmp_bsd_partition
=`echo "$tmp_part" \
157 | sed "s%[^a-g]*\([a-g]\)$%\1%"`
158 tmp_drive
=`echo "$tmp_drive" \
159 | sed "s%)%,$tmp_bsd_partition)%"`
162 freebsd
* | kfreebsd
*-gnu)
163 if echo $tmp_part |
grep "^s" >/dev
/null
; then
164 tmp_pc_slice
=`echo $tmp_part \
165 | sed "s%s\([0-9]*\)[a-h]*$%\1%"`
166 tmp_drive
=`echo "$tmp_drive" \
167 | sed "s%)%,\`expr "$tmp_pc_slice" - 1\
`)%"`
169 if echo $tmp_part |
grep "[a-h]$" >/dev
/null
; then
170 tmp_bsd_partition
=`echo "$tmp_part" \
171 | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
172 tmp_drive
=`echo "$tmp_drive" \
173 | sed "s%)%,$tmp_bsd_partition)%"`
176 netbsd
* | knetbsd
*-gnu)
177 if echo $tmp_part |
grep "^[abe-p]$" >/dev
/null
; then
178 tmp_bsd_partition
=`echo "$tmp_part" \
179 | sed "s%\([a-p]\)$%\1%"`
180 tmp_drive
=`echo "$tmp_drive" \
181 | sed "s%)%,$tmp_bsd_partition)%"`
186 # If no partition is specified, just print the drive name.
191 # Usage: resolve_symlink file
192 # Find the real file/device that file points at
196 while test -L $tmp_fname; do
197 tmp_new_fname
=`ls -al $tmp_fname | sed -n 's%.*-> \(.*\)%\1%p'`
198 if test -z "$tmp_new_fname"; then
199 echo "Unrecognized ls output" 2>&1
203 # Convert relative symlinks
204 case $tmp_new_fname in
205 /*) tmp_fname
="$tmp_new_fname"
207 *) tmp_fname
="`echo $tmp_fname | sed 's%/[^/]*$%%'`/$tmp_new_fname"
214 # Usage: find_device file
215 # Find block device on which the file resides.
217 # For now, this uses the program `df' to get the device name, but is
218 # this really portable?
219 tmp_fname
=`df $1/ | sed -n 's%.*\(/dev/[^ ]*\).*%\1%p'`
221 if test -z "$tmp_fname"; then
222 echo "Could not find device for $1" 2>&1
226 tmp_fname
=`resolve_symlink $tmp_fname`
231 # Check the arguments.
232 for option
in "$@"; do
238 echo "grub-install (GNU GRUB ${VERSION})"
241 rootdir
=`echo "$option" | sed 's/--root-directory=//'` ;;
243 grub_shell
=`echo "$option" | sed 's/--grub-shell=//'` ;;
245 no_floppy
="--no-floppy" ;;
247 force_lba
="--force-lba" ;;
250 # This is an undocumented feature...
254 echo "Unrecognized option \`$option'" 1>&2
259 if test "x$install_device" != x
; then
260 echo "More than one install_devices?" 1>&2
264 install_device
="${option}" ;;
268 if test "x$install_device" = x
; then
269 echo "install_device not specified." 1>&2
274 # If the debugging feature is enabled, print commands.
275 if test $debug = yes; then
279 # Initialize these directories here, since ROOTDIR was initialized.
282 # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub
283 # instead of /boot/grub.
288 # Use /boot/grub by default.
289 bootdir
=${rootdir}/boot
293 grubdir
=${bootdir}/grub
294 device_map
=${grubdir}/device.map
296 # Check if GRUB is installed.
297 # This is necessary, because the user can specify "grub --read-only".
298 set $grub_shell dummy
299 if test -f "$1"; then
302 echo "$1: Not found." 1>&2
306 if test -f "$pkglibdir/stage1"; then
309 echo "${pkglibdir}/stage1: Not found." 1>&2
313 if test -f "$pkglibdir/stage2"; then
316 echo "${pkglibdir}/stage2: Not found." 1>&2
320 # Don't check for *stage1_5, because it is not fatal even if any
321 # Stage 1.5 does not exist.
323 # Create the GRUB directory if it is not present.
324 test -d "$bootdir" || mkdir
"$bootdir" ||
exit 1
325 test -d "$grubdir" || mkdir
"$grubdir" ||
exit 1
327 # If --recheck is specified, remove the device map, if present.
328 if test $recheck = yes; then
332 # Create the device map file if it is not present.
333 if test -f "$device_map"; then
336 # Create a safe temporary file.
337 test -n "$mklog" && log_file
=`$mklog`
339 $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
342 if grep "Error [0-9]*: " $log_file >/dev
/null
; then
350 # Make sure that there is no duplicated entry.
351 tmp
=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \
352 | sort | uniq -d | sed -n 1p`
353 if test -n "$tmp"; then
354 echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2
358 # Check for INSTALL_DEVICE.
359 case "$install_device" in
361 install_device
=`resolve_symlink "$install_device"`
362 install_drive
=`convert "$install_device"`
363 # I don't know why, but some shells wouldn't die if exit is
364 # called in a function.
365 if test "x$install_drive" = x
; then
369 install_drive
="$install_device" ;;
371 # The GRUB format with no parenthesis.
372 install_drive
="($install_device)" ;;
374 echo "Format of install_device not recognized." 1>&2
379 # Get the root drive.
380 root_device
=`find_device ${rootdir}`
381 bootdir_device
=`find_device ${bootdir}`
383 # Check if the boot directory is in the same device as the root directory.
384 if test "x$root_device" != "x$bootdir_device"; then
385 # Perhaps the user has a separate boot partition.
386 root_device
=$bootdir_device
390 # Convert the root device to a GRUB drive.
391 root_drive
=`convert "$root_device"`
392 if test "x$root_drive" = x
; then
396 # Check if the root directory exists in the same device as the grub
398 grubdir_device
=`find_device ${grubdir}`
400 if test "x$grubdir_device" != "x$root_device"; then
401 # For now, cannot deal with this situation.
403 You must set the root directory by the option --root-directory, because
404 $grubdir does not exist in the root device $root_device.
409 # Copy the GRUB images to the GRUB directory.
410 for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5
; do
411 rm -f $file ||
exit 1
414 ${pkglibdir}/stage1 ${pkglibdir}/stage2 ${pkglibdir}/*stage1_5
; do
415 cp -f $file ${grubdir} ||
exit 1
418 # Make a default file.
419 ${grub_set_default} --root-directory=${rootdir} default
421 # Make sure that GRUB reads the same images as the host OS.
422 test -n "$mkimg" && img_file
=`$mkimg`
423 test -n "$mklog" && log_file
=`$mklog`
425 for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5
; do
427 tmp
=`echo $file | sed "s|^${grubdir}|${grub_prefix}|"`
428 while test $count -gt 0; do
429 $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
430 dump ${root_drive}${tmp} ${img_file}
433 if grep "Error [0-9]*: " $log_file >/dev
/null
; then
435 elif cmp $file $img_file >/dev
/null
; then
439 count
=`expr $count - 1`
441 if test $count -eq 0; then
442 echo "The file $file not read correctly." 1>&2
450 # Create a safe temporary file.
451 test -n "$mklog" && log_file
=`$mklog`
453 # Now perform the installation.
454 $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
456 setup $force_lba --stage2=$grubdir/stage2 --prefix=$grub_prefix $install_drive
460 if grep "Error [0-9]*: " $log_file >/dev
/null ||
test $debug = yes; then
467 # Prompt the user to check if the device map is correct.
468 echo "Installation finished. No error reported."
469 echo "This is the contents of the device map $device_map."
470 echo "Check if this is correct or not. If any of the lines is incorrect,"
471 echo "fix it and re-run the script \`grub-install'."