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