1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # $Id: mr-grub 3733 2019-11-18 00:44:18Z bruno $
|
---|
4 | #
|
---|
5 | # mr-grub ------ 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 | # Work only for grub v1
|
---|
17 | local i
|
---|
18 | bootpart=""
|
---|
19 | BOOTPATHNAME=""
|
---|
20 | for sz in /boot / ; do
|
---|
21 | bootpart=`grep " $sz " $2 | cut -d' ' -f1 | head -n1`
|
---|
22 | [ "$bootpart" ] && [ -e "/mnt/RESTORING/$bootpart" ] && break
|
---|
23 | done
|
---|
24 | [ ! "$bootpart" ] && Die "Cannot find boot partition in mountlist"
|
---|
25 |
|
---|
26 | if [ "$sz" = "/boot" ] ; then
|
---|
27 | BOOTPATHNAME=/grub
|
---|
28 | else
|
---|
29 | BOOTPATHNAME=/boot/grub
|
---|
30 | fi
|
---|
31 | if [ -e "$MNT_RESTORING/boot/grub/stage1" ] ; then
|
---|
32 | echo "All right then."
|
---|
33 | return
|
---|
34 | fi
|
---|
35 |
|
---|
36 | cd $MNT_RESTORING
|
---|
37 | for i in usr/lib/grub/* ; do
|
---|
38 | echo "Checking $i"
|
---|
39 | if [ -e "$i/stage1" ] ; then
|
---|
40 | BOOTPATHNAME=/$i
|
---|
41 | mkdir -p /boot/grub
|
---|
42 | cp -au $i/* /boot/grub/
|
---|
43 | echo "BOOTPATHNAME is now $BOOTPATHNAME"
|
---|
44 | return 0
|
---|
45 | fi
|
---|
46 | done
|
---|
47 | cd /
|
---|
48 | echo "Cannot find BOOTPATHNAME"
|
---|
49 | return 1
|
---|
50 | }
|
---|
51 |
|
---|
52 | FindPathOfRESTExe() {
|
---|
53 | local path
|
---|
54 | for path in /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin \
|
---|
55 | /bin /sbin ; do
|
---|
56 | if [ -f "/mnt/RESTORING/$path/$1" ] ; then
|
---|
57 | echo "/mnt/RESTORING/$path/$1"
|
---|
58 | return
|
---|
59 | fi
|
---|
60 | done
|
---|
61 | which grub
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | try_grub_hack() {
|
---|
66 | local driveno extraline partno
|
---|
67 | driveno=$1
|
---|
68 | partno=$2
|
---|
69 | extraline="$3"
|
---|
70 |
|
---|
71 | echo -en "\
|
---|
72 | device (hd$driveno) $mbrdev\n\
|
---|
73 | $extraline\n\
|
---|
74 | root (hd$driveno,$partno)\n\
|
---|
75 | setup (hd$driveno)\n\
|
---|
76 | quit \n" > /tmp/feed.txt
|
---|
77 | cat /tmp/feed.txt
|
---|
78 | log_file=/tmp/grub.output
|
---|
79 | if [ "$MNT_RESTORING" ] ; then
|
---|
80 | chroot $MNT_RESTORING grub --batch < /tmp/feed.txt > $log_file
|
---|
81 | else
|
---|
82 | grub --batch < /tmp/feed.txt > $log_file
|
---|
83 | fi
|
---|
84 | cat $log_file
|
---|
85 | if grep "Error [0-9]*: " $log_file >/dev/null; then
|
---|
86 | return 1
|
---|
87 | else
|
---|
88 | return 0
|
---|
89 | fi
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | # ---------------------------------- main -----------------------------------
|
---|
94 |
|
---|
95 | if [ "$#" -ne "2" ] ; then
|
---|
96 | Die "mr-grub <boot drive> <mountlist filename>; e.g. mr-grub /dev/hda /tmp/mountlist.txt"
|
---|
97 | fi
|
---|
98 | [ ! -f "$2" ] && Die "mountlist file not found"
|
---|
99 |
|
---|
100 | if [ -e "/mnt/RESTORING/boot" ] ; then
|
---|
101 | MNT_RESTORING=/mnt/RESTORING
|
---|
102 | else
|
---|
103 | MNT_RESTORING=""
|
---|
104 | fi
|
---|
105 |
|
---|
106 | # For some OpenSuSE
|
---|
107 | res=1
|
---|
108 | if [ "$MNT_RESTORING" ] ; then
|
---|
109 | if [ -x $MNT_RESTORING/usr/sbin/grub-install.unsupported ]; then
|
---|
110 | echo "Now I'll use OpenSuSE/SLES new grub-install in chroot" >> $LOGFILE
|
---|
111 | chroot $MNT_RESTORING /usr/sbin/grub-install.unsupported >> $LOGFILE 2>> $LOGFILE
|
---|
112 | res=$?
|
---|
113 | echo "grub-install.unsupported in chroot returned $res" >> $LOGFILE
|
---|
114 | fi
|
---|
115 | else
|
---|
116 | if [ -x /usr/sbin/grub-install.unsupported ]; then
|
---|
117 | echo "Now I'll use OpenSuSE/SLES new grub-install locally" >> $LOGFILE
|
---|
118 | /usr/sbin/grub-install.unsupported >> $LOGFILE 2>> $LOGFILE
|
---|
119 | res=$?
|
---|
120 | echo "grub-install.unsupported returned $res" >> $LOGFILE
|
---|
121 | fi
|
---|
122 | fi
|
---|
123 | [ "$res" -eq "0" ] && exit 0
|
---|
124 |
|
---|
125 | echo "Now I'll use grub-install" >> $LOGFILE
|
---|
126 | if [ "$MNT_RESTORING" ] ; then
|
---|
127 | echo "Launching: chroot $MNT_RESTORING grub-install $1" >> $LOGFILE
|
---|
128 | chroot $MNT_RESTORING grub-install $1 >> $LOGFILE 2>> $LOGFILE
|
---|
129 | res=$?
|
---|
130 | else
|
---|
131 | echo "Launching: grub-install $1" >> $LOGFILE
|
---|
132 | grub-install $1 >> $LOGFILE 2>> $LOGFILE
|
---|
133 | res=$?
|
---|
134 | fi
|
---|
135 | echo "grub-install returned $res" >> $LOGFILE
|
---|
136 | [ "$res" -eq "0" ] && exit 0
|
---|
137 |
|
---|
138 | if [ "$MNT_RESTORING" ] ; then
|
---|
139 | devfile="$MNT_RESTORING/boot/grub2/device.map"
|
---|
140 | else
|
---|
141 | devfile="/boot/grub2/device.map"
|
---|
142 | fi
|
---|
143 | echo "Content of $devfile" >> $LOGFILE
|
---|
144 | echo "-------------------" >> $LOGFILE
|
---|
145 | if [ -f "$devfile" ]; then
|
---|
146 | cat $devfile >> $LOGFILE
|
---|
147 | else
|
---|
148 | echo "No file $devfile" >> $LOGFILE
|
---|
149 | fi
|
---|
150 |
|
---|
151 | echo "Now I'll use grub2-install" >> $LOGFILE
|
---|
152 | if [ "$MNT_RESTORING" ] ; then
|
---|
153 | echo "Launching: chroot $MNT_RESTORING grub2-install --force $1" >> $LOGFILE
|
---|
154 | chroot $MNT_RESTORING grub2-install --force $1 >> $LOGFILE 2>> $LOGFILE
|
---|
155 | res=$?
|
---|
156 | else
|
---|
157 | echo "Launching: grub2-install --force $1" >> $LOGFILE
|
---|
158 | grub2-install --force $1 >> $LOGFILE 2>> $LOGFILE
|
---|
159 | res=$?
|
---|
160 | fi
|
---|
161 | echo "grub2-install returned $res" >> $LOGFILE
|
---|
162 | [ "$res" -eq "0" ] && exit 0
|
---|
163 |
|
---|
164 | echo "Trying a hack" >> $LOGFILE
|
---|
165 | FindBootPart $1 $2 2>&1 | tee -a $LOGFILE
|
---|
166 | mbrdev=`echo $1 | sed 's/\([^0-9]*\)[0-9]*$/\1/'`
|
---|
167 | if echo $mbrdev | grep "/cciss/" > /dev/null ; then
|
---|
168 | partno=`basename $mbrdev | cut -d'p' -f2`
|
---|
169 | mbrdev=`echo $mbrdev | cut -d'p' -f1`
|
---|
170 | elif echo $mbrdev | grep "/mapper/mpath" > /dev/null ; then
|
---|
171 | partno=`basename $mbrdev | cut -d'p' -f3`
|
---|
172 | mbrdev=`echo $mbrdev | cut -d'p' -f1-4`
|
---|
173 | else
|
---|
174 | partno=`basename $mbrdev | sed 's/[a-z]*//'`
|
---|
175 | fi
|
---|
176 | if [ ! "$partno" ] ; then
|
---|
177 | partno=0
|
---|
178 | else
|
---|
179 | partno=$(($partno-1))
|
---|
180 | fi
|
---|
181 | if echo $mbrdev | grep "/md" > /dev/null ; then
|
---|
182 | # FIXME: Why this if not used later
|
---|
183 | base=`basename $bootpart`
|
---|
184 | line=`grep $base /proc/mdstat | head -n1`
|
---|
185 | echo "mbrdev was $mbrdev" 2>&1 | tee -a $LOGFILE
|
---|
186 | mbrdev=`mr-parted2fdisk -l 2>/dev/null | grep /dev/ | head -n1 | tr ':' ' ' | cut -d' ' -f2`
|
---|
187 | echo "mbrdev is $mbrdev" 2>&1 | tee -a $LOGFILE
|
---|
188 | partno="0"; # cheating - FIXME
|
---|
189 | fi
|
---|
190 | echo ".............Cool." 2>&1 | tee -a $LOGFILE
|
---|
191 |
|
---|
192 | grub=`FindPathOfRESTExe grub | head -1`
|
---|
193 | mkdir -p /boot
|
---|
194 | [ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
|
---|
195 | [ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub2 /boot/grub2
|
---|
196 |
|
---|
197 | # ---------------------------------
|
---|
198 |
|
---|
199 | for driveno in 0 1 2 ; do
|
---|
200 | try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
|
---|
201 | try_grub_hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
|
---|
202 | done
|
---|
203 | res=$?
|
---|
204 | echo "Hack returned res=$res" >> $LOGFILE
|
---|
205 | [ "$res" -eq "0" ] && exit 0
|
---|
206 |
|
---|
207 | # ---------------------------------
|
---|
208 |
|
---|
209 | echo "Now I'll use mr-grub-install.patched" >> $LOGFILE
|
---|
210 | cp -f `which mr-grub-install.patched` /mnt/RESTORING/sbin
|
---|
211 | chroot /mnt/RESTORING mr-grub-install.patched $1 >> $LOGFILE 2>> $LOGFILE
|
---|
212 | res=$?
|
---|
213 | echo "mr-grub-install.patched returned $res" >> $LOGFILE
|
---|
214 | [ "$res" -eq "0" ] && exit 0
|
---|
215 |
|
---|
216 | # ---------------------------------
|
---|
217 |
|
---|
218 | if [ -f "/mnt/RESTORING/boot/grub/menu.lst" ]; then
|
---|
219 | grep -vE '^#' /boot/grub/menu.lst > /mnt/RESTORING/tmp/grub.conf
|
---|
220 | elif [ -f "/mnt/RESTORING/boot/grub/grub.cfg" ]; then
|
---|
221 | grep -vE '^#' /boot/grub/grub.cfg > /mnt/RESTORING/tmp/grub.conf
|
---|
222 | elif [ -f "/mnt/RESTORING/boot/grub2/grub.cfg" ]; then
|
---|
223 | grep -vE '^#' /boot/grub2/grub.cfg > /mnt/RESTORING/tmp/grub.conf
|
---|
224 | elif [ -d "/mnt/RESTORING/boot/efi" ]; then
|
---|
225 | grep -vE '^#' `find /boot/efi -name grub.conf` > /mnt/RESTORING/tmp/grub.conf
|
---|
226 | else
|
---|
227 | echo "Unable to find Grub conf file" | tee -a $LOGFILE
|
---|
228 | exit -1
|
---|
229 | fi
|
---|
230 | echo "Trying to use the existing grub conf file in batch mode" >> $LOGFILE
|
---|
231 | cat /mnt/RESTORING/tmp/grub.conf >> $LOGFILE
|
---|
232 | chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/tmp/grub.conf
|
---|
233 | res=$?
|
---|
234 | echo "grub conf file approach returned $res" >> $LOGFILE
|
---|
235 | rm -f /mnt/RESTORING/tmp/grub.conf
|
---|
236 | [ "$res" -eq "0" ] && exit 0
|
---|
237 |
|
---|
238 | echo "mr-grub returned res=$res" >> $LOGFILE
|
---|
239 |
|
---|
240 | exit $res
|
---|