Add scripts, systemd unit

This commit is contained in:
ABelliqueux 2024-04-08 18:23:16 +02:00
commit 73eb499261
3 changed files with 159 additions and 0 deletions

View File

@ -0,0 +1,14 @@
[Unit]
Description=GIO mount smb share-name
After=network.target
Before=mpd.service
[Service]
Type=oneshot
ExecStart=/home/%u/.local/bin/multimount.sh
ExecStop=/home/%u/.local/bin/multimount.sh -u
ExecReload=/home/%u/.local/bin/multimount.sh -u && /home/%u/.local/bin/multimount.sh
RemainAfterExit=true
[Install]
WantedBy=default.target

115
.local/bin/mount-share.sh Executable file
View File

@ -0,0 +1,115 @@
#! /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

30
.local/bin/multimount.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/env bash
# Mount multiples smb shares in a raw with the mount-share script
#
# Path to script
MOUNTSCRIPT="/home/$USER/.local/bin/mount-share.sh"
# Names of mounts to iterate over
MOUNTS=("medias" "medias_uno" "medias_dos")
# For verbose output
VERB="Mounting"
if [[ "$1" == "-u" ]];then
UMOUNT=1
VERB="Unmounting"
elif [[ "$1" != "" ]];then
echo "Bad argument..."
fi
# Un/Mount mounts in $MOUNTS :)
for mount in "${MOUNTS[@]}"
do
echo "$VERB $mount..."
if [[ "$UMOUNT" == 1 ]];then
"$MOUNTSCRIPT" "-u" "$mount"
else
"$MOUNTSCRIPT" "$mount"
fi
done
exit 1