[1] | 1 | #!/bin/sh
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | # grub-MR ------ a rudimentary replacement for grub-install
|
---|
| 5 | #
|
---|
| 6 | #
|
---|
| 7 | #
|
---|
[139] | 8 | # 2005/11/25
|
---|
| 9 | # - try_grub_hack : do not return success when grub wrote 'Error '
|
---|
| 10 | #
|
---|
[1] | 11 | # 2004/06/28
|
---|
| 12 | # - added support for /dev/md0==GRUB
|
---|
| 13 | #
|
---|
| 14 | # 2004/06/27
|
---|
| 15 | # - re-enable the call to grub-install and the 'echo' thing as a backup
|
---|
| 16 | # - hacked the hack :) to improve MDK9.2 support
|
---|
| 17 | # - added try_grub_hack()
|
---|
| 18 | #
|
---|
| 19 | # 2004/06/14
|
---|
| 20 | # - just use grub-install.patched; nothing else
|
---|
| 21 | #
|
---|
| 22 | # 2004/05/05
|
---|
| 23 | # - try EVERYTHING - grub-install, grub --batch, etc.
|
---|
| 24 | #
|
---|
| 25 | # 2004/04/18
|
---|
| 26 | # - try grub --batch < /etc/grub.conf first
|
---|
| 27 | # - if it fails, grub-install.patched instead
|
---|
| 28 | # - if _that_ fails, fail.
|
---|
| 29 | #
|
---|
| 30 | # 2004/04/15
|
---|
| 31 | # - added grub-install.patched to the mix
|
---|
| 32 | #
|
---|
| 33 | # 2004/04/01
|
---|
| 34 | # - use grub-install if there's an NTFS partition on the disk
|
---|
| 35 | #
|
---|
| 36 | # 2004/03/31
|
---|
| 37 | # - disabled direct call to grub
|
---|
| 38 | #
|
---|
| 39 | # 2004/03/28
|
---|
| 40 | # - call grub directly if possible
|
---|
| 41 | #
|
---|
| 42 | # 2004/03/26
|
---|
| 43 | # - find stage 1 if possible; more stable, reliable call to grub
|
---|
| 44 | #
|
---|
| 45 | # 2003/08/24
|
---|
| 46 | # - fixed line 26 (Christian)
|
---|
| 47 | #
|
---|
| 48 | # 2002/11/18
|
---|
| 49 | # - first incarnation
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | LOGFILE=/var/log/mondo-archive.log
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | Die() {
|
---|
| 56 | echo "$1" >> /dev/stderr
|
---|
| 57 | exit 1
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | FindBootPart() {
|
---|
| 62 | local i
|
---|
| 63 | bootpart=""
|
---|
| 64 | BOOTPATHNAME=""
|
---|
| 65 | for sz in /boot / ; do
|
---|
[273] | 66 | bootpart=`grep " $sz " $2 | cut -d' ' -f1 | head -n1`
|
---|
[1] | 67 | [ "$bootpart" ] && [ -e "/mnt/RESTORING/$bootpart" ] && break
|
---|
| 68 | done
|
---|
| 69 | [ ! "$bootpart" ] && Die "Cannot find boot partition in mountlist"
|
---|
| 70 |
|
---|
| 71 | if [ "$sz" = "/boot" ] ; then
|
---|
| 72 | BOOTPATHNAME=/grub
|
---|
| 73 | else
|
---|
| 74 | BOOTPATHNAME=/boot/grub
|
---|
| 75 | fi
|
---|
| 76 | if [ -e "$MNT_RESTORING/boot/grub/stage1" ] ; then
|
---|
| 77 | echo "Alright then."
|
---|
| 78 | return
|
---|
| 79 | fi
|
---|
| 80 |
|
---|
| 81 | cd $MNT_RESTORING
|
---|
| 82 | for i in usr/lib/grub/* ; do
|
---|
| 83 | echo "Checking $i"
|
---|
| 84 | if [ -e "$i/stage1" ] ; then
|
---|
| 85 | BOOTPATHNAME=/$i
|
---|
| 86 | mkdir -p /boot/grub
|
---|
| 87 | cp -au $i/* /boot/grub/
|
---|
| 88 | echo "BOOTPATHNAME is now $BOOTPATHNAME"
|
---|
| 89 | return 0
|
---|
| 90 | fi
|
---|
| 91 | done
|
---|
| 92 | cd /
|
---|
| 93 | echo "Cannot find BOOTPATHNAME"
|
---|
| 94 | return 1
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | FindPathOfRESTExe() {
|
---|
| 98 | local path
|
---|
| 99 | for path in /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin \
|
---|
| 100 | /bin /sbin ; do
|
---|
| 101 | if [ -f "/mnt/RESTORING/$path/$1" ] ; then
|
---|
| 102 | echo "/mnt/RESTORING/$path/$1"
|
---|
| 103 | return
|
---|
| 104 | fi
|
---|
| 105 | done
|
---|
| 106 | which grub
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | try_grub_hack() {
|
---|
| 111 | local driveno extraline partno
|
---|
| 112 | driveno=$1
|
---|
| 113 | partno=$2
|
---|
| 114 | extraline="$3"
|
---|
| 115 |
|
---|
| 116 | echo -en "\
|
---|
| 117 | device (hd$driveno) $mbrpart\n\
|
---|
| 118 | $extraline\n\
|
---|
| 119 | root (hd$driveno,$partno)\n\
|
---|
| 120 | setup (hd$driveno)\n\
|
---|
| 121 | quit \n" > /tmp/feed.txt
|
---|
| 122 | cat /tmp/feed.txt
|
---|
[139] | 123 | log_file=/tmp/grub.output
|
---|
[1] | 124 | if [ "$MNT_RESTORING" ] ; then
|
---|
[139] | 125 | chroot /mnt/RESTORING grub --batch < /tmp/feed.txt > $log_file
|
---|
[1] | 126 | else
|
---|
[139] | 127 | grub --batch < /tmp/feed.txt > $log_file
|
---|
[1] | 128 | fi
|
---|
[139] | 129 | cat $log_file
|
---|
| 130 | if grep "Error [0-9]*: " $log_file >/dev/null; then
|
---|
| 131 | return 1
|
---|
| 132 | else
|
---|
| 133 | return 0
|
---|
| 134 | fi
|
---|
[1] | 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | # ---------------------------------- main -----------------------------------
|
---|
| 139 |
|
---|
| 140 | if [ "$#" -ne "2" ] ; then
|
---|
| 141 | Die "grub-MR <MBR drive> <mountlist filename>; e.g. grub-MR /dev/hda /tmp/mountlist.txt"
|
---|
| 142 | fi
|
---|
| 143 | [ ! -f "$2" ] && Die "mountlist file not found"
|
---|
| 144 |
|
---|
| 145 | if [ -e "/mnt/RESTORING/boot" ] ; then
|
---|
| 146 | MNT_RESTORING=/mnt/RESTORING
|
---|
| 147 | else
|
---|
| 148 | MNT_RESTORING=""
|
---|
| 149 | fi
|
---|
| 150 |
|
---|
[375] | 151 | echo "Now I'll use grub-install" >> $LOGFILE
|
---|
| 152 | chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
|
---|
| 153 | res=$?
|
---|
| 154 | echo "grub-install returned $res" >> $LOGFILE
|
---|
| 155 | [ "$res" -eq "0" ] && exit 0
|
---|
| 156 |
|
---|
[1] | 157 | echo "Trying a hack" >> $LOGFILE
|
---|
| 158 | FindBootPart $1 $2
|
---|
| 159 | mbrpart=$1
|
---|
| 160 | if echo $bootpart | grep "/cciss/" > /dev/null ; then
|
---|
| 161 | partno=`basename $bootpart | cut -d'p' -f2`
|
---|
| 162 | else
|
---|
| 163 | partno=`basename $bootpart | sed s/[a-z]*//`
|
---|
| 164 | fi
|
---|
| 165 | if [ ! "$partno" ] ; then
|
---|
| 166 | partno=0
|
---|
| 167 | else
|
---|
| 168 | partno=$(($partno-1))
|
---|
| 169 | fi
|
---|
| 170 | if echo $bootpart | grep "/md" > /dev/null ; then
|
---|
| 171 | base=`basename $bootpart`
|
---|
[273] | 172 | line=`grep $base /proc/mdstat | head -n1`
|
---|
[1] | 173 | echo "mbrpart was $mbrpart"
|
---|
[196] | 174 | mbrpart=`parted2fdisk -l | grep /dev/ | head -n1 | tr ':' ' ' \
|
---|
[1] | 175 | | cut -d' ' -f2`
|
---|
| 176 | echo "mbrpart is $mbrpart"
|
---|
| 177 | partno="0"; # cheating - FIXME
|
---|
| 178 | fi
|
---|
| 179 | echo ".............Cool."
|
---|
| 180 |
|
---|
| 181 | grub=`FindPathOfRESTExe grub`
|
---|
| 182 | mkdir -p /boot
|
---|
| 183 | [ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
|
---|
| 184 |
|
---|
| 185 | # ---------------------------------
|
---|
| 186 |
|
---|
| 187 | for driveno in 0 1 2 ; do
|
---|
| 188 | try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
|
---|
| 189 | try_grub_Hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
|
---|
| 190 | done
|
---|
| 191 | res=$?
|
---|
| 192 | echo "Hack returned res=$res" >> $LOGFILE
|
---|
| 193 | [ "$res" -eq "0" ] && exit 0
|
---|
| 194 |
|
---|
| 195 | # ---------------------------------
|
---|
| 196 |
|
---|
| 197 | echo "Now I'll use grub-install.patched" >> $LOGFILE
|
---|
| 198 | cp -f `which grub-install.patched` /mnt/RESTORING/sbin
|
---|
| 199 | chroot /mnt/RESTORING grub-install.patched $1 >> $LOGFILE
|
---|
| 200 | res=$?
|
---|
| 201 | echo "grub-install.patched returned $res" >> $LOGFILE
|
---|
| 202 | [ "$res" -eq "0" ] && exit 0
|
---|
| 203 |
|
---|
| 204 | # ---------------------------------
|
---|
| 205 |
|
---|
| 206 | echo "Trying to use the existing grub.conf file in batch mode" >> $LOGFILE
|
---|
| 207 | chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/etc/grub.conf
|
---|
| 208 | res=$?
|
---|
| 209 | echo "Grub.conf approach returned $res" >> $LOGFILE
|
---|
| 210 | [ "$res" -eq "0" ] && exit 0
|
---|
| 211 |
|
---|
| 212 | #echo "Now I'll use grub-install" >> $LOGFILE
|
---|
| 213 | #chroot /mnt/RESTORING grub-install $1 >> $LOGFILE
|
---|
| 214 | #res=$?
|
---|
| 215 | #echo "grub-install returned $res" >> $LOGFILE
|
---|
| 216 | #[ "$res" -eq "0" ] && exit 0
|
---|
| 217 |
|
---|
| 218 | echo "grub-MR returned res=$res" >> $LOGFILE
|
---|
| 219 |
|
---|
| 220 | exit $res
|
---|