1*1b8adde7SWilliam Kucharski#! /bin/sh 2*1b8adde7SWilliam Kucharski 3*1b8adde7SWilliam Kucharski# Install GRUB on your drive. 4*1b8adde7SWilliam Kucharski# Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc. 5*1b8adde7SWilliam Kucharski# 6*1b8adde7SWilliam Kucharski# This file is free software; you can redistribute it and/or modify it 7*1b8adde7SWilliam Kucharski# under the terms of the GNU General Public License as published by 8*1b8adde7SWilliam Kucharski# the Free Software Foundation; either version 2 of the License, or 9*1b8adde7SWilliam Kucharski# (at your option) any later version. 10*1b8adde7SWilliam Kucharski# 11*1b8adde7SWilliam Kucharski# This program is distributed in the hope that it will be useful, but 12*1b8adde7SWilliam Kucharski# WITHOUT ANY WARRANTY; without even the implied warranty of 13*1b8adde7SWilliam Kucharski# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14*1b8adde7SWilliam Kucharski# General Public License for more details. 15*1b8adde7SWilliam Kucharski# 16*1b8adde7SWilliam Kucharski# You should have received a copy of the GNU General Public License 17*1b8adde7SWilliam Kucharski# along with this program; if not, write to the Free Software 18*1b8adde7SWilliam Kucharski# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19*1b8adde7SWilliam Kucharski 20*1b8adde7SWilliam Kucharski# Initialize some variables. 21*1b8adde7SWilliam Kucharskiprefix=@prefix@ 22*1b8adde7SWilliam Kucharskiexec_prefix=@exec_prefix@ 23*1b8adde7SWilliam Kucharskisbindir=@sbindir@ 24*1b8adde7SWilliam Kucharskilibdir=@libdir@ 25*1b8adde7SWilliam KucharskiPACKAGE=@PACKAGE@ 26*1b8adde7SWilliam KucharskiVERSION=@VERSION@ 27*1b8adde7SWilliam Kucharskihost_cpu=@host_cpu@ 28*1b8adde7SWilliam Kucharskihost_os=@host_os@ 29*1b8adde7SWilliam Kucharskihost_vendor=@host_vendor@ 30*1b8adde7SWilliam Kucharskipkglibdir=${libdir}/${PACKAGE}/${host_cpu}-${host_vendor} 31*1b8adde7SWilliam Kucharski 32*1b8adde7SWilliam Kucharskigrub_shell=${sbindir}/grub 33*1b8adde7SWilliam Kucharskigrub_set_default=${sbindir}/grub-set-default 34*1b8adde7SWilliam Kucharskilog_file=/tmp/grub-install.log.$$ 35*1b8adde7SWilliam Kucharskiimg_file=/tmp/grub-install.img.$$ 36*1b8adde7SWilliam Kucharskirootdir= 37*1b8adde7SWilliam Kucharskigrub_prefix=/boot/grub 38*1b8adde7SWilliam Kucharski 39*1b8adde7SWilliam Kucharskiinstall_device= 40*1b8adde7SWilliam Kucharskino_floppy= 41*1b8adde7SWilliam Kucharskiforce_lba= 42*1b8adde7SWilliam Kucharskirecheck=no 43*1b8adde7SWilliam Kucharskidebug=no 44*1b8adde7SWilliam Kucharski 45*1b8adde7SWilliam Kucharski# look for secure tempfile creation wrappers on this platform 46*1b8adde7SWilliam Kucharskiif test -x /bin/tempfile; then 47*1b8adde7SWilliam Kucharski mklog="/bin/tempfile --prefix=grub" 48*1b8adde7SWilliam Kucharski mkimg="/bin/tempfile --prefix=grub" 49*1b8adde7SWilliam Kucharskielif test -x /bin/mktemp; then 50*1b8adde7SWilliam Kucharski mklog="/bin/mktemp /tmp/grub-install.log.XXXXXX" 51*1b8adde7SWilliam Kucharski mkimg="/bin/mktemp /tmp/grub-install.img.XXXXXX" 52*1b8adde7SWilliam Kucharskielse 53*1b8adde7SWilliam Kucharski mklog="" 54*1b8adde7SWilliam Kucharski mkimg="" 55*1b8adde7SWilliam Kucharskifi 56*1b8adde7SWilliam Kucharski 57*1b8adde7SWilliam Kucharski# Usage: usage 58*1b8adde7SWilliam Kucharski# Print the usage. 59*1b8adde7SWilliam Kucharskiusage () { 60*1b8adde7SWilliam Kucharski cat <<EOF 61*1b8adde7SWilliam KucharskiUsage: grub-install [OPTION] install_device 62*1b8adde7SWilliam KucharskiInstall GRUB on your drive. 63*1b8adde7SWilliam Kucharski 64*1b8adde7SWilliam Kucharski -h, --help print this message and exit 65*1b8adde7SWilliam Kucharski -v, --version print the version information and exit 66*1b8adde7SWilliam Kucharski --root-directory=DIR install GRUB images under the directory DIR 67*1b8adde7SWilliam Kucharski instead of the root directory 68*1b8adde7SWilliam Kucharski --grub-shell=FILE use FILE as the grub shell 69*1b8adde7SWilliam Kucharski --no-floppy do not probe any floppy drive 70*1b8adde7SWilliam Kucharski --force-lba force GRUB to use LBA mode even for a buggy 71*1b8adde7SWilliam Kucharski BIOS 72*1b8adde7SWilliam Kucharski --recheck probe a device map even if it already exists 73*1b8adde7SWilliam Kucharski 74*1b8adde7SWilliam KucharskiINSTALL_DEVICE can be a GRUB device name or a system device filename. 75*1b8adde7SWilliam Kucharski 76*1b8adde7SWilliam Kucharskigrub-install copies GRUB images into the DIR/boot directory specfied by 77*1b8adde7SWilliam Kucharski--root-directory, and uses the grub shell to install grub into the boot 78*1b8adde7SWilliam Kucharskisector. 79*1b8adde7SWilliam Kucharski 80*1b8adde7SWilliam KucharskiReport bugs to <bug-grub@gnu.org>. 81*1b8adde7SWilliam KucharskiEOF 82*1b8adde7SWilliam Kucharski} 83*1b8adde7SWilliam Kucharski 84*1b8adde7SWilliam Kucharski# Usage: convert os_device 85*1b8adde7SWilliam Kucharski# Convert an OS device to the corresponding GRUB drive. 86*1b8adde7SWilliam Kucharski# This part is OS-specific. 87*1b8adde7SWilliam Kucharskiconvert () { 88*1b8adde7SWilliam Kucharski # First, check if the device file exists. 89*1b8adde7SWilliam Kucharski if test -e "$1"; then 90*1b8adde7SWilliam Kucharski : 91*1b8adde7SWilliam Kucharski else 92*1b8adde7SWilliam Kucharski echo "$1: Not found or not a block device." 1>&2 93*1b8adde7SWilliam Kucharski exit 1 94*1b8adde7SWilliam Kucharski fi 95*1b8adde7SWilliam Kucharski 96*1b8adde7SWilliam Kucharski # Break the device name into the disk part and the partition part. 97*1b8adde7SWilliam Kucharski case "$host_os" in 98*1b8adde7SWilliam Kucharski linux*) 99*1b8adde7SWilliam Kucharski tmp_disk=`echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \ 100*1b8adde7SWilliam Kucharski -e 's%\(d[0-9]*\)p[0-9]*$%\1%' \ 101*1b8adde7SWilliam Kucharski -e 's%\(fd[0-9]*\)$%\1%' \ 102*1b8adde7SWilliam Kucharski -e 's%/part[0-9]*$%/disc%' \ 103*1b8adde7SWilliam Kucharski -e 's%\(c[0-7]d[0-9]*\).*$%\1%'` 104*1b8adde7SWilliam Kucharski tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \ 105*1b8adde7SWilliam Kucharski -e 's%.*d[0-9]*p%%' \ 106*1b8adde7SWilliam Kucharski -e 's%.*/fd[0-9]*$%%' \ 107*1b8adde7SWilliam Kucharski -e 's%.*/floppy/[0-9]*$%%' \ 108*1b8adde7SWilliam Kucharski -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \ 109*1b8adde7SWilliam Kucharski -e 's%.*c[0-7]d[0-9]*p%%'` 110*1b8adde7SWilliam Kucharski ;; 111*1b8adde7SWilliam Kucharski gnu*) 112*1b8adde7SWilliam Kucharski tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'` 113*1b8adde7SWilliam Kucharski tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;; 114*1b8adde7SWilliam Kucharski freebsd* | kfreebsd*-gnu) 115*1b8adde7SWilliam Kucharski tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%r\1%' \ 116*1b8adde7SWilliam Kucharski | sed 's%r\{0,1\}\(da[0-9]*\).*$%r\1%'` 117*1b8adde7SWilliam Kucharski tmp_part=`echo "$1" \ 118*1b8adde7SWilliam Kucharski | sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \ 119*1b8adde7SWilliam Kucharski | sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"` 120*1b8adde7SWilliam Kucharski ;; 121*1b8adde7SWilliam Kucharski netbsd* | knetbsd*-gnu) 122*1b8adde7SWilliam Kucharski tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \ 123*1b8adde7SWilliam Kucharski | sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'` 124*1b8adde7SWilliam Kucharski tmp_part=`echo "$1" \ 125*1b8adde7SWilliam Kucharski | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"` 126*1b8adde7SWilliam Kucharski ;; 127*1b8adde7SWilliam Kucharski *) 128*1b8adde7SWilliam Kucharski echo "grub-install does not support your OS yet." 1>&2 129*1b8adde7SWilliam Kucharski exit 1 ;; 130*1b8adde7SWilliam Kucharski esac 131*1b8adde7SWilliam Kucharski 132*1b8adde7SWilliam Kucharski # Get the drive name. 133*1b8adde7SWilliam Kucharski tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \ 134*1b8adde7SWilliam Kucharski | sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%'` 135*1b8adde7SWilliam Kucharski 136*1b8adde7SWilliam Kucharski # If not found, print an error message and exit. 137*1b8adde7SWilliam Kucharski if test "x$tmp_drive" = x; then 138*1b8adde7SWilliam Kucharski echo "$1 does not have any corresponding BIOS drive." 1>&2 139*1b8adde7SWilliam Kucharski exit 1 140*1b8adde7SWilliam Kucharski fi 141*1b8adde7SWilliam Kucharski 142*1b8adde7SWilliam Kucharski if test "x$tmp_part" != x; then 143*1b8adde7SWilliam Kucharski # If a partition is specified, we need to translate it into the 144*1b8adde7SWilliam Kucharski # GRUB's syntax. 145*1b8adde7SWilliam Kucharski case "$host_os" in 146*1b8adde7SWilliam Kucharski linux*) 147*1b8adde7SWilliam Kucharski echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;; 148*1b8adde7SWilliam Kucharski gnu*) 149*1b8adde7SWilliam Kucharski if echo $tmp_part | grep "^s" >/dev/null; then 150*1b8adde7SWilliam Kucharski tmp_pc_slice=`echo $tmp_part \ 151*1b8adde7SWilliam Kucharski | sed "s%s\([0-9]*\)[a-g]*$%\1%"` 152*1b8adde7SWilliam Kucharski tmp_drive=`echo "$tmp_drive" \ 153*1b8adde7SWilliam Kucharski | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"` 154*1b8adde7SWilliam Kucharski fi 155*1b8adde7SWilliam Kucharski if echo $tmp_part | grep "[a-g]$" >/dev/null; then 156*1b8adde7SWilliam Kucharski tmp_bsd_partition=`echo "$tmp_part" \ 157*1b8adde7SWilliam Kucharski | sed "s%[^a-g]*\([a-g]\)$%\1%"` 158*1b8adde7SWilliam Kucharski tmp_drive=`echo "$tmp_drive" \ 159*1b8adde7SWilliam Kucharski | sed "s%)%,$tmp_bsd_partition)%"` 160*1b8adde7SWilliam Kucharski fi 161*1b8adde7SWilliam Kucharski echo "$tmp_drive" ;; 162*1b8adde7SWilliam Kucharski freebsd* | kfreebsd*-gnu) 163*1b8adde7SWilliam Kucharski if echo $tmp_part | grep "^s" >/dev/null; then 164*1b8adde7SWilliam Kucharski tmp_pc_slice=`echo $tmp_part \ 165*1b8adde7SWilliam Kucharski | sed "s%s\([0-9]*\)[a-h]*$%\1%"` 166*1b8adde7SWilliam Kucharski tmp_drive=`echo "$tmp_drive" \ 167*1b8adde7SWilliam Kucharski | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"` 168*1b8adde7SWilliam Kucharski fi 169*1b8adde7SWilliam Kucharski if echo $tmp_part | grep "[a-h]$" >/dev/null; then 170*1b8adde7SWilliam Kucharski tmp_bsd_partition=`echo "$tmp_part" \ 171*1b8adde7SWilliam Kucharski | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"` 172*1b8adde7SWilliam Kucharski tmp_drive=`echo "$tmp_drive" \ 173*1b8adde7SWilliam Kucharski | sed "s%)%,$tmp_bsd_partition)%"` 174*1b8adde7SWilliam Kucharski fi 175*1b8adde7SWilliam Kucharski echo "$tmp_drive" ;; 176*1b8adde7SWilliam Kucharski netbsd* | knetbsd*-gnu) 177*1b8adde7SWilliam Kucharski if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then 178*1b8adde7SWilliam Kucharski tmp_bsd_partition=`echo "$tmp_part" \ 179*1b8adde7SWilliam Kucharski | sed "s%\([a-p]\)$%\1%"` 180*1b8adde7SWilliam Kucharski tmp_drive=`echo "$tmp_drive" \ 181*1b8adde7SWilliam Kucharski | sed "s%)%,$tmp_bsd_partition)%"` 182*1b8adde7SWilliam Kucharski fi 183*1b8adde7SWilliam Kucharski echo "$tmp_drive" ;; 184*1b8adde7SWilliam Kucharski esac 185*1b8adde7SWilliam Kucharski else 186*1b8adde7SWilliam Kucharski # If no partition is specified, just print the drive name. 187*1b8adde7SWilliam Kucharski echo "$tmp_drive" 188*1b8adde7SWilliam Kucharski fi 189*1b8adde7SWilliam Kucharski} 190*1b8adde7SWilliam Kucharski 191*1b8adde7SWilliam Kucharski# Usage: resolve_symlink file 192*1b8adde7SWilliam Kucharski# Find the real file/device that file points at 193*1b8adde7SWilliam Kucharskiresolve_symlink () { 194*1b8adde7SWilliam Kucharski tmp_fname=$1 195*1b8adde7SWilliam Kucharski # Resolve symlinks 196*1b8adde7SWilliam Kucharski while test -L $tmp_fname; do 197*1b8adde7SWilliam Kucharski tmp_new_fname=`ls -al $tmp_fname | sed -n 's%.*-> \(.*\)%\1%p'` 198*1b8adde7SWilliam Kucharski if test -z "$tmp_new_fname"; then 199*1b8adde7SWilliam Kucharski echo "Unrecognized ls output" 2>&1 200*1b8adde7SWilliam Kucharski exit 1 201*1b8adde7SWilliam Kucharski fi 202*1b8adde7SWilliam Kucharski 203*1b8adde7SWilliam Kucharski # Convert relative symlinks 204*1b8adde7SWilliam Kucharski case $tmp_new_fname in 205*1b8adde7SWilliam Kucharski /*) tmp_fname="$tmp_new_fname" 206*1b8adde7SWilliam Kucharski ;; 207*1b8adde7SWilliam Kucharski *) tmp_fname="`echo $tmp_fname | sed 's%/[^/]*$%%'`/$tmp_new_fname" 208*1b8adde7SWilliam Kucharski ;; 209*1b8adde7SWilliam Kucharski esac 210*1b8adde7SWilliam Kucharski done 211*1b8adde7SWilliam Kucharski echo "$tmp_fname" 212*1b8adde7SWilliam Kucharski} 213*1b8adde7SWilliam Kucharski 214*1b8adde7SWilliam Kucharski# Usage: find_device file 215*1b8adde7SWilliam Kucharski# Find block device on which the file resides. 216*1b8adde7SWilliam Kucharskifind_device () { 217*1b8adde7SWilliam Kucharski # For now, this uses the program `df' to get the device name, but is 218*1b8adde7SWilliam Kucharski # this really portable? 219*1b8adde7SWilliam Kucharski tmp_fname=`df $1/ | sed -n 's%.*\(/dev/[^ ]*\).*%\1%p'` 220*1b8adde7SWilliam Kucharski 221*1b8adde7SWilliam Kucharski if test -z "$tmp_fname"; then 222*1b8adde7SWilliam Kucharski echo "Could not find device for $1" 2>&1 223*1b8adde7SWilliam Kucharski exit 1 224*1b8adde7SWilliam Kucharski fi 225*1b8adde7SWilliam Kucharski 226*1b8adde7SWilliam Kucharski tmp_fname=`resolve_symlink $tmp_fname` 227*1b8adde7SWilliam Kucharski 228*1b8adde7SWilliam Kucharski echo "$tmp_fname" 229*1b8adde7SWilliam Kucharski} 230*1b8adde7SWilliam Kucharski 231*1b8adde7SWilliam Kucharski# Check the arguments. 232*1b8adde7SWilliam Kucharskifor option in "$@"; do 233*1b8adde7SWilliam Kucharski case "$option" in 234*1b8adde7SWilliam Kucharski -h | --help) 235*1b8adde7SWilliam Kucharski usage 236*1b8adde7SWilliam Kucharski exit 0 ;; 237*1b8adde7SWilliam Kucharski -v | --version) 238*1b8adde7SWilliam Kucharski echo "grub-install (GNU GRUB ${VERSION})" 239*1b8adde7SWilliam Kucharski exit 0 ;; 240*1b8adde7SWilliam Kucharski --root-directory=*) 241*1b8adde7SWilliam Kucharski rootdir=`echo "$option" | sed 's/--root-directory=//'` ;; 242*1b8adde7SWilliam Kucharski --grub-shell=*) 243*1b8adde7SWilliam Kucharski grub_shell=`echo "$option" | sed 's/--grub-shell=//'` ;; 244*1b8adde7SWilliam Kucharski --no-floppy) 245*1b8adde7SWilliam Kucharski no_floppy="--no-floppy" ;; 246*1b8adde7SWilliam Kucharski --force-lba) 247*1b8adde7SWilliam Kucharski force_lba="--force-lba" ;; 248*1b8adde7SWilliam Kucharski --recheck) 249*1b8adde7SWilliam Kucharski recheck=yes ;; 250*1b8adde7SWilliam Kucharski # This is an undocumented feature... 251*1b8adde7SWilliam Kucharski --debug) 252*1b8adde7SWilliam Kucharski debug=yes ;; 253*1b8adde7SWilliam Kucharski -*) 254*1b8adde7SWilliam Kucharski echo "Unrecognized option \`$option'" 1>&2 255*1b8adde7SWilliam Kucharski usage 256*1b8adde7SWilliam Kucharski exit 1 257*1b8adde7SWilliam Kucharski ;; 258*1b8adde7SWilliam Kucharski *) 259*1b8adde7SWilliam Kucharski if test "x$install_device" != x; then 260*1b8adde7SWilliam Kucharski echo "More than one install_devices?" 1>&2 261*1b8adde7SWilliam Kucharski usage 262*1b8adde7SWilliam Kucharski exit 1 263*1b8adde7SWilliam Kucharski fi 264*1b8adde7SWilliam Kucharski install_device="${option}" ;; 265*1b8adde7SWilliam Kucharski esac 266*1b8adde7SWilliam Kucharskidone 267*1b8adde7SWilliam Kucharski 268*1b8adde7SWilliam Kucharskiif test "x$install_device" = x; then 269*1b8adde7SWilliam Kucharski echo "install_device not specified." 1>&2 270*1b8adde7SWilliam Kucharski usage 271*1b8adde7SWilliam Kucharski exit 1 272*1b8adde7SWilliam Kucharskifi 273*1b8adde7SWilliam Kucharski 274*1b8adde7SWilliam Kucharski# If the debugging feature is enabled, print commands. 275*1b8adde7SWilliam Kucharskiif test $debug = yes; then 276*1b8adde7SWilliam Kucharski set -x 277*1b8adde7SWilliam Kucharskifi 278*1b8adde7SWilliam Kucharski 279*1b8adde7SWilliam Kucharski# Initialize these directories here, since ROOTDIR was initialized. 280*1b8adde7SWilliam Kucharskicase "$host_os" in 281*1b8adde7SWilliam Kucharskinetbsd* | openbsd*) 282*1b8adde7SWilliam Kucharski # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub 283*1b8adde7SWilliam Kucharski # instead of /boot/grub. 284*1b8adde7SWilliam Kucharski grub_prefix=/grub 285*1b8adde7SWilliam Kucharski bootdir=${rootdir} 286*1b8adde7SWilliam Kucharski ;; 287*1b8adde7SWilliam Kucharski*) 288*1b8adde7SWilliam Kucharski # Use /boot/grub by default. 289*1b8adde7SWilliam Kucharski bootdir=${rootdir}/boot 290*1b8adde7SWilliam Kucharski ;; 291*1b8adde7SWilliam Kucharskiesac 292*1b8adde7SWilliam Kucharski 293*1b8adde7SWilliam Kucharskigrubdir=${bootdir}/grub 294*1b8adde7SWilliam Kucharskidevice_map=${grubdir}/device.map 295*1b8adde7SWilliam Kucharski 296*1b8adde7SWilliam Kucharski# Check if GRUB is installed. 297*1b8adde7SWilliam Kucharski# This is necessary, because the user can specify "grub --read-only". 298*1b8adde7SWilliam Kucharskiset $grub_shell dummy 299*1b8adde7SWilliam Kucharskiif test -f "$1"; then 300*1b8adde7SWilliam Kucharski : 301*1b8adde7SWilliam Kucharskielse 302*1b8adde7SWilliam Kucharski echo "$1: Not found." 1>&2 303*1b8adde7SWilliam Kucharski exit 1 304*1b8adde7SWilliam Kucharskifi 305*1b8adde7SWilliam Kucharski 306*1b8adde7SWilliam Kucharskiif test -f "$pkglibdir/stage1"; then 307*1b8adde7SWilliam Kucharski : 308*1b8adde7SWilliam Kucharskielse 309*1b8adde7SWilliam Kucharski echo "${pkglibdir}/stage1: Not found." 1>&2 310*1b8adde7SWilliam Kucharski exit 1 311*1b8adde7SWilliam Kucharskifi 312*1b8adde7SWilliam Kucharski 313*1b8adde7SWilliam Kucharskiif test -f "$pkglibdir/stage2"; then 314*1b8adde7SWilliam Kucharski : 315*1b8adde7SWilliam Kucharskielse 316*1b8adde7SWilliam Kucharski echo "${pkglibdir}/stage2: Not found." 1>&2 317*1b8adde7SWilliam Kucharski exit 1 318*1b8adde7SWilliam Kucharskifi 319*1b8adde7SWilliam Kucharski 320*1b8adde7SWilliam Kucharski# Don't check for *stage1_5, because it is not fatal even if any 321*1b8adde7SWilliam Kucharski# Stage 1.5 does not exist. 322*1b8adde7SWilliam Kucharski 323*1b8adde7SWilliam Kucharski# Create the GRUB directory if it is not present. 324*1b8adde7SWilliam Kucharskitest -d "$bootdir" || mkdir "$bootdir" || exit 1 325*1b8adde7SWilliam Kucharskitest -d "$grubdir" || mkdir "$grubdir" || exit 1 326*1b8adde7SWilliam Kucharski 327*1b8adde7SWilliam Kucharski# If --recheck is specified, remove the device map, if present. 328*1b8adde7SWilliam Kucharskiif test $recheck = yes; then 329*1b8adde7SWilliam Kucharski rm -f $device_map 330*1b8adde7SWilliam Kucharskifi 331*1b8adde7SWilliam Kucharski 332*1b8adde7SWilliam Kucharski# Create the device map file if it is not present. 333*1b8adde7SWilliam Kucharskiif test -f "$device_map"; then 334*1b8adde7SWilliam Kucharski : 335*1b8adde7SWilliam Kucharskielse 336*1b8adde7SWilliam Kucharski # Create a safe temporary file. 337*1b8adde7SWilliam Kucharski test -n "$mklog" && log_file=`$mklog` 338*1b8adde7SWilliam Kucharski 339*1b8adde7SWilliam Kucharski $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file 340*1b8adde7SWilliam Kucharskiquit 341*1b8adde7SWilliam KucharskiEOF 342*1b8adde7SWilliam Kucharski if grep "Error [0-9]*: " $log_file >/dev/null; then 343*1b8adde7SWilliam Kucharski cat $log_file 1>&2 344*1b8adde7SWilliam Kucharski exit 1 345*1b8adde7SWilliam Kucharski fi 346*1b8adde7SWilliam Kucharski 347*1b8adde7SWilliam Kucharski rm -f $log_file 348*1b8adde7SWilliam Kucharskifi 349*1b8adde7SWilliam Kucharski 350*1b8adde7SWilliam Kucharski# Make sure that there is no duplicated entry. 351*1b8adde7SWilliam Kucharskitmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \ 352*1b8adde7SWilliam Kucharski | sort | uniq -d | sed -n 1p` 353*1b8adde7SWilliam Kucharskiif test -n "$tmp"; then 354*1b8adde7SWilliam Kucharski echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2 355*1b8adde7SWilliam Kucharski exit 1 356*1b8adde7SWilliam Kucharskifi 357*1b8adde7SWilliam Kucharski 358*1b8adde7SWilliam Kucharski# Check for INSTALL_DEVICE. 359*1b8adde7SWilliam Kucharskicase "$install_device" in 360*1b8adde7SWilliam Kucharski/dev/*) 361*1b8adde7SWilliam Kucharski install_device=`resolve_symlink "$install_device"` 362*1b8adde7SWilliam Kucharski install_drive=`convert "$install_device"` 363*1b8adde7SWilliam Kucharski # I don't know why, but some shells wouldn't die if exit is 364*1b8adde7SWilliam Kucharski # called in a function. 365*1b8adde7SWilliam Kucharski if test "x$install_drive" = x; then 366*1b8adde7SWilliam Kucharski exit 1 367*1b8adde7SWilliam Kucharski fi ;; 368*1b8adde7SWilliam Kucharski\([hf]d[0-9]*\)) 369*1b8adde7SWilliam Kucharski install_drive="$install_device" ;; 370*1b8adde7SWilliam Kucharski[hf]d[0-9]*) 371*1b8adde7SWilliam Kucharski # The GRUB format with no parenthesis. 372*1b8adde7SWilliam Kucharski install_drive="($install_device)" ;; 373*1b8adde7SWilliam Kucharski*) 374*1b8adde7SWilliam Kucharski echo "Format of install_device not recognized." 1>&2 375*1b8adde7SWilliam Kucharski usage 376*1b8adde7SWilliam Kucharski exit 1 ;; 377*1b8adde7SWilliam Kucharskiesac 378*1b8adde7SWilliam Kucharski 379*1b8adde7SWilliam Kucharski# Get the root drive. 380*1b8adde7SWilliam Kucharskiroot_device=`find_device ${rootdir}` 381*1b8adde7SWilliam Kucharskibootdir_device=`find_device ${bootdir}` 382*1b8adde7SWilliam Kucharski 383*1b8adde7SWilliam Kucharski# Check if the boot directory is in the same device as the root directory. 384*1b8adde7SWilliam Kucharskiif test "x$root_device" != "x$bootdir_device"; then 385*1b8adde7SWilliam Kucharski # Perhaps the user has a separate boot partition. 386*1b8adde7SWilliam Kucharski root_device=$bootdir_device 387*1b8adde7SWilliam Kucharski grub_prefix="/grub" 388*1b8adde7SWilliam Kucharskifi 389*1b8adde7SWilliam Kucharski 390*1b8adde7SWilliam Kucharski# Convert the root device to a GRUB drive. 391*1b8adde7SWilliam Kucharskiroot_drive=`convert "$root_device"` 392*1b8adde7SWilliam Kucharskiif test "x$root_drive" = x; then 393*1b8adde7SWilliam Kucharski exit 1 394*1b8adde7SWilliam Kucharskifi 395*1b8adde7SWilliam Kucharski 396*1b8adde7SWilliam Kucharski# Check if the root directory exists in the same device as the grub 397*1b8adde7SWilliam Kucharski# directory. 398*1b8adde7SWilliam Kucharskigrubdir_device=`find_device ${grubdir}` 399*1b8adde7SWilliam Kucharski 400*1b8adde7SWilliam Kucharskiif test "x$grubdir_device" != "x$root_device"; then 401*1b8adde7SWilliam Kucharski # For now, cannot deal with this situation. 402*1b8adde7SWilliam Kucharski cat <<EOF 1>&2 403*1b8adde7SWilliam KucharskiYou must set the root directory by the option --root-directory, because 404*1b8adde7SWilliam Kucharski$grubdir does not exist in the root device $root_device. 405*1b8adde7SWilliam KucharskiEOF 406*1b8adde7SWilliam Kucharski exit 1 407*1b8adde7SWilliam Kucharskifi 408*1b8adde7SWilliam Kucharski 409*1b8adde7SWilliam Kucharski# Copy the GRUB images to the GRUB directory. 410*1b8adde7SWilliam Kucharskifor file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do 411*1b8adde7SWilliam Kucharski rm -f $file || exit 1 412*1b8adde7SWilliam Kucharskidone 413*1b8adde7SWilliam Kucharskifor file in \ 414*1b8adde7SWilliam Kucharski ${pkglibdir}/stage1 ${pkglibdir}/stage2 ${pkglibdir}/*stage1_5; do 415*1b8adde7SWilliam Kucharski cp -f $file ${grubdir} || exit 1 416*1b8adde7SWilliam Kucharskidone 417*1b8adde7SWilliam Kucharski 418*1b8adde7SWilliam Kucharski# Make a default file. 419*1b8adde7SWilliam Kucharski${grub_set_default} --root-directory=${rootdir} default 420*1b8adde7SWilliam Kucharski 421*1b8adde7SWilliam Kucharski# Make sure that GRUB reads the same images as the host OS. 422*1b8adde7SWilliam Kucharskitest -n "$mkimg" && img_file=`$mkimg` 423*1b8adde7SWilliam Kucharskitest -n "$mklog" && log_file=`$mklog` 424*1b8adde7SWilliam Kucharski 425*1b8adde7SWilliam Kucharskifor file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do 426*1b8adde7SWilliam Kucharski count=5 427*1b8adde7SWilliam Kucharski tmp=`echo $file | sed "s|^${grubdir}|${grub_prefix}|"` 428*1b8adde7SWilliam Kucharski while test $count -gt 0; do 429*1b8adde7SWilliam Kucharski $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file 430*1b8adde7SWilliam Kucharskidump ${root_drive}${tmp} ${img_file} 431*1b8adde7SWilliam Kucharskiquit 432*1b8adde7SWilliam KucharskiEOF 433*1b8adde7SWilliam Kucharski if grep "Error [0-9]*: " $log_file >/dev/null; then 434*1b8adde7SWilliam Kucharski : 435*1b8adde7SWilliam Kucharski elif cmp $file $img_file >/dev/null; then 436*1b8adde7SWilliam Kucharski break 437*1b8adde7SWilliam Kucharski fi 438*1b8adde7SWilliam Kucharski sleep 1 439*1b8adde7SWilliam Kucharski count=`expr $count - 1` 440*1b8adde7SWilliam Kucharski done 441*1b8adde7SWilliam Kucharski if test $count -eq 0; then 442*1b8adde7SWilliam Kucharski echo "The file $file not read correctly." 1>&2 443*1b8adde7SWilliam Kucharski exit 1 444*1b8adde7SWilliam Kucharski fi 445*1b8adde7SWilliam Kucharskidone 446*1b8adde7SWilliam Kucharski 447*1b8adde7SWilliam Kucharskirm -f $img_file 448*1b8adde7SWilliam Kucharskirm -f $log_file 449*1b8adde7SWilliam Kucharski 450*1b8adde7SWilliam Kucharski# Create a safe temporary file. 451*1b8adde7SWilliam Kucharskitest -n "$mklog" && log_file=`$mklog` 452*1b8adde7SWilliam Kucharski 453*1b8adde7SWilliam Kucharski# Now perform the installation. 454*1b8adde7SWilliam Kucharski$grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file 455*1b8adde7SWilliam Kucharskiroot $root_drive 456*1b8adde7SWilliam Kucharskisetup $force_lba --stage2=$grubdir/stage2 --prefix=$grub_prefix $install_drive 457*1b8adde7SWilliam Kucharskiquit 458*1b8adde7SWilliam KucharskiEOF 459*1b8adde7SWilliam Kucharski 460*1b8adde7SWilliam Kucharskiif grep "Error [0-9]*: " $log_file >/dev/null || test $debug = yes; then 461*1b8adde7SWilliam Kucharski cat $log_file 1>&2 462*1b8adde7SWilliam Kucharski exit 1 463*1b8adde7SWilliam Kucharskifi 464*1b8adde7SWilliam Kucharski 465*1b8adde7SWilliam Kucharskirm -f $log_file 466*1b8adde7SWilliam Kucharski 467*1b8adde7SWilliam Kucharski# Prompt the user to check if the device map is correct. 468*1b8adde7SWilliam Kucharskiecho "Installation finished. No error reported." 469*1b8adde7SWilliam Kucharskiecho "This is the contents of the device map $device_map." 470*1b8adde7SWilliam Kucharskiecho "Check if this is correct or not. If any of the lines is incorrect," 471*1b8adde7SWilliam Kucharskiecho "fix it and re-run the script \`grub-install'." 472*1b8adde7SWilliam Kucharskiecho 473*1b8adde7SWilliam Kucharski 474*1b8adde7SWilliam Kucharskicat $device_map 475*1b8adde7SWilliam Kucharski 476*1b8adde7SWilliam Kucharski# Bye. 477*1b8adde7SWilliam Kucharskiexit 0 478