1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # $Id: grub-MR 1315 2007-04-16 14:13:59Z bruno $
|
---|
4 | #
|
---|
5 | # grub-MR ------ a rudimentary replacement for grub-install
|
---|
6 | #
|
---|
7 | #
|
---|
8 |
|
---|
9 | Die() {
|
---|
10 | echo "$1" >> /dev/stderr
|
---|
11 | exit 1
|
---|
12 | }
|
---|
13 |
|
---|
14 |
|
---|
15 | FindBootPart() {
|
---|
16 | local i
|
---|
17 | bootpart=""
|
---|
18 | BOOTPATHNAME=""
|
---|
19 | for sz in /boot / ; do
|
---|
20 | bootpart=`grep " $sz " $2 | cut -d' ' -f1 | head -n1`
|
---|
21 | [ "$bootpart" ] && [ -e "/mnt/RESTORING/$bootpart" ] && break
|
---|
22 | done
|
---|
23 | [ ! "$bootpart" ] && Die "Cannot find boot partition in mountlist"
|
---|
24 |
|
---|
25 | if [ "$sz" = "/boot" ] ; then
|
---|
26 | BOOTPATHNAME=/grub
|
---|
27 | else
|
---|
28 | BOOTPATHNAME=/boot/grub
|
---|
29 | fi
|
---|
30 | if [ -e "$MNT_RESTORING/boot/grub/stage1" ] ; then
|
---|
31 | echo "Alright then."
|
---|
32 | return
|
---|
33 | fi
|
---|
34 |
|
---|
35 | cd $MNT_RESTORING
|
---|
36 | for i in usr/lib/grub/* ; do
|
---|
37 | echo "Checking $i"
|
---|
38 | if [ -e "$i/stage1" ] ; then
|
---|
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
|
---|
45 | done
|
---|
46 | cd /
|
---|
47 | echo "Cannot find BOOTPATHNAME"
|
---|
48 | return 1
|
---|
49 | }
|
---|
50 |
|
---|
51 | FindPathOfRESTExe() {
|
---|
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 |
|
---|
64 | try_grub_hack() {
|
---|
65 | local driveno extraline partno
|
---|
66 | driveno=$1
|
---|
67 | partno=$2
|
---|
68 | extraline="$3"
|
---|
69 |
|
---|
70 | echo -en "\
|
---|
71 | device (hd$driveno) $mbrpart\n\
|
---|
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
|
---|
77 | log_file=/tmp/grub.output
|
---|
78 | if [ "$MNT_RESTORING" ] ; then
|
---|
79 | chroot /mnt/RESTORING grub --batch < /tmp/feed.txt > $log_file
|
---|
80 | else
|
---|
81 | grub --batch < /tmp/feed.txt > $log_file
|
---|
82 | fi
|
---|
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
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | # ---------------------------------- main -----------------------------------
|
---|
93 |
|
---|
94 | if [ "$#" -ne "2" ] ; then
|
---|
95 | Die "grub-MR <MBR drive> <mountlist filename>; e.g. grub-MR /dev/hda /tmp/mountlist.txt"
|
---|
96 | fi
|
---|
97 | [ ! -f "$2" ] && Die "mountlist file not found"
|
---|
98 |
|
---|
99 | if [ -e "/mnt/RESTORING/boot" ] ; then
|
---|
100 | MNT_RESTORING=/mnt/RESTORING
|
---|
101 | else
|
---|
102 | MNT_RESTORING=""
|
---|
103 | fi
|
---|
104 |
|
---|
105 | echo "Now I'll use grub-install" >> $LOGFILE
|
---|
106 | chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
|
---|
107 | res=$?
|
---|
108 | echo "grub-install returned $res" >> $LOGFILE
|
---|
109 | [ "$res" -eq "0" ] && exit 0
|
---|
110 |
|
---|
111 | echo "Trying a hack" >> $LOGFILE
|
---|
112 | FindBootPart $1 $2
|
---|
113 | mbrpart=$1
|
---|
114 | if echo $bootpart | grep "/cciss/" > /dev/null ; then
|
---|
115 | partno=`basename $bootpart | cut -d'p' -f2`
|
---|
116 | else
|
---|
117 | partno=`basename $bootpart | sed s/[a-z]*//`
|
---|
118 | fi
|
---|
119 | if [ ! "$partno" ] ; then
|
---|
120 | partno=0
|
---|
121 | else
|
---|
122 | partno=$(($partno-1))
|
---|
123 | fi
|
---|
124 | if echo $bootpart | grep "/md" > /dev/null ; then
|
---|
125 | base=`basename $bootpart`
|
---|
126 | line=`grep $base /proc/mdstat | head -n1`
|
---|
127 | echo "mbrpart was $mbrpart"
|
---|
128 | mbrpart=`parted2fdisk -l | grep /dev/ | head -n1 | tr ':' ' ' \
|
---|
129 | | cut -d' ' -f2`
|
---|
130 | echo "mbrpart is $mbrpart"
|
---|
131 | partno="0"; # cheating - FIXME
|
---|
132 | fi
|
---|
133 | echo ".............Cool."
|
---|
134 |
|
---|
135 | grub=`FindPathOfRESTExe grub`
|
---|
136 | mkdir -p /boot
|
---|
137 | [ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
|
---|
138 |
|
---|
139 | # ---------------------------------
|
---|
140 |
|
---|
141 | for driveno in 0 1 2 ; do
|
---|
142 | try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
|
---|
143 | try_grub_Hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
|
---|
144 | done
|
---|
145 | res=$?
|
---|
146 | echo "Hack returned res=$res" >> $LOGFILE
|
---|
147 | [ "$res" -eq "0" ] && exit 0
|
---|
148 |
|
---|
149 | # ---------------------------------
|
---|
150 |
|
---|
151 | echo "Now I'll use grub-install.patched" >> $LOGFILE
|
---|
152 | cp -f `which grub-install.patched` /mnt/RESTORING/sbin
|
---|
153 | chroot /mnt/RESTORING grub-install.patched $1 >> $LOGFILE
|
---|
154 | res=$?
|
---|
155 | echo "grub-install.patched returned $res" >> $LOGFILE
|
---|
156 | [ "$res" -eq "0" ] && exit 0
|
---|
157 |
|
---|
158 | # ---------------------------------
|
---|
159 |
|
---|
160 | echo "Trying to use the existing grub.conf file in batch mode" >> $LOGFILE
|
---|
161 | chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/etc/grub.conf
|
---|
162 | res=$?
|
---|
163 | echo "Grub.conf approach returned $res" >> $LOGFILE
|
---|
164 | [ "$res" -eq "0" ] && exit 0
|
---|
165 |
|
---|
166 | #echo "Now I'll use grub-install" >> $LOGFILE
|
---|
167 | #chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
|
---|
168 | #res=$?
|
---|
169 | #echo "grub-install returned $res" >> $LOGFILE
|
---|
170 | #[ "$res" -eq "0" ] && exit 0
|
---|
171 |
|
---|
172 | echo "grub-MR returned res=$res" >> $LOGFILE
|
---|
173 |
|
---|
174 | exit $res
|
---|