#!/bin/bash
#
# cryptfs       Mount crypted filesystems.
#
# chkconfig: 2345 25 75
# description: Mounts and unmounts all dmcrypt encrypted filesystems.
### BEGIN INIT INFO
# Provides: $local_fs
### END INIT INFO

. /etc/init.d/functions

DMCFSTAB=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/dmcrypt\/./ && $4 ~ /noauto/ { print $1 }' /etc/fstab`
DMCMTAB=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/dmcrypt\/./ { print $2 }' /proc/mounts`

[ -r /etc/dmcrypttab ] || exit

[ ! -d /dev/dmcrypt ] && mkdir -p /dev/dmcrypt

# See how we were called.
case "$1" in
  start)
	for i in $DMCFSTAB; do
		echo $"Mounting encrypted filesystems: "
		NAME=`echo $i | LC_ALL=C sed -e's#/dev/dmcrypt/##'`
		DEV=`LC_ALL=C awk '!/^#/ && $2 ~ /^'$NAME'$/ { print $1 }' /etc/dmcrypttab | head -1`
		if [ -n "$DEV" -a -n "$NAME" ]; then
			[ -b /dev/mapper/$NAME ] && cryptsetup remove $NAME \
			&& echo -n "  device \"$NAME\" " \
			&& cryptsetup create $NAME $DEV \
			&& ln -sf /dev/mapper/$NAME /dev/dmcrypt/$NAME \
			&& mount $i
		fi
	done
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		success $"Mounting encrypted filesystems."
		touch /var/lock/subsys/cryptfs
	else
		failure $"Mounting encrypted filesystems."
	fi
	echo
	;;
  stop)
        # Unmount loopback stuff first
	remaining=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts`
	devremaining=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts`
	[ -n "$remaining" ] && {
	        sig=
		retry=3
		while [ -n "$remaining" -a "$retry" -gt 0 ]
		do
			if [ "$retry" -lt 3 ]; then
				action $"Unmounting loopback filesystems (retry):" umount $remaining
			else
				action $"Unmounting loopback filesystems: " umount $remaining
		        fi
			for dev in $devremaining ; do
				losetup $dev >/dev/null 2>&1 && \
				action $"Detaching loopback device $dev: " losetup -d $dev
			done	
			remaining=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts`
			devremaining=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts`
			[ -z "$remaining" ] && break
			/sbin/fuser -k -m $sig $remaining >/dev/null
			sleep 5
			retry=$(($retry -1))
			sig=-9
		done
	}
  	[ -n "$DMCMTAB" ] && {
		sig=
		retry=3
		remaining=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/dmcrypt\/./ {print $2}' /proc/mounts`
		while [ -n "$remaining" -a "$retry" -gt 0 ]
		do
			if [ "$retry" -lt 3 ]; then
				echo -n $"Unmounting encrypted filesystems (retry): "
				for fs in $remaining; do umount $fs; done
				RETVAL=$?
			else
				echo -n $"Unmounting encrypted filesystems: "
				for fs in $remaining; do umount $fs; done
				RETVAL=$?
			fi
			sleep 2
			remaining=`LC_ALL=C awk '!/^#/ && $1 ~ /^\/dev\/dmcrypt\/./ {print $2}' /proc/mounts`
			[ -z "$remaining" ] && break
			/sbin/fuser -k -m $sig $remaining >/dev/null
			sleep 5
			retry=$(($retry - 1))
			sig=-9
		done
		if [ $RETVAL -eq 0 ]; then
			success $"Unmounting encrypted filesystems."
			rm -f /var/lock/subsys/cryptfs
		else
			failure $"Unmounting encrypted filesystems."
		fi
		echo
	}
	;;
  status)
	if [ -f /proc/mounts ] ; then
	        [ -n "$DMCFSTAB" ] && {
		     echo $"Configured encrypted mountpoints: "
		     for fs in $DMCFSTAB; do echo $fs ; done
		}
	else
		echo $"/proc filesystem unavailable"
	fi
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|status}"
	exit 1
esac

exit 0
