Skip to content

Instantly share code, notes, and snippets.

@nogo
Created November 14, 2011 18:58
Show Gist options
  • Save nogo/1364779 to your computer and use it in GitHub Desktop.
Save nogo/1364779 to your computer and use it in GitHub Desktop.
fusemount - mount easy different locations via sshfs, smb, curlftpfs
#!/bin/bash
#
# fusemount - easy use mount tool with config files
#
CONFIG=~/.fusemount
function usage
{
more << EOF
fusemount CONFIGNAME [OPTIONS]
Options:
-h, --help show this help
-hp, --hostpath change configured hostpath
-l, --list list possible config names
-m, --mount mount config with different mount point
-u, --umount umount
Configname:
configfile in "$CONFIG"
Example:
fusemount -l
fusemount NAME
fusemount NAME -u [/path/to/different/mountpoint]
fusemount NAME -m [/path/to/different/mountpoint]
fusemount NAME -hp /change/path/at/host
fusemount NAME -m /path/to/different/mountpoint -hp /change/path/at/host
EOF
}
CONFIGFILE=
case $1 in
-h | --help)
usage
exit 1
;;
-l | --list)
echo "List of CONFIGNAMES:"
find $CONFIG -type f -exec echo "{}" \; | sed 's\'"$CONFIG"'/\\'
exit 1
;;
*)
[[ -z $1 ]] && { echo "No CONFIGNAME given"; usage; exit 1; }
CONFIGFILE=$CONFIG/$1
;;
esac
# Check if config file exists
[[ ! -f $CONFIGFILE ]] && { echo "Config file in $CONFIGFILE does not exist"; exit 1; }
# Include config file variables
shopt -s extglob
while IFS='=' read lhs rhs
do
if [[ $line != *( )#* ]]
then
declare $lhs="$rhs"
fi
done < "$CONFIGFILE"
ACTION="mount"
if [ -n $2 ]; then
while [ "$2" != "" ]; do
case $2 in
-m | --mount)
shift
MOUNTPOINT=$2
;;
-hp | --hostpath)
shift
HOSTPATH=$2
;;
-u | --umount)
shift
MOUNTPOINT=$2
ACTION="umount"
;;
esac
shift
done
fi
[[ ! -d $MOUNTPOINT ]] && { echo "Mountpoint [$MOUNTPOINT] does not exists"; exit 1;}
function m {
case $CMD in
"sshfs") EXEC="$CMD $HOST:$HOSTPATH $MOUNTPOINT $OPTIONS";;
*) EXEC="$CMD $HOST $MOUNTPOINT $OPTIONS" ;;
esac
echo "mount: [$EXEC]";
if [ "$SU" -eq "1" ]; then
echo -n "SU-"
su -c "$EXEC"
else
$EXEC
fi
}
function um {
case $CMD in
"sshfs") EXEC="fusermount -u $MOUNTPOINT" ;;
*) EXEC="umount $MOUNTPOINT" ;;
esac
if [ "$SU" -eq 1 ]; then
echo -n "SU-"
su -c "$EXEC"
else
$EXEC
fi
}
if [ ! -e /dev/fuse ]; then
echo -n "create fuse dev - "
su -c "mknod -m 666 /dev/fuse c 10 229"
fi
case $ACTION in
"mount") m ;;
"umount") um ;;
esac
@nogo
Copy link
Author

nogo commented Nov 14, 2011

The configuration for fusemount is in ~/.fusemount

Example:

~/.fusemount/smb_config_example:

CMD=mount.cifs
HOST=//hostname/share
MOUNTPOINT=/path/to/mount
OPTIONS=-o dir_mode=0777,file_mode=0777,rw,uid=1000
SU=1

~/.fusemount/sshfs_config_example:

CMD=sshfs
HOST=user@hostname
HOSTPATH=/path
MOUNTPOINT=/path/to/mount
OPTIONS=
SU=0

If SU equals "1" then the command runs with "su -c .."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment