cluster-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
From: Marian Marinov <mm@yuhu.biz>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH] gfs2 init script
Date: Tue, 11 Nov 2008 12:44:09 +0200	[thread overview]
Message-ID: <200811111244.09548.mm@yuhu.biz> (raw)
In-Reply-To: <200811101805.43569.mm@yuhu.biz>

Hello,

My previous script was not tested at all. Today I tested it and rewrote many 
parts.

This patch now changes the output of the status option and have everything 
from the old patch tested and it should work.

Sorry for the very stupid previous patch.

diff --git a/gfs2/init.d/gfs2.in b/gfs2/init.d/gfs2.in
old mode 100644
new mode 100755
index 35688a0..cb9bee9
--- 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()
-{
-    echo -ne "[  OK  ]\r"
+function local_success() {
+    echo -ne "[  OK  ]\n"
 }

-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,77 @@ 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 /proc/modules
+       # check if gfs2 module is loaded
+       # check if we have gfs2 as a module
+       if [ -f /proc/modules ] &&
+               ( ! grep gfs2 /proc/modules > /dev/null ) &&
+               ( modprobe -l|grep gfs2 > /dev/null ); then
+                       echo -n "Loading gfs2 module: "
+                       if ( modprobe gfs2 ); then
+                               local_success
+                       else
+                               local_failure 6
+                       fi
+       fi
+       # check if we have gfs2 mountpoints in /etc/fstab
+       # check if these mountpoints are not mounted already
+       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 ( mount -a -t gfs2 ); then
+                       if ( check_mounted && touch $LOCK_FILE ); then
+                               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" ]; 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 ( umount -a -t gfs2 ); then
+                               local_success
                        else
-                               $failure
-                               echo
+                               local_failure
                        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 ( umount -l -a -t gfs2 ); 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 +140,67 @@ 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
-               }
-               [ -n "$GFS2MTAB" ] && {
-                      echo "Active GFS2 mountpoints: "
-                     for fs in $GFS2MTAB; do echo $fs ; done
+function status_gfs() {
+       # check if we have module for gfs2
+       if [ -f /proc/modules ] && ( modprobe -l|grep gfs2 > /dev/null ); then
+               echo -ne 'GFS2 module: '
+               if ( grep gfs2 /proc/modules > /dev/null ); then
+                       echo "[   loaded   ]"
+               else
+                       echo "[ not loaded ]"
+               fi
+       fi
+       if [ -f /proc/mounts ]; then
+               [ -n "$GFS2FSTAB" ] && {
+                       echo "Configured GFS2 mountpoints: "
+                       for fs in $GFS2FSTAB; do
+                               echo -ne "\t$fs ";
+                               if ( grep "$fs" /proc/mounts > /dev/null ); 
then
+                                       echo "[   mounted   ]"
+                               else
+                                       echo "[ not mounted ]"
+                               fi
+                       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




Best regards
Marian Marinov




  reply	other threads:[~2008-11-11 10:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-10 16:05 [Cluster-devel] [PATCH] gfs2 init script Marian Marinov
2008-11-11 10:44 ` Marian Marinov [this message]
2008-11-13  6:15   ` Fabio M. Di Nitto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200811111244.09548.mm@yuhu.biz \
    --to=mm@yuhu.biz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).