[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Cluster-devel] [PATCH] gfs2 init script



Hello,
I revised the gfs2 init script in the hope to make it more LSB conforming.

1. I tried to make the script using only one style of codding
2. Added more secure path
3. Added permission check at the beginning
4. Changed the runlevels on which this script should be run
5. Added function check_mounted. This function is used to determine if all 
gfs2 mountpoints have been actually mounted. 
6. Cleared the case body from all code 
7. Created stop_gfs/start_gfs/status_gfs function which are then used in the 
case structure 
8. Added option in the script to load the necessary module if it is not 
already loaded. Should I add lock_dlm and lock_nolock to the checked 
modules ?
9. Added check if there is at all gfs2 mountpoint mounted on stop
10. Added check if the modprobe -r has finshed correctly 

All are written but have not been tested since I don't have gfs2 cluster 
currently running on my home cluster.

Please provide me with any feedback and possibly a ToDo for the init scripts. 
I'll be more then glad to assist with writing perl/shell scripts for GFS.

This is the patch:

diff --git a/gfs2/init.d/gfs2.in b/gfs2/init.d/gfs2.in
index 35688a0..e11184b 100644
--- a/gfs2/init.d/gfs2.in
+++ b/gfs2/init.d/gfs2.in
@@ -2,7 +2,7 @@
 #
 # gfs2 mount/unmount helper
 #
-# chkconfig: - 26 74
+# chkconfig: 2345 26 74
 # description: mount/unmount gfs2 filesystems configured in /etc/fstab

 ### BEGIN INIT INFO
@@ -15,6 +15,15 @@
 # Description:         mount/unmount gfs2 filesystems configured 
in /etc/fstab
 ### END INIT INFO

+# set secure PATH
+PATH="/bin:/usr/bin:/sbin:/usr/sbin"
+
+# Check privileges
+if [ "$1" != 'status' ] && [ "$(whoami)" != 'root' ]; then
+       echo "You are not allowed to run this script."
+       exit 4;
+fi
+
 # rpm based distros
 if [ -d /etc/sysconfig ]; then
        [ -f @INITDDIR@/functions ] && . @INITDDIR@/functions
@@ -34,14 +43,16 @@ if [ -d /etc/default ]; then
        failure=failure
 fi

-local_success()
-{
+function local_success() {
     echo -ne "[  OK  ]\r"
 }

-local_failure()
-{
+function local_failure() {
     echo -ne "[FAILED]\r"
+       # if we have exit code, use it
+       if [ "$1" = [0-9] ]; then
+               exit $1
+       fi
 }

 #
@@ -50,61 +61,87 @@ local_failure()
 GFS2FSTAB=$(LC_ALL=C awk '!/^#/ && $3 == "gfs2" && $4 !~ /noauto/ { print 
$2 }' /etc/fstab)
 GFS2MTAB=$(LC_ALL=C awk '!/^#/ && $3 == "gfs2" && $2 != "/" { print 
$2 }' /proc/mounts)

-# See how we were called.
-case "$1" in
-  start)
-        if [ -n "$GFS2FSTAB" ]
-       then
+function check_mounted() {
+       mounted=0
+       count=0
+       for mountpoint in $GFS2FSTAB; do
+               let count++
+               if ( grep " $mountpoint " /proc/mounts > /dev/null ); then
+                       let mounted++
+               fi
+       done
+       if [ $(($count-$mounted)) != 0 ]; then
+               return 1
+       else
+               return 0
+       fi
+}
+
+function start_gfs() {
+       # check if we have gfs2 as a module
+       if ( modprobe -l|grep gfs2 > /dev/null ); then
+               echo "Loading gfs2 module: "
+               # check if we have /proc/modules
+               if [ -f /proc/modules ]; then
+                       # check if gfs2 module is loaded
+                       if ( ! grep gfs2 /proc/modules > /dev/null ); then
+                               modprobe gfs2
+                               if [ "$?" = 0 ]; then
+                                       local_success
+                               else
+                                       local_failure 6
+                               fi
+                       else
+                               local_failure 6
+                       fi
+               else
+                       local_failure 6
+               fi
+       fi
+       if [ -n "$GFS2FSTAB" ] && ( check_mounted ); then
                echo -n "Mounting GFS2 filesystems: "
                mount -a -t gfs2
                rtrn=$?
                if [ $rtrn = 0 ]; then
-                       touch $LOCK_FILE
-                       $success
-                       echo
+                       if ( check_mounted ); then
+                               touch $LOCK_FILE
+                               local_success
+                       else
+                               local_failure 7
+                       fi
                else
-                       $failure
-                       echo
-               fi
+                       local_failure 1
+               fi
        fi
-       ;;
+       exit 0
+}

-  stop)
-       if [ -n "$GFS2MTAB" ]
-       then
+function stop_gfs() {
+       if [ -n "$GFS2MTAB" ] && ( ! check_mounted ); then
                sig=
                retry=6
-               remaining=`LC_ALL=C awk '!/^#/ && $3 == "gfs2" && $2 != "/" 
{print $2}' /proc/mounts`
-               while [ -n "$remaining" -a "$retry" -gt 0 ]
-               do
+               remaining=$(LC_ALL=C awk '!/^#/ && $3 == "gfs2" && $2 != "/" 
{print $2}' /proc/mounts)
+               while [ -n "$remaining" -a "$retry" -gt 0 ]; do
                        echo -n "Unmounting GFS2 filesystems: "
                        umount -a -t gfs2
-                       rtrn=$?
-                       if [ $rtrn = 0 ]; then
-                               $success
-                               echo
+                       if [ "$?" = 0 ]; then
+                               local_success
                        else
-                               $failure
-                               echo
+                               local_failure 1
                        fi
-
-                       if [ $retry -eq 0 ]
-                       then
+                       if [ $retry -eq 0 ]; then
                                echo -n "Unmounting GFS2 filesystems (lazy): "
                                umount -l -a -t gfs2
-                               rtrn=$?
-                               if [ $rtrn = 0 ]; then
-                                       $success
-                                       echo
+                               if [ "$?" = 0 ]; then
+                                       local_success
                                else
-                                       $failure
-                                       echo
+                                       local_failure 1
                                fi
                                break
                        fi

                        sleep 2
-                       remaining=`LC_ALL=C awk '!/^#/ && $3 == "gfs2" && 
$2 != "/" {print $2}' /proc/mounts`
+                       remaining=$(LC_ALL=C awk '!/^#/ && $3 == "gfs2" && 
$2 != "/" {print $2}' /proc/mounts)
                        [ -z "$remaining" ] && break
                        fuser -k -m $sig $remaining > /dev/null 2>&1
                        sleep 10
@@ -113,36 +150,55 @@ case "$1" in
                done
        fi

-       modprobe -r gfs2
-       rm -f $LOCK_FILE
-       ;;
+       if ( modprobe -r gfs2 && rm -f $LOCK_FILE ) ; then
+               exit 0
+       else
+               exit 1
+       fi
+}

-  status)
-       if [ -f /proc/mounts ]
-       then
-               [ -n "$GFS2FSTAB" ] && {
-                    echo "Configured GFS2 mountpoints: "
-                    for fs in $GFS2FSTAB; do echo $fs ; done
+function status_gfs() {
+       if [ -f /proc/mounts ]; then
+               [ -n "$GFS2FSTAB" ] && {
+                       echo "Configured GFS2 mountpoints: "
+                       for fs in $GFS2FSTAB; do echo $fs ; done
                }
                [ -n "$GFS2MTAB" ] && {
-                      echo "Active GFS2 mountpoints: "
-                     for fs in $GFS2MTAB; do echo $fs ; done
+                       echo "Active GFS2 mountpoints: "
+                       for fs in $GFS2MTAB; do echo $fs ; done
                }
+               exit 0
        else
                echo "/proc filesystem unavailable"
+               exit 4
        fi
+}
+
+# See how we were called.
+case "$1" in
+  start)
+       start_gfs
+       ;;
+
+  stop)
+       stop_gfs
+       ;;
+
+  status)
+       status_gfs
        ;;

   restart)
-       $0 stop
-       $0 start
+       stop_gfs
+       start_gfs
        ;;

   reload)
-        $0 start
+       start_gfs
        ;;
+
   *)
-       echo $"Usage: $0 {start|stop|restart|reload|status}"
+       echo "Usage: $0 {start|stop|restart|reload|status}"
        exit 1
 esac

-------------------------
If you want you could download the whole script from here:


Best regards 
Marian Marinov


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]