source: branches/3.3/mondo/src/restore-scripts/mondo/mr-grub @ 3698

Last change on this file since 3698 was 3698, checked in by Bruno Cornec, 2 years ago

Improve logging of device.map file for grub2

  • Property svn:keywords set to Id
  • Property svn:unix-mode set to 755
File size: 6.4 KB
Line 
1#!/bin/sh
2#
3# $Id: mr-grub 3698 2018-03-27 22:41:36Z bruno $
4#
5# mr-grub ------ a rudimentary replacement for grub-install
6#
7#
8
9Die() {
10    echo "$1" >> /dev/stderr
11    exit 1
12}
13
14
15FindBootPart() {
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
52FindPathOfRESTExe() {
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
65try_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
95if [ "$#" -ne "2" ] ; then
96    Die "mr-grub <boot drive> <mountlist filename>; e.g. mr-grub /dev/hda /tmp/mountlist.txt"
97fi
98[ ! -f "$2" ] && Die "mountlist file not found"
99
100if [ -e "/mnt/RESTORING/boot" ] ; then
101    MNT_RESTORING=/mnt/RESTORING
102else
103    MNT_RESTORING=""
104fi
105
106# For some OpenSuSE
107res=1
108if [ "$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 >> $LOGFILE 2>> $LOGFILE
112        res=$?
113        echo "grub-install in chroot returned $res" >> $LOGFILE
114    fi
115else
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 >> $LOGFILE 2>> $LOGFILE
119        res=$?
120        echo "grub-install returned $res" >> $LOGFILE
121    fi
122fi
123[ "$res" -eq "0" ] && exit 0
124
125echo "Now I'll use grub-install" >> $LOGFILE
126if [ "$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=$?
130else
131    echo "Launching: grub-install $1" >> $LOGFILE
132    grub-install $1 >> $LOGFILE 2>> $LOGFILE
133    res=$?
134fi
135echo "grub-install returned $res" >> $LOGFILE
136[ "$res" -eq "0" ] && exit 0
137
138if [ "$MNT_RESTORING" ] ; then
139    devfile="$MNT_RESTORING/boot/grub2/device.map"
140else
141    devfile="/boot/grub2/device.map"
142fi
143echo "Content of $devfile"
144echo "-------------------"
145if [ -f "$devfile" ]; then
146        cat $devfile
147else
148        echo "No file $devfile"
149fi
150
151echo "Now I'll use grub2-install" >> $LOGFILE
152if [ "$MNT_RESTORING" ] ; then
153    chroot $MNT_RESTORING grub2-install --force $1 >> $LOGFILE 2>> $LOGFILE
154    res=$?
155else
156    grub2-install --force $1 >> $LOGFILE 2>> $LOGFILE
157    res=$?
158fi
159echo "grub2-install returned $res" >> $LOGFILE
160[ "$res" -eq "0" ] && exit 0
161
162echo "Trying a hack" >> $LOGFILE
163FindBootPart $1 $2 2>&1 | tee -a $LOGFILE
164mbrdev=`echo $1 | sed 's/\([^0-9]*\)[0-9]*$/\1/'`
165if echo $mbrdev | grep "/cciss/" > /dev/null ; then
166    partno=`basename $mbrdev | cut -d'p' -f2`
167    mbrdev=`echo $mbrdev | cut -d'p' -f1`
168elif 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`
171else
172    partno=`basename $mbrdev | sed 's/[a-z]*//'`
173fi
174if [ ! "$partno" ] ; then
175    partno=0
176else
177    partno=$(($partno-1))
178fi
179if echo $mbrdev | grep "/md" > /dev/null ; then
180    # FIXME: Why this if not used later
181    base=`basename $bootpart`
182    line=`grep $base /proc/mdstat | head -n1`
183    echo "mbrdev was $mbrdev" 2>&1 | tee -a $LOGFILE
184    mbrdev=`mr-parted2fdisk -l 2>/dev/null | grep /dev/ | head -n1 | tr ':' ' ' | cut -d' ' -f2`
185    echo "mbrdev is $mbrdev" 2>&1 | tee -a $LOGFILE
186    partno="0"; # cheating - FIXME   
187fi
188echo ".............Cool." 2>&1 | tee -a $LOGFILE
189
190grub=`FindPathOfRESTExe grub | head -1`
191mkdir -p /boot
192[ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub /boot/grub
193[ "$MNT_RESTORING" ] && ln -sf /mnt/RESTORING/boot/grub2 /boot/grub2
194
195# ---------------------------------
196
197for driveno in 0 1 2 ; do
198    try_grub_hack $driveno $partno "" >> $LOGFILE 2>> $LOGFILE && break
199    try_grub_hack $driveno $partno "find $BOOTPATHNAME/stage1" >> $LOGFILE 2>> $LOGFILE && break
200done
201res=$?
202echo "Hack returned res=$res" >> $LOGFILE
203[ "$res" -eq "0" ] && exit 0
204
205# ---------------------------------
206
207echo "Now I'll use mr-grub-install.patched" >> $LOGFILE
208cp -f `which mr-grub-install.patched` /mnt/RESTORING/sbin
209chroot /mnt/RESTORING mr-grub-install.patched $1 >> $LOGFILE 2>> $LOGFILE
210res=$?
211echo "mr-grub-install.patched returned $res" >> $LOGFILE
212[ "$res" -eq "0" ] && exit 0
213
214# ---------------------------------
215
216if [ -f "/mnt/RESTORING/boot/grub/menu.lst" ]; then
217    grep -vE '^#' /boot/grub/menu.lst > /mnt/RESTORING/tmp/grub.conf
218elif [ -f "/mnt/RESTORING/boot/grub/grub.cfg" ]; then
219    grep -vE '^#' /boot/grub/grub.cfg > /mnt/RESTORING/tmp/grub.conf
220elif [ -f "/mnt/RESTORING/boot/grub2/grub.cfg" ]; then
221    grep -vE '^#' /boot/grub2/grub.cfg > /mnt/RESTORING/tmp/grub.conf
222elif [ -d "/mnt/RESTORING/boot/efi" ]; then
223    grep -vE '^#' `find /boot/efi -name grub.conf` > /mnt/RESTORING/tmp/grub.conf
224else
225    echo "Unable to find Grub conf file" | tee -a $LOGFILE
226    exit -1
227fi
228echo "Trying to use the existing grub conf file in batch mode" >> $LOGFILE
229cat /mnt/RESTORING/tmp/grub.conf >> $LOGFILE
230chroot /mnt/RESTORING grub --batch < /mnt/RESTORING/tmp/grub.conf
231res=$?
232echo "grub conf file approach returned $res" >> $LOGFILE
233rm -f /mnt/RESTORING/tmp/grub.conf
234[ "$res" -eq "0" ] && exit 0
235
236echo "mr-grub returned res=$res" >> $LOGFILE
237
238exit $res
Note: See TracBrowser for help on using the repository browser.