1 #! /bin/sh 2 3 # Install GRUB on your drive. 4 # Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. 5 # 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. 10 # 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. 15 # 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. 19 20 # Initialize some variables. 21 prefix=/usr/local 22 exec_prefix=${prefix} 23 sbindir=${exec_prefix}/sbin 24 libdir=${exec_prefix}/lib 25 PACKAGE=grub 26 VERSION=0.97 27 host_cpu=x86_64 28 host_os=linux-gnu 29 host_vendor=unknown 30 pkglibdir=${libdir}/${PACKAGE}/${host_cpu}-${host_vendor} 31 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.$$ 36 rootdir= 37 grub_prefix=/boot/grub 38 39 install_device= 40 no_floppy= 41 force_lba= 42 recheck=no 43 debug=no 44 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" 52 else 53 mklog="" 54 mkimg="" 55 fi 56 57 # Usage: usage 58 # Print the usage. 59 usage () { 60 cat <<EOF 61 Usage: grub-install [OPTION] install_device 62 Install GRUB on your drive. 63 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 71 BIOS 72 --recheck probe a device map even if it already exists 73 74 INSTALL_DEVICE can be a GRUB device name or a system device filename. 75 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 78 sector. 79 80 Report bugs to <bug-grub@gnu.org>. 81 EOF 82 } 83 84 # Usage: convert os_device 85 # Convert an OS device to the corresponding GRUB drive. 86 # This part is OS-specific. 87 convert () { 88 # First, check if the device file exists. 89 if test -e "$1"; then 90 : 91 else 92 echo "$1: Not found or not a block device." 1>&2 93 exit 1 94 fi 95 96 # Break the device name into the disk part and the partition part. 97 case "$host_os" in 98 linux*) 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%%'` 110 ;; 111 gnu*) 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%"` 120 ;; 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%"` 126 ;; 127 *) 128 echo "grub-install does not support your OS yet." 1>&2 129 exit 1 ;; 130 esac 131 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%'` 135 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 139 exit 1 140 fi 141 142 if test "x$tmp_part" != x; then 143 # If a partition is specified, we need to translate it into the 144 # GRUB's syntax. 145 case "$host_os" in 146 linux*) 147 echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;; 148 gnu*) 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\`)%"` 154 fi 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)%"` 160 fi 161 echo "$tmp_drive" ;; 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\`)%"` 168 fi 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)%"` 174 fi 175 echo "$tmp_drive" ;; 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)%"` 182 fi 183 echo "$tmp_drive" ;; 184 esac 185 else 186 # If no partition is specified, just print the drive name. 187 echo "$tmp_drive" 188 fi 189 } 190 191 # Usage: resolve_symlink file 192 # Find the real file/device that file points at 193 resolve_symlink () { 194 tmp_fname=$1 195 # Resolve symlinks 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 200 exit 1 201 fi 202 203 # Convert relative symlinks 204 case $tmp_new_fname in 205 /*) tmp_fname="$tmp_new_fname" 206 ;; 207 *) tmp_fname="`echo $tmp_fname | sed 's%/[^/]*$%%'`/$tmp_new_fname" 208 ;; 209 esac 210 done 211 echo "$tmp_fname" 212 } 213 214 # Usage: find_device file 215 # Find block device on which the file resides. 216 find_device () { 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'` 220 221 if test -z "$tmp_fname"; then 222 echo "Could not find device for $1" 2>&1 223 exit 1 224 fi 225 226 tmp_fname=`resolve_symlink $tmp_fname` 227 228 echo "$tmp_fname" 229 } 230 231 # Check the arguments. 232 for option in "$@"; do 233 case "$option" in 234 -h | --help) 235 usage 236 exit 0 ;; 237 -v | --version) 238 echo "grub-install (GNU GRUB ${VERSION})" 239 exit 0 ;; 240 --root-directory=*) 241 rootdir=`echo "$option" | sed 's/--root-directory=//'` ;; 242 --grub-shell=*) 243 grub_shell=`echo "$option" | sed 's/--grub-shell=//'` ;; 244 --no-floppy) 245 no_floppy="--no-floppy" ;; 246 --force-lba) 247 force_lba="--force-lba" ;; 248 --recheck) 249 recheck=yes ;; 250 # This is an undocumented feature... 251 --debug) 252 debug=yes ;; 253 -*) 254 echo "Unrecognized option \`$option'" 1>&2 255 usage 256 exit 1 257 ;; 258 *) 259 if test "x$install_device" != x; then 260 echo "More than one install_devices?" 1>&2 261 usage 262 exit 1 263 fi 264 install_device="${option}" ;; 265 esac 266 done 267 268 if test "x$install_device" = x; then 269 echo "install_device not specified." 1>&2 270 usage 271 exit 1 272 fi 273 274 # If the debugging feature is enabled, print commands. 275 if test $debug = yes; then 276 set -x 277 fi 278 279 # Initialize these directories here, since ROOTDIR was initialized. 280 case "$host_os" in 281 netbsd* | openbsd*) 282 # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub 283 # instead of /boot/grub. 284 grub_prefix=/grub 285 bootdir=${rootdir} 286 ;; 287 *) 288 # Use /boot/grub by default. 289 bootdir=${rootdir}/boot 290 ;; 291 esac 292 293 grubdir=${bootdir}/grub 294 device_map=${grubdir}/device.map 295 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 300 : 301 else 302 echo "$1: Not found." 1>&2 303 exit 1 304 fi 305 306 if test -f "$pkglibdir/stage1"; then 307 : 308 else 309 echo "${pkglibdir}/stage1: Not found." 1>&2 310 exit 1 311 fi 312 313 if test -f "$pkglibdir/stage2"; then 314 : 315 else 316 echo "${pkglibdir}/stage2: Not found." 1>&2 317 exit 1 318 fi 319 320 # Don't check for *stage1_5, because it is not fatal even if any 321 # Stage 1.5 does not exist. 322 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 326 327 # If --recheck is specified, remove the device map, if present. 328 if test $recheck = yes; then 329 rm -f $device_map 330 fi 331 332 # Create the device map file if it is not present. 333 if test -f "$device_map"; then 334 : 335 else 336 # Create a safe temporary file. 337 test -n "$mklog" && log_file=`$mklog` 338 339 $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file 340 quit 341 EOF 342 if grep "Error [0-9]*: " $log_file >/dev/null; then 343 cat $log_file 1>&2 344 exit 1 345 fi 346 347 rm -f $log_file 348 fi 349 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 355 exit 1 356 fi 357 358 # Check for INSTALL_DEVICE. 359 case "$install_device" in 360 /dev/*) 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 366 exit 1 367 fi ;; 368 \([hf]d[0-9]*\)) 369 install_drive="$install_device" ;; 370 [hf]d[0-9]*) 371 # The GRUB format with no parenthesis. 372 install_drive="($install_device)" ;; 373 *) 374 echo "Format of install_device not recognized." 1>&2 375 usage 376 exit 1 ;; 377 esac 378 379 # Get the root drive. 380 root_device=`find_device ${rootdir}` 381 bootdir_device=`find_device ${bootdir}` 382 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 387 grub_prefix="/grub" 388 fi 389 390 # Convert the root device to a GRUB drive. 391 root_drive=`convert "$root_device"` 392 if test "x$root_drive" = x; then 393 exit 1 394 fi 395 396 # Check if the root directory exists in the same device as the grub 397 # directory. 398 grubdir_device=`find_device ${grubdir}` 399 400 if test "x$grubdir_device" != "x$root_device"; then 401 # For now, cannot deal with this situation. 402 cat <<EOF 1>&2 403 You must set the root directory by the option --root-directory, because 404 $grubdir does not exist in the root device $root_device. 405 EOF 406 exit 1 407 fi 408 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 412 done 413 for file in \ 414 ${pkglibdir}/stage1 ${pkglibdir}/stage2 ${pkglibdir}/*stage1_5; do 415 cp -f $file ${grubdir} || exit 1 416 done 417 418 # Make a default file. 419 ${grub_set_default} --root-directory=${rootdir} default 420 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` 424 425 for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do 426 count=5 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} 431 quit 432 EOF 433 if grep "Error [0-9]*: " $log_file >/dev/null; then 434 : 435 elif cmp $file $img_file >/dev/null; then 436 break 437 fi 438 sleep 1 439 count=`expr $count - 1` 440 done 441 if test $count -eq 0; then 442 echo "The file $file not read correctly." 1>&2 443 exit 1 444 fi 445 done 446 447 rm -f $img_file 448 rm -f $log_file 449 450 # Create a safe temporary file. 451 test -n "$mklog" && log_file=`$mklog` 452 453 # Now perform the installation. 454 $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file 455 root $root_drive 456 setup $force_lba --stage2=$grubdir/stage2 --prefix=$grub_prefix $install_drive 457 quit 458 EOF 459 460 if grep "Error [0-9]*: " $log_file >/dev/null || test $debug = yes; then 461 cat $log_file 1>&2 462 exit 1 463 fi 464 465 rm -f $log_file 466 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'." 472 echo 473 474 cat $device_map 475 476 # Bye. 477 exit 0 478