3 # Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
5 # This file is part of the lvm2 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
21 echo "usage: $0 <command>"
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>]"
28 echo "Global options:"
29 echo "Config file location: --file <configfile>"
80 function validate_args
82 [ -z "$CONFIGFILE" ] && CONFIGFILE="/etc/lvm/lvm.conf"
84 if [ ! -f "$CONFIGFILE" ]
86 echo "$CONFIGFILE does not exist"
90 if [ -z "$LOCKING_TYPE" ] && [ -z "$LOCKINGLIBDIR" ]; then
95 if [ -n "$LOCKINGLIBDIR" ]; then
97 if [ "${LOCKINGLIBDIR:0:1}" != "/" ]
99 echo "Prefix must be an absolute path name (starting with a /)"
103 if [ -n "$LOCKINGLIB" ] && [ ! -f "$LOCKINGLIBDIR/$LOCKINGLIB" ]
105 echo "$LOCKINGLIBDIR/$LOCKINGLIB does not exist, did you do a \"make install\" ?"
111 if [ "$LOCKING_TYPE" = "1" ] && [ -n "$LOCKINGLIBDIR" -o -n "$LOCKINGLIB" ]; then
112 echo "Superfluous locking lib parameter, ignoring"
123 SCRIPTFILE=/etc/lvm/.lvmconf-script.tmp
124 TMPFILE=/etc/lvm/.lvmconf-tmp.tmp
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!
134 grep -q '^[[:blank:]]*locking_type[[:blank:]]*=' $CONFIGFILE
137 grep -q '^[[:blank:]]*library_dir[[:blank:]]*=' $CONFIGFILE
140 grep -q '^[[:blank:]]*locking_library[[:blank:]]*=' $CONFIGFILE
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" ]
147 # See if we can find it...
148 grep -q '^[[:blank:]]*global[[:blank:]]*{' $CONFIGFILE
151 if [ "$have_global" = "1" ]
153 echo "global keys but no 'global {' found, can't edit file"
158 if [ "$LOCKING_TYPE" = "2" ] && [ -z "$LOCKINGLIBDIR" ] && [ "$have_dir" = "1" ]; then
159 echo "no library_dir specified in $CONFIGFILE"
163 # So if we don't have "global {" we need to create one and
166 if [ "$have_global" = "1" ]
168 if [ -z "$LOCKING_TYPE" ]; then
171 if [ "$LOCKING_TYPE" = "3" ] || [ "$LOCKING_TYPE" = "2" ]; then
172 cat $CONFIGFILE - <<EOF > $TMPFILE
174 # Enable locking for cluster LVM
175 locking_type = $LOCKING_TYPE
176 library_dir = "$LOCKINGLIBDIR"
180 echo "failed to create temporary config file, $CONFIGFILE not updated"
183 if [ -n "$LOCKINGLIB" ]; then
184 cat - <<EOF >> $TMPFILE
185 locking_library = "$LOCKINGLIB"
189 echo "failed to create temporary config file, $CONFIGFILE not updated"
193 cat - <<EOF >> $TMPFILE
196 fi # if we aren't setting cluster locking, we don't need to create a global section
200 echo "failed to create temporary config file, $CONFIGFILE not updated"
205 # We have a "global {" section, so add or replace the
206 # locking entries as appropriate
209 if [ -n "$LOCKING_TYPE" ]; then
210 if [ "$have_type" = "0" ]
212 SEDCMD=" s/^[[:blank:]]*locking_type[[:blank:]]*=.*/\ \ \ \ locking_type = $LOCKING_TYPE/g"
214 SEDCMD=" /global[[:blank:]]*{/a\ \ \ \ locking_type = $LOCKING_TYPE"
218 if [ -n "$LOCKINGLIBDIR" ]; then
219 if [ "$have_dir" = "0" ]
221 SEDCMD="${SEDCMD}\ns'^[[:blank:]]*library_dir[[:blank:]]*=.*'\ \ \ \ library_dir = \"$LOCKINGLIBDIR\"'g"
223 SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ library_dir = \"$LOCKINGLIBDIR\""
227 if [ -n "$LOCKINGLIB" ]; then
228 if [ "$have_library" = "0" ]
230 SEDCMD="${SEDCMD}\ns/^[[:blank:]]*locking_library[[:blank:]]*=.*/\ \ \ \ locking_library = \"$LOCKINGLIB\"/g"
232 SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ locking_library = \"$LOCKINGLIB\""
236 echo -e $SEDCMD > $SCRIPTFILE
237 sed <$CONFIGFILE >$TMPFILE -f $SCRIPTFILE
240 echo "sed failed, $CONFIGFILE not updated"
245 # Now we have a suitably editted config file in a temp place,
246 # backup the original and copy our new one into place.
248 cp $CONFIGFILE $CONFIGFILE.lvmconfold
251 echo "failed to backup old config file, $CONFIGFILE not updated"
255 cp $TMPFILE $CONFIGFILE
258 echo "failed to copy new config file into place, check $CONFIGFILE is still OK"
262 rm -f $SCRIPTFILE $TMPFILE