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