1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # stabgrub-me
|
---|
4 | #
|
---|
5 | # 04/08/2003
|
---|
6 | # - cleaned up backup func a bit
|
---|
7 | #
|
---|
8 | #
|
---|
9 | #
|
---|
10 | # forked from stablilo-me on 2002/11/20
|
---|
11 | #
|
---|
12 | #####################################################################
|
---|
13 |
|
---|
14 |
|
---|
15 | LOGFILE=/tmp/mondo-restore.log
|
---|
16 |
|
---|
17 | QuitIfNotFound() {
|
---|
18 | if [ ! -f "$1" ] ; then
|
---|
19 | LogIt "(stabgrub-me) Where's $1? I cannot continue." 1
|
---|
20 | exit 1
|
---|
21 | fi
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | LocateOldFstab() {
|
---|
29 | old_fstab=""
|
---|
30 | old_grubconf=""
|
---|
31 | if [ -f "/mnt/RESTORING/etc/fstab" ] ; then
|
---|
32 | LogIt "No need for fstab search." 2
|
---|
33 | # fstab_list=/mnt/RESTORING/etc/fstab
|
---|
34 | old_fstab=/mnt/RESTORING/etc/fstab
|
---|
35 | old_grubconf=/mnt/RESTORING/etc/grub.conf
|
---|
36 | # For some distros, e.g. Debian, /etc/grub.conf is a symbolic link
|
---|
37 | # which we need to resolve and prepend with /mnt/RESTORING because
|
---|
38 | # we run this outside the chroot.
|
---|
39 | if [ -L "$old_grubconf" ] ; then
|
---|
40 | old_grubconf=/mnt/RESTORING/etc/`readlink "$old_grubconf"`
|
---|
41 | fi
|
---|
42 | return 0
|
---|
43 | elif [ -f "/mnt/cdrom/archives/CUCKOO" ] ; then
|
---|
44 | LogIt "Because I'm cuckoo, I'll stop searching." 2
|
---|
45 | return 1
|
---|
46 | else
|
---|
47 | LogIt "Looking for fstab. Please wait." 2
|
---|
48 | fstab_list=`find /mnt/RESTORING -name fstab 2> /dev/null`
|
---|
49 | fi
|
---|
50 | where_they_live=""
|
---|
51 | for curr_fstab in $fstab_list ; do
|
---|
52 | curr_dir=`echo $curr_fstab | gawk '{i=index($0,"/fstab");print substr($0,0,i-1);}'`
|
---|
53 | resA=$?
|
---|
54 | curr_inetd=`find $curr_dir -name inetd.conf | grep -v "linuxconf"`
|
---|
55 | resB=$?
|
---|
56 | if [ "$resA" -eq "0" ] ; then
|
---|
57 | if [ "$where_they_live" != "" ] ; then
|
---|
58 | LogIt "Two directories found! One is $where_they_live, the other $curr_dir" 1
|
---|
59 | LogIt "I don't know which to choose. I'll abort the search." 1
|
---|
60 | return 1
|
---|
61 | fi
|
---|
62 | where_they_live=$curr_dir
|
---|
63 | fi
|
---|
64 | done
|
---|
65 | if [ "$where_they_live" = "" ] ; then
|
---|
66 | LogIt "Cannot find any folder which holds fstab _and_ inetd.conf" 1
|
---|
67 | return 1
|
---|
68 | fi
|
---|
69 | old_grubconf=$where_they_live/grub.conf
|
---|
70 | old_fstab=$where_they_live/fstab
|
---|
71 | LogIt "GRUB and fstab found." 2
|
---|
72 | return 0
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | BEFORE=stabgrub-me.PRE
|
---|
77 |
|
---|
78 | MakeBackups() {
|
---|
79 | local i
|
---|
80 | LogIt "Backing up original files before modifying them..." 2
|
---|
81 | for i in $old_grubconf $old_fstab ; do
|
---|
82 | LogIt "Backing up $i"
|
---|
83 | [ -e "$i" ] && [ ! -e "$i.$BEFORE" ] && cp -f $i $i.$BEFORE
|
---|
84 | done
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | RestoreBackups() {
|
---|
89 | LogIt "Restoring original versions of modified files..." 2
|
---|
90 | cp -f $old_grubconf /tmp/grub.conf.ugly
|
---|
91 | cp -f $old_fstab /tmp/fstab.ugly
|
---|
92 | for i in $old_grubconf $old_fstab ; do
|
---|
93 | LogIt "Restoring $i"
|
---|
94 | [ -f "$i.$BEFORE" ] && cp -f $i.$BEFORE $i
|
---|
95 | done
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | # --------------------------------- main -------------------------------
|
---|
100 |
|
---|
101 | if [ "$#" -ne "1" ] ; then
|
---|
102 | LogIt "stabgrub-me </dev/bootdrive>"
|
---|
103 | exit 1
|
---|
104 | fi
|
---|
105 |
|
---|
106 | bootdrive=$1
|
---|
107 |
|
---|
108 | LogIt "stabgrub-me '$1' --- starting"
|
---|
109 | LocateOldFstab
|
---|
110 | old_mountlist=/tmp/mountlist.original
|
---|
111 | new_mountlist=/tmp/mountlist.txt
|
---|
112 | QuitIfNotFound $old_mountlist
|
---|
113 | QuitIfNotFound $new_mountlist
|
---|
114 | QuitIfNotFound $old_fstab
|
---|
115 | QuitIfNotFound $old_grubconf
|
---|
116 | LogIt "OK so far: I've found all the files I need." 2
|
---|
117 | new_fstab=/mnt/RESTORING/etc/fstab.NEW
|
---|
118 | new_grubconf=/mnt/RESTORING/etc/grub.conf.NEW
|
---|
119 | # change back to /tmp if /mnt/RESTORING/etc be problematic
|
---|
120 |
|
---|
121 | MakeBackups
|
---|
122 |
|
---|
123 | LogIt "old_mountlist = $old_mountlist"
|
---|
124 | LogIt "new_mountlist = $new_mountlist"
|
---|
125 | LogIt "old_fstab = $old_fstab"
|
---|
126 | LogIt "new_fstab = $new_fstab"
|
---|
127 | LogIt "where_they_live = $where_they_live"
|
---|
128 |
|
---|
129 | LogIt "Calling hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab"
|
---|
130 |
|
---|
131 | outval=0
|
---|
132 | hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab
|
---|
133 | res=$?
|
---|
134 | if [ "$res" -ne "0" ] ; then
|
---|
135 | LogIt "Warning - hack-fstab failed"
|
---|
136 | outval=$(($outval+$res))
|
---|
137 | else
|
---|
138 | LogIt "Back from hack-fstab OK" 1
|
---|
139 | fi
|
---|
140 |
|
---|
141 | if [ "$outval" -ne "0" ] ; then
|
---|
142 | LogIt "Fstab and/or grub modifications failed" 3
|
---|
143 | RestoreBackups
|
---|
144 | else
|
---|
145 | LogIt "Modifications succeeded." 2
|
---|
146 | LogIt "Copying $new_fstab over $old_fstab" 2
|
---|
147 | cp -f $new_fstab $old_fstab
|
---|
148 | cp -f $new_fstab /tmp/fstab
|
---|
149 | outval=$(($outval+$?))
|
---|
150 | LogIt "Copying over $old_grubconf" 2
|
---|
151 | if [ -f "$new_grubconf" ] ; then
|
---|
152 | cp -f $new_grubconf $old_grubconf
|
---|
153 | outval=$(($outval+$?))
|
---|
154 | fi
|
---|
155 | if [ "$outval" -ne "0" ] ; then
|
---|
156 | LogIt "Modifications (copying) failed. Restoring from backups." 3
|
---|
157 | RestoreBackups
|
---|
158 | else
|
---|
159 | LogIt "Fstab modified ok." 2
|
---|
160 | fi
|
---|
161 | cd /mnt/RESTORING
|
---|
162 | # cd $where_they_live
|
---|
163 | # cd ..
|
---|
164 | LogIt "Running grub..." 2
|
---|
165 | LogIt "grub-MR $bootdrive $new_mountlist"
|
---|
166 | grub-MR $bootdrive $new_mountlist >> $LOGFILE 2>> $LOGFILE
|
---|
167 | grub_res=$?
|
---|
168 | if [ "$grub_res" -ne "0" ] ; then
|
---|
169 | LogIt "grub-install failed. Running grub-MR..."
|
---|
170 | chroot /mnt/RESTORING grub-install '(hd0)' >> $LOGFILE 2>> $LOGFILE
|
---|
171 | grub_res=$?
|
---|
172 | fi
|
---|
173 | if [ "$grub_res" -ne "0" ] ; then
|
---|
174 | LogIt "GRUB failed." 3
|
---|
175 | else
|
---|
176 | LogIt "GRUB ran ok." 2
|
---|
177 | fi
|
---|
178 | fi
|
---|
179 |
|
---|
180 | if [ "$outval" -ne "0" ] ; then
|
---|
181 | LogIt "Error(s) occurred during grub/fstab processing. "
|
---|
182 | LogIt "Restoring originals. "
|
---|
183 | RestoreBackups
|
---|
184 | elif [ "$grub_res" -ne "0" ] ; then
|
---|
185 | LogIt "Fstab was modified OK but grub failed to run. "
|
---|
186 | outval=$(($outval+1))
|
---|
187 | else
|
---|
188 | LogIt "/etc/fstab was modified ok. GRUB ran ok. "
|
---|
189 | fi
|
---|
190 | LogIt "stabgrub-me --- leaving"
|
---|
191 | echo -en "\n\n\n"
|
---|
192 | exit $outval
|
---|