source: branches/3.0/mondo/src/restore-scripts/mondo/stabgrub-me @ 2944

Last change on this file since 2944 was 2944, checked in by Bruno Cornec, 9 years ago
  • Adds grub2 support (Michael Caerwyn mcaerwyn_at_gmail.com)
  • Property svn:keywords set to Id
  • Property svn:unix-mode set to 755
File size: 5.9 KB
Line 
1#!/bin/sh
2#
3# $Id: stabgrub-me 2944 2012-02-03 01:12:47Z bruno $
4#
5#####################################################################
6
7
8QuitIfNotFound() {
9    if [ ! -f "$1" ] ; then
10        LogIt "(stabgrub-me) Where's $1? I cannot continue." 1
11        exit 1
12    fi
13}
14
15
16
17
18
19LocateOldFstab() {
20    old_fstab=""
21    if [ -f "/mnt/RESTORING/etc/fstab" ] ; then
22        LogIt "No need for fstab search." 2
23#        fstab_list=/mnt/RESTORING/etc/fstab
24        old_fstab=/mnt/RESTORING/etc/fstab
25        return 0
26    elif [ -f "/mnt/cdrom/archives/CUCKOO" ] ; then
27        LogIt "Because I'm cuckoo, I'll stop searching." 2
28        return 1
29    else
30        LogIt "Looking for fstab. Please wait." 2
31        fstab_list=`find /mnt/RESTORING -name fstab 2> /dev/null`
32    fi
33    where_they_live=""
34    for curr_fstab in $fstab_list ; do
35        curr_dir=`echo $curr_fstab | gawk '{i=index($0,"/fstab");print substr($0,0,i-1);}'`
36        resA=$?
37        curr_inetd=`find $curr_dir -name inetd.conf | grep -v "linuxconf"`
38        resB=$?
39        if [ "$resA" -eq "0" ] ; then
40            if [ "$where_they_live" != "" ] ; then
41                LogIt "Two directories found! One is $where_they_live, the other $curr_dir" 1
42                LogIt "I don't know which to choose. I'll abort the search." 1
43                return 1
44            fi
45            where_they_live=$curr_dir
46        fi
47    done
48    if [ "$where_they_live" = "" ] ; then
49        LogIt "Cannot find any folder which holds fstab _and_ inetd.conf" 1
50        return 1
51    fi
52    old_fstab=$where_they_live/fstab
53    LogIt "fstab found." 2
54    return 0
55}
56
57LocateOldGrub() {
58    old_grubconf=""
59    if [ -f "/mnt/RESTORING/boot/grub/menu.lst" ] || [ -f "/mnt/RESTORING/boot/grub/grub.cfg" ] || [ -f "/mnt/RESTORING/boot/grub2/grub.cfg" ] ; then
60        LogIt "No need for menu.lst/grub.cfg search." 2
61        if [ -f "/mnt/RESTORING/boot/grub/menu.lst" ]; then
62            old_grubconf=/mnt/RESTORING/boot/grub/menu.lst
63        elif [ -f "/mnt/RESTORING/boot/grub/grub.cfg" ]; then
64            old_grubconf=/mnt/RESTORING/boot/grub/grub.cfg
65        elif [ -f "/mnt/RESTORING/boot/grub2/grub.cfg" ]; then
66            old_grubconf=/mnt/RESTORING/boot/grub2/grub.cfg
67        fi
68        if [ -L "$old_grubconf" ] ; then
69            l=`readlink "$old_grubconf"`
70            if [ _"`echo $l | cut -c1`" = _"/" ]; then
71                # If readlink gives an absolute path it's related to the chroot
72                        old_grubconf=/mnt/RESTORING/$l
73            else
74                # If readlink gives a relative path, it's in the same dir
75                    d=`dirname "$old_grubconf"`
76                        old_grubconf=$d/$l
77            fi
78        fi
79        return 0
80    fi
81    LogIt "GRUB not found." 2
82    return 1
83}
84
85
86BEFORE=stabgrub-me.PRE
87
88MakeBackups() {
89    local i
90    LogIt "Backing up original files before modifying them..." 2
91    for i in $old_grubconf $old_fstab ; do
92        LogIt "Backing up $i"
93        [ -e "$i" ] && [ ! -e "$i.$BEFORE" ] && cp -f $i $i.$BEFORE
94    done
95}
96
97
98RestoreBackups() {
99    LogIt "Restoring original versions of modified files..." 2
100    cp -f $old_grubconf /tmp/grub.conf.ugly
101    cp -f $old_fstab /tmp/fstab.ugly
102    for i in $old_grubconf $old_fstab ; do
103        LogIt "Restoring $i"
104        [ -f "$i.$BEFORE" ] && cp -f $i.$BEFORE $i
105    done
106}
107
108
109# --------------------------------- main -------------------------------
110
111if [ "$#" -ne "1" ] ; then
112    LogIt "stabgrub-me </dev/bootdrive>"
113    exit 1
114fi
115
116bootdrive=$1
117
118LogIt "stabgrub-me '$1' --- starting"
119LocateOldFstab
120LocateOldGrub
121old_mountlist=/tmp/mountlist.original
122new_mountlist=/tmp/mountlist.txt
123QuitIfNotFound $old_mountlist
124QuitIfNotFound $new_mountlist
125QuitIfNotFound $old_fstab
126QuitIfNotFound $old_grubconf
127LogIt "OK so far: I've found all the files I need." 2
128new_fstab=/mnt/RESTORING/etc/fstab.NEW
129if [ -f /mnt/RESTORING/boot/grub/menu.lst ]; then
130    new_grubconf=/mnt/RESTORING/boot/grub/menu.lst.NEW
131elif [ -f /mnt/RESTORING/boot/grub/grub.cfg ]; then
132    new_grubconf=/mnt/RESTORING/boot/grub/grub.cfg.NEW
133elif [ -f /mnt/RESTORING/boot/grub2/grub.cfg ]; then
134    new_grubconf=/mnt/RESTORING/boot/grub2/grub.cfg.NEW
135fi
136# change back to /tmp if /mnt/RESTORING/etc be problematic
137
138MakeBackups
139
140LogIt "old_mountlist = $old_mountlist"
141LogIt "new_mountlist = $new_mountlist"
142LogIt "old_fstab = $old_fstab"
143LogIt "new_fstab = $new_fstab"
144LogIt "where_they_live = $where_they_live"
145
146LogIt "Calling hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab"
147
148outval=0
149hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab
150res=$?
151if [ "$res" -ne "0" ] ; then
152    LogIt "Warning - hack-fstab failed"
153    outval=$(($outval+$res))
154else
155    LogIt "Back from hack-fstab OK" 1
156fi
157
158if [ "$outval" -ne "0" ] ; then
159    LogIt "Fstab and/or grub modifications failed" 3
160    RestoreBackups
161else
162    LogIt "Modifications succeeded." 2
163    LogIt "Copying $new_fstab over $old_fstab" 2
164    cp -f $new_fstab $old_fstab
165    cp -f $new_fstab /tmp/fstab
166    outval=$(($outval+$?))
167    LogIt "Copying over $old_grubconf" 2
168    if [ -f "$new_grubconf" ] ; then
169        cp -f $new_grubconf $old_grubconf
170        outval=$(($outval+$?))
171    fi
172    if [ "$outval" -ne "0" ] ; then
173        LogIt "Modifications (copying) failed. Restoring from backups." 3
174        RestoreBackups
175    else
176        LogIt "Fstab modified ok." 2
177    fi
178    cd /mnt/RESTORING
179#    cd $where_they_live
180#    cd ..
181    LogIt "Running grub..." 2
182    LogIt "grub-MR $bootdrive $new_mountlist"
183    grub-MR $bootdrive $new_mountlist >> $LOGFILE 2>> $LOGFILE
184    grub_res=$?
185    if [ "$grub_res" -ne "0" ] ; then
186        LogIt "grub-install failed. Running grub-MR..."
187        chroot /mnt/RESTORING grub-install '(hd0)' >> $LOGFILE 2>> $LOGFILE
188        grub_res=$?
189    fi
190    if [ "$grub_res" -ne "0" ] ; then
191    LogIt "GRUB failed." 3
192    else
193        LogIt "GRUB ran ok." 2
194    fi
195fi
196
197if [ "$outval" -ne "0" ] ; then
198    LogIt "Error(s) occurred during grub/fstab processing.              "
199    LogIt "Restoring originals.                                         "
200    RestoreBackups
201elif [ "$grub_res" -ne "0" ] ; then
202    LogIt "Fstab was modified OK but grub failed to run.                 "
203    outval=$(($outval+1))
204else
205    LogIt "/etc/fstab was modified ok. GRUB ran ok.                      "
206fi
207LogIt "stabgrub-me --- leaving"
208echo -en "\n\n\n"
209exit $outval
Note: See TracBrowser for help on using the repository browser.