Skip to content

Instantly share code, notes, and snippets.

@gboudreau
Last active September 14, 2020 03:57
Show Gist options
  • Save gboudreau/1876068 to your computer and use it in GitHub Desktop.
Save gboudreau/1876068 to your computer and use it in GitHub Desktop.
mount_shares_locally for Fedora/CentOS
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: mount_shares_locally
# Required-Start: $network $local_fs $remote_fs smb mysqld
# Required-Stop: $network $local_fs $remote_fs smb
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mount Samba shares locally
### END INIT INFO
username="your username"
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi
start () {
uid=`id -u $username`
gid=`id -g $username`
echo -n $"Mounting Samba shares locally: "
mkdir -p /mnt/samba/
cd /mnt/samba/
testparm -s /etc/samba/smb.conf 2>/dev/null | grep "^\[" | grep -v "\[global\]\|\[homes\]\|\[netlogon\]\|\[sysvol\]\|\[printers\]" | awk -F'[' '{print $2}' | awk -F']' '{print $1}' | xargs -d "\n" mkdir -p
sleep 5
opt="credentials=/home/${username}/.smb_credentials,uid=${uid},gid=${gid},file_mode=0660,dir_mode=0770,nobrl,hard,_netdev,iocharset=utf8,noserverino,mfsymlinks"
if mount.cifs 2>&1 | grep vers= >/dev/null; then
opt="$opt,vers=3.0"
elif man mount.cifs 2>&1 | grep vers= >/dev/null; then
opt="$opt,vers=3.0"
fi
while IFS='' read -r d; do
if [ "`mount | grep "//127.0.0.1/$d/* on " | wc -l`" = "0" ]; then
/sbin/mount.cifs "//127.0.0.1/$d" "$d" -o $opt
else
echo " Share [$d] is already mounted."
fi
done < <(testparm -s /etc/samba/smb.conf 2>/dev/null | grep "^\[" | grep -v "\[global\]\|\[homes\]\|\[netlogon\]\|\[sysvol\]\|\[printers\]" | awk -F'[' '{print $2}' | awk -F']' '{print $1}')
touch /var/lock/subsys/mount_shares_locally
success $"$base startup"
echo
return 0
}
stop () {
echo -n $"Unmounting locally mounted Samba shares: "
/bin/umount -l /mnt/samba/*
rm -f /var/lock/subsys/mount_shares_locally
success $"$base shutdown"
echo
return 0
}
restart () {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment