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