#!/bin/bash
#
# vstorage-fs         Mount vstorage filesystems.
#
# chkconfig: 345 56 75
# description: Mounts and unmounts all vstorage File System mount points.
### BEGIN INIT INFO
# Short-Description: Mount and unmount vstorage network filesystems.
# Description: Mount and unmount vstorage network filesystems.
# Provides: pstorage-fs vstorage-fs
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO

if [ -r /etc/rc.d/init.d/functions ]; then
	source /etc/rc.d/init.d/functions
elif [ -r /lib/lsb/init-functions ]; then
	source /lib/lsb/init-functions;
else
	echo "Unsupported init system!"
	exit 255
fi

fn_exists() {
	declare -f "$1" > /dev/null 2>&1;
}

# we are working on non-RHEL distro, define missing functions
fn_exists "action"
if [ "$?" -ne 0 ]; then
	action() {
		local STRING rc

		STRING=$1
		shift
		"$@" && log_success_msg $"$STRING" || log_failure_msg $"$STRING"
		rc=$?
		echo
		return $rc
	}
fi

PSTORAGEFSTAB=$(LC_ALL=C awk '!/^#/ && $3 ~ /^fuse.[pv]storage/ && $4 !~ /noauto/ { print $2 }' /etc/fstab | uniq)
PSTORAGEMTAB=$(LC_ALL=C awk '$3 ~ /^p?fuse.[pv]storage/ && $2 != "/" { print $2 }' /proc/mounts)
PSTORAGE_FS_TYPE="65735546"
PSTORAGE_OUT="/tmp/vstoragefs_out.$$"
TIMEOUT=120
export PSTORAGE_MOUNT_FROM_INIT="1"

check_mount() {
	local mpoint=$1
	# Check if vstorage really mounted. If timeout expired exit with error code
	rm -f $PSTORAGE_OUT > /dev/null 2>&1
	fstype=""
	stat -f -c %t $mpoint > $PSTORAGE_OUT 2>/dev/null &
	pid="$!"
	while [ $TIMEOUT -ne 0 ]; do
		kill -0 $pid >& /dev/null
		if [ $? -ne 0 ]; then
			# stat exit
			fstype=`cat $PSTORAGE_OUT`
			rm -f $PSTORAGE_OUT > /dev/null 2>&1
			if [ "x$fstype" = "x$PSTORAGE_FS_TYPE" ]; then
				# OK!
				break
			else
				# Try again
				fstype=""
				stat -f -c %t $mpoint > $PSTORAGE_OUT 2>/dev/null &
				pid="$!"
			fi
		fi
		TIMEOUT=$((TIMEOUT-1))
		sleep 1
	done

	if [ $TIMEOUT -eq 0 -a "x$fstype" != "x$PSTORAGE_FS_TYPE" ]; then
		echo "Timeout waiting vstorage availability on $mpoint expired"
		return 1
	fi

	return 0
}

# See how we were called.
case "$1" in
  start)
        [ "$EUID" != "0" ] && exit 4
        [ ! -n "$PSTORAGEFSTAB" ] && exit 0
        for pstorage_mpoint in $PSTORAGEFSTAB; do
		# Check that it's already mounted
		already_mounted=0
		for pstorage_mounted in $PSTORAGEMTAB; do
			if [ $pstorage_mounted = $pstorage_mpoint ]; then
				already_mounted=1
				break
			fi
		done

		# Mount it
		if [ $already_mounted -eq 0 ]; then
			# get latest entry for $pstorage_mpoint in /etc/fstab
			dev=$(LC_ALL=C awk -vmp="$pstorage_mpoint" '!/^#/ && $3 ~ /^fuse.[pv]storage/ && $2==mp { print $1}' /etc/fstab | tail -n 1)
			action $"Mounting vstorage filesystem $pstorage_mpoint ($dev): " mount $dev
			[ $? -ne 0 ] && continue
		fi

		# Wait for vstorage really mounted
		check_mount $pstorage_mpoint
	done
	[ -d "/var/lock/subsys" ] && touch /var/lock/subsys/vstorage-fs
	;;
  stop)
        [ "$EUID" != "0" ] && exit 4
	[ -n "$PSTORAGEMTAB" ] && action $"Unmounting vstorage filesystems: " umount -a -t fuse.pstorage,pfuse.pstorage,fuse.vstorage
	[ -d "/var/lock/subsys" ] && rm -f /var/lock/subsys/vstorage-fs
	;;
  check)
        [ "$EUID" != "0" ] && exit 4
        [ ! -n "$PSTORAGEFSTAB" ] && exit 0
	for pstorage_mpoint in $PSTORAGEFSTAB; do
		check_mount $pstorage_mpoint
		[ $? -ne 0 ] && exit 1
	done
	exit 0
	;;
  status)
	if [ -f /proc/mounts ] ; then
	        [ -n "$PSTORAGEFSTAB" ] && {
		     echo $"Configured vstorage mountpoints: "
		     for fs in $PSTORAGEFSTAB; do echo $fs ; done
		}
	else
		echo $"/proc filesystem unavailable"
	fi
	[ -r /var/lock/subsys/vstorage-fs ] || exit 3
	;;
  restart)
	$0 stop
	$0 start
	exit $?
	;;
  reload)
        $0 start
	exit $?
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|status}"
	exit 2
esac

exit 0
