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