3 # Edit an lvm.conf file to enable cluster locking.
5 # $1 is the directory where the locking library is installed.
6 # $2 (optional) is the config file
7 # $3 (optional) is the locking library name
16 echo "usage: $0 <prefix> [<config file>] [<library>]"
18 echo "<prefix>|UNDO location of the cluster locking shared library. (no default)"
19 echo " UNDO will reset the locking back to local"
20 echo "<config file> name of the LVM config file (default: /etc/lvm/lvm.conf)"
21 echo "<library> name of the shared library (default: liblvm2clusterlock.so)"
26 [ -z "$LVMCONF" ] && LVMCONF
="/etc/lvm/lvm.conf"
27 [ -z "$LIB" ] && LIB
="liblvm2clusterlock.so"
29 if [ "$PREFIX" = "UNDO" ]
35 if [ "${PREFIX:0:1}" != "/" ]
37 echo "Prefix must be an absolute path name (starting with a /)"
41 if [ ! -f "$PREFIX/$LIB" ]
43 echo "$PREFIX/$LIB does not exist, did you do a \"make install\" ?"
48 if [ ! -f "$LVMCONF" ]
50 echo "$LVMCONF does not exist"
55 SCRIPTFILE
=`mktemp -t lvmscript.XXXXXXXXXX`
56 TMPFILE
=`mktemp -t lvmtmp.XXXXXXXXXX`
59 # Flags so we know which parts of the file we can replace and which need
60 # adding. These are return codes from grep, so zero means it IS present!
66 grep -q '^[[:blank:]]*locking_type[[:blank:]]*=' $LVMCONF
69 grep -q '^[[:blank:]]*library_dir[[:blank:]]*=' $LVMCONF
72 grep -q '^[[:blank:]]*locking_library[[:blank:]]*=' $LVMCONF
75 # Those options are in section "global {" so we must have one if any are present.
76 if [ "$have_type" = "0" -o "$have_dir" = "0" -o "$have_library" = "0" ]
79 # See if we can find it...
80 grep -q '^[[:blank:]]*global[[:blank:]]*{' $LVMCONF
83 if [ "$have_global" = "1" ]
85 echo "global keys but no 'global {' found, can't edit file"
90 # So if we don't have "global {" we need to create one and
93 if [ "$have_global" = "1" ]
95 cat $LVMCONF - <<EOF > $TMPFILE
97 # Enable locking for cluster LVM
98 locking_type = $locking_type
99 library_dir = "$PREFIX"
100 locking_library = "$LIB"
105 echo "failed to create temporary config file, $LVMCONF not updated"
110 # We have a "global {" section, so add or replace the
111 # locking entries as appropriate
114 if [ "$have_type" = "0" ]
116 SEDCMD
=" s/^[[:blank:]]*locking_type[[:blank:]]*=.*/\ \ \ \ locking_type = $locking_type/g"
118 SEDCMD
=" /global[[:blank:]]*{/a\ \ \ \ locking_type = 2"
121 if [ "$have_dir" = "0" ]
123 SEDCMD
="${SEDCMD}\ns'^[[:blank:]]*library_dir[[:blank:]]*=.*'\ \ \ \ library_dir = \"$PREFIX\"'g"
125 SEDCMD
="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ library_dir = \"$PREFIX\""
128 if [ "$have_library" = "0" ]
130 SEDCMD
="${SEDCMD}\ns/^[[:blank:]]*locking_library[[:blank:]]*=.*/\ \ \ \ locking_library = \"$LIB\"/g"
132 SEDCMD
="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ locking_library = \"$LIB\""
135 echo -e $SEDCMD > $SCRIPTFILE
136 sed <$LVMCONF >$TMPFILE -f $SCRIPTFILE
139 echo "sed failed, $LVMCONF not updated"
144 # Now we have a suitably editted config file in a temp place,
145 # backup the original and copy our new one into place.
147 cp $LVMCONF $LVMCONF.nocluster
150 echo "failed to backup old config file, $LVMCONF not updated"
157 echo "failed to copy new config file into place, check $LVMCONF is still OK"
161 rm -f $SCRIPTFILE $TMPFILE