source: branches/stable/mondo/mondo/restore-scripts/mondo/grub-MR @ 567

Last change on this file since 567 was 567, checked in by bcornec, 13 years ago

Same for mondo: setup modes correctly in SVN and get rid of %attr an %defattr in spec files to get correct packages

  • Property svn:keywords set to Id
  • Property svn:unix-mode set to 755
File size: 5.0 KB
Line 
1#!/bin/sh
2
3
4# grub-MR ------ a rudimentary replacement for grub-install
5#
6#
7#
8# 2005/11/25
9# - try_grub_hack : do not return success when grub wrote 'Error '
10#
11# 2004/06/28
12# - added support for /dev/md0==GRUB
13#
14# 2004/06/27
15# - re-enable the call to grub-install and the 'echo' thing as a backup
16# - hacked the hack :) to improve MDK9.2 support
17# - added try_grub_hack()
18#
19# 2004/06/14
20# - just use grub-install.patched; nothing else
21#
22# 2004/05/05
23# - try EVERYTHING - grub-install, grub --batch, etc.
24#
25# 2004/04/18
26# - try grub --batch < /etc/grub.conf first
27# - if it fails, grub-install.patched instead
28# - if _that_ fails, fail.
29#
30# 2004/04/15
31# - added grub-install.patched to the mix
32#
33# 2004/04/01
34# - use grub-install if there's an NTFS partition on the disk
35#
36# 2004/03/31
37# - disabled direct call to grub
38#
39# 2004/03/28
40# - call grub directly if possible
41#
42# 2004/03/26
43# - find stage 1 if possible; more stable, reliable call to grub
44#
45# 2003/08/24
46# - fixed line 26 (Christian)
47#
48# 2002/11/18
49# - first incarnation
50
51
52LOGFILE=/var/log/mondo-archive.log
53
54
55Die() {
56    echo "$1" >> /dev/stderr
57    exit 1
58}
59
60
61FindBootPart() {
62    local i
63    bootpart=""
64    BOOTPATHNAME=""
65    for sz in /boot / ; do
66    bootpart=`grep " $sz " $2 | cut -d' ' -f1 | head -n1`
67    [ "$bootpart" ] && [ -e "/mnt/RESTORING/$bootpart" ] && break
68    done
69    [ ! "$bootpart" ] && Die "Cannot find boot partition in mountlist"
70
71    if [ "$sz" = "/boot" ] ; then
72    BOOTPATHNAME=/grub
73    else
74    BOOTPATHNAME=/boot/grub
75    fi
76    if [ -e "$MNT_RESTORING/boot/grub/stage1" ] ; then
77    echo "Alright then."
78    return
79    fi
80   
81    cd $MNT_RESTORING
82    for i in usr/lib/grub/* ; do
83    echo "Checking $i"
84        if [ -e "$i/stage1" ] ; then
85        BOOTPATHNAME=/$i
86        mkdir -p /boot/grub
87        cp -au $i/* /boot/grub/
88        echo "BOOTPATHNAME is now $BOOTPATHNAME"
89        return 0
90    fi
91    done
92    cd /
93    echo "Cannot find BOOTPATHNAME"
94    return 1
95}
96
97FindPathOfRESTExe() {
98    local path
99    for path in /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin \
100/bin /sbin ; do
101        if [ -f "/mnt/RESTORING/$path/$1" ] ; then 
102            echo "/mnt/RESTORING/$path/$1"
103        return
104        fi
105    done
106    which grub
107}
108
109
110try_grub_hack() {
111    local driveno extraline partno
112    driveno=$1
113    partno=$2
114    extraline="$3"
115
116    echo -en "\
117    device (hd$driveno) $mbrpart\n\
118    $extraline\n\
119    root (hd$driveno,$partno)\n\
120    setup (hd$driveno)\n\
121    quit \n" > /tmp/feed.txt
122    cat /tmp/feed.txt
123    log_file=/tmp/grub.output
124    if [ "$MNT_RESTORING" ] ; then
125        chroot /mnt/RESTORING grub --batch < /tmp/feed.txt > $log_file
126    else
127        grub --batch < /tmp/feed.txt > $log_file
128    fi
129    cat $log_file
130    if grep "Error [0-9]*: " $log_file >/dev/null; then
131            return 1
132    else
133            return 0
134    fi
135}
136
137
138# ---------------------------------- main -----------------------------------
139
140if [ "$#" -ne "2" ] ; then
141    Die "grub-MR <MBR drive> <mountlist filename>; e.g. grub-MR /dev/hda /tmp/mountlist.txt"
142fi
143[ ! -f "$2" ] && Die "mountlist file not found"
144
145if [ -e "/mnt/RESTORING/boot" ] ; then
146    MNT_RESTORING=/mnt/RESTORING
147else
148    MNT_RESTORING=""
149fi
150
151echo "Now I'll use grub-install" >> $LOGFILE
152chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
153res=$?
154echo "grub-install returned $res" >> $LOGFILE
155[ "$res" -eq "0" ] && exit 0
156
157echo "Trying a hack" >> $LOGFILE
158FindBootPart $1 $2
159mbrpart=$1
160if echo $bootpart | grep "/cciss/" > /dev/null ; then
161    partno=`basename $bootpart | cut -d'p' -f2`
162else
163    partno=`basename $bootpart | sed s/[a-z]*//`
164fi
165if [ ! "$partno" ] ; then
166    partno=0
167else
168    partno=$(($partno-1))
169fi
170if echo $bootpart | grep "/md" > /dev/null ; then
171    base=`basename $bootpart`
172    line=`grep $base /proc/mdstat | head -n1`
173    echo "mbrpart was $mbrpart"
174    mbrpart=`parted2fdisk -l | grep /dev/ | head -n1 | tr ':' ' ' \
175| cut -d' ' -f2`
176    echo "mbrpart is $mbrpart"
177    partno="0"; # cheating - FIXME   
178fi
179echo ".............Cool."
180
181grub=`FindPathOfRESTExe grub`
182mkdir -p /boot
183[ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
184
185# ---------------------------------
186
187for driveno in 0 1 2 ; do
188    try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
189    try_grub_Hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
190done
191res=$?
192echo "Hack returned res=$res" >> $LOGFILE
193[ "$res" -eq "0" ] && exit 0
194
195# ---------------------------------
196
197echo "Now I'll use grub-install.patched" >> $LOGFILE
198cp -f `which grub-install.patched` /mnt/RESTORING/sbin
199chroot /mnt/RESTORING grub-install.patched $1 >> $LOGFILE
200res=$?
201echo "grub-install.patched returned $res" >> $LOGFILE
202[ "$res" -eq "0" ] && exit 0
203
204# ---------------------------------
205
206echo "Trying to use the existing grub.conf file in batch mode" >> $LOGFILE
207chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/etc/grub.conf
208res=$?
209echo "Grub.conf approach returned $res" >> $LOGFILE
210[ "$res" -eq "0" ] && exit 0
211
212#echo "Now I'll use grub-install" >> $LOGFILE
213#chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
214#res=$?
215#echo "grub-install returned $res" >> $LOGFILE
216#[ "$res" -eq "0" ] && exit 0
217
218echo "grub-MR returned res=$res" >> $LOGFILE
219
220exit $res
Note: See TracBrowser for help on using the repository browser.