Skip to content

Instantly share code, notes, and snippets.

@gboudreau
Last active September 14, 2020 03:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gboudreau/1099419 to your computer and use it in GitHub Desktop.
Save gboudreau/1099419 to your computer and use it in GitHub Desktop.
mount_shares_locally for Ubuntu/Debian
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: mount_shares_locally
# Required-Start: $network $local_fs $remote_fs samba
# Required-Stop: $network $local_fs $remote_fs samba
# 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 /lib/lsb/init-functions ]; then
. /lib/lsb/init-functions
fi
LOCKFILE=/var/lock/mount_shares_locally
lock_mount_shares_locally() {
if [ -x /usr/bin/lockfile-create ]; then
lockfile-create $LOCKFILE
lockfile-touch $LOCKFILE &
fi
}
unlock_mount_shares_locally() { if [ -x /usr/bin/lockfile-create ] ; then
lockfile-remove $LOCKFILE
fi
}
start () {
uid=`id -u $username`
gid=`id -g $username`
log_daemon_msg "Mounting Samba shares locally..."
if [ ! -x /mnt/samba/ ]; then
mkdir -p /mnt/samba/
fi
cd /mnt/samba/
lock_mount_shares_locally
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}')
unlock_mount_shares_locally
log_end_msg
return 0
}
stop () {
log_daemon_msg "Unmounting locally mounted Samba shares..."
/bin/umount -l /mnt/samba/*
log_end_msg
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop && sleep 2 && $0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment