1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # $Id: hack-fstab 1755 2007-10-31 00:11:44Z bruno $
|
---|
4 | #
|
---|
5 | # Recreate /etc/fstab from first mountlist analysis
|
---|
6 | # and then complement wirh old fstab content.
|
---|
7 |
|
---|
8 | LogIt() {
|
---|
9 | echo "$1" >> /dev/stderr
|
---|
10 | }
|
---|
11 |
|
---|
12 |
|
---|
13 | AddFancyParams() {
|
---|
14 | local incoming device mountpoint format size original_fstab_line label uuid
|
---|
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`
|
---|
22 | label=`echo "$incoming" | cut -d' ' -f5`
|
---|
23 | original_fstab_line=`grep " $mountpoint " $old_fstab | grep -v "#" | tr -s ' ' ' '`
|
---|
24 | if [ "$format" = "ext2" ] || [ "$format" = "ext3" ] ; then
|
---|
25 | if [ "`echo "$original_fstab_line" | grep "LABEL="`" != "" ] ; then
|
---|
26 | device="LABEL=$label"
|
---|
27 | fi
|
---|
28 | if [ "`echo "$original_fstab_line" | grep "UUID="`" != "" ] ; then
|
---|
29 | device="UUID=$label"
|
---|
30 | fi
|
---|
31 | fi
|
---|
32 |
|
---|
33 | echo -e -n "$device $mountpoint $format "
|
---|
34 |
|
---|
35 | if [ "$original_fstab_line" != "" ] ; then
|
---|
36 | echo "$original_fstab_line" | cut -d' ' -f4- | tr -s ' ' ' '
|
---|
37 | else
|
---|
38 | echo -e "defaults 0 0"
|
---|
39 | fi
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | ProcessFstab() {
|
---|
45 | local incoming dev mountlist_entry blanklines new_i
|
---|
46 |
|
---|
47 | read incoming
|
---|
48 | blanklines=0
|
---|
49 | while [ "$blanklines" -lt "5" ] ; do
|
---|
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
|
---|
64 | done
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | HackIncomingIfLABELused() {
|
---|
69 | local incoming col1 col2 col_rest orig out result
|
---|
70 |
|
---|
71 | # Keep LABEL or UUID if originally there in fstab
|
---|
72 | result=""
|
---|
73 | incoming=`echo "$1" | tr -s '\t' ' '`
|
---|
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 ' ' ' '
|
---|
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
|
---|
92 |
|
---|
93 | read incoming
|
---|
94 | while [ "$incoming" != "" ] ; do
|
---|
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
|
---|
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
|
---|