1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # $Id: stabgrub-me 2095 2008-12-17 16:26:31Z 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" ] ; 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 |
|
---|
79 | BEFORE=stabgrub-me.PRE
|
---|
80 |
|
---|
81 | MakeBackups() {
|
---|
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 |
|
---|
91 | RestoreBackups() {
|
---|
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 |
|
---|
104 | if [ "$#" -ne "1" ] ; then
|
---|
105 | LogIt "stabgrub-me </dev/bootdrive>"
|
---|
106 | exit 1
|
---|
107 | fi
|
---|
108 |
|
---|
109 | bootdrive=$1
|
---|
110 |
|
---|
111 | LogIt "stabgrub-me '$1' --- starting"
|
---|
112 | LocateOldFstab
|
---|
113 | LocateOldGrub
|
---|
114 | old_mountlist=/tmp/mountlist.original
|
---|
115 | new_mountlist=/tmp/mountlist.txt
|
---|
116 | QuitIfNotFound $old_mountlist
|
---|
117 | QuitIfNotFound $new_mountlist
|
---|
118 | QuitIfNotFound $old_fstab
|
---|
119 | QuitIfNotFound $old_grubconf
|
---|
120 | LogIt "OK so far: I've found all the files I need." 2
|
---|
121 | new_fstab=/mnt/RESTORING/etc/fstab.NEW
|
---|
122 | new_grubconf=/mnt/RESTORING/boot/grub/menu.lst.NEW
|
---|
123 | # change back to /tmp if /mnt/RESTORING/etc be problematic
|
---|
124 |
|
---|
125 | MakeBackups
|
---|
126 |
|
---|
127 | LogIt "old_mountlist = $old_mountlist"
|
---|
128 | LogIt "new_mountlist = $new_mountlist"
|
---|
129 | LogIt "old_fstab = $old_fstab"
|
---|
130 | LogIt "new_fstab = $new_fstab"
|
---|
131 | LogIt "where_they_live = $where_they_live"
|
---|
132 |
|
---|
133 | LogIt "Calling hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab"
|
---|
134 |
|
---|
135 | outval=0
|
---|
136 | hack-fstab $old_mountlist $old_fstab $new_mountlist $new_fstab
|
---|
137 | res=$?
|
---|
138 | if [ "$res" -ne "0" ] ; then
|
---|
139 | LogIt "Warning - hack-fstab failed"
|
---|
140 | outval=$(($outval+$res))
|
---|
141 | else
|
---|
142 | LogIt "Back from hack-fstab OK" 1
|
---|
143 | fi
|
---|
144 |
|
---|
145 | if [ "$outval" -ne "0" ] ; then
|
---|
146 | LogIt "Fstab and/or grub modifications failed" 3
|
---|
147 | RestoreBackups
|
---|
148 | else
|
---|
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
|
---|
182 | fi
|
---|
183 |
|
---|
184 | if [ "$outval" -ne "0" ] ; then
|
---|
185 | LogIt "Error(s) occurred during grub/fstab processing. "
|
---|
186 | LogIt "Restoring originals. "
|
---|
187 | RestoreBackups
|
---|
188 | elif [ "$grub_res" -ne "0" ] ; then
|
---|
189 | LogIt "Fstab was modified OK but grub failed to run. "
|
---|
190 | outval=$(($outval+1))
|
---|
191 | else
|
---|
192 | LogIt "/etc/fstab was modified ok. GRUB ran ok. "
|
---|
193 | fi
|
---|
194 | LogIt "stabgrub-me --- leaving"
|
---|
195 | echo -en "\n\n\n"
|
---|
196 | exit $outval
|
---|