source: MondoRescue/trunk/mondo/mondo/restore-scripts/mondo/grub-install.patched@ 30

Last change on this file since 30 was 30, checked in by bcornec, 19 years ago

Id property added on files to allow for better conf. management

  • Property svn:keywords set to Id
File size: 14.4 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 # Copy the GRUB images to the GRUB directory.
285# for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
286# rm -f $file || exit 1
287# done
288 for file in \
289 ${pkgdatadir}/stage1 ${pkgdatadir}/stage2 ${pkgdatadir}/*stage1_5; do
290 cp -f $file ${grubdir} || exit 1
291 done
292}
293
294# Check the arguments.
295for option in "$@"; do
296 case "$option" in
297 -h | --help)
298 usage
299 exit 0 ;;
300 -v | --version)
301 echo "grub-install (GNU GRUB ${VERSION})"
302 exit 0 ;;
303 --root-directory=*)
304 rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
305 --grub-shell=*)
306 grub_shell=`echo "$option" | sed 's/--grub-shell=//'` ;;
307 --force-lba)
308 force_lba="--force-lba" ;;
309 --recheck)
310 recheck=yes ;;
311 --just-copy)
312 justcopy=yes ;;
313 # This is an undocumented feature...
314 --debug)
315 debug=yes ;;
316 -*)
317 echo "Unrecognized option \`$option'" 1>&2
318 usage
319 exit 1
320 ;;
321 *)
322 if test "x$install_device" != x; then
323 echo "More than one install_devices?" 1>&2
324 usage
325 exit 1
326 fi
327 install_device="${option}" ;;
328 esac
329done
330
331# If the debugging feature is enabled, print commands.
332if test $debug = yes; then
333 set -x
334fi
335
336# Initialize these directories here, since ROOTDIR was initialized.
337case "$host_os" in
338netbsd*)
339 # Because /boot is used for the boot block in NetBSD, use /grub
340 # instead of /boot/grub.
341 grub_prefix=/grub
342 bootdir=${rootdir}
343 ;;
344*)
345 # Use /boot/grub by default.
346 bootdir=${rootdir}/boot
347 ;;
348esac
349
350grubdir=${bootdir}/grub
351device_map=${grubdir}/device.map
352
353# if they just want the images copied, copy the images and then exit
354if test $justcopy = yes; then
355 copy_images
356 exit 0
357fi
358
359if test "x$install_device" = x; then
360 echo "install_device not specified." 1>&2
361 usage
362 exit 1
363fi
364
365# Check if GRUB is installed
366if test -f "$grub_shell"; then
367 :
368else
369 echo "${grub_shell}: Not found." 1>&2
370 exit 1
371fi
372
373if test -f "$pkgdatadir/stage1"; then
374 :
375else
376 echo "${pkgdatadir}/stage1: Not found." 1>&2
377 exit 1
378fi
379
380if test -f "$pkgdatadir/stage2"; then
381 :
382else
383 echo "${pkgdatadir}/stage2: Not found." 1>&2
384 exit 1
385fi
386
387# Don't check for *stage1_5, because it is not fatal even if any
388# Stage 1.5 does not exist.
389
390# Create the GRUB directory if it is not present.
391test -d "$bootdir" || mkdir "$bootdir" || exit 1
392test -d "$grubdir" || mkdir "$grubdir" || exit 1
393
394copy_images
395
396# If --recheck is specified, remove the device map, if present.
397if test $recheck = yes; then
398 rm -f $device_map
399fi
400
401# Create the device map file if it is not present.
402if test -f "$device_map"; then
403 :
404else
405 # Create a safe temporary file.
406 test -n "$mklog" && log_file=`$mklog`
407
408 # Before all invocations of the grub shell, call sync to make sure
409 # the raw device is in sync with any bufferring in filesystems.
410 sync
411
412 $grub_shell --batch --device-map=$device_map <<EOF >$log_file
413quit
414EOF
415 if grep "Error [0-9]*: " $log_file >/dev/null; then
416 cat $log_file 1>&2
417 exit 1
418 fi
419
420 rm -f $log_file
421fi
422
423# Make sure that there is no duplicated entry.
424tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \
425 | sort | uniq -d | sed -n 1p`
426if test -n "$tmp"; then
427 echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2
428 exit 1
429fi
430
431# Check for INSTALL_DEVICE.
432case "$install_device" in
433/dev/*)
434 install_drive=`convert "$install_device"`
435 # I don't know why, but some shells wouldn't die if exit is
436 # called in a function.
437 if test "x$install_drive" = x; then
438 exit 1
439 fi ;;
440\([hf]d[0-9]*\))
441 install_drive="$install_device" ;;
442*)
443 echo "Format of install_device not recognized." 1>&2
444 usage
445 exit 1 ;;
446esac
447
448# Get the root drive.
449root_device=`find_device ${rootdir}`
450bootdir_device=`find_device ${bootdir}`
451
452# Check if the boot directory is in the same device as the root directory.
453if test "x$root_device" != "x$bootdir_device"; then
454 # Perhaps the user has a separate boot partition.
455 root_device=$bootdir_device
456 grub_prefix="/grub"
457fi
458
459# Convert the root device to a GRUB drive.
460root_drive=`convert "$root_device"`
461if test "x$root_drive" = x; then
462 exit 1
463fi
464
465# Check if the root directory exists in the same device as the grub
466# directory.
467grubdir_device=`find_device ${grubdir}`
468
469if test "x$grubdir_device" != "x$root_device"; then
470 # For now, cannot deal with this situation.
471 cat <<EOF 1>&2
472You must set the root directory by the option --root-directory, because
473$grubdir does not exist in the root device $root_device.
474EOF
475 exit 1
476fi
477
478# Make sure that GRUB reads the same images as the host OS.
479test -n "$mkimg" && img_file=`$mkimg`
480test -n "$mklog" && log_file=`$mklog`
481
482for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
483 count=5
484 tmp=`echo $file | sed "s|^${grubdir}|${grub_prefix}|"`
485 while test $count -gt 0; do
486 $grub_shell --batch --device-map=$device_map <<EOF >$log_file
487dump ${root_drive}${tmp} ${img_file}
488quit
489EOF
490 if grep "Error [0-9]*: " $log_file >/dev/null; then
491 :
492 elif cmp $file $img_file >/dev/null; then
493 break
494 fi
495 sleep 1
496 count=`expr $count - 1`
497 done
498 if test $count -eq 0; then
499 echo "The file $file not read correctly." 1>&2
500 exit 1
501 fi
502done
503
504rm -f $img_file
505rm -f $log_file
506
507if ! test -e ${grubdir}/grub.conf ; then
508 test -e ${grubdir}/menu.lst && ln -s ./menu.lst ${grubdir}/grub.conf
509fi
510
511# Create a safe temporary file.
512test -n "$mklog" && log_file=`$mklog`
513
514# Before all invocations of the grub shell, call sync to make sure
515# the raw device is in sync with any bufferring in filesystems.
516sync
517
518# Now perform the installation.
519$grub_shell --batch --device-map=$device_map <<EOF >$log_file
520root $root_drive
521setup $force_lba --stage2=$grubdir/stage2 --prefix=$grub_prefix $install_drive
522quit
523EOF
524
525if grep "Error [0-9]*: " $log_file >/dev/null || test $debug = yes; then
526 cat $log_file 1>&2
527 exit 1
528fi
529
530rm -f $log_file
531
532# Prompt the user to check if the device map is correct.
533echo "Installation finished. No error reported."
534echo "This is the contents of the device map $device_map."
535echo "Check if this is correct or not. If any of the lines is incorrect,"
536echo "fix it and re-run the script \`grub-install'."
537echo
538
539cat $device_map
540
541# Bye.
542exit 0
Note: See TracBrowser for help on using the repository browser.