source: MondoRescue/branches/2.2.5/mondo/src/restore-scripts/mondo/hack-fstab@ 1546

Last change on this file since 1546 was 1546, checked in by Bruno Cornec, 17 years ago

Fix what seems to appear a huge number of bugs in hack-fstab (illustration of 1 LOC = 1 bug :-)
Especially improve LABEL and UUID support.
Should fix #185

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