source: branches/2.05/mondo/mondo/restore-scripts/mondo/grub-MR @ 196

Last change on this file since 196 was 196, checked in by bcornec, 15 years ago

Usage of parted2fdisk instead of fdisk everywhere.
on ia64 this is mandatory, and simplifies the infrastructure
on other archs, it doesn't change anything as parted2fdisk here is a link to fdisk

  • Property svn:keywords set to Id
File size: 4.9 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=`cat $2 | grep " $sz " | 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 "Trying a hack" >> $LOGFILE
152FindBootPart $1 $2
153mbrpart=$1
154if echo $bootpart | grep "/cciss/" > /dev/null ; then
155    partno=`basename $bootpart | cut -d'p' -f2`
156else
157    partno=`basename $bootpart | sed s/[a-z]*//`
158fi
159if [ ! "$partno" ] ; then
160    partno=0
161else
162    partno=$(($partno-1))
163fi
164if echo $bootpart | grep "/md" > /dev/null ; then
165    base=`basename $bootpart`
166    line=`cat /proc/mdstat | grep $base | head -n1`
167    echo "mbrpart was $mbrpart"
168    mbrpart=`parted2fdisk -l | grep /dev/ | head -n1 | tr ':' ' ' \
169| cut -d' ' -f2`
170    echo "mbrpart is $mbrpart"
171    partno="0"; # cheating - FIXME   
172fi
173echo ".............Cool."
174
175grub=`FindPathOfRESTExe grub`
176mkdir -p /boot
177[ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
178
179# ---------------------------------
180
181for driveno in 0 1 2 ; do
182    try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
183    try_grub_Hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
184done
185res=$?
186echo "Hack returned res=$res" >> $LOGFILE
187[ "$res" -eq "0" ] && exit 0
188
189# ---------------------------------
190
191echo "Now I'll use grub-install.patched" >> $LOGFILE
192cp -f `which grub-install.patched` /mnt/RESTORING/sbin
193chroot /mnt/RESTORING grub-install.patched $1 >> $LOGFILE
194res=$?
195echo "grub-install.patched returned $res" >> $LOGFILE
196[ "$res" -eq "0" ] && exit 0
197
198# ---------------------------------
199
200echo "Trying to use the existing grub.conf file in batch mode" >> $LOGFILE
201chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/etc/grub.conf
202res=$?
203echo "Grub.conf approach returned $res" >> $LOGFILE
204[ "$res" -eq "0" ] && exit 0
205
206#echo "Now I'll use grub-install" >> $LOGFILE
207#chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
208#res=$?
209#echo "grub-install returned $res" >> $LOGFILE
210#[ "$res" -eq "0" ] && exit 0
211
212echo "grub-MR returned res=$res" >> $LOGFILE
213
214exit $res
Note: See TracBrowser for help on using the repository browser.