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