Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / scripts / lvmconf.sh
blobf5c431314c34f6373f4114adbc35fb3e08c182e9
1 #!/bin/sh
3 # Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 # This file is part of the lvm2-cluster package.
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.
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
16 # Edit an lvm.conf file to adjust various properties
19 function usage
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 ""
34 function parse_args
36 while [ -n "$1" ]; do
37 case $1 in
38 --enable-cluster)
39 LOCKING_TYPE=2
40 shift
42 --disable-cluster)
43 LOCKING_TYPE=1
44 shift
46 --lockinglibdir)
47 if [ -n "$2" ]; then
48 LOCKINGLIBDIR=$2
49 shift 2
50 else
51 usage
52 exit 1
55 --lockinglib)
56 if [ -n "$2" ]; then
57 LOCKINGLIB=$2
58 shift 2
59 else
60 usage
61 exit 1
64 --file)
65 if [ -n "$2" ]; then
66 CONFIGFILE=$2
67 shift 2
68 else
69 usage
70 exit 1
74 usage
75 exit 1
76 esac
77 done
80 function validate_args
82 [ -z "$CONFIGFILE" ] && CONFIGFILE="/etc/lvm/lvm.conf"
84 if [ ! -f "$CONFIGFILE" ]
85 then
86 echo "$CONFIGFILE does not exist"
87 exit 10
90 if [ -z "$LOCKING_TYPE" ] && [ -z "$LOCKINGLIBDIR" ]; then
91 usage
92 exit 1
95 if [ -n "$LOCKINGLIBDIR" ]; then
97 [ -z "$LOCKINGLIB" ] && LOCKINGLIB="liblvm2clusterlock.so"
99 if [ "${LOCKINGLIBDIR:0:1}" != "/" ]
100 then
101 echo "Prefix must be an absolute path name (starting with a /)"
102 exit 12
105 if [ ! -f "$LOCKINGLIBDIR/$LOCKINGLIB" ]
106 then
107 echo "$LOCKINGLIBDIR/$LOCKINGLIB does not exist, did you do a \"make install\" ?"
108 exit 11
113 if [ "$LOCKING_TYPE" = "1" ] && [ -n "$LOCKINGLIBDIR" -o -n "$LOCKINGLIB" ]; then
114 echo "Superfluous locking lib parameter, ignoring"
118 umask 0077
120 parse_args "$@"
122 validate_args
125 SCRIPTFILE=/etc/lvm/.lvmconf-script.tmp
126 TMPFILE=/etc/lvm/.lvmconf-tmp.tmp
129 # Flags so we know which parts of the file we can replace and which need
130 # adding. These are return codes from grep, so zero means it IS present!
131 have_type=1
132 have_dir=1
133 have_library=1
134 have_global=1
136 grep -q '^[[:blank:]]*locking_type[[:blank:]]*=' $CONFIGFILE
137 have_type=$?
139 grep -q '^[[:blank:]]*library_dir[[:blank:]]*=' $CONFIGFILE
140 have_dir=$?
142 grep -q '^[[:blank:]]*locking_library[[:blank:]]*=' $CONFIGFILE
143 have_library=$?
145 # Those options are in section "global {" so we must have one if any are present.
146 if [ "$have_type" = "0" -o "$have_dir" = "0" -o "$have_library" = "0" ]
147 then
149 # See if we can find it...
150 grep -q '^[[:blank:]]*global[[:blank:]]*{' $CONFIGFILE
151 have_global=$?
153 if [ "$have_global" = "1" ]
154 then
155 echo "global keys but no 'global {' found, can't edit file"
156 exit 13
160 if [ "$LOCKING_TYPE" = "2" ] && [ -z "$LOCKINGLIBDIR" ] && [ "$have_dir" = "1" ]; then
161 echo "no library_dir specified in $CONFIGFILE"
162 exit 16
165 # So if we don't have "global {" we need to create one and
166 # populate it
168 if [ "$have_global" = "1" ]
169 then
170 if [ -z "$LOCKING_TYPE" ]; then
171 LOCKING_TYPE=1
173 if [ "$LOCKING_TYPE" = "2" ]; then
174 cat $CONFIGFILE - <<EOF > $TMPFILE
175 global {
176 # Enable locking for cluster LVM
177 locking_type = $LOCKING_TYPE
178 library_dir = "$LOCKINGLIBDIR"
179 locking_library = "$LOCKINGLIB"
182 fi # if we aren't setting cluster locking, we don't need to create a global section
184 if [ $? != 0 ]
185 then
186 echo "failed to create temporary config file, $CONFIGFILE not updated"
187 exit 14
189 else
191 # We have a "global {" section, so add or replace the
192 # locking entries as appropriate
195 if [ -n "$LOCKING_TYPE" ]; then
196 if [ "$have_type" = "0" ]
197 then
198 SEDCMD=" s/^[[:blank:]]*locking_type[[:blank:]]*=.*/\ \ \ \ locking_type = $LOCKING_TYPE/g"
199 else
200 SEDCMD=" /global[[:blank:]]*{/a\ \ \ \ locking_type = $LOCKING_TYPE"
204 if [ -n "$LOCKINGLIBDIR" ]; then
205 if [ "$have_dir" = "0" ]
206 then
207 SEDCMD="${SEDCMD}\ns'^[[:blank:]]*library_dir[[:blank:]]*=.*'\ \ \ \ library_dir = \"$LOCKINGLIBDIR\"'g"
208 else
209 SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ library_dir = \"$LOCKINGLIBDIR\""
212 if [ "$have_library" = "0" ]
213 then
214 SEDCMD="${SEDCMD}\ns/^[[:blank:]]*locking_library[[:blank:]]*=.*/\ \ \ \ locking_library = \"$LOCKINGLIB\"/g"
215 else
216 SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ locking_library = \"$LOCKINGLIB\""
220 if [ "$LOCKING_TYPE" = "1" ]; then
221 # if we're not using cluster locking, remove the library dir and locking library name
222 if [ "$have_dir" = "0" ]
223 then
224 SEDCMD="${SEDCMD}\n/^[[:blank:]]*library_dir[[:blank:]]*=.*/d"
227 if [ "$have_library" = "0" ]
228 then
229 SEDCMD="${SEDCMD}\n/^[[:blank:]]*locking_library[[:blank:]]*=.*/d"
233 echo -e $SEDCMD > $SCRIPTFILE
234 sed <$CONFIGFILE >$TMPFILE -f $SCRIPTFILE
235 if [ $? != 0 ]
236 then
237 echo "sed failed, $CONFIGFILE not updated"
238 exit 15
242 # Now we have a suitably editted config file in a temp place,
243 # backup the original and copy our new one into place.
245 cp $CONFIGFILE $CONFIGFILE.lvmconfold
246 if [ $? != 0 ]
247 then
248 echo "failed to backup old config file, $CONFIGFILE not updated"
249 exit 2
252 cp $TMPFILE $CONFIGFILE
253 if [ $? != 0 ]
254 then
255 echo "failed to copy new config file into place, check $CONFIGFILE is still OK"
256 exit 3
259 rm -f $SCRIPTFILE $TMPFILE