Last active
June 12, 2023 11:26
-
-
Save tano297/166f37389c275f3c090bdf688d3048df to your computer and use it in GitHub Desktop.
Mount and unmount remotes with sshfs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remote mount functions | |
remote-mount() | |
{ | |
# Mount a remote ssh root directory in /media/$USER/remotes/[remote alias] | |
# This function requires 1 argument, which is the ssh alias you made for the | |
# remote in your .ssh/config. | |
# If the directory is not created, it will try to, but this part is probably only | |
# going to be called the first time for each remote, unless you change the alias. | |
# To unmount, see remote-unmount(). | |
# check that I am passing one argument (remote name) | |
if [ "$#" -ne 1 ]; | |
then | |
echo "At least 1 parameter should be passed, with the name of the remote" | |
return 22 # invalid argument | |
fi | |
# check that the argument is a valid ssh target (check existance of root directory) | |
if ! ssh -q $1 [[ -d / ]]; | |
then | |
echo "$1 is an invalid ssh target. Check your ~/.ssh/config" | |
return 22 # invalid argument | |
else echo "$1 is a valid ssh target" | |
fi | |
# verbose | |
echo "Trying to mount remote $1 in /media/$USER/remotes/$1" | |
# if directory doesnt exist, create it | |
if [ ! -d "/media/$USER/remotes/$1" ] | |
then | |
# create it | |
echo "Directory /media/$USER/remotes/$1 DOES NOT exist. Creating it" | |
mkdir -p /media/$USER/remotes/$1 | |
# check if successful or exit | |
if [ -d "/media/$USER/remotes/$1" ] | |
then echo "Success creating directory!" | |
else echo "Failed creating directory. Maybe check permissions?"; return 2 # no such file | |
fi | |
else echo "Directory /media/$USER/remotes/$1 exists!" | |
fi | |
# Check if the directory is empty, otherwise mounting will fail | |
if [ "$(ls -A /media/$USER/remotes/$1)" ] | |
then | |
echo "Directory /media/$USER/remotes/$1 is not empty, mount will fail." | |
echo "This means it's either already mounted or used for something else." | |
echo "Exiting..." | |
return 39 # directory not empty | |
else echo "Directory is empty, everything ready for mount :)" | |
fi | |
# Mount | |
if sshfs -o reconnect $1:/ /media/$USER/remotes/$1/ | |
then echo "Success mounting! Enjoy your remote data access." | |
else echo "Something went wrong with sshfs. I am not a general purpose AI agent, so you'll have to ask Andres..." | |
fi | |
} | |
remote-unmount() | |
{ | |
# reverses the sshfs mount that was done with remote-mount() | |
# check that I am passing one argument (remote name) | |
if [ "$#" -ne 1 ]; | |
then | |
echo "At least 1 parameter should be passed, with the name of the remote" | |
return 22 # invalid argument | |
fi | |
# check if it is effectively mounted | |
if grep -qs "/media/$USER/remotes/$1 " /proc/mounts | |
then echo "The remote is actually mounted. Proceeding with unmount" | |
else | |
echo "I can't find $1 in mounted volumes" | |
return 22 | |
fi | |
echo "Trying to unmount remote $1" | |
if fusermount -u /media/$USER/remotes/$1 | |
then echo "Successful unmount!" | |
else | |
UNMOUNT_ERROR_CODE=$? | |
echo "Something went wrong :/" | |
return $UNMOUNT_ERROR_CODE | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS: the reason to mount the root directory is completely personal, because I have multiple disks mounted on many different places with a weird personal preference configuration. If you feel more comfortable with it, you can replace the "/" paths in the mount, with the path to the remote home directory (/home/user/) or your path of preference.