wiki:SshfsMnt

Table of Contents

  1. mnt-sshfs
  2. umnt-sshfs

SSHFS Mount Script

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

mnt-sshfs

#!/bin/bash

# Mount a filesystem using SSHFS 
# http://fuse.sourceforge.net/sshfs.html
# 
# Copyright 2015 Chris Croome
# Webarchitects Co-operative
# http://wwww.webarchitects.co.uk/
#
# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# check that the script is being run by root
if [[ "$(id -u)" != "0" ]] ; then
  echo "You must run '$0' as root or via sudo" 
  exit 1
fi

# the place file systems are to be mounted
MOUNT="/media/sshfs"
if [[ ! -d "$MOUNT" ]]; then
  echo "$MOUNT doesn't exist"
  exit 1
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 1
fi

# check if the mount point exists and if not create it
if [[ ! -d "$MOUNT/$SERVER" ]]; then
  echo "Creating $MOUNT/$SERVER"
  mkdir "$MOUNT/$SERVER" || { echo "Failed to create $MOUNT/$SERVER" ; exit 1 ; }
fi

# check if the file system is mounted
DF=$(df | awk -F' ' '{ print $6 }' | grep "$MOUNT/$SERVER")
if [[ $DF ]]; then
  echo "$SERVER is already mounted at $DF"
  # nothing needs to be done,
  exit 0
fi

# Mount the file system
sshfs $SERVER:/ $MOUNT/$SERVER/ && \
  # if this succeeded
  { echo "Mounted $SERVER:/ on $MOUNT/$SERVER/" ; exit 0 ; } || \
  # if this failed
  { echo "Problem mounting $SERVER:/ $MOUNT/$SERVER/" ; exit 1 ; }

echo "Oops how did we get here?"
exit 1

umnt-sshfs

#!/bin/bash

# Un-ount a filesystem using SSHFS 
# http://fuse.sourceforge.net/sshfs.html
# 
# Copyright 2015 Chris Croome 
# Webarchitects Co-operative
# http://wwww.webarchitects.co.uk/
#
# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# check that the script is being run by root
if [[ "$(id -u)" != "0" ]] ; then
  echo "You must run '$0' as root or via sudo" 
  exit 1
fi

# the place file systems are to be mounted
MOUNT="/media/sshfs"
if [[ ! -d "$MOUNT" ]]; then
  echo "It looks like $MOUNT doesn't exist?"
  exit 1
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 mount point exists 
if [[ ! -d "$MOUNT/$SERVER" ]]; then
  echo "It looks like "$MOUNT/$SERVER" doesn't exist?"
  exit 1
fi
  
# check if the file system is mounted
DF=$(df | awk -F' ' '{ print $6 }' | grep -w "$MOUNT/$SERVER")
if [[ $DF ]]; then
  # unmount the filesystem
  fusermount -u $MOUNT/$SERVER && \
    # if success
    { echo "Umounted $SERVER at $DF" ; exit 0 ; } || \
    # if mounting failed
    { echo "Problem unmounting $MOUNT/$SERVER" ; exit 1 ; }
else
  echo "It appears that $SERVER is not mounted at $MOUNT/$SERVER"
  exit 1
fi

echo "Oops how did we get here?"
exit 1
Last modified 3 years ago Last modified on Oct 14, 2015, 2:40:21 PM