#!/bin/sh
#
# aoe initialisation
#
# chkconfig:   2345 15 85
# description: aoe module initialisation

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

prog=aoe

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {
    action $"Inserting aoe kernel module" /sbin/modprobe aoe
    rc=$?
    if [ $rc -eq 0 ]; then
      touch $lockfile
      sleep 8
      if [ -n "$LVM_VG" ]; then
        action $"Scanning for LVM volumes" /sbin/vgscan
        action $"Activating LVM volumes" /sbin/vgchange -a y $LVM_VG
        test -n "$AOE_DEBUG" && lvdisplay -c
      fi
      if [ -n "$MOUNT" ]; then
        for i in $MOUNT; do
          action "Mounting aoe volume $i" /bin/mount $i
        done
      fi
      if [ -n "$SERVICES" ]; then
        for i in $SERVICES; do
          /sbin/service $i start
        done
      fi
    fi
    return $rc
}

stop() { 
    if [ -n "$SERVICES" ]; then
      for i in $SERVICES; do
        /sbin/service $i stop
      done
    fi
    if [ -n "$MOUNT" ]; then
      for i in $MOUNT; do
        action "Unmounting aoe volume $i" /bin/umount -f $i
        sleep 1
      done 
    fi
    if [ -n "$LVM_VG" ]; then
      action $"Deactivating LVM volumes" /sbin/vgchange -a n $LVM_VG
    fi
    action $"Remove aoe kernel module" /sbin/rmmod aoe
    rc=$?
    if [ $rc -eq 0 ]; then
      rm -f $lockfile
    fi
    return $rc
}

restart() {
    stop
    start
}

case "$1" in
    start|stop|restart)
        $1
        ;;
    *)  
        echo $"Usage: $0 {start|stop|restart}"
        exit 2
esac  

