While gvfs is an acronym for Gnome Virtual File System it's usage is not depending on Gnome. https://forum.manjaro.org/t/100723
Find a file
2024-01-03 10:39:06 +01:00
gio-prototype.sh Add gio-prototype.sh 2024-01-03 10:37:15 +01:00
LICENSE Initial commit 2024-01-03 10:31:12 +01:00
README.md Update README.md 2024-01-03 10:39:06 +01:00

utility-gio-mount-share

Utility to mount samba share as user

While gvfs is an acronym for Gnome Virtual File System it's usage is not depending on Gnome. It comes with very few dependencies and even Manjaro Plasma has the gvfs package installed and as such this will work on any Linux.

The Linux mount utility does not allow for a user to mount a samba share and this results in Manjaro users setting up fstab to automount the share. Such automounts may cease working due to upstream changes e.g. when the SMB1 protocol was renamed to NT1 thus causing old shares to stop working.

Install the toolset

This script relies on the packages gvfs-smb which will pull the necessary smb-client dependency. The following command will do the trick. If you want the dependencies to be explicitly installed add gvfs and smbclient to the command. The --needed argument will skip syncing packages already present.

sudo pacman -Syu gvfs-smb --needed

Copy the main script gio-prototype.sh to ~/.local/bin and name it gio-mount-<sharename>.sh where <sharename> is what you like it to be.

mkdir -p ~/.local/bin
chmod +x ~/.local/bin/gio-mount-<sharename>.sh

Modify the variables to use your samba server and share name.

# your samba server's hostname or IP address
HOST="my-server"

# the share name on the server
SHARENAME="my-share"

# credentials
USERNAME=
WORKGROUP=
PASSWD=

The symlinks folder defaults to ~/SMBLinks and this can of course be changed as you see fit.

Usage

Running the script will attempt mounting the share.

gio-mount-<sharename>.sh

If credentials are needed you will be challenged. Enter the correct credentials to continue.

To unmount a mounted share run with -u argument.

gio-mount-<sharename>.sh -u

To avoid entering credentials at runtime you can fill in the credentials inside the script - they will then be fed to the mount option using a file in a hidden configuration folder .credentials.

user service

You can complement this with a systemd user service to automate things even more.

Create the folder

mkdir -p ~/.local/systemd/user

Create the service file

touch ~/.local/systemd/user/gio-mount-<sharename>.service

Edit the service file with your favorite text editor and paste below content

[Unit]
Description=GIO mount smb share-name

[Service]
Type=oneshot
ExecStart=/home/%u/.local/bin/gio-mount-<sharename>.sh
ExecStop=/home/%u/.local/bin/gio-mount-<sharename>.sh -u
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

Enable and start the service

systemctl --user enable gio-mount-<sharename>.service
systemctl --user start gio-mount-<sharename>.service

To simplify maintenance you can move the script to the service folder and change the ExecStart and ExecStop paths in the service file to /home/%u/.local/systemd/user/.

Extend the utility

If you are in a situation where your smb share is not available you could extend the script with a service check - e.g. ping the service at regular intervals - if it's available mount - if it becomes unavailable unmount.

E.g. using nmap

sudo pacman -Syu nmap

Then in a function run the check

nmap $HOST -PN -p 445 | grep open

Then mount or unmount depending on result

Prototype 2

checkConnection(){
    # use nmap to check the host for smb service
    if [ -z  $(nmap $HOST -PN -p 445 | grep up | cut -d' ' -f2) ] ; then
        echo "$HOST is reachable"
    else
        echo "$HOST not reachable"
    fi
}

Reference: https://root.nix.dk/en/utility-scripts/mount-samba-share-as-user