2010-05-14 15:33:20 +04:00
#!/bin/bash
2004-06-24 12:02:38 +04:00
#
# Edit an lvm.conf file to enable cluster locking.
#
# $1 is the directory where the locking library is installed.
# $2 (optional) is the config file
# $3 (optional) is the locking library name
#
#
PREFIX = $1
LVMCONF = $2
LIB = $3
if [ -z " $PREFIX " ]
then
echo " usage: $0 <prefix> [<config file>] [<library>] "
echo ""
2004-12-01 17:47:31 +03:00
echo "<prefix>|UNDO location of the cluster locking shared library. (no default)"
echo " UNDO will reset the locking back to local"
echo "<config file> name of the LVM config file (default: /etc/lvm/lvm.conf)"
echo "<library> name of the shared library (default: liblvm2clusterlock.so)"
2004-06-24 12:02:38 +04:00
echo ""
exit 0
fi
[ -z " $LVMCONF " ] && LVMCONF = "/etc/lvm/lvm.conf"
[ -z " $LIB " ] && LIB = "liblvm2clusterlock.so"
2004-12-01 17:47:31 +03:00
if [ " $PREFIX " = "UNDO" ]
2004-06-24 12:02:38 +04:00
then
2004-12-01 17:47:31 +03:00
locking_type = "1"
else
locking_type = "2"
if [ " ${ PREFIX : 0 : 1 } " != "/" ]
then
echo "Prefix must be an absolute path name (starting with a /)"
exit 12
fi
if [ ! -f " $PREFIX / $LIB " ]
then
echo " $PREFIX / $LIB does not exist, did you do a \"make install\" ? "
exit 11
fi
2004-06-24 12:02:38 +04:00
fi
if [ ! -f " $LVMCONF " ]
then
echo " $LVMCONF does not exist "
exit 10
fi
2017-06-29 23:03:00 +03:00
SCRIPTFILE = $( mktemp -t lvmscript.XXXXXXXXXX)
TMPFILE = $( mktemp -t lvmtmp.XXXXXXXXXX)
2004-06-24 12:02:38 +04:00
# Flags so we know which parts of the file we can replace and which need
# adding. These are return codes from grep, so zero means it IS present!
have_type = 1
have_dir = 1
have_library = 1
have_global = 1
2017-06-29 23:07:49 +03:00
grep -q '^[[:blank:]]*locking_type[[:blank:]]*=' " $LVMCONF "
2004-06-24 12:02:38 +04:00
have_type = $?
2017-06-29 23:07:49 +03:00
grep -q '^[[:blank:]]*library_dir[[:blank:]]*=' " $LVMCONF "
2004-06-24 12:02:38 +04:00
have_dir = $?
2017-06-29 23:07:49 +03:00
grep -q '^[[:blank:]]*locking_library[[:blank:]]*=' " $LVMCONF "
2004-06-24 12:02:38 +04:00
have_library = $?
# Those options are in section "global {" so we must have one if any are present.
2017-06-29 23:07:49 +03:00
if [ " $have_type " = 0 ] || [ " $have_dir " = 0 ] || [ " $have_library " = 0 ] ; then
2004-06-24 12:02:38 +04:00
# See if we can find it...
2017-06-29 23:07:49 +03:00
grep -q '^[[:blank:]]*global[[:blank:]]*{' " $LVMCONF "
2004-06-24 12:02:38 +04:00
have_global = $?
if [ " $have_global " = "1" ]
then
echo "global keys but no 'global {' found, can't edit file"
exit 12
fi
fi
# So if we don't have "global {" we need to create one and
# populate it
if [ " $have_global " = "1" ]
then
2017-06-29 23:07:49 +03:00
cat " $LVMCONF " - <<EOF > "$TMPFILE "
2004-06-24 12:02:38 +04:00
global {
# Enable locking for cluster LVM
2004-12-01 17:47:31 +03:00
locking_type = $locking_type
2004-06-24 12:02:38 +04:00
library_dir = " $PREFIX "
locking_library = " $LIB "
}
EOF
if [ $? != 0 ]
then
echo " failed to create temporary config file, $LVMCONF not updated "
exit 1
fi
else
#
# We have a "global {" section, so add or replace the
# locking entries as appropriate
#
if [ " $have_type " = "0" ]
then
2004-12-01 17:47:31 +03:00
SEDCMD = " s/^[[:blank:]]*locking_type[[:blank:]]*=.*/\ \ \ \ locking_type = $locking_type /g "
2004-06-24 12:02:38 +04:00
else
SEDCMD = " /global[[:blank:]]*{/a\ \ \ \ locking_type = 2"
fi
if [ " $have_dir " = "0" ]
then
SEDCMD = " ${ SEDCMD } \ns'^[[:blank:]]*library_dir[[:blank:]]*=.*'\ \ \ \ library_dir = \" $PREFIX \"'g "
else
SEDCMD = " ${ SEDCMD } \n/global[[:blank:]]*{/a\ \ \ \ library_dir = \" $PREFIX \" "
fi
if [ " $have_library " = "0" ]
then
SEDCMD = " ${ SEDCMD } \ns/^[[:blank:]]*locking_library[[:blank:]]*=.*/\ \ \ \ locking_library = \" $LIB \"/g "
else
SEDCMD = " ${ SEDCMD } \n/global[[:blank:]]*{/a\ \ \ \ locking_library = \" $LIB \" "
fi
2017-06-29 23:07:49 +03:00
echo -e " $SEDCMD " > " $SCRIPTFILE "
sed <" $LVMCONF " >" $TMPFILE " -f " $SCRIPTFILE "
2004-06-24 12:02:38 +04:00
if [ $? != 0 ]
then
echo " sed failed, $LVMCONF not updated "
exit 1
fi
fi
# Now we have a suitably editted config file in a temp place,
# backup the original and copy our new one into place.
2017-06-29 23:07:49 +03:00
cp " $LVMCONF " " $LVMCONF .nocluster "
2004-06-24 12:02:38 +04:00
if [ $? != 0 ]
then
echo " failed to backup old config file, $LVMCONF not updated "
exit 2
fi
2017-06-29 23:07:49 +03:00
cp " $TMPFILE " " $LVMCONF "
2004-06-24 12:02:38 +04:00
if [ $? != 0 ]
then
echo " failed to copy new config file into place, check $LVMCONF is still OK "
exit 3
fi
2017-06-29 23:07:49 +03:00
rm -f " $SCRIPTFILE " " $TMPFILE "
2004-06-24 12:02:38 +04:00