Tizen 2.1 base
[external/device-mapper.git] / scripts / lvmconf.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4 #
5 # This file is part of the lvm2 package.
6 #
7 # This copyrighted material is made available to anyone wishing to use,
8 # modify, copy, or redistribute it subject to the terms and conditions
9 # of the GNU General Public License v.2.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software Foundation,
13 # Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14
15 #
16 # Edit an lvm.conf file to adjust various properties
17 #
18
19 function usage
20 {
21     echo "usage: $0 <command>"
22     echo ""
23     echo "Commands:"
24     echo "Enable clvm:  --enable-cluster [--lockinglibdir <dir>] [--lockinglib <lib>]"
25     echo "Disable clvm: --disable-cluster"
26     echo "Set locking library: --lockinglibdir <dir> [--lockinglib <lib>]"
27     echo ""
28     echo "Global options:"
29     echo "Config file location: --file <configfile>"
30     echo ""
31 }
32
33
34 function parse_args
35 {
36     while [ -n "$1" ]; do
37         case $1 in
38             --enable-cluster)
39                 LOCKING_TYPE=3
40                 shift
41                 ;;
42             --disable-cluster)
43                 LOCKING_TYPE=1
44                 shift
45                 ;;
46             --lockinglibdir)
47                 if [ -n "$2" ]; then
48                     LOCKINGLIBDIR=$2
49                     shift 2
50                 else
51                     usage
52                     exit 1
53                 fi
54                 ;;
55             --lockinglib)
56                 if [ -n "$2" ]; then
57                     LOCKINGLIB=$2
58                     shift 2
59                 else
60                     usage
61                     exit 1
62                 fi
63                 ;;
64             --file)
65                 if [ -n "$2" ]; then
66                     CONFIGFILE=$2
67                     shift 2
68                 else
69                     usage
70                     exit 1
71                 fi
72                 ;;
73             *)
74                 usage
75                 exit 1
76         esac
77     done
78 }
79
80 function validate_args
81 {
82     [ -z "$CONFIGFILE" ] && CONFIGFILE="/etc/lvm/lvm.conf"
83
84     if [ ! -f "$CONFIGFILE" ]
85             then
86             echo "$CONFIGFILE does not exist"
87             exit 10
88     fi
89
90     if [ -z "$LOCKING_TYPE" ] && [ -z "$LOCKINGLIBDIR" ]; then
91         usage
92         exit 1
93     fi
94
95     if [ -n "$LOCKINGLIBDIR" ]; then
96
97         if [ "${LOCKINGLIBDIR:0:1}" != "/" ]
98             then
99             echo "Prefix must be an absolute path name (starting with a /)"
100             exit 12
101         fi
102
103         if [ -n "$LOCKINGLIB" ] && [ ! -f "$LOCKINGLIBDIR/$LOCKINGLIB" ]
104             then
105             echo "$LOCKINGLIBDIR/$LOCKINGLIB does not exist, did you do a \"make install\" ?"
106             exit 11
107         fi
108
109     fi
110
111     if [ "$LOCKING_TYPE" = "1" ] && [ -n "$LOCKINGLIBDIR" -o -n "$LOCKINGLIB" ]; then
112         echo "Superfluous locking lib parameter, ignoring"
113     fi
114 }
115
116 umask 0077
117
118 parse_args "$@"
119
120 validate_args
121
122
123 SCRIPTFILE=/etc/lvm/.lvmconf-script.tmp
124 TMPFILE=/etc/lvm/.lvmconf-tmp.tmp
125
126
127 # Flags so we know which parts of the file we can replace and which need
128 # adding. These are return codes from grep, so zero means it IS present!
129 have_type=1
130 have_dir=1
131 have_library=1
132 have_global=1
133
134 grep -q '^[[:blank:]]*locking_type[[:blank:]]*=' $CONFIGFILE
135 have_type=$?
136
137 grep -q '^[[:blank:]]*library_dir[[:blank:]]*=' $CONFIGFILE
138 have_dir=$?
139
140 grep -q '^[[:blank:]]*locking_library[[:blank:]]*=' $CONFIGFILE
141 have_library=$?
142
143 # Those options are in section "global {" so we must have one if any are present.
144 if [ "$have_type" = "0" -o "$have_dir" = "0" -o "$have_library" = "0" ]
145 then
146
147     # See if we can find it...
148     grep -q '^[[:blank:]]*global[[:blank:]]*{' $CONFIGFILE
149     have_global=$?
150
151     if [ "$have_global" = "1" ] 
152         then
153         echo "global keys but no 'global {' found, can't edit file"
154         exit 13
155     fi
156 fi
157
158 if [ "$LOCKING_TYPE" = "2" ] && [ -z "$LOCKINGLIBDIR" ] && [ "$have_dir" = "1" ]; then
159         echo "no library_dir specified in $CONFIGFILE"
160         exit 16
161 fi
162
163 # So if we don't have "global {" we need to create one and 
164 # populate it
165
166 if [ "$have_global" = "1" ]
167 then
168     if [ -z "$LOCKING_TYPE" ]; then
169         LOCKING_TYPE=1
170     fi
171     if [ "$LOCKING_TYPE" = "3" ] || [ "$LOCKING_TYPE" = "2" ]; then
172         cat $CONFIGFILE - <<EOF > $TMPFILE
173 global {
174     # Enable locking for cluster LVM
175     locking_type = $LOCKING_TYPE
176     library_dir = "$LOCKINGLIBDIR"
177 EOF
178         if [ $? != 0 ]
179         then
180             echo "failed to create temporary config file, $CONFIGFILE not updated"
181             exit 14
182         fi
183         if [ -n "$LOCKINGLIB" ]; then
184             cat - <<EOF >> $TMPFILE
185     locking_library = "$LOCKINGLIB"
186 EOF
187             if [ $? != 0 ]
188             then
189                 echo "failed to create temporary config file, $CONFIGFILE not updated"
190                 exit 16
191             fi
192         fi
193         cat - <<EOF >> $TMPFILE
194 }
195 EOF
196     fi # if we aren't setting cluster locking, we don't need to create a global section
197
198     if [ $? != 0 ]
199     then
200         echo "failed to create temporary config file, $CONFIGFILE not updated"
201         exit 17
202     fi
203 else
204     #
205     # We have a "global {" section, so add or replace the
206     # locking entries as appropriate
207     #
208
209     if [ -n "$LOCKING_TYPE" ]; then
210         if [ "$have_type" = "0" ] 
211         then
212             SEDCMD=" s/^[[:blank:]]*locking_type[[:blank:]]*=.*/\ \ \ \ locking_type = $LOCKING_TYPE/g"
213         else
214             SEDCMD=" /global[[:blank:]]*{/a\ \ \ \ locking_type = $LOCKING_TYPE"
215         fi
216     fi
217
218     if [ -n "$LOCKINGLIBDIR" ]; then
219         if [ "$have_dir" = "0" ] 
220             then
221             SEDCMD="${SEDCMD}\ns'^[[:blank:]]*library_dir[[:blank:]]*=.*'\ \ \ \ library_dir = \"$LOCKINGLIBDIR\"'g"
222         else
223             SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ library_dir = \"$LOCKINGLIBDIR\""
224         fi
225     fi
226
227     if [ -n "$LOCKINGLIB" ]; then
228         if [ "$have_library" = "0" ]
229             then
230             SEDCMD="${SEDCMD}\ns/^[[:blank:]]*locking_library[[:blank:]]*=.*/\ \ \ \ locking_library = \"$LOCKINGLIB\"/g"
231         else
232             SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ locking_library = \"$LOCKINGLIB\""
233         fi
234     fi
235
236     echo -e $SEDCMD > $SCRIPTFILE
237     sed  <$CONFIGFILE >$TMPFILE -f $SCRIPTFILE
238     if [ $? != 0 ]
239     then
240         echo "sed failed, $CONFIGFILE not updated"
241         exit 15
242     fi
243 fi
244
245 # Now we have a suitably editted config file in a temp place,
246 # backup the original and copy our new one into place.
247
248 cp $CONFIGFILE $CONFIGFILE.lvmconfold
249 if [ $? != 0 ]
250     then
251     echo "failed to backup old config file, $CONFIGFILE not updated"
252     exit 2
253 fi
254
255 cp $TMPFILE $CONFIGFILE
256 if [ $? != 0 ]
257     then
258     echo "failed to copy new config file into place, check $CONFIGFILE is still OK"
259     exit 3
260 fi
261
262 rm -f $SCRIPTFILE $TMPFILE