[1] | 1 | #!/bin/sh
|
---|
| 2 | #
|
---|
[1315] | 3 | # $Id: grub-MR 3047 2012-10-09 15:13:00Z bruno $
|
---|
[1] | 4 | #
|
---|
[1315] | 5 | # grub-MR ------ a rudimentary replacement for grub-install
|
---|
[1] | 6 | #
|
---|
[139] | 7 | #
|
---|
[1] | 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
|
---|
[2137] | 20 | bootpart=`grep " $sz " $2 | cut -d' ' -f1 | head -n1`
|
---|
| 21 | [ "$bootpart" ] && [ -e "/mnt/RESTORING/$bootpart" ] && break
|
---|
[1] | 22 | done
|
---|
| 23 | [ ! "$bootpart" ] && Die "Cannot find boot partition in mountlist"
|
---|
| 24 |
|
---|
| 25 | if [ "$sz" = "/boot" ] ; then
|
---|
[2137] | 26 | BOOTPATHNAME=/grub
|
---|
[1] | 27 | else
|
---|
[2137] | 28 | BOOTPATHNAME=/boot/grub
|
---|
[1] | 29 | fi
|
---|
| 30 | if [ -e "$MNT_RESTORING/boot/grub/stage1" ] ; then
|
---|
[2920] | 31 | echo "All right then."
|
---|
[2137] | 32 | return
|
---|
[1] | 33 | fi
|
---|
| 34 |
|
---|
| 35 | cd $MNT_RESTORING
|
---|
| 36 | for i in usr/lib/grub/* ; do
|
---|
[2137] | 37 | echo "Checking $i"
|
---|
[1] | 38 | if [ -e "$i/stage1" ] ; then
|
---|
[2137] | 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
|
---|
[1] | 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 "\
|
---|
[2615] | 71 | device (hd$driveno) $mbrdev\n\
|
---|
[1] | 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
|
---|
[139] | 77 | log_file=/tmp/grub.output
|
---|
[1] | 78 | if [ "$MNT_RESTORING" ] ; then
|
---|
[2615] | 79 | chroot $MNT_RESTORING grub --batch < /tmp/feed.txt > $log_file
|
---|
[1] | 80 | else
|
---|
[139] | 81 | grub --batch < /tmp/feed.txt > $log_file
|
---|
[1] | 82 | fi
|
---|
[139] | 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
|
---|
[1] | 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 |
|
---|
[2669] | 105 | # For some OpenSuSE
|
---|
| 106 | res=1
|
---|
[2615] | 107 | if [ "$MNT_RESTORING" ] ; then
|
---|
[2669] | 108 | if [ -x $MNT_RESTORING/usr/sbin/grub-install.unsupported ]; then
|
---|
[3047] | 109 | echo "Now I'll use OpenSuSE/SLES new grub-install in chroot" >> $LOGFILE
|
---|
| 110 | chroot $MNT_RESTORING /usr/sbin/grub-install >> $LOGFILE 2>> $LOGFILE
|
---|
[2669] | 111 | res=$?
|
---|
[3047] | 112 | echo "grub-install in chroot returned $res" >> $LOGFILE
|
---|
[2669] | 113 | fi
|
---|
[2615] | 114 | else
|
---|
[2669] | 115 | if [ -x /usr/sbin/grub-install.unsupported ]; then
|
---|
[3047] | 116 | echo "Now I'll use OpenSuSE/SLES new grub-install locally" >> $LOGFILE
|
---|
| 117 | /usr/sbin/grub-install >> $LOGFILE 2>> $LOGFILE
|
---|
[2669] | 118 | res=$?
|
---|
[3047] | 119 | echo "grub-install returned $res" >> $LOGFILE
|
---|
[2669] | 120 | fi
|
---|
[2615] | 121 | fi
|
---|
[375] | 122 | [ "$res" -eq "0" ] && exit 0
|
---|
| 123 |
|
---|
[2669] | 124 | echo "Now I'll use grub-install" >> $LOGFILE
|
---|
[2615] | 125 | if [ "$MNT_RESTORING" ] ; then
|
---|
[2996] | 126 | echo "Launching: chroot $MNT_RESTORING grub-install $1" >> $LOGFILE
|
---|
[2671] | 127 | chroot $MNT_RESTORING grub-install $1 >> $LOGFILE 2>> $LOGFILE
|
---|
[2615] | 128 | res=$?
|
---|
| 129 | else
|
---|
[2996] | 130 | echo "Launching: grub-install $1" >> $LOGFILE
|
---|
[2671] | 131 | grub-install $1 >> $LOGFILE 2>> $LOGFILE
|
---|
[2615] | 132 | res=$?
|
---|
| 133 | fi
|
---|
[2669] | 134 | echo "grub-install returned $res" >> $LOGFILE
|
---|
[2615] | 135 | [ "$res" -eq "0" ] && exit 0
|
---|
| 136 |
|
---|
[2944] | 137 |
|
---|
| 138 | echo "Now I'll use grub2-install" >> $LOGFILE
|
---|
| 139 | if [ "$MNT_RESTORING" ] ; then
|
---|
| 140 | chroot $MNT_RESTORING grub2-install $1 >> $LOGFILE 2>> $LOGFILE
|
---|
| 141 | res=$?
|
---|
| 142 | else
|
---|
| 143 | grub2-install $1 >> $LOGFILE 2>> $LOGFILE
|
---|
| 144 | res=$?
|
---|
| 145 | fi
|
---|
| 146 | echo "grub2-install returned $res" >> $LOGFILE
|
---|
| 147 | [ "$res" -eq "0" ] && exit 0
|
---|
| 148 |
|
---|
[1] | 149 | echo "Trying a hack" >> $LOGFILE
|
---|
[2615] | 150 | FindBootPart $1 $2 2>&1 | tee -a $LOGFILE
|
---|
[2619] | 151 | mbrdev=`echo $1 | sed 's/\([^0-9]*\)[0-9]*$/\1/'`
|
---|
[2920] | 152 | if echo $mbrdev | grep "/cciss/" > /dev/null ; then
|
---|
| 153 | partno=`basename $mbrdev | cut -d'p' -f2`
|
---|
[2619] | 154 | mbrdev=`echo $mbrdev | cut -d'p' -f1`
|
---|
[2920] | 155 | elif echo $mbrdev | grep "/mapper/mpath" > /dev/null ; then
|
---|
| 156 | partno=`basename $mbrdev | cut -d'p' -f3`
|
---|
| 157 | mbrdev=`echo $mbrdev | cut -d'p' -f1-4`
|
---|
[1] | 158 | else
|
---|
[2920] | 159 | partno=`basename $mbrdev | sed 's/[a-z]*//'`
|
---|
[1] | 160 | fi
|
---|
| 161 | if [ ! "$partno" ] ; then
|
---|
| 162 | partno=0
|
---|
| 163 | else
|
---|
| 164 | partno=$(($partno-1))
|
---|
| 165 | fi
|
---|
[2920] | 166 | if echo $mbrdev | grep "/md" > /dev/null ; then
|
---|
[2615] | 167 | # FIXME: Why this if not used later
|
---|
[1] | 168 | base=`basename $bootpart`
|
---|
[273] | 169 | line=`grep $base /proc/mdstat | head -n1`
|
---|
[2615] | 170 | echo "mbrdev was $mbrdev" 2>&1 | tee -a $LOGFILE
|
---|
| 171 | mbrdev=`parted2fdisk -l | grep /dev/ | head -n1 | tr ':' ' ' | cut -d' ' -f2`
|
---|
| 172 | echo "mbrdev is $mbrdev" 2>&1 | tee -a $LOGFILE
|
---|
[1] | 173 | partno="0"; # cheating - FIXME
|
---|
| 174 | fi
|
---|
[2615] | 175 | echo ".............Cool." 2>&1 | tee -a $LOGFILE
|
---|
[1] | 176 |
|
---|
| 177 | grub=`FindPathOfRESTExe grub`
|
---|
| 178 | mkdir -p /boot
|
---|
| 179 | [ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
|
---|
| 180 |
|
---|
| 181 | # ---------------------------------
|
---|
| 182 |
|
---|
| 183 | for driveno in 0 1 2 ; do
|
---|
| 184 | try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
|
---|
[2619] | 185 | try_grub_hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
|
---|
[1] | 186 | done
|
---|
| 187 | res=$?
|
---|
| 188 | echo "Hack returned res=$res" >> $LOGFILE
|
---|
| 189 | [ "$res" -eq "0" ] && exit 0
|
---|
| 190 |
|
---|
| 191 | # ---------------------------------
|
---|
| 192 |
|
---|
| 193 | echo "Now I'll use grub-install.patched" >> $LOGFILE
|
---|
| 194 | cp -f `which grub-install.patched` /mnt/RESTORING/sbin
|
---|
[2671] | 195 | chroot /mnt/RESTORING grub-install.patched $1 >> $LOGFILE 2>> $LOGFILE
|
---|
[1] | 196 | res=$?
|
---|
| 197 | echo "grub-install.patched returned $res" >> $LOGFILE
|
---|
| 198 | [ "$res" -eq "0" ] && exit 0
|
---|
| 199 |
|
---|
| 200 | # ---------------------------------
|
---|
| 201 |
|
---|
[2449] | 202 | if [ -f "/mnt/RESTORING/boot/grub/menu.lst" ]; then
|
---|
[2619] | 203 | grep -vE '^#' /boot/grub/menu.lst > /mnt/RESTORING/tmp/grub.conf
|
---|
[2449] | 204 | elif [ -f "/mnt/RESTORING/boot/grub/grub.cfg" ]; then
|
---|
[2619] | 205 | grep -vE '^#' /boot/grub/grub.cfg > /mnt/RESTORING/tmp/grub.conf
|
---|
[2956] | 206 | elif [ -f "/mnt/RESTORING/boot/grub2/grub.cfg" ]; then
|
---|
| 207 | grep -vE '^#' /boot/grub2/grub.cfg > /mnt/RESTORING/tmp/grub.conf
|
---|
[2449] | 208 | else
|
---|
[2619] | 209 | echo "Unable to find Grub conf file" | tee -a $LOGFILE
|
---|
[2449] | 210 | exit -1
|
---|
| 211 | fi
|
---|
[2619] | 212 | echo "Trying to use the existing grub conf file in batch mode" >> $LOGFILE
|
---|
| 213 | cat /mnt/RESTORING/tmp/grub.conf >> $LOGFILE
|
---|
| 214 | chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/tmp/grub.conf
|
---|
[1] | 215 | res=$?
|
---|
[2619] | 216 | echo "grub conf file approach returned $res" >> $LOGFILE
|
---|
| 217 | rm -f /mnt/RESTORING/tmp/grub.conf
|
---|
[1] | 218 | [ "$res" -eq "0" ] && exit 0
|
---|
| 219 |
|
---|
| 220 | echo "grub-MR returned res=$res" >> $LOGFILE
|
---|
| 221 |
|
---|
| 222 | exit $res
|
---|