wiki:SshfsMnt

Version 6 (modified by chris, 3 years ago) (diff)

--

Table of Contents

  1. Mount
  2. Unmount

SSHFS Mount Script

Following are a pair of scripts to mount and unmount files systems via SFTP using Fuse, they are used on Crin3.

Mount

#!/bin/bash

# http://fuse.sourceforge.net/sshfs.html

# the place file systems are to be mounted
MOUNT_POINT="/media"
if [[ ! -d "$MOUNT_POINT" ]]; then
  echo "It looks like $MOUNT_POINT doesn't exist?"
  exit
fi

# check for server to mount on standard input 
if [[ $1 ]]; then
  SERVER=$1
elif [[ ! $1 ]]; then
  echo "Type the server you want to mount and then [ENTER]:"
  read server
  SERVER=$server
fi

# check the server is setup in ~/.ssh/config
SSH_CONFIG=$(grep "Host $SERVER" ~/.ssh/config)
if [[ ! $SSH_CONFIG ]]; then
  echo "It looks like you need to edit yur ~/.ssh/config"
  echo "to add entries like this for each server:"
  echo
  echo "Host example"
  echo "   User root"
  echo "   Hostname example.org"
  echo
  exit
fi

# check if the mount point exists and if not create it
if [[ ! -d "$MOUNT_POINT/$SERVER"]]; then
  echo "Creating $MOUNT_POINT/$SERVER"
  mkdir "$MOUNT_POINT/$SERVER"
fi

# check if the file system appears to already be mounted
DF=$(df | awk -F' ' '{ print $6 }' | grep "$MOUNT_POINT/$SERVER")
if [[ $DF ]]; then
  echo "It appears that $SERVER is already mounted at $DF?"
  exit
fi

# Mount the file system
sshfs $SERVER:/ $MOUNT_POINT/$SERVER/

Unmount

#!/bin/bash

# http://fuse.sourceforge.net/sshfs.html

# the place file systems are to be mounted
MOUNT_POINT="/media"
if [[ ! -d "$MOUNT_POINT" ]]; then
  echo "It looks like $MOUNT_POINT doesn't exist?"
  exit
fi

# check for server to unmount on standard input 
if [[ $1 ]]; then
  SERVER=$1
elif [[ ! $1 ]]; then
  echo "Type the server you want to unmount and then [ENTER]:"
  read server
  SERVER=$server
fi

# check if the file system appears to already be mounted
DF=$(df | awk -F' ' '{ print $6 }' | grep "$MOUNT_POINT/$SERVER")
if [[ $DF ]]; then
  echo "Umounting $SERVER at $DF?"
  fusermount -u $MOUNT_POINT/$SERVER
  exit
else
  echo "It appears that $SERVER is not mounted"
fi