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 |
|
---|
52 | LOGFILE=/var/log/mondo-archive.log
|
---|
53 |
|
---|
54 |
|
---|
55 | Die() {
|
---|
56 | echo "$1" >> /dev/stderr
|
---|
57 | exit 1
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | FindBootPart() {
|
---|
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 |
|
---|
97 | FindPathOfRESTExe() {
|
---|
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 |
|
---|
110 | try_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 |
|
---|
140 | if [ "$#" -ne "2" ] ; then
|
---|
141 | Die "grub-MR <MBR drive> <mountlist filename>; e.g. grub-MR /dev/hda /tmp/mountlist.txt"
|
---|
142 | fi
|
---|
143 | [ ! -f "$2" ] && Die "mountlist file not found"
|
---|
144 |
|
---|
145 | if [ -e "/mnt/RESTORING/boot" ] ; then
|
---|
146 | MNT_RESTORING=/mnt/RESTORING
|
---|
147 | else
|
---|
148 | MNT_RESTORING=""
|
---|
149 | fi
|
---|
150 |
|
---|
151 | echo "Trying a hack" >> $LOGFILE
|
---|
152 | FindBootPart $1 $2
|
---|
153 | mbrpart=$1
|
---|
154 | if echo $bootpart | grep "/cciss/" > /dev/null ; then
|
---|
155 | partno=`basename $bootpart | cut -d'p' -f2`
|
---|
156 | else
|
---|
157 | partno=`basename $bootpart | sed s/[a-z]*//`
|
---|
158 | fi
|
---|
159 | if [ ! "$partno" ] ; then
|
---|
160 | partno=0
|
---|
161 | else
|
---|
162 | partno=$(($partno-1))
|
---|
163 | fi
|
---|
164 | if 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
|
---|
172 | fi
|
---|
173 | echo ".............Cool."
|
---|
174 |
|
---|
175 | grub=`FindPathOfRESTExe grub`
|
---|
176 | mkdir -p /boot
|
---|
177 | [ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
|
---|
178 |
|
---|
179 | # ---------------------------------
|
---|
180 |
|
---|
181 | for 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
|
---|
184 | done
|
---|
185 | res=$?
|
---|
186 | echo "Hack returned res=$res" >> $LOGFILE
|
---|
187 | [ "$res" -eq "0" ] && exit 0
|
---|
188 |
|
---|
189 | # ---------------------------------
|
---|
190 |
|
---|
191 | echo "Now I'll use grub-install.patched" >> $LOGFILE
|
---|
192 | cp -f `which grub-install.patched` /mnt/RESTORING/sbin
|
---|
193 | chroot /mnt/RESTORING grub-install.patched $1 >> $LOGFILE
|
---|
194 | res=$?
|
---|
195 | echo "grub-install.patched returned $res" >> $LOGFILE
|
---|
196 | [ "$res" -eq "0" ] && exit 0
|
---|
197 |
|
---|
198 | # ---------------------------------
|
---|
199 |
|
---|
200 | echo "Trying to use the existing grub.conf file in batch mode" >> $LOGFILE
|
---|
201 | chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/etc/grub.conf
|
---|
202 | res=$?
|
---|
203 | echo "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 |
|
---|
212 | echo "grub-MR returned res=$res" >> $LOGFILE
|
---|
213 |
|
---|
214 | exit $res
|
---|