source: trunk/mondo/mondo/restore-scripts/mondo/grub-MR @ 30

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

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

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