mount from remote system using fuse and sshfs


Forum: DSL Tips and Tricks
Topic: mount from remote system using fuse and sshfs
started by: fredvej

Posted by fredvej on Feb. 26 2007,18:34
Here's a script to mount something from a remote system running an SSH server.  On your local machine you need a mountpoint, load the fuse module and mount using sshfs.
Code Sample

# script mounts a path from a remote system using fuse and sshfs

THISPROG=`basename $0`
PUBLICIP=12.34.56.78
LOCALIP=192.168.0.2
SSHPORT=22

USAGE="
Usage : $THISPROG [options] {username} {remote path to mount} {local mount point}

local mount point must be an existing directory

options :

-r {IP address} : use IP adress for remote host , e.g. -r 123.45.67.89
  Default remote IP is $PUBLICIP

-p {port number} : Port number to use for ssh connection , default $SSHPORT

-l {IP address | d} : remote host at local LAN , no caching.
  Use provided IP address or default (d) $LOCALIP
"

############ FUNCTIONS ######################

Exit_if_Error ()
{
 if [ $RETURN -ne 0 ]
 then
   echo "$COMMAND failed with return code $RETURN"
   exit $RETURN
 fi
}

LoadModule ()
{
 MOD2LOAD=$1
 ISLOADED=`lsmod | grep $MOD2LOAD`
 if [ -z "$ISLOADED" ]
 then
   COMMAND="$SUDO /sbin/modprobe -v $MOD2LOAD"
   echo "probing module with $COMMAND"
   $COMMAND
   RETURN=$?
   Exit_if_Error
 fi
}

############ MAIN ###########################

# follow_symlinks not in DSL 3.2 #FSOPTIONS="-o compression=yes,transform_symlinks,follow_symlinks,allow_other,nonempty"
FSOPTIONS="-o compression=yes,transform_symlinks,allow_other,nonempty"
HOSTIP=$PUBLICIP

while getopts 'r:p:l:hH' OPTION
do
 case $OPTION in
 r)
    HOSTIP=$OPTARG
 ;;
 l)
   HOSTIP=$OPTARG
   if [ "$HOSTIP" = "d" ]
   then
     HOSTIP=$LOCALIP
   fi
   FSOPTIONS="${FSOPTIONS},sshfs_sync,no_readahead,cache=no"
 ;;
 p)
   SSHPORT=$OPTARG
 ;;
 h|H)
   echo "$USAGE"
   exit 1
 ;;
 *)
   echo "$USAGE"
   exit 1
 ;;
 esac
done

shift `expr $OPTIND - 1`
FSOPTIONS="-p $SSHPORT $FSOPTIONS"

LOGINAS=$1
RPATH="$2"
MPOINT="$3"

if [ $# -ne 3 ]
then
 echo "
Number of parameters must be 3 - aborting !"
 echo "$USAGE"
 exit 1
fi

USELOGIN="${LOGINAS}@"

if [ ! -d "$MPOINT" ]
then
 echo "
mount point '$MPOINT' is not found or is not a directory - aborting !"
 echo "$USAGE"
 exit 2
fi

USERID=`id -u`
if [ "$USERID" = "0" ]
then
 SUDO=""
else
 SUDO=/usr/bin/sudo
 if [ -n "$FSOPTIONS" ]
 then
# local uid on local mountpoint, remote user's id on remote filesystem
   FSOPTIONS="${FSOPTIONS},uid=$USERID"
 fi
fi

LoadModule fuse

COMMAND="$SUDO sshfs \"${USELOGIN}${HOSTIP}:${RPATH}\" \"$MPOINT\" ${FSOPTIONS}"
echo "mounting with $COMMAND"
$SUDO sshfs "${USELOGIN}${HOSTIP}:${RPATH}" "$MPOINT" ${FSOPTIONS}
RETURN=$?
Exit_if_Error


Unmount :
Code Sample
sudo fusermount -u $MPOINT


Enjoy

Posted by fredvej on Feb. 28 2007,15:18
Sorry, follow_symlinks is not recognised by DSL as an sshfs option.

Correction to the script :
FSOPTIONS="-o compression=yes,transform_symlinks,allow_other,nonempty"

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.