Skip to content

Instantly share code, notes, and snippets.

@dale3h
Created Nov 8, 2020
Embed
What would you like to do?
[Shell] SSHFS Mount Utilities
remote_user=pi
remote_host=192.168.1.100
remote_path=/home/pi
remote_port=22
#!/usr/bin/env bash
# ==============================================================================
# dotfiles: bin / rmount
# Utility to mount remote SSHFS volumes.
#
# Example usage:
# $ rmount example
# ==============================================================================
readonly me="$( basename "${BASH_SOURCE[0]}" )"
readonly mount_root="/mnt"
declare volume_name
declare mount_path
declare mount_opts
declare extra_opts
declare identity_file
declare remote_user
declare remote_host
declare remote_path
declare remote_port
declare sshfs_opts
if [[ -z "${1}" ]]; then
echo "${me}: mount name required"
exit 1
fi
if [[ ! -f "${mount_root}/${1}.conf" ]]; then
echo "${me}: ${mount_root}/${1}.conf: missing or invalid mount configuration"
exit 1
fi
# shellcheck source=/dev/null
source "${mount_root}/${1}.conf"
# Validate config variables.
if [[ -z "${volume_name}" ]]; then
volume_name="${1}"
fi
if [[ -z "${mount_path}" ]]; then
mount_path="${mount_root}/${volume_name}"
fi
if [[ -z "${identity_file}" ]]; then
identity_file=~/.ssh/id_rsa
fi
if [[ -z "${remote_port}" ]]; then
remote_port=22
fi
sshfs_opts="reconnect,IdentityFile=${identity_file},volname=${volume_name}"
if [[ -n "${mount_opts}" ]]; then
printf -v extra_opts '%s,' "${mount_opts[@]}"
sshfs_opts="${extra_opts}${sshfs_opts}"
fi
if [[ ! -d "${mount_path}" ]]; then
echo "Creating mount point: ${mount_path}"
sudo mkdir -p "${mount_path}" \
&& sudo chown -R "${USER}:staff" "${mount_path}"
fi
sshfs -o "${sshfs_opts}" -p "${remote_port}" "${remote_user}"@"${remote_host}":"${remote_path}" "${mount_path}"
#!/usr/bin/env bash
# ==============================================================================
# dotfiles: bin / rumount
# Utility to unmount remote SSHFS volumes.
#
# Example usage:
# $ rumount example
# ==============================================================================
readonly me="$( basename "${BASH_SOURCE[0]}" )"
readonly mount_root="/mnt"
declare volume_name
declare mount_path
if [[ -z "${1}" ]]; then
echo "${me}: mount name required"
exit 1
fi
if [[ ! -f "${mount_root}/${1}.conf" ]]; then
echo "${me}: ${mount_root}/${1}.conf: missing or invalid mount configuration"
exit 1
fi
# shellcheck source=/dev/null
source "${mount_root}/${1}.conf"
# Validate config variables.
if [[ -z "${volume_name}" ]]; then
volume_name="${1}"
fi
if [[ -z "${mount_path}" ]]; then
mount_path="${mount_root}/${volume_name}"
fi
umount "${mount_path}" \
|| diskutil unmount "${mount_path}" \
|| umount -f "${mount_path}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment