[1] | 1 | #!/bin/sh
|
---|
| 2 | #
|
---|
[1546] | 3 | # $Id: hack-fstab 2087 2008-12-14 14:50:37Z bruno $
|
---|
[1] | 4 | #
|
---|
[1546] | 5 | # Recreate /etc/fstab from first mountlist analysis
|
---|
| 6 | # and then complement wirh old fstab content.
|
---|
[1] | 7 |
|
---|
| 8 | LogIt() {
|
---|
| 9 | echo "$1" >> /dev/stderr
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 |
|
---|
[1546] | 13 | AddFancyParams() {
|
---|
| 14 | local incoming device mountpoint format size original_fstab_line label uuid
|
---|
[1] | 15 |
|
---|
| 16 | incoming=`echo "$1" | tr -s '\t' ' '`
|
---|
| 17 | [ "$incoming" = "" ] && return
|
---|
| 18 | device=`echo "$incoming" | cut -d' ' -f1`
|
---|
| 19 | mountpoint=`echo "$incoming" | cut -d' ' -f2`
|
---|
| 20 | format=`echo "$incoming" | cut -d' ' -f3`
|
---|
| 21 | size=`echo "$incoming" | cut -d' ' -f4`
|
---|
[1755] | 22 | label=`echo "$incoming" | cut -d' ' -f5`
|
---|
[273] | 23 | original_fstab_line=`grep " $mountpoint " $old_fstab | grep -v "#" | tr -s ' ' ' '`
|
---|
[2087] | 24 | if [ "$format" = "ext2" ] || [ "$format" = "ext3" ] || [ "$format" = "ext4" ]; then
|
---|
[1546] | 25 | if [ "`echo "$original_fstab_line" | grep "LABEL="`" != "" ] ; then
|
---|
| 26 | device="LABEL=$label"
|
---|
| 27 | fi
|
---|
| 28 | if [ "`echo "$original_fstab_line" | grep "UUID="`" != "" ] ; then
|
---|
[1755] | 29 | device="UUID=$label"
|
---|
[1546] | 30 | fi
|
---|
[1] | 31 | fi
|
---|
| 32 |
|
---|
| 33 | echo -e -n "$device $mountpoint $format "
|
---|
| 34 |
|
---|
| 35 | if [ "$original_fstab_line" != "" ] ; then
|
---|
[1546] | 36 | echo "$original_fstab_line" | cut -d' ' -f4- | tr -s ' ' ' '
|
---|
[1] | 37 | else
|
---|
[1546] | 38 | echo -e "defaults 0 0"
|
---|
[1] | 39 | fi
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | ProcessFstab() {
|
---|
| 45 | local incoming dev mountlist_entry blanklines new_i
|
---|
[1546] | 46 |
|
---|
[1] | 47 | read incoming
|
---|
| 48 | blanklines=0
|
---|
| 49 | while [ "$blanklines" -lt "5" ] ; do
|
---|
[1546] | 50 | if [ "$incoming" = "" ] ; then
|
---|
| 51 | blanklines=$(($blanklines+1))
|
---|
| 52 | read incoming
|
---|
| 53 | continue
|
---|
| 54 | fi
|
---|
| 55 | incoming=`echo "$incoming" | tr -s '\t' ' '`
|
---|
| 56 | if [ "`echo "$incoming" | grep -vE "LABEL=|UUID="`" ] ; then
|
---|
| 57 | dev=`echo "$incoming" | cut -d' ' -f1`
|
---|
| 58 | mountlist_entry=`grep "$dev " $old_mountlist`
|
---|
| 59 | if [ "$mountlist_entry" = "" ] ; then
|
---|
| 60 | echo "$incoming"
|
---|
| 61 | fi
|
---|
| 62 | fi
|
---|
| 63 | read incoming
|
---|
[1] | 64 | done
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | HackIncomingIfLABELused() {
|
---|
| 69 | local incoming col1 col2 col_rest orig out result
|
---|
[1546] | 70 |
|
---|
| 71 | # Keep LABEL or UUID if originally there in fstab
|
---|
[1] | 72 | result=""
|
---|
| 73 | incoming=`echo "$1" | tr -s '\t' ' '`
|
---|
[1546] | 74 | col1=`echo "$incoming" | cut -d' ' -f1`
|
---|
| 75 | col2=`echo "$incoming" | cut -d' ' -f2`
|
---|
| 76 | col_rest=`echo "$incoming" | cut -d' ' -f3- | tr -s ' ' ' '`
|
---|
| 77 | orig="`grep " $col2 " $old_fstab | cut -d' ' -f1`"
|
---|
| 78 | if [ "`echo "$orig" | grep -E "LABEL=|UUID="`" != "" ] ; then
|
---|
| 79 | echo "orig = $orig" >> /dev/stderr
|
---|
| 80 | echo -e "$orig $col2 $col_rest" | tr -s ' ' ' '
|
---|
[1] | 81 | fi
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | #HackIncomingIfLABELused "LABEL=/ / ext2 defaults 0,0,0"
|
---|
| 86 | #exit 0
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | ProcessMountlist() {
|
---|
| 91 | local incoming outstr res spc
|
---|
[1546] | 92 |
|
---|
[1] | 93 | read incoming
|
---|
| 94 | while [ "$incoming" != "" ] ; do
|
---|
[1546] | 95 | incoming=`echo "$incoming" | tr -s '\t' ' '`
|
---|
| 96 | res=`HackIncomingIfLABELused "$incoming"`
|
---|
| 97 | if [ ! "$res" ] ; then
|
---|
| 98 | outstr=`AddFancyParams "$incoming"`
|
---|
| 99 | else
|
---|
| 100 | outstr=`AddFancyParams "$res"`
|
---|
| 101 | fi
|
---|
| 102 | spc="`echo "$outstr" | tr -s '\t' ' '`"
|
---|
| 103 | if [ "$spc" != "" ] && [ "$spc" != " " ] && [ "`echo "$spc" | grep "raid raid"`" = "" ] ; then
|
---|
| 104 | echo "$spc"
|
---|
| 105 | fi
|
---|
| 106 | read incoming
|
---|
[1] | 107 | done
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | # ----------------- main ---------------
|
---|
| 111 |
|
---|
| 112 | LogIt "hack-fstab --- starting"
|
---|
| 113 |
|
---|
| 114 | if [ "$#" -ne "4" ] ; then
|
---|
| 115 | LogIt "hack-fstab <old mountlist> <old fstab> <new mountlist> <new fstab>" 1
|
---|
| 116 | LogIt "NB: the new fstab file is outgoing; all other files are incoming." 1
|
---|
| 117 | exit 1
|
---|
| 118 | fi
|
---|
| 119 |
|
---|
| 120 | LogIt "hack-fstab '$1' '$2' '$3' '$4'"
|
---|
| 121 |
|
---|
| 122 | old_mountlist=$1
|
---|
| 123 | old_fstab=$2
|
---|
| 124 | new_mountlist=$3
|
---|
| 125 | outfile=$4
|
---|
| 126 |
|
---|
| 127 | > $outfile
|
---|
| 128 | LogIt "Processing mountlist"
|
---|
| 129 | ProcessMountlist < $new_mountlist >> $outfile
|
---|
| 130 | LogIt "Processing fstab"
|
---|
| 131 | ProcessFstab < $old_fstab >> $outfile
|
---|
| 132 | if [ ! -e "$outfile.old" ] ; then
|
---|
| 133 | LogIt "Moving $outfile to $outfile.old" 1
|
---|
| 134 | mv $outfile $outfile.old
|
---|
| 135 | else
|
---|
| 136 | LogIt "$outfile.old exists already - assuming it _is_ the old one & using it" 1
|
---|
| 137 | fi
|
---|
| 138 | cat $outfile.old | tr -s ' ' ' ' \
|
---|
| 139 | | gawk '{printf "%-15s %-18s %-10s ",$1,$2,$3; for(i=4;i<=NF;i++) {printf "%s ",$i;};print "";};' \
|
---|
| 140 | | sort -u > $outfile
|
---|
| 141 |
|
---|
| 142 | LogIt "Finished writing to outfile ($outfile)"
|
---|
| 143 | LogIt "hack-fstab --- leaving"
|
---|
| 144 | exit 0
|
---|