1#!/bin/sh
2#
3# Edit an lvm.conf file to enable cluster locking.
4#
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
8#
9#
10PREFIX=$1
11LVMCONF=$2
12LIB=$3
13
14if [ -z "$PREFIX" ]
15then
16  echo "usage: $0 <prefix> [<config file>] [<library>]"
17  echo ""
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)"
22  echo ""
23  exit 0
24fi
25
26[ -z "$LVMCONF" ] && LVMCONF="/etc/lvm/lvm.conf"
27[ -z "$LIB" ] && LIB="liblvm2clusterlock.so"
28
29if [ "$PREFIX" = "UNDO" ]
30then
31  locking_type="1"
32else
33  locking_type="2"
34
35  if [ "${PREFIX:0:1}" != "/" ]
36  then
37    echo "Prefix must be an absolute path name (starting with a /)"
38    exit 12
39  fi
40
41  if [ ! -f "$PREFIX/$LIB" ]
42  then
43    echo "$PREFIX/$LIB does not exist, did you do a \"make install\" ?"
44    exit 11
45  fi
46fi
47
48if [ ! -f "$LVMCONF" ]
49then
50  echo "$LVMCONF does not exist"
51  exit 10
52fi
53
54
55SCRIPTFILE=`mktemp -t lvmscript.XXXXXXXXXX`
56TMPFILE=`mktemp -t lvmtmp.XXXXXXXXXX`
57
58
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!
61have_type=1
62have_dir=1
63have_library=1
64have_global=1
65
66grep -q '^[[:blank:]]*locking_type[[:blank:]]*=' $LVMCONF
67have_type=$?
68
69grep -q '^[[:blank:]]*library_dir[[:blank:]]*=' $LVMCONF
70have_dir=$?
71
72grep -q '^[[:blank:]]*locking_library[[:blank:]]*=' $LVMCONF
73have_library=$?
74
75# Those options are in section "global {" so we must have one if any are present.
76if [ "$have_type" = "0" -o "$have_dir" = "0" -o "$have_library" = "0" ]
77then
78
79    # See if we can find it...
80    grep -q '^[[:blank:]]*global[[:blank:]]*{' $LVMCONF
81    have_global=$?
82
83    if [ "$have_global" = "1" ]
84	then
85	echo "global keys but no 'global {' found, can't edit file"
86	exit 12
87    fi
88fi
89
90# So if we don't have "global {" we need to create one and
91# populate it
92
93if [ "$have_global" = "1" ]
94then
95    cat $LVMCONF - <<EOF > $TMPFILE
96global {
97    # Enable locking for cluster LVM
98    locking_type = $locking_type
99    library_dir = "$PREFIX"
100    locking_library = "$LIB"
101}
102EOF
103    if [ $? != 0 ]
104    then
105	echo "failed to create temporary config file, $LVMCONF not updated"
106	exit 1
107    fi
108else
109    #
110    # We have a "global {" section, so add or replace the
111    # locking entries as appropriate
112    #
113
114    if [ "$have_type" = "0" ]
115    then
116	SEDCMD=" s/^[[:blank:]]*locking_type[[:blank:]]*=.*/\ \ \ \ locking_type = $locking_type/g"
117    else
118	SEDCMD=" /global[[:blank:]]*{/a\ \ \ \ locking_type = 2"
119    fi
120
121    if [ "$have_dir" = "0" ]
122    then
123	SEDCMD="${SEDCMD}\ns'^[[:blank:]]*library_dir[[:blank:]]*=.*'\ \ \ \ library_dir = \"$PREFIX\"'g"
124    else
125	SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ library_dir = \"$PREFIX\""
126    fi
127
128    if [ "$have_library" = "0" ]
129    then
130	SEDCMD="${SEDCMD}\ns/^[[:blank:]]*locking_library[[:blank:]]*=.*/\ \ \ \ locking_library = \"$LIB\"/g"
131    else
132	SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ locking_library = \"$LIB\""
133    fi
134
135    echo -e $SEDCMD > $SCRIPTFILE
136    sed  <$LVMCONF >$TMPFILE -f $SCRIPTFILE
137    if [ $? != 0 ]
138    then
139	echo "sed failed, $LVMCONF not updated"
140	exit 1
141    fi
142fi
143
144# Now we have a suitably editted config file in a temp place,
145# backup the original and copy our new one into place.
146
147cp $LVMCONF $LVMCONF.nocluster
148if [ $? != 0 ]
149    then
150    echo "failed to backup old config file, $LVMCONF not updated"
151    exit 2
152fi
153
154cp $TMPFILE $LVMCONF
155if [ $? != 0 ]
156    then
157    echo "failed to copy new config file into place, check $LVMCONF is still OK"
158    exit 3
159fi
160
161rm -f $SCRIPTFILE $TMPFILE
162
163