source: MondoRescue/branches/2.2.7/mondo/src/restore-scripts/mondo/grub-install.patched @ 1999

Last change on this file since 1999 was 1999, checked in by Bruno Cornec, 13 years ago
  • Protect recursive remove of tempdir with test on the look and feel of that dir to avoid removing blindly useful dir :-(
  • Property svn:keywords set to Id
  • Property svn:unix-mode set to 755
File size: 14.3 KB
Line 
1#! /bin/sh
2
3# 06/25/2004
4# - better Mandrake 9.2 compatibility
5#
6# 06/14/2004
7# - hacked about a bit by Hugo, to incorporate Red Hat's patches
8#   and to add SuSE compatibility
9#
10#-----------------------------------------------------------------------
11# Install GRUB on your drive.
12#   Copyright (C) 1999,2000,2001,2002 Free Software Foundation, Inc.
13#
14# This file is free software; you can redistribute it and/or modify it
15# under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# This program is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22# General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28# Initialize some variables.
29prefix=/usr
30exec_prefix=/usr
31sbindir=/sbin
32datadir=/usr/share
33PACKAGE=grub
34VERSION=0.93
35host_cpu=`uname -i`
36host_os=linux-gnu
37host_vendor=redhat
38pkgdatadir=${datadir}/${PACKAGE}/${host_cpu}-${host_vendor}
39
40if [ ! -e "$pkgdatadir" ] && [ -e "/boot" ] ; then
41    pkgdatadir=""
42    for i in `find $prefix/*/grub $prefix/*/*/grub -maxdepth 1 2>/dev/null`; do
43    last_resort=$i
44    if echo "$i" | grep `uname -i` &> /dev/null && [ -e "$i/stage1" ]; then
45        pkgdatadir=$i
46    fi
47    done
48    [ ! "$pkgdatadir" ] && pkgdatadir=$last_resort
49    if [ ! -e "$pkgdatadir/stage1" ] && [ -e "/boot/grub/stage1" ] ; then
50        pkgdatadir=/tmp/grub
51    mkdir -p $pkgdatadir
52    cp -af /boot/grub/* $pkgdatadir
53    fi
54    datadir=/boot
55fi
56echo "pkgdatadir is now $pkgdatadir"
57echo "datadir is now $datadir"
58
59grub_shell=${sbindir}/grub
60[ ! -e "$grub_shell" ] && grub_shell=`which grub`
61log_file=/tmp/grub-install.log.$$
62img_file=/tmp/grub-install.img.$$
63rootdir=
64grub_prefix=/boot/grub
65
66install_device=
67force_lba=
68recheck=no
69debug=no
70justcopy=no
71
72# look for secure tempfile creation wrappers on this platform
73if test -x /bin/tempfile; then
74    mklog="/bin/tempfile --prefix=grub"
75    mkimg="/bin/tempfile --prefix=grub"
76elif test -x /bin/mktemp; then
77    mklog="/bin/mktemp /tmp/grub-install.log.XXXXXX"
78    mkimg="/bin/mktemp /tmp/grub-install.img.XXXXXX"
79else
80    mklog=""
81    mkimg=""
82fi
83
84# Usage: usage
85# Print the usage.
86usage () {
87    cat <<EOF
88Usage: grub-install [OPTION] install_device
89Install GRUB on your drive.
90
91  -h, --help              print this message and exit
92  -v, --version           print the version information and exit
93  --root-directory=DIR    install GRUB images under the directory DIR
94                          instead of the root directory
95  --grub-shell=FILE       use FILE as the grub shell
96  --force-lba             force GRUB to use LBA mode even for a buggy
97                          BIOS
98  --recheck               probe a device map even if it already exists
99
100INSTALL_DEVICE can be a GRUB device name or a system device filename.
101
102Report bugs to <bug-grub@gnu.org>.
103EOF
104}
105
106# Usage: convert os_device
107# Convert an OS device to the corresponding GRUB drive.
108# This part is OS-specific.
109convert () {
110    # First, check if the device file exists.
111    if test -e "$1"; then
112    :
113    else
114    echo "$1: Not found or not a block device." 1>&2
115    exit 1
116    fi
117
118    # Break the device name into the disk part and the partition part.
119    case "$host_os" in
120    linux*)
121    var=`echo $1 | grep -v '/dev/md'`
122    if [ $? -eq 0 ]; then
123
124        tmp_disk=`echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
125                      -e 's%\(fd[0-9]*\)$%\1%' \
126                      -e 's%/part[0-9]*$%/disc%' \
127                      -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
128        tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
129                      -e 's%.*/fd[0-9]*$%%' \
130                      -e 's%.*/floppy/[0-9]*$%%' \
131                      -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
132                      -e 's%.*c[0-7]d[0-9]*p*%%'`
133    else
134
135        LN1=`grep -n "$1" /etc/raidtab | cut -f1 -d\:`
136        LN2=`grep -n raiddev /etc/raidtab | grep -A 1 "$LN1"  | \
137                     cut -f1 -d\: | grep -v "$LN1"`
138        let LN2='LN2 - 1'
139        let LN='LN2 - LN1'
140
141        one=`grep -A $LN "$1" /etc/raidtab | grep device | head -1 \
142                     | sed s/device//g | tr '\011' '\0' | sed 's/ *//g'`
143
144        tmp_disk=`echo "$one" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
145                      -e 's%\(fd[0-9]*\)$%\1%' \
146                      -e 's%/part[0-9]*$%/disc%' \
147                      -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
148        tmp_part=`echo "$one" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
149                      -e 's%.*/fd[0-9]*$%%' \
150                      -e 's%.*/floppy/[0-9]*$%%' \
151                      -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
152                      -e 's%.*c[0-7]d[0-9]*p*%%'`
153
154    fi
155    ;;
156    gnu*)
157    tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
158    tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
159    freebsd*)
160    tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%r\1%' \
161                | sed 's%r\{0,1\}\(da[0-9]*\).*$%r\1%'`
162    tmp_part=`echo "$1" \
163        | sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
164            | sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
165    ;;
166    netbsd*)
167    tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
168        | sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
169    tmp_part=`echo "$1" \
170        | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
171    ;;
172    *)
173    echo "grub-install does not support your OS yet." 1>&2
174    exit 1 ;;
175    esac
176
177var=`echo $1 | grep -v '/dev/md'`
178if [ $? -eq 0 ]; then
179
180    # Get the drive name.
181    tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
182    | sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%'`
183
184else
185
186    LN1=`grep -n "$1" /etc/raidtab | cut -f1 -d\:`
187    LN2=`grep -n raiddev /etc/raidtab | grep -A 1 "$LN1"  | \
188         cut -f1 -d\: | grep -v "$LN1"`
189    let LN2='LN2 - 1'
190    let LN='LN2 - LN1'
191
192    tmp_raid=`grep -A $LN "$1" /etc/raidtab | grep device | head -1 \
193        | sed s/device//g | tr '\011' '\0' | sed 's/ *//g' | sed s/[0-9]//g`
194    tmp_drive=`grep -v '^#' $device_map | grep "$tmp_raid *$" \
195        | sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%'`
196
197fi
198
199    # If not found, print an error message and exit.
200    if test "x$tmp_drive" = x; then
201    echo "$1 does not have any corresponding BIOS drive." 1>&2
202    exit 1
203    fi
204
205    if test "x$tmp_part" != x; then
206    # If a partition is specified, we need to translate it into the
207    # GRUB's syntax.
208    case "$host_os" in
209    linux*)
210        echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
211    gnu*)
212        if echo $tmp_part | grep "^s" >/dev/null; then
213        tmp_pc_slice=`echo $tmp_part \
214            | sed "s%s\([0-9]*\)[a-g]*$%\1%"`
215        tmp_drive=`echo "$tmp_drive" \
216            | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
217        fi
218        if echo $tmp_part | grep "[a-g]$" >/dev/null; then
219        tmp_bsd_partition=`echo "$tmp_part" \
220            | sed "s%[^a-g]*\([a-g]\)$%\1%"`
221        tmp_drive=`echo "$tmp_drive" \
222            | sed "s%)%,$tmp_bsd_partition)%"`
223        fi
224        echo "$tmp_drive" ;;
225    freebsd*)
226        if echo $tmp_part | grep "^s" >/dev/null; then
227        tmp_pc_slice=`echo $tmp_part \
228            | sed "s%s\([0-9]*\)[a-h]*$%\1%"`
229        tmp_drive=`echo "$tmp_drive" \
230            | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
231        fi
232        if echo $tmp_part | grep "[a-h]$" >/dev/null; then
233        tmp_bsd_partition=`echo "$tmp_part" \
234            | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
235        tmp_drive=`echo "$tmp_drive" \
236            | sed "s%)%,$tmp_bsd_partition)%"`
237        fi
238        echo "$tmp_drive" ;;
239    netbsd*)
240        if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
241        tmp_bsd_partition=`echo "$tmp_part" \
242            | sed "s%\([a-p]\)$%\1%"`
243        tmp_drive=`echo "$tmp_drive" \
244            | sed "s%)%,$tmp_bsd_partition)%"`
245        fi
246        echo "$tmp_drive" ;;
247    esac
248    else
249    # If no partition is specified, just print the drive name.
250    echo "$tmp_drive"
251    fi
252}
253
254# Usage: find_device file
255# Find block device on which the file resides.
256find_device () {
257    # For now, this uses the program `df' to get the device name, but is
258    # this really portable?
259    tmp_fname=`df $1/ | sed -n 's%.*\(/dev/[^   ]*\).*%\1%p'`
260
261    if test -z "$tmp_fname"; then
262    echo "Could not find device for $1" 2>&1
263    exit 1
264    fi
265
266    # Resolve symlinks
267    while test -L $tmp_fname; do
268    tmp_new_fname=`ls -al $tmp_fname | sed -n 's%.*-> %\1%p'`
269    if test -z "$tmp_new_fname"; then
270        echo "Unrecognized ls output" 2>&1
271        exit 1
272    fi
273
274    # Convert relative symlinks
275    case $tmp_new_fname in
276        /*) tmp_fname="$tmp_new_fname" ;;
277        *) tmp_fname="`echo $tmp_fname | sed 's%/[^/]*$%%'`/$tmp_new_fname" ;;
278    esac
279    done
280    echo "$tmp_fname"
281}
282
283copy_images() {
284    for file in ${pkgdatadir}/stage1 ${pkgdatadir}/stage2 ${pkgdatadir}/*stage1_5; do
285        cp -f $file ${grubdir} || exit 1
286    done
287}
288
289# Check the arguments.
290for option in "$@"; do
291    case "$option" in
292    -h | --help)
293    usage
294    exit 0 ;;
295    -v | --version)
296    echo "grub-install (GNU GRUB ${VERSION})"
297    exit 0 ;;
298    --root-directory=*)
299    rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
300    --grub-shell=*)
301    grub_shell=`echo "$option" | sed 's/--grub-shell=//'` ;;
302    --force-lba)
303    force_lba="--force-lba" ;;
304    --recheck)
305    recheck=yes ;;
306    --just-copy)
307        justcopy=yes ;;
308    # This is an undocumented feature...
309    --debug)
310    debug=yes ;;
311    -*)
312    echo "Unrecognized option \`$option'" 1>&2
313    usage
314    exit 1
315    ;;
316    *)
317    if test "x$install_device" != x; then
318        echo "More than one install_devices?" 1>&2
319        usage
320        exit 1
321    fi
322    install_device="${option}" ;;
323    esac
324done
325
326# If the debugging feature is enabled, print commands.
327if test $debug = yes; then
328    set -x
329fi
330
331# Initialize these directories here, since ROOTDIR was initialized.
332case "$host_os" in
333netbsd*)
334    # Because /boot is used for the boot block in NetBSD, use /grub
335    # instead of /boot/grub.
336    grub_prefix=/grub
337    bootdir=${rootdir}
338    ;;
339*)
340    # Use /boot/grub by default.
341    bootdir=${rootdir}/boot
342    ;;
343esac
344
345grubdir=${bootdir}/grub
346device_map=${grubdir}/device.map
347
348# if they just want the images copied, copy the images and then exit
349if test $justcopy = yes; then
350    copy_images
351    exit 0
352fi
353
354if test "x$install_device" = x; then
355    echo "install_device not specified." 1>&2
356    usage
357    exit 1
358fi
359
360# Check if GRUB is installed
361if test -f "$grub_shell"; then
362    :
363else
364    echo "${grub_shell}: Not found." 1>&2
365    exit 1
366fi
367
368if test -f "$pkgdatadir/stage1"; then
369    :
370else
371    echo "${pkgdatadir}/stage1: Not found." 1>&2
372    exit 1
373fi
374
375if test -f "$pkgdatadir/stage2"; then
376    :
377else
378    echo "${pkgdatadir}/stage2: Not found." 1>&2
379    exit 1
380fi
381
382# Don't check for *stage1_5, because it is not fatal even if any
383# Stage 1.5 does not exist.
384
385# Create the GRUB directory if it is not present.
386test -d "$bootdir" || mkdir "$bootdir" || exit 1
387test -d "$grubdir" || mkdir "$grubdir" || exit 1
388
389copy_images
390
391# If --recheck is specified, remove the device map, if present.
392if test $recheck = yes; then
393    rm -f $device_map
394fi
395
396# Create the device map file if it is not present.
397if test -f "$device_map"; then
398    :
399else
400    # Create a safe temporary file.
401    test -n "$mklog" && log_file=`$mklog`
402
403    # Before all invocations of the grub shell, call sync to make sure
404    # the raw device is in sync with any bufferring in filesystems.
405    sync
406
407    $grub_shell --batch --device-map=$device_map <<EOF >$log_file
408quit
409EOF
410    if grep "Error [0-9]*: " $log_file >/dev/null; then
411    cat $log_file 1>&2
412    exit 1
413    fi
414
415    rm -f $log_file
416fi
417
418# Make sure that there is no duplicated entry.
419tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \
420    | sort | uniq -d | sed -n 1p`
421if test -n "$tmp"; then
422    echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2
423    exit 1
424fi
425
426# Check for INSTALL_DEVICE.
427case "$install_device" in
428/dev/*)
429    install_drive=`convert "$install_device"`
430    # I don't know why, but some shells wouldn't die if exit is
431    # called in a function.
432    if test "x$install_drive" = x; then
433    exit 1
434    fi ;;
435\([hf]d[0-9]*\))
436    install_drive="$install_device" ;;
437*)
438    echo "Format of install_device not recognized." 1>&2
439    usage
440    exit 1 ;;
441esac
442
443# Get the root drive.
444root_device=`find_device ${rootdir}`
445bootdir_device=`find_device ${bootdir}`
446
447# Check if the boot directory is in the same device as the root directory.
448if test "x$root_device" != "x$bootdir_device"; then
449    # Perhaps the user has a separate boot partition.
450    root_device=$bootdir_device
451    grub_prefix="/grub"
452fi
453
454# Convert the root device to a GRUB drive.
455root_drive=`convert "$root_device"`
456if test "x$root_drive" = x; then
457    exit 1
458fi
459
460# Check if the root directory exists in the same device as the grub
461# directory.
462grubdir_device=`find_device ${grubdir}`
463
464if test "x$grubdir_device" != "x$root_device"; then
465    # For now, cannot deal with this situation.
466    cat <<EOF 1>&2
467You must set the root directory by the option --root-directory, because
468$grubdir does not exist in the root device $root_device.
469EOF
470    exit 1
471fi
472
473# Make sure that GRUB reads the same images as the host OS.
474test -n "$mkimg" && img_file=`$mkimg`
475test -n "$mklog" && log_file=`$mklog`
476
477for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
478    count=5
479    tmp=`echo $file | sed "s|^${grubdir}|${grub_prefix}|"`
480    while test $count -gt 0; do
481    $grub_shell --batch --device-map=$device_map <<EOF >$log_file
482dump ${root_drive}${tmp} ${img_file}
483quit
484EOF
485    if grep "Error [0-9]*: " $log_file >/dev/null; then
486        :
487    elif cmp $file $img_file >/dev/null; then
488        break
489    fi
490    sleep 1
491    count=`expr $count - 1`   
492    done
493    if test $count -eq 0; then
494    echo "The file $file not read correctly." 1>&2
495    exit 1
496    fi
497done
498
499rm -f $img_file
500rm -f $log_file
501
502if ! test -e ${grubdir}/grub.conf ; then
503    test -e ${grubdir}/menu.lst && ln -s ./menu.lst ${grubdir}/grub.conf
504fi
505
506# Create a safe temporary file.
507test -n "$mklog" && log_file=`$mklog`
508
509# Before all invocations of the grub shell, call sync to make sure
510# the raw device is in sync with any bufferring in filesystems.
511sync
512
513# Now perform the installation.
514$grub_shell --batch --device-map=$device_map <<EOF >$log_file
515root $root_drive
516setup $force_lba --stage2=$grubdir/stage2 --prefix=$grub_prefix $install_drive
517quit
518EOF
519
520if grep "Error [0-9]*: " $log_file >/dev/null || test $debug = yes; then
521    cat $log_file 1>&2
522    exit 1
523fi
524
525rm -f $log_file
526
527# Prompt the user to check if the device map is correct.
528echo "Installation finished. No error reported."
529echo "This is the contents of the device map $device_map."
530echo "Check if this is correct or not. If any of the lines is incorrect,"
531echo "fix it and re-run the script \`grub-install'."
532echo
533
534cat $device_map
535
536# Bye.
537exit 0
Note: See TracBrowser for help on using the repository browser.