Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 397bca0d02d3e23c5b55320e2a79550f > files > 43

zfs-fuse-0.7.0-11.fc18.i686.rpm

#! /bin/bash
#
# zfs-fuse - startup script for zfs-fuse daemon
#
# chkconfig:   - 26 74
# description: zfs-fuse daemon
#
### BEGIN INIT INFO
# Provides:           zfs-fuse
# Required-Start:     fuse
# Required-Stop:  
# Should-Start: 
# Should-Stop: 
# Default-Start:  
# Default-Stop: 
# Short-Description:  Start the zfs-fuse daemon
# Description:        zfs-fuse daemon
### END INIT INFO
# Conforming to http://fedoraproject.org/wiki/FCNewInit/Initscripts
# script requires the following RPMs
# awk
# coreutils /usr/bin/seq /bin/cut
# lsof
# util-linux-ng: /usr/bin/renice
# upstart: /sbin/runlevel


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

prog="zfs-fuse"
zfsfuse="/usr/sbin/$prog"
zfs="/usr/sbin/zfs"
zpool="/usr/sbin/zfs"
config=/etc/sysconfig/$prog

[ -e $config ] && . $config

PIDFILE=/var/run/$prog.pid
LOCKFILE=/var/lock/subsys/$prog
export PATH=/sbin:/bin:/usr/sbin:/usr/bin

unset LANG
ulimit -v unlimited
ulimit -l unlimited
ulimit -s unlimited


get_orphans() {
	orphans=`lsof -w -n | awk '$4 == "cwd" && $5 == "unknown" {print $2}'`
	echo "$orphans"
}


kill_orphans() {

	orphans=`get_orphans`
	[ "$orphans" == "" ] && return

	action "Terminating processes with unknown working directory:" kill $orphans
	echo -n "Waiting for processes to exit: "
	for a in `seq 20` ; do
		orphans=`get_orphans`
		if [ "$orphans" == "" ] ; then success ; echo ; return 0 ; fi
		echo -n .
		sleep 0.5
	done
	warning ; echo

	action "Forcefully killing stuck processes:" kill -9 $orphans
	echo -n "Waiting for processes to die: "
	for a in `seq 40` ; do
		orphans=`get_orphans`
		if [ "$orphans" == "" ] ; then success ; echo ; return 0 ; fi
		echo -n .
		sleep 0.25
	done
	failure ; echo
	return 8

}


start() {

	# check if the zfs-fuse binary exists and can be executable
	for a in $zfsfuse $zfs ; do if [ ! -x $a ] ; then
		echo -n "$a not present or executable"
		failure ; echo
		return 127
	fi ; done

	# check if ZFS is running
	status -p $PIDFILE -l $LOCKFILE $prog > /dev/null 2>&1
	start_status=$?
	case "$start_status" in
	0)
		# ZFS-FUSE is running.  Do nothing
		return 0
		;;
	1)
		action "Cleaning up stale ZFS-FUSE PID file:" rm -f "$PIDFILE"
		ret=$? ; [ $ret -ne 0 ] && return $ret
		;;
	2)
		action "Cleaning up stale ZFS-FUSE lock file:" rm -f "$LOCKFILE"
		ret=$? ; [ $ret -ne 0 ] && return $ret
		;;
	3)
		# not running.  awesome, we continue
		;;
	*)
		echo -n "Unknown result $start_status"
		failure ; echo
		return $start_status
	esac

	if [ "$ZFS_KILL_ORPHANS" == "yes_really" -o "$ZFS_KILL_ORPHANS" == "yes" ] ; then
		if [ -x /usr/sbin/lsof ] ; then
			kill_orphans
			ret=$? ; [ $ret -ne 0 ] && return $ret
		else
			echo -n "To kill orphan processes, you must have lsof installed"
			failure ; echo
			return 126
		fi
	fi

	# ZFS_DAEMONOPTS deprecated; refer to /etc/zfs/zfsrc now
	if [ -n "$ZFS_DAEMONOPTS" ]; then
		echo -n "Deprecated use of ZFS_DAEMONOPTS. Refer to /etc/zfs/zfsrc instead"
		warning
		echo
	fi
	action "Starting the ZFS-FUSE daemon:" daemon $zfsfuse -p "$PIDFILE" $ZFS_DAEMONOPTS
	ret=$? ; [ $ret -ne 0 ] && return $ret

	echo -n "Waiting for ZFS-FUSE to stabilize: "
	for a in `seq 20 -1 0` ; do
		if [ $a == 0 ] ; then
			failure "$prog startup"
			echo
			return 3
		elif $zfs list >/dev/null 2>&1 ; then
			success
			echo
			break
		else
			echo -n .
			sleep 0.5
		fi
	done
	
	PID=`pidofproc -p "$PIDFILE" $prog`

	echo -n "Immunizing ZFS-FUSE against the OOM killer: "
	echo -17 > "/proc/$PID/oom_adj"
	ret=$? ; [ $ret -ne 0 ] && warning || success
	echo

	if [ "$ZFS_PRIORITY" != "" ] ; then
		if [ -x /usr/sbin/renice ] ; then
			action "Setting ZFS-FUSE priority to $ZFS_PRIORITY:" renice $ZFS_PRIORITY -g $PID > /dev/null
			ret=$? ; [ $ret -ne 0 ] && return $ret
		else
 			echo -n "To re-prioritize ZFS-FUSE, you must have renice installed"
			failure
			echo
			return 126
		fi
	fi

	# automount?
	[ "$ZFS_AUTOMOUNT" == "" ] && ZFS_AUTOMOUNT=yes
	if [ "$ZFS_AUTOMOUNT" == "1" ] ; then
		echo -n "Deprecated use of ZFS_AUTOMOUNT. It must be either yes or no."
		warning
		echo
		ZFS_AUTOMOUNT=yes
	fi

	if [ "$ZFS_AUTOMOUNT" == "yes" ] ; then
		action "Mounting ZFS filesystems" zfs mount -a
		ret=$? ; [ $ret -ne 0 ] && return $ret
	fi

	touch /var/lock/subsys/$prog

}

stop() {

	# check if the zfs-fuse binary exists and can be executed
	for a in $zfsfuse $zfs ; do if [ ! -x $a ] ; then
		echo -n "$a not present or executable"
		failure ; echo
		return 127
	fi ; done

	# check if ZFS is running
	status -p $PIDFILE -l $LOCKFILE $prog > /dev/null 2>&1
	start_status=$?
	case "$start_status" in
	0)
		# ZFS-FUSE is running.  Awesome, we continue
		;;
	1)
		action "Cleaning up stale ZFS-FUSE PID file:" rm -f "$PIDFILE"
		return $?
		;;
	2)
		action "Cleaning up stale ZFS-FUSE lock file:" rm -f "$LOCKFILE"
		return $?
		;;
	3)
		# zfs-fuse is not running.
		return 0
		;;
	*)
		echo -n "Unknown result $start_status"
		failure
		echo
		return $start_status
	esac

	action "Syncing disks:" sync
	# hehe, sync CANNOT FAIL

	[ "$ZFS_FORCE_UNMOUNT" == "only_on_shutdown" ] && ZFS_FORCE_UNMOUNT=on_shutdown
	runlevel=`runlevel | cut -d " " -f 2`
	shuttingdown=
	[ "$runlevel" == "6" -o "$runlevel" == "0" ] && shuttingdown=1
	action "Unmounting ZFS filesystems:" zfs unmount -a
	ret=$?
	if [ $ret -ne 0 ] ; then
		if [ "$ZFS_FORCE_UNMOUNT" == "yes" ] ; then
			action "Forcibly unmounting ZFS filesystems:" zfs unmount -f -a
			ret=$? ; [ $ret -ne 0 ] && return $ret
		elif [ "$ZFS_FORCE_UNMOUNT" == "on_shutdown" -a "$shuttingdown" == "1" ] ; then
			action "Forcibly unmounting ZFS filesystems on shutdown:" zfs unmount -f -a
			ret=$? ; [ $ret -ne 0 ] && return $ret
		else
			return $ret
		fi
	fi

	PID=`pidofproc -p "$PIDFILE" $prog`
	echo -n "Stopping ZFS-FUSE: "
	killproc -p "$PIDFILE"  $prog -15 >/dev/null 2>&1
	ret=$? ; [ $ret -ne 0 ] && { failure "$prog shutdown" ; echo ; return $ret ; }

	for a in `seq 20 -1 0` ; do
		if [ $a == 0 ] ; then
			failure "$prog shutdown"
			echo
			return 3
		elif kill -0 $PID >/dev/null 2>&1 ; then
			echo -n .
			sleep 0.5
		else
			success
			echo
			break
		fi
	done

	rm -f "$PIDFILE"

	action "Syncing disks again:" sync

	rm -f /var/lock/subsys/$prog

}

restart() {
	stop || return $?
	start || return $?
}

case "$1" in
	start)
		$1
		;;
	stop)
		$1
		;;
	restart)
		restart
		;;
	reload)
		restart
		;;
	status)
		status -p $PIDFILE -l $LOCKFILE $prog
		;;
	extendedstatus)
		if [ ! -x $zpool ] ; then
			echo "$zpool not present or executable"
			return 127
		fi
		status -p $PIDFILE -l $LOCKFILE $prog && $zpool status -v
		;;
	condrestart|try-restart)
		status -p $PIDFILE -l $LOCKFILE $prog > /dev/null 2>&1
		[ $? -ne 3 ] && restart # semantics dictate we restart ONLY if the process is running or was supposed to be running
		;;
	*)
		echo $"Usage: $0 {start|stop|status|extendedstatus|reload|restart|condrestart|try-restart}"
		exit 64
esac
exit $?