source: MondoRescue/branches/2.2.9/mondo/src/restore-scripts/mondo/grub-MR@ 2619

Last change on this file since 2619 was 2619, checked in by Bruno Cornec, 15 years ago
  • Fixes for #414: remove comments from grub conf file if used and improve grub conf file generation
  • Property svn:keywords set to Id
  • Property svn:unix-mode set to 755
File size: 5.1 KB
RevLine 
[1]1#!/bin/sh
2#
[1315]3# $Id: grub-MR 2619 2010-04-08 02:16:04Z bruno $
[1]4#
[1315]5# grub-MR ------ a rudimentary replacement for grub-install
[1]6#
[139]7#
[1]8
9Die() {
10 echo "$1" >> /dev/stderr
11 exit 1
12}
13
14
15FindBootPart() {
16 local i
17 bootpart=""
18 BOOTPATHNAME=""
19 for sz in /boot / ; do
[2137]20 bootpart=`grep " $sz " $2 | cut -d' ' -f1 | head -n1`
21 [ "$bootpart" ] && [ -e "/mnt/RESTORING/$bootpart" ] && break
[1]22 done
23 [ ! "$bootpart" ] && Die "Cannot find boot partition in mountlist"
24
25 if [ "$sz" = "/boot" ] ; then
[2137]26 BOOTPATHNAME=/grub
[1]27 else
[2137]28 BOOTPATHNAME=/boot/grub
[1]29 fi
30 if [ -e "$MNT_RESTORING/boot/grub/stage1" ] ; then
[2137]31 echo "Alright then."
32 return
[1]33 fi
34
35 cd $MNT_RESTORING
36 for i in usr/lib/grub/* ; do
[2137]37 echo "Checking $i"
[1]38 if [ -e "$i/stage1" ] ; then
[2137]39 BOOTPATHNAME=/$i
40 mkdir -p /boot/grub
41 cp -au $i/* /boot/grub/
42 echo "BOOTPATHNAME is now $BOOTPATHNAME"
43 return 0
44 fi
[1]45 done
46 cd /
47 echo "Cannot find BOOTPATHNAME"
48 return 1
49}
50
51FindPathOfRESTExe() {
52 local path
53 for path in /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin \
54/bin /sbin ; do
55 if [ -f "/mnt/RESTORING/$path/$1" ] ; then
56 echo "/mnt/RESTORING/$path/$1"
57 return
58 fi
59 done
60 which grub
61}
62
63
64try_grub_hack() {
65 local driveno extraline partno
66 driveno=$1
67 partno=$2
68 extraline="$3"
69
70 echo -en "\
[2615]71 device (hd$driveno) $mbrdev\n\
[1]72 $extraline\n\
73 root (hd$driveno,$partno)\n\
74 setup (hd$driveno)\n\
75 quit \n" > /tmp/feed.txt
76 cat /tmp/feed.txt
[139]77 log_file=/tmp/grub.output
[1]78 if [ "$MNT_RESTORING" ] ; then
[2615]79 chroot $MNT_RESTORING grub --batch < /tmp/feed.txt > $log_file
[1]80 else
[139]81 grub --batch < /tmp/feed.txt > $log_file
[1]82 fi
[139]83 cat $log_file
84 if grep "Error [0-9]*: " $log_file >/dev/null; then
85 return 1
86 else
87 return 0
88 fi
[1]89}
90
91
92# ---------------------------------- main -----------------------------------
93
94if [ "$#" -ne "2" ] ; then
95 Die "grub-MR <MBR drive> <mountlist filename>; e.g. grub-MR /dev/hda /tmp/mountlist.txt"
96fi
97[ ! -f "$2" ] && Die "mountlist file not found"
98
99if [ -e "/mnt/RESTORING/boot" ] ; then
100 MNT_RESTORING=/mnt/RESTORING
101else
102 MNT_RESTORING=""
103fi
104
[375]105echo "Now I'll use grub-install" >> $LOGFILE
[2615]106if [ "$MNT_RESTORING" ] ; then
[2619]107 chroot $MNT_RESTORING grub-install $1 2>&1 | tee -a $LOGFILE
[2615]108 res=$?
109else
[2619]110 grub-install $1 2>&1 | tee -a $LOGFILE
[2615]111 res=$?
112fi
[375]113echo "grub-install returned $res" >> $LOGFILE
114[ "$res" -eq "0" ] && exit 0
115
[2615]116# For some OpenSuSE
117echo "Now I'll use grub-install.unsupported" >> $LOGFILE
118if [ "$MNT_RESTORING" ] ; then
[2619]119 chroot $MNT_RESTORING grub-install.unsupported $1 2>&1 | tee -a $LOGFILE
[2615]120 res=$?
121else
[2619]122 grub-install.unsupported $1 2>&1 | tee -a $LOGFILE
[2615]123 res=$?
124fi
125echo "grub-install.unsupported returned $res" >> $LOGFILE
126[ "$res" -eq "0" ] && exit 0
127
[1]128echo "Trying a hack" >> $LOGFILE
[2615]129FindBootPart $1 $2 2>&1 | tee -a $LOGFILE
[2619]130mbrdev=`echo $1 | sed 's/\([^0-9]*\)[0-9]*$/\1/'`
[1]131if echo $bootpart | grep "/cciss/" > /dev/null ; then
132 partno=`basename $bootpart | cut -d'p' -f2`
[2619]133 mbrdev=`echo $mbrdev | cut -d'p' -f1`
[2137]134elif echo $bootpart | grep "/mapper/mpath" > /dev/null ; then
[2185]135 partno=`basename $bootpart | cut -d'p' -f3`
[2619]136 mbrdev=`echo $mbrdev | cut -d'p' -f1`
[1]137else
[2615]138 partno=`basename $bootpart | sed 's/[a-z]*//'`
[1]139fi
140if [ ! "$partno" ] ; then
141 partno=0
142else
143 partno=$(($partno-1))
144fi
145if echo $bootpart | grep "/md" > /dev/null ; then
[2615]146 # FIXME: Why this if not used later
[1]147 base=`basename $bootpart`
[273]148 line=`grep $base /proc/mdstat | head -n1`
[2615]149 echo "mbrdev was $mbrdev" 2>&1 | tee -a $LOGFILE
150 mbrdev=`parted2fdisk -l | grep /dev/ | head -n1 | tr ':' ' ' | cut -d' ' -f2`
151 echo "mbrdev is $mbrdev" 2>&1 | tee -a $LOGFILE
[1]152 partno="0"; # cheating - FIXME
153fi
[2615]154echo ".............Cool." 2>&1 | tee -a $LOGFILE
[1]155
156grub=`FindPathOfRESTExe grub`
157mkdir -p /boot
158[ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
159
160# ---------------------------------
161
162for driveno in 0 1 2 ; do
163 try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
[2619]164 try_grub_hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
[1]165done
166res=$?
167echo "Hack returned res=$res" >> $LOGFILE
168[ "$res" -eq "0" ] && exit 0
169
170# ---------------------------------
171
172echo "Now I'll use grub-install.patched" >> $LOGFILE
173cp -f `which grub-install.patched` /mnt/RESTORING/sbin
[2619]174chroot /mnt/RESTORING grub-install.patched $1 2>&1 | tee -a $LOGFILE
[1]175res=$?
176echo "grub-install.patched returned $res" >> $LOGFILE
177[ "$res" -eq "0" ] && exit 0
178
179# ---------------------------------
180
[2449]181if [ -f "/mnt/RESTORING/boot/grub/menu.lst" ]; then
[2619]182 grep -vE '^#' /boot/grub/menu.lst > /mnt/RESTORING/tmp/grub.conf
[2449]183elif [ -f "/mnt/RESTORING/boot/grub/grub.cfg" ]; then
[2619]184 grep -vE '^#' /boot/grub/grub.cfg > /mnt/RESTORING/tmp/grub.conf
[2449]185else
[2619]186 echo "Unable to find Grub conf file" | tee -a $LOGFILE
[2449]187 exit -1
188fi
[2619]189echo "Trying to use the existing grub conf file in batch mode" >> $LOGFILE
190cat /mnt/RESTORING/tmp/grub.conf >> $LOGFILE
191chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/tmp/grub.conf
[1]192res=$?
[2619]193echo "grub conf file approach returned $res" >> $LOGFILE
194rm -f /mnt/RESTORING/tmp/grub.conf
[1]195[ "$res" -eq "0" ] && exit 0
196
197echo "grub-MR returned res=$res" >> $LOGFILE
198
199exit $res
Note: See TracBrowser for help on using the repository browser.