Home | History | Annotate | Line # | Download | only in scripts
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
      4 #
      5 # This file is part of the lvm2-cluster package.
      6 #
      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.
     10 #
     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
     14 
     15 #
     16 # Edit an lvm.conf file to adjust various properties
     17 #
     18 
     19 function usage
     20 {
     21     echo "usage: $0 <command>"
     22     echo ""
     23     echo "Commands:"
     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>]"
     27     echo ""
     28     echo "Global options:"
     29     echo "Config file location: --file <configfile>"
     30     echo ""
     31 }
     32 
     33 
     34 function parse_args
     35 {
     36     while [ -n "$1" ]; do
     37         case $1 in
     38             --enable-cluster)
     39                 LOCKING_TYPE=2
     40                 shift
     41                 ;;
     42             --disable-cluster)
     43                 LOCKING_TYPE=1
     44                 shift
     45                 ;;
     46             --lockinglibdir)
     47                 if [ -n "$2" ]; then
     48                     LOCKINGLIBDIR=$2
     49                     shift 2
     50                 else
     51                     usage
     52                     exit 1
     53                 fi
     54                 ;;
     55             --lockinglib)
     56                 if [ -n "$2" ]; then
     57                     LOCKINGLIB=$2
     58                     shift 2
     59                 else
     60                     usage
     61                     exit 1
     62                 fi
     63                 ;;
     64             --file)
     65                 if [ -n "$2" ]; then
     66                     CONFIGFILE=$2
     67                     shift 2
     68                 else
     69                     usage
     70                     exit 1
     71                 fi
     72                 ;;
     73             *)
     74                 usage
     75                 exit 1
     76         esac
     77     done
     78 }
     79 
     80 function validate_args
     81 {
     82     [ -z "$CONFIGFILE" ] && CONFIGFILE="/etc/lvm/lvm.conf"
     83 
     84     if [ ! -f "$CONFIGFILE" ]
     85             then
     86             echo "$CONFIGFILE does not exist"
     87             exit 10
     88     fi
     89 
     90     if [ -z "$LOCKING_TYPE" ] && [ -z "$LOCKINGLIBDIR" ]; then
     91         usage
     92         exit 1
     93     fi
     94 
     95     if [ -n "$LOCKINGLIBDIR" ]; then
     96 
     97         [ -z "$LOCKINGLIB" ] && LOCKINGLIB="liblvm2clusterlock.so"
     98             
     99         if [ "${LOCKINGLIBDIR:0:1}" != "/" ]
    100             then
    101             echo "Prefix must be an absolute path name (starting with a /)"
    102             exit 12
    103         fi
    104     
    105         if [ ! -f "$LOCKINGLIBDIR/$LOCKINGLIB" ]
    106             then
    107             echo "$LOCKINGLIBDIR/$LOCKINGLIB does not exist, did you do a \"make install\" ?"
    108             exit 11
    109         fi
    110         
    111     fi
    112 
    113     if [ "$LOCKING_TYPE" = "1" ] && [ -n "$LOCKINGLIBDIR" -o -n "$LOCKINGLIB" ]; then
    114 	echo "Superfluous locking lib parameter, ignoring"
    115     fi
    116 }
    117 
    118 umask 0077
    119 
    120 parse_args "$@"
    121 
    122 validate_args
    123 
    124 
    125 SCRIPTFILE=/etc/lvm/.lvmconf-script.tmp
    126 TMPFILE=/etc/lvm/.lvmconf-tmp.tmp
    127 
    128 
    129 # Flags so we know which parts of the file we can replace and which need
    130 # adding. These are return codes from grep, so zero means it IS present!
    131 have_type=1
    132 have_dir=1
    133 have_library=1
    134 have_global=1
    135 
    136 grep -q '^[[:blank:]]*locking_type[[:blank:]]*=' $CONFIGFILE
    137 have_type=$?
    138 
    139 grep -q '^[[:blank:]]*library_dir[[:blank:]]*=' $CONFIGFILE
    140 have_dir=$?
    141 
    142 grep -q '^[[:blank:]]*locking_library[[:blank:]]*=' $CONFIGFILE
    143 have_library=$?
    144 
    145 # Those options are in section "global {" so we must have one if any are present.
    146 if [ "$have_type" = "0" -o "$have_dir" = "0" -o "$have_library" = "0" ]
    147 then
    148 
    149     # See if we can find it...
    150     grep -q '^[[:blank:]]*global[[:blank:]]*{' $CONFIGFILE
    151     have_global=$?
    152     
    153     if [ "$have_global" = "1" ] 
    154 	then
    155 	echo "global keys but no 'global {' found, can't edit file"
    156 	exit 13
    157     fi
    158 fi
    159 
    160 if [ "$LOCKING_TYPE" = "2" ] && [ -z "$LOCKINGLIBDIR" ] && [ "$have_dir" = "1" ]; then
    161 	echo "no library_dir specified in $CONFIGFILE"
    162 	exit 16
    163 fi
    164 
    165 # So if we don't have "global {" we need to create one and 
    166 # populate it
    167 
    168 if [ "$have_global" = "1" ]
    169 then
    170     if [ -z "$LOCKING_TYPE" ]; then
    171 	LOCKING_TYPE=1
    172     fi
    173     if [ "$LOCKING_TYPE" = "2" ]; then
    174         cat $CONFIGFILE - <<EOF > $TMPFILE
    175 global {
    176     # Enable locking for cluster LVM
    177     locking_type = $LOCKING_TYPE
    178     library_dir = "$LOCKINGLIBDIR"
    179     locking_library = "$LOCKINGLIB"
    180 }
    181 EOF
    182     fi # if we aren't setting cluster locking, we don't need to create a global section
    183 
    184     if [ $? != 0 ]
    185     then
    186 	echo "failed to create temporary config file, $CONFIGFILE not updated"
    187 	exit 14
    188     fi
    189 else
    190     #
    191     # We have a "global {" section, so add or replace the
    192     # locking entries as appropriate
    193     #
    194 
    195     if [ -n "$LOCKING_TYPE" ]; then
    196 	if [ "$have_type" = "0" ] 
    197 	then
    198 	    SEDCMD=" s/^[[:blank:]]*locking_type[[:blank:]]*=.*/\ \ \ \ locking_type = $LOCKING_TYPE/g"
    199 	else
    200 	    SEDCMD=" /global[[:blank:]]*{/a\ \ \ \ locking_type = $LOCKING_TYPE"
    201 	fi
    202     fi
    203     
    204     if [ -n "$LOCKINGLIBDIR" ]; then
    205         if [ "$have_dir" = "0" ] 
    206             then
    207             SEDCMD="${SEDCMD}\ns'^[[:blank:]]*library_dir[[:blank:]]*=.*'\ \ \ \ library_dir = \"$LOCKINGLIBDIR\"'g"
    208         else
    209             SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ library_dir = \"$LOCKINGLIBDIR\""
    210         fi
    211 
    212         if [ "$have_library" = "0" ] 
    213             then
    214             SEDCMD="${SEDCMD}\ns/^[[:blank:]]*locking_library[[:blank:]]*=.*/\ \ \ \ locking_library = \"$LOCKINGLIB\"/g"
    215         else
    216             SEDCMD="${SEDCMD}\n/global[[:blank:]]*{/a\ \ \ \ locking_library = \"$LOCKINGLIB\""
    217         fi
    218     fi
    219 
    220     if [ "$LOCKING_TYPE" = "1" ]; then
    221         # if we're not using cluster locking, remove the library dir and locking library name
    222         if [ "$have_dir" = "0" ] 
    223             then
    224             SEDCMD="${SEDCMD}\n/^[[:blank:]]*library_dir[[:blank:]]*=.*/d"
    225         fi
    226 
    227         if [ "$have_library" = "0" ] 
    228             then
    229             SEDCMD="${SEDCMD}\n/^[[:blank:]]*locking_library[[:blank:]]*=.*/d"
    230         fi
    231     fi
    232 
    233     echo -e $SEDCMD > $SCRIPTFILE
    234     sed  <$CONFIGFILE >$TMPFILE -f $SCRIPTFILE
    235     if [ $? != 0 ]
    236     then
    237 	echo "sed failed, $CONFIGFILE not updated"
    238 	exit 15
    239     fi
    240 fi
    241 
    242 # Now we have a suitably editted config file in a temp place,
    243 # backup the original and copy our new one into place.
    244 
    245 cp $CONFIGFILE $CONFIGFILE.lvmconfold
    246 if [ $? != 0 ]
    247     then
    248     echo "failed to backup old config file, $CONFIGFILE not updated"
    249     exit 2
    250 fi
    251 
    252 cp $TMPFILE $CONFIGFILE
    253 if [ $? != 0 ]
    254     then
    255     echo "failed to copy new config file into place, check $CONFIGFILE is still OK"
    256     exit 3
    257 fi
    258 
    259 rm -f $SCRIPTFILE $TMPFILE
    260