wiki:SshfsMnt

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

Sorted out exit codes

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

# 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 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_POINT/$SERVER" ]]; then
  echo "Creating $MOUNT_POINT/$SERVER"
  mkdir "$MOUNT_POINT/$SERVER" || { echo "Failed to create $MOUNT_POINT/$SERVER" ; exit 1 ; }
fi

# check is 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 1
fi

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

umnt-sshfs

#!/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 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_POINT/$SERVER" ]]; then
  echo "It looks like "$MOUNT_POINT/$SERVER" doesn't exist?"
  exit 1
fi

# check if the file system appears to already be mounted
DF=$(df | awk -F' ' '{ print $6 }' | grep -w "$MOUNT_POINT/$SERVER")
if [[ $DF ]]; then
  # unmount the filesystem
  fusermount -u $MOUNT_POINT/$SERVER && \
    # if success
    { echo "Umounted $SERVER at $DF" ; exit 0 ; } || \
    # if mounting failed
    { echo "Problem unmounting $MOUNT_POINT/$SERVER" ; exit 1 ; }
else
  echo "It appears that $SERVER is not mounted at $MOUNT_POINT/$SERVER"
  exit 1
fi