Skip to content

Instantly share code, notes, and snippets.

@aglyzov
Created February 18, 2017 18:36
Show Gist options
  • Save aglyzov/181f079630dcb5a525497c39149e589b to your computer and use it in GitHub Desktop.
Save aglyzov/181f079630dcb5a525497c39149e589b to your computer and use it in GitHub Desktop.
A shell script that mounts a gocrypto volume and then auto-unmounts it when the shell exits
#!/bin/sh
# This script mounts an gocryptfs filesystem, starts a shell in the mounted
# directory, and then unmounts the filesystem when the shell exits.
# This is an equivalent of the cfssh utility for cfs.
# Contributed by David Rosenstrauch.
canonicalize() {
cd "$1"
pwd
}
if [ -z "$1" -o "$1" = "-h" ]; then
echo Usage: gocryptfssh encrypted_directory [unencrypted-directory [-p]]
echo " -p mount the unencrypted directory as public"
exit 1
fi
enc_dir=$1
unenc_dir_given=false
mount_public=false
if [ ! -z "$2" ]; then
unenc_dir_given=true
unenc_dir=$2
for arg in "$@" ; do
if [ "$arg" = "-p" ]; then
mount_public=true
fi
done
[ -d "$unenc_dir" ] || mkdir $unenc_dir
else
unenc_dir=$(mktemp -d .XXXXXXXX)
fi
if [ ! -d "$enc_dir" ]; then
mkdir $enc_dir
fi
enc_dir=$(canonicalize "$enc_dir")
unenc_dir=$(canonicalize "$unenc_dir")
options=""
if $unenc_dir_given; then
if $mount_public; then
options="-- -o allow_other"
fi
fi
# Attach the directory and change into it
if gocryptfs $enc_dir $unenc_dir $options; then :; else
echo "gocryptfs failed"
rmdir $unenc_dir
exit 1
fi
if ! $unenc_dir_given; then
chmod 700 $unenc_dir
fi
echo "Directory is $unenc_dir"
orig_dir=$(pwd)
cd $unenc_dir
# Fall back to umount if fusermount is not available (e.g., on OS X)
FUSE_UMOUNT="$(which 2>/dev/null fusermount)"
FUSE_UMOUNT="${FUSE_UMOUNT:+fusermount -u}"
FUSE_UMOUNT="${FUSE_UMOUNT:-umount}"
# Set the shell up
exec /bin/sh -c "$SHELL ; cd $orig_dir ; $FUSE_UMOUNT $unenc_dir ; if ! $unenc_dir_given; then rmdir $unenc_dir; fi"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment