]> sourceware.org Git - lvm2.git/blob - scripts/clvmd_fix_conf.sh
thin: tighten discard string conversions
[lvm2.git] / scripts / clvmd_fix_conf.sh
1 #!/bin/bash
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 #
10 PREFIX=$1
11 LVMCONF=$2
12 LIB=$3
13
14 if [ -z "$PREFIX" ]
15 then
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
24 fi
25
26 [ -z "$LVMCONF" ] && LVMCONF="/etc/lvm/lvm.conf"
27 [ -z "$LIB" ] && LIB="liblvm2clusterlock.so"
28
29 if [ "$PREFIX" = "UNDO" ]
30 then
31 locking_type="1"
32 else
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
46 fi
47
48 if [ ! -f "$LVMCONF" ]
49 then
50 echo "$LVMCONF does not exist"
51 exit 10
52 fi
53
54
55 SCRIPTFILE=`mktemp -t lvmscript.XXXXXXXXXX`
56 TMPFILE=`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!
61 have_type=1
62 have_dir=1
63 have_library=1
64 have_global=1
65
66 grep -q '^[[:blank:]]*locking_type[[:blank:]]*=' $LVMCONF
67 have_type=$?
68
69 grep -q '^[[:blank:]]*library_dir[[:blank:]]*=' $LVMCONF
70 have_dir=$?
71
72 grep -q '^[[:blank:]]*locking_library[[:blank:]]*=' $LVMCONF
73 have_library=$?
74
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" ]
77 then
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
88 fi
89
90 # So if we don't have "global {" we need to create one and
91 # populate it
92
93 if [ "$have_global" = "1" ]
94 then
95 cat $LVMCONF - <<EOF > $TMPFILE
96 global {
97 # Enable locking for cluster LVM
98 locking_type = $locking_type
99 library_dir = "$PREFIX"
100 locking_library = "$LIB"
101 }
102 EOF
103 if [ $? != 0 ]
104 then
105 echo "failed to create temporary config file, $LVMCONF not updated"
106 exit 1
107 fi
108 else
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
142 fi
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
147 cp $LVMCONF $LVMCONF.nocluster
148 if [ $? != 0 ]
149 then
150 echo "failed to backup old config file, $LVMCONF not updated"
151 exit 2
152 fi
153
154 cp $TMPFILE $LVMCONF
155 if [ $? != 0 ]
156 then
157 echo "failed to copy new config file into place, check $LVMCONF is still OK"
158 exit 3
159 fi
160
161 rm -f $SCRIPTFILE $TMPFILE
162
This page took 0.068017 seconds and 5 git commands to generate.