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