commit 73eb499261601a2334d2e0e251d0ac9321482ce5 Author: ABelliqueux Date: Mon Apr 8 18:23:16 2024 +0200 Add scripts, systemd unit diff --git a/.config/systemd/user/gio-mount-myshares.service b/.config/systemd/user/gio-mount-myshares.service new file mode 100644 index 0000000..c4bacc4 --- /dev/null +++ b/.config/systemd/user/gio-mount-myshares.service @@ -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 \ No newline at end of file diff --git a/.local/bin/mount-share.sh b/.local/bin/mount-share.sh new file mode 100755 index 0000000..e7c02de --- /dev/null +++ b/.local/bin/mount-share.sh @@ -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 . +# +# @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 diff --git a/.local/bin/multimount.sh b/.local/bin/multimount.sh new file mode 100755 index 0000000..08a5aab --- /dev/null +++ b/.local/bin/multimount.sh @@ -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 +