skip navigation

www.Hilands.com


Content:: Samba mount script

Samba mount script
Last Modified: 2012-10-21
Instead of using the fstab to mount shared drives we will be using a shell script and the smbmount command.

Table of Contents
Samba credentials file
We will be using a samba credentials file, which gives us the ability to store the user name and password in a "secure file."

The command will look like the following
	smbmount //<server>/<share> <local-mount-path> -o credentials=<path-to-credentials-file>,uid=<userid>,gid=<groupid>
First we will create a credentials file. Its a basic text file with two paramenters: username and password.
My preferences is storing the credentials in a folder named ".credentials" in my home directory. Then creating another file named after the server name and user name. # mkdir ~/.credentials
# nano ~/.credentials/<server>_<user>
The basic Shell Script
We will bring in two options mount and umount within a case statement. It will also send a how to use if no options are sent.
	#!/bin/bash
	case "$1" in
		mount)
		;;
		umount)
		;;
		*)
			echo "Usage: $0 { umount | mount }"
		exit 1
	esac
	exit 0
Triggering sudo with echo
We will be adding a line to trigger the sudo password prompt. This is primarily aesthetics to get the password prompt at the beginning of the script output. As sudo only requires a password prompt per terminal session, or in this case per script execution, we will only have to enter the password one time. Mounting with cifs will deny us the ability to mount giving us the reasons of permissions, which is why we are using sudo.
	sudo echo -n "" >/dev/null 2>&1
Getting the User and Group ID
Next we can collect the User ID and Group ID to add to our command. By default Debian automatically defines the $UID so all we will need to do is
	#UID=`id -u` # get User ID
	GID=`id -g` # get Group ID
smbmount command
The smbmount will may appear lengthy but it is fairly simple. Note as we are running this through sudo we must use the full path for the users home directory not just the tilde "~/".
	sudo smbmount //<server>/<share> /mnt/<server>/<share> -o credentials=/home/<user>/.credentials/.<server>_<user>,uid=$UID,gid=$GID>/dev/null 2>&1
If you have multiple mount points you may want to simplify this by creating a string variable for the credentials path.
	strCredPath="/home/<user>/.credentials/.<server>_<user>"
Using the credentials variable will change our smbmount command to
	sudo smbmount //<server>/<share> /mnt/<server>/<share> -o credentials=$strCredPath,uid=$UID,gid=$GID>/dev/null 2>&1
Adding a touch of verbose
To make the script really fancy, we can add a bit of verbosity to it. Using the -n switch with echo will keep the print out to the display on the same line.
echo -n "Mounting drives"
We can add a simple progress counter, displaying a period "." while the script is progressing.
echo -n "."
Finishing off the script we'll display a message stating that the job is complete and send a pas with the read command.
	echo "mount complete"
	read -n1 -r -p "Press any key to continue..." key
Bringing it all together
Our final script will look like the following
	#!/bin/bash
	strCredPath="/home/<user>/.credentials/.<server>_<user>"
	case "$1" in
		mount)
			sudo echo -n "" >/dev/null 2>&1
			echo -n "Mounting drives"
			echo -n "."
			#UID=`id -u` # get User ID
			echo -n "."
			GID=`id -g` # get Group ID
			echo -n "."
			sudo smbmount //<server>/<share> /mnt/<server>/<share> -o credentials=$strCredPath,uid=$UID,gid=$GID>/dev/null 2>&1
			echo -n "."
			echo "mount complete"
			read -n1 -r -p "Press any key to continue..." key
		;;
		umount)
			sudo echo -n "" >/dev/null 2>&1
			echo -n "Un-mounting drives"
			echo -n "."
			sudo umount /mnt/<server>/<share>/ >/dev/null 2>&1
			echo -n "."
			echo "umount complete"
			read -n1 -r -p "Press any key to continue..." key
		;;
		*)
			echo "Usage: $0 { umount | mount }"
		exit 1
	esac
	exit 0