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