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