[1] | 1 | #!/bin/sh
|
---|
| 2 | #
|
---|
[1315] | 3 | # $Id: mr-grub 3698 2018-03-27 22:41:36Z 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 |
|
---|
[3698] | 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"
|
---|
| 144 | echo "-------------------"
|
---|
| 145 | if [ -f "$devfile" ]; then
|
---|
| 146 | cat $devfile
|
---|
| 147 | else
|
---|
| 148 | echo "No file $devfile"
|
---|
| 149 | fi
|
---|
[2944] | 150 |
|
---|
| 151 | echo "Now I'll use grub2-install" >> $LOGFILE
|
---|
| 152 | if [ "$MNT_RESTORING" ] ; then
|
---|
[3683] | 153 | chroot $MNT_RESTORING grub2-install --force $1 >> $LOGFILE 2>> $LOGFILE
|
---|
[2944] | 154 | res=$?
|
---|
| 155 | else
|
---|
[3683] | 156 | grub2-install --force $1 >> $LOGFILE 2>> $LOGFILE
|
---|
[2944] | 157 | res=$?
|
---|
| 158 | fi
|
---|
| 159 | echo "grub2-install returned $res" >> $LOGFILE
|
---|
| 160 | [ "$res" -eq "0" ] && exit 0
|
---|
| 161 |
|
---|
[1] | 162 | echo "Trying a hack" >> $LOGFILE
|
---|
[2615] | 163 | FindBootPart $1 $2 2>&1 | tee -a $LOGFILE
|
---|
[2619] | 164 | mbrdev=`echo $1 | sed 's/\([^0-9]*\)[0-9]*$/\1/'`
|
---|
[2920] | 165 | if echo $mbrdev | grep "/cciss/" > /dev/null ; then
|
---|
| 166 | partno=`basename $mbrdev | cut -d'p' -f2`
|
---|
[2619] | 167 | mbrdev=`echo $mbrdev | cut -d'p' -f1`
|
---|
[2920] | 168 | elif echo $mbrdev | grep "/mapper/mpath" > /dev/null ; then
|
---|
| 169 | partno=`basename $mbrdev | cut -d'p' -f3`
|
---|
| 170 | mbrdev=`echo $mbrdev | cut -d'p' -f1-4`
|
---|
[1] | 171 | else
|
---|
[2920] | 172 | partno=`basename $mbrdev | sed 's/[a-z]*//'`
|
---|
[1] | 173 | fi
|
---|
| 174 | if [ ! "$partno" ] ; then
|
---|
| 175 | partno=0
|
---|
| 176 | else
|
---|
| 177 | partno=$(($partno-1))
|
---|
| 178 | fi
|
---|
[2920] | 179 | if echo $mbrdev | grep "/md" > /dev/null ; then
|
---|
[2615] | 180 | # FIXME: Why this if not used later
|
---|
[1] | 181 | base=`basename $bootpart`
|
---|
[273] | 182 | line=`grep $base /proc/mdstat | head -n1`
|
---|
[2615] | 183 | echo "mbrdev was $mbrdev" 2>&1 | tee -a $LOGFILE
|
---|
[3377] | 184 | mbrdev=`mr-parted2fdisk -l 2>/dev/null | grep /dev/ | head -n1 | tr ':' ' ' | cut -d' ' -f2`
|
---|
[2615] | 185 | echo "mbrdev is $mbrdev" 2>&1 | tee -a $LOGFILE
|
---|
[1] | 186 | partno="0"; # cheating - FIXME
|
---|
| 187 | fi
|
---|
[2615] | 188 | echo ".............Cool." 2>&1 | tee -a $LOGFILE
|
---|
[1] | 189 |
|
---|
[3645] | 190 | grub=`FindPathOfRESTExe grub | head -1`
|
---|
[1] | 191 | mkdir -p /boot
|
---|
| 192 | [ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
|
---|
[3645] | 193 | [ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub2 /boot/grub2
|
---|
[1] | 194 |
|
---|
| 195 | # ---------------------------------
|
---|
| 196 |
|
---|
| 197 | for driveno in 0 1 2 ; do
|
---|
| 198 | try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
|
---|
[2619] | 199 | try_grub_hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
|
---|
[1] | 200 | done
|
---|
| 201 | res=$?
|
---|
| 202 | echo "Hack returned res=$res" >> $LOGFILE
|
---|
| 203 | [ "$res" -eq "0" ] && exit 0
|
---|
| 204 |
|
---|
| 205 | # ---------------------------------
|
---|
| 206 |
|
---|
[3542] | 207 | echo "Now I'll use mr-grub-install.patched" >> $LOGFILE
|
---|
| 208 | cp -f `which mr-grub-install.patched` /mnt/RESTORING/sbin
|
---|
| 209 | chroot /mnt/RESTORING mr-grub-install.patched $1 >> $LOGFILE 2>> $LOGFILE
|
---|
[1] | 210 | res=$?
|
---|
[3542] | 211 | echo "mr-grub-install.patched returned $res" >> $LOGFILE
|
---|
[1] | 212 | [ "$res" -eq "0" ] && exit 0
|
---|
| 213 |
|
---|
| 214 | # ---------------------------------
|
---|
| 215 |
|
---|
[2449] | 216 | if [ -f "/mnt/RESTORING/boot/grub/menu.lst" ]; then
|
---|
[2619] | 217 | grep -vE '^#' /boot/grub/menu.lst > /mnt/RESTORING/tmp/grub.conf
|
---|
[2449] | 218 | elif [ -f "/mnt/RESTORING/boot/grub/grub.cfg" ]; then
|
---|
[2619] | 219 | grep -vE '^#' /boot/grub/grub.cfg > /mnt/RESTORING/tmp/grub.conf
|
---|
[2956] | 220 | elif [ -f "/mnt/RESTORING/boot/grub2/grub.cfg" ]; then
|
---|
| 221 | grep -vE '^#' /boot/grub2/grub.cfg > /mnt/RESTORING/tmp/grub.conf
|
---|
[3378] | 222 | elif [ -d "/mnt/RESTORING/boot/efi" ]; then
|
---|
| 223 | grep -vE '^#' `find /boot/efi -name grub.conf` > /mnt/RESTORING/tmp/grub.conf
|
---|
[2449] | 224 | else
|
---|
[2619] | 225 | echo "Unable to find Grub conf file" | tee -a $LOGFILE
|
---|
[2449] | 226 | exit -1
|
---|
| 227 | fi
|
---|
[2619] | 228 | echo "Trying to use the existing grub conf file in batch mode" >> $LOGFILE
|
---|
| 229 | cat /mnt/RESTORING/tmp/grub.conf >> $LOGFILE
|
---|
| 230 | chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/tmp/grub.conf
|
---|
[1] | 231 | res=$?
|
---|
[2619] | 232 | echo "grub conf file approach returned $res" >> $LOGFILE
|
---|
| 233 | rm -f /mnt/RESTORING/tmp/grub.conf
|
---|
[1] | 234 | [ "$res" -eq "0" ] && exit 0
|
---|
| 235 |
|
---|
[3542] | 236 | echo "mr-grub returned res=$res" >> $LOGFILE
|
---|
[1] | 237 |
|
---|
| 238 | exit $res
|
---|