source: MondoRescue/branches/2.2.8/mondo/src/restore-scripts/mondo/stabgrub-me@ 2095

Last change on this file since 2095 was 2095, checked in by Bruno Cornec, 15 years ago

Use /boot/grub/menu.lst everywhere instead of meesing up with /etc/grub.conf

  • Property svn:keywords set to Id
  • Property svn:unix-mode set to 755
File size: 5.2 KB
Line 
1#!/bin/sh
2#
3# $Id: stabgrub-me 2095 2008-12-17 16:26:31Z 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" ] ; then
60 LogIt "No need for menu.lst search." 2
61 old_grubconf=/mnt/RESTORING/boot/grub/menu.lst
62 if [ -L "$old_grubconf" ] ; then
63 l=`readlink "$old_grubconf"`
64 if [ _"`echo $l | cut -c1`" = _"/" ]; then
65 # If readlink gives an absolute path it's related to the chroot
66 old_grubconf=/mnt/RESTORING/$l
67 else
68 # If readlink gives a relative path, it's in the same dir
69 old_grubconf=/mnt/RESTORING/boot/grub/$l
70 fi
71 fi
72 return 0
73 fi
74 LogIt "GRUB not found." 2
75 return 1
76}
77
78
79BEFORE=stabgrub-me.PRE
80
81MakeBackups() {
82 local i
83 LogIt "Backing up original files before modifying them..." 2
84 for i in $old_grubconf $old_fstab ; do
85 LogIt "Backing up $i"
86 [ -e "$i" ] && [ ! -e "$i.$BEFORE" ] && cp -f $i $i.$BEFORE
87 done
88}
89
90
91RestoreBackups() {
92 LogIt "Restoring original versions of modified files..." 2
93 cp -f $old_grubconf /tmp/grub.conf.ugly
94 cp -f $old_fstab /tmp/fstab.ugly
95 for i in $old_grubconf $old_fstab ; do
96 LogIt "Restoring $i"
97 [ -f "$i.$BEFORE" ] && cp -f $i.$BEFORE $i
98 done
99}
100
101
102# --------------------------------- main -------------------------------
103
104if [ "$#" -ne "1" ] ; then
105 LogIt "stabgrub-me </dev/bootdrive>"
106 exit 1
107fi
108
109bootdrive=$1
110
111LogIt "stabgrub-me '$1' --- starting"
112LocateOldFstab
113LocateOldGrub
114old_mountlist=/tmp/mountlist.original
115new_mountlist=/tmp/mountlist.txt
116QuitIfNotFound $old_mountlist
117QuitIfNotFound $new_mountlist
118QuitIfNotFound $old_fstab
119QuitIfNotFound $old_grubconf
120LogIt "OK so far: I've found all the files I need." 2
121new_fstab=/mnt/RESTORING/etc/fstab.NEW
122new_grubconf=/mnt/RESTORING/boot/grub/menu.lst.NEW
123# change back to /tmp if /mnt/RESTORING/etc be problematic
124
125MakeBackups
126
127LogIt "old_mountlist = $old_mountlist"
128LogIt "new_mountlist = $new_mountlist"
129LogIt "old_fstab = $old_fstab"
130LogIt "new_fstab = $new_fstab"
131LogIt "where_they_live = $where_they_live"
132
133LogIt "Calling hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab"
134
135outval=0
136hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab
137res=$?
138if [ "$res" -ne "0" ] ; then
139 LogIt "Warning - hack-fstab failed"
140 outval=$(($outval+$res))
141else
142 LogIt "Back from hack-fstab OK" 1
143fi
144
145if [ "$outval" -ne "0" ] ; then
146 LogIt "Fstab and/or grub modifications failed" 3
147 RestoreBackups
148else
149 LogIt "Modifications succeeded." 2
150 LogIt "Copying $new_fstab over $old_fstab" 2
151 cp -f $new_fstab $old_fstab
152 cp -f $new_fstab /tmp/fstab
153 outval=$(($outval+$?))
154 LogIt "Copying over $old_grubconf" 2
155 if [ -f "$new_grubconf" ] ; then
156 cp -f $new_grubconf $old_grubconf
157 outval=$(($outval+$?))
158 fi
159 if [ "$outval" -ne "0" ] ; then
160 LogIt "Modifications (copying) failed. Restoring from backups." 3
161 RestoreBackups
162 else
163 LogIt "Fstab modified ok." 2
164 fi
165 cd /mnt/RESTORING
166# cd $where_they_live
167# cd ..
168 LogIt "Running grub..." 2
169 LogIt "grub-MR $bootdrive $new_mountlist"
170 grub-MR $bootdrive $new_mountlist >> $LOGFILE 2>> $LOGFILE
171 grub_res=$?
172 if [ "$grub_res" -ne "0" ] ; then
173 LogIt "grub-install failed. Running grub-MR..."
174 chroot /mnt/RESTORING grub-install '(hd0)' >> $LOGFILE 2>> $LOGFILE
175 grub_res=$?
176 fi
177 if [ "$grub_res" -ne "0" ] ; then
178 LogIt "GRUB failed." 3
179 else
180 LogIt "GRUB ran ok." 2
181 fi
182fi
183
184if [ "$outval" -ne "0" ] ; then
185 LogIt "Error(s) occurred during grub/fstab processing. "
186 LogIt "Restoring originals. "
187 RestoreBackups
188elif [ "$grub_res" -ne "0" ] ; then
189 LogIt "Fstab was modified OK but grub failed to run. "
190 outval=$(($outval+1))
191else
192 LogIt "/etc/fstab was modified ok. GRUB ran ok. "
193fi
194LogIt "stabgrub-me --- leaving"
195echo -en "\n\n\n"
196exit $outval
Note: See TracBrowser for help on using the repository browser.