116 lines
3.3 KiB
Bash
Executable File
116 lines
3.3 KiB
Bash
Executable File
#! /bin/bash
|
|
# 2023 - based on https://root.nix.dk/en/utility-scripts/mount-samba-share-as-user
|
|
# Corrected some typos and added an exit code for systemd usage
|
|
#
|
|
# User script for mounting and unmounting a samba share
|
|
#
|
|
# - No fstab entry
|
|
# - No mount units
|
|
# - Easy customization
|
|
# - Symlinks are located in designated folder (default: $HOME/SMBLinks)
|
|
# - Symlink can be named (default: $SHARENAME)
|
|
# - Using `-u` argument will unmount and remove the symlink
|
|
# - Option for providing credentials
|
|
# - Option for a user service to mount at login
|
|
#
|
|
# 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 <https://www.gnu.org/licenses/>.
|
|
#
|
|
# @linux-aarhus - root.nix.dk
|
|
#
|
|
|
|
###########################################
|
|
# NECESSARY - MODIFY THESE VARIABLES
|
|
|
|
# your samba server's hostname or IP address
|
|
HOST=""
|
|
|
|
# the share name on the server
|
|
#SHARENAME="medias"
|
|
|
|
# credentials
|
|
USERNAME="foo"
|
|
WORKGROUP=""
|
|
PASSWD="bar"
|
|
|
|
###########################################
|
|
# don't modify below this line
|
|
# - unless you know what you are doing
|
|
|
|
#LINKNAME="$SHARENAME"
|
|
SYMLINKS="$HOME/SMBLinks"
|
|
SCRIPTNAME=$(basename "$0")
|
|
VERSION="0.2.1"
|
|
|
|
# check argument $1
|
|
if [[ "$1" == "-u" ]]; then
|
|
if [[ "$2" == "" ]]; then
|
|
echo "No sahre name provided, aborting..."
|
|
exit 1
|
|
else
|
|
SHARENAME="$2"
|
|
LINKNAME="$SHARENAME"
|
|
fi
|
|
# remove symlink
|
|
rm -f "$SYMLINKS/$LINKNAME"
|
|
# unmount share
|
|
gio mount -u "smb://$HOST/$SHARENAME"
|
|
exit
|
|
elif [[ "$1" == "" ]]; then
|
|
echo ":: $SCRIPTNAME v$VERSION"
|
|
echo "==> missing argument:"
|
|
echo "Usage: "
|
|
echo " mount SMB : $SCRIPTNAME sharename"
|
|
echo " umount SMB: $SCRIPTNAME -u sharename"
|
|
exit 1
|
|
else
|
|
SHARENAME="$1"
|
|
fi
|
|
|
|
LINKNAME="$SHARENAME"
|
|
|
|
# Create credentials folder
|
|
if ! [ -d "$HOME/.credentials" ]; then
|
|
mkdir -p $HOME/.credentials
|
|
chmod 700 $HOME/.credentials
|
|
fi
|
|
|
|
# ----------------------------------------------------------------
|
|
# mount command
|
|
if ! [[ -z "${USERNAME}" ]]; then
|
|
# create credentials file
|
|
fname="$HOME/.credentials/$USERNAME-$HOST-$SHARENAME"
|
|
printf ${USERNAME}'\n'${WORKGROUP}'\n'${PASSWD}'\n' > $fname
|
|
chmod 600 $fname
|
|
# mount and feed the credentials to the mount command
|
|
gio mount "smb://$HOST/$SHARENAME" < $fname
|
|
else
|
|
# mount (if credentials are required you will be prompted
|
|
gio mount "smb://$HOST/$SHARENAME"
|
|
fi
|
|
# ----------------------------------------------------------------
|
|
|
|
# easy reference to gio mount point
|
|
ENDPOINT=/run/user/$UID/gvfs/''smb-share:server=$HOST,share=$SHARENAME''
|
|
|
|
# create the subfolder
|
|
if ! [ -d "$SYMLINKS" ]; then
|
|
# use --parents to suppress warnings if folder exist
|
|
mkdir -p "$SYMLINKS"
|
|
fi
|
|
|
|
# use --force argument to suppress warning if link exist
|
|
ln -sf "$ENDPOINT" "$SYMLINKS/$LINKNAME"
|
|
|
|
exit 1
|