Todd A. Jacobs | 4 Oct 11:36 2007

New Script: encfs_manage.sh v1.2

#!/bin/bash

##
## Name:
##      encfs_manage.sh
##
## Version:
##      $Revision: 1.2 $
##
## Purpose:
##      Mount/unmount encfs filesystems. Compatible with cryptkeeper
##      naming conventions.
##
## Usage:
##      encfs_manage.sh [ -d ] <container_name>
##      encfs_manage.sh [ -h | -u | -l ]
##
## Options:
##      -h = show documentation
##      -u = show usage
##	-l = list mounts
##      -d = detach mount
##
## Errorlevels:
##      0 = Success
##      1 = Failure
##      2 = Other
##
## Caveats:
##	The container_name can't be a pathname. The script expects to
##	find a directory matching the container_name in MOUNTPOINT.
##
## Copyright:
##      Copyright (c) 2007 by Todd A. Jacobs <bash_junkie@...>
##      All Rights Reserved
##
## License:
##      Released under the GNU General Public License (GPL)
##      http://www.gnu.org/copyleft/gpl.html
##
##      This program is free software; you can redistribute it and/or
##      modify it under the terms of the GNU General Public License
##      as published by the Free Software Foundation; either version 2
##      of the License, or (at your option) any later version.
##
##      This program is distributed in the hope that it will be useful,
##      but WITHOUT ANY WARRANTY; without even the implied warranty of
##      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##      GNU General Public License for more details.
##

######################################################################
# User-definable defaults.
######################################################################
# Location of encfs container files and mountpoints. Cryptkeeper stores
# both in the same root, so we'll do the same for compatibility.
MOUNTPOINT="$HOME/encfs"

######################################################################
# Initialization and option processing.
######################################################################
# Set a sensible path.
export PATH=$(getconf PATH)

# Make sure fuse is available and loaded into the kernel. Does anyone
# use fuse in a monolithic kernel?
sudo /sbin/modprobe fuse || exit 1

# Check that encfs is in PATH.
if ! which encfs > /dev/null; then
    echo "Error: encfs not in path: $PATH" >&2
    exit 1
fi

# Show brief help on usage.
function ShowUsage {
    egrep -A2 "^## Usage" $0 | sed -e 's/## //'
    exit 2
}

# Check fuse filesystems for encfs mounts that match a given name.
CheckMount () {
    mount -t fuse | grep "^encfs" | fgrep -q "$*"
}

ShowMount () {
    mount -t fuse | grep "^encfs"
    exit 2
}

# Process command-line options.
while getopts ':huld:' opt; do
    case $opt in
        h)  
	    # If passed "-h" for help, use egrep to show the help
	    # comments designated by an inital double-octothorpe and
	    # exit with errorlevel 2.
            egrep '^##[[:space:]]' $0 | sed 's/## //'
            exit 2
            ;;
	d)
	    # Unmount the encfs container and remove its dynamic mount
	    # point.
	    _DIR="${MOUNTPOINT}/${OPTARG}"
	    if CheckMount "$_DIR"; then
		fusermount -u "$_DIR" && rmdir "$_DIR" && exit
		exit 1
	    else
		echo "Error: not mounted: ${_DIR##*/}" >&2
		exit 1
	    fi
	    ;;
	l) ShowMount 
	    ;;
        \? | u) ShowUsage
	    ;;
    esac # End "case $opt"
done # End "while getopts"

# Shift processed options out of the way.
shift $(($OPTIND - 1))

######################################################################
# Main
######################################################################
# Make sure we have a container name to work with.
if (( $# < 1 )); then
    ShowUsage
fi

# Cryptkeeper-compatible encfs container.
CONTAINER="$MOUNTPOINT/.$1_encfs"

# Cryptkeeper-compatible mount point.
DECRYPTED="$MOUNTPOINT/$1"

if [[ -d "$CONTAINER" ]]; then
    if CheckMount "$1"; then
	echo "Error: already mounted: $1" >&2
    else
	mkdir --parents --mode=700 "$DECRYPTED"
	# Try to use ssh-askpass if we're running under X. This relies
	# on the fact that we can set an empty string for extpass.
	encfs ${DISPLAY:+--extpass=$(which ssh-askpass)} \
	    "$CONTAINER" "$DECRYPTED"
    fi
else
    echo "Error: no encfs container: $CONTAINER" >&2
    exit 1
fi

--

-- 
"Oh, look: rocks!"
	-- Doctor Who, "Destiny of the Daleks"

Gmane