Mounting Samba Shares on Startup and Unmounting on Shutdown

Forum rules
Before you post please read how to get help
ugly
Level 2
Level 2
Posts: 92
Joined: Thu Nov 24, 2016 9:17 pm

Mounting Samba Shares on Startup and Unmounting on Shutdown

Postby ugly » Sun Jul 16, 2017 7:55 pm

I'm trying to automatically mount Samba shares from a remote PC on startup and then unmount them automatically on shutdown. I had it working on Mint 17.3, but it stopped working when I upgraded to 18.1. However, I'm guessing there's a more obvious and easy way of doing this since what I'm doing seems a bit complicated.

I have one PC (I'll refer to it as ZBOX) that runs a few automated tasks and has a few Samba shares available. I access the Samba shares from my main PC (I'll refer to it as mainbox). Neither PC is on all the time. I'm trying to make it so that if the ZBOX is already on, then my mainbox will automatically mount the Samba shares after I boot. And if my mainbox is already on, and I start up my ZBOX then the mainbox will mount the Samba shares when they're available. I have this working so far.

I'd also like to have the Samba shares unmounted when the ZBOX is shut down first. This is because when the ZBOX is shutdown and the Samba shares haven't been unmounted on the mainbox then there is a fairly long delay when I try to shutdown the mainbox.

Essentially, what I did was make a couple of scripts that ping the other computer on startup and shutdown.

On my mainbox I have one script in /etc/init.d/ZBOXMount:

Code: Select all

#!/bin/bash
### BEGIN INIT INFO
# Provides:          ZBOXMount
# Required-Start:    network
# Required-Stop:
# Should-Start:     
# Default-Start:     5
# Default-Stop:
# Short-Description: Mounts ZBOX samba share
# Description:       Checks if ZBOX is accessible and mounts the shared and laptop folder if it is
### END INIT INFO


#address of local IP - zbox
localaddress=192.168.1.74
mainboxuser=rick
mainboxgroup=rick

ping -c 1 $localaddress 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
#  echo "Host found"
  mount.cifs //$localaddress/Shared /media/zbox/Shared -o iocharset=utf8,credentials=/home/$mainboxuser/.smbcredentials,uid=$mainboxuser,gid=$mainboxgroup,noauto,sec=ntlm,nounix 0 0
  mount.cifs //$localaddress/laptop /media/zbox/laptop -o iocharset=utf8,credentials=/home/$mainboxuser/.smbcredentials,uid=$mainboxuser,gid=$mainboxgroup,noauto,sec=ntlm,nounix 0 0
fi

I have this symlinked to /etc/rc2.d/S01ZBOXMount and to /etc/rc5.d/S01ZBOXMOUNT

This seems to work. Basically, I ping the local IP of the ZBOX, and if it's there, then I mount my two shares.

I also had to add to visudo:

Code: Select all

rick ALL = NOPASSWD: /sbin/mount.cifs, /bin/umount


On my ZBOX, I made one script in /etc/init.d/MountSambaFromZBOXStartup:

Code: Select all

#!/bin/bash
### BEGIN INIT INFO
# Provides:          MountSambaFromZBOXStartup
# Required-Start:    network
# Required-Stop:
# Should-Start:     
# Default-Start:     5
# Default-Stop:
# Short-Description: Mounts ZBOX samba share when ZBOX starts
# Description:       Checks if PC is accessible and mounts the shared and laptop folder if it is
### END INIT INFO

#address of local IP - mainPC
localaddressmain=192.168.1.73
localaddresszbox=192.168.1.74
mainboxuser=rick
mainboxgroup=rick
zboxuser=rick

ping -c 1 $localaddressmain 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
  #echo "Host found"
sleep 10
  ssh -i /home/$zboxuser/.ssh/id_rsa $mainboxuser@$localaddressmain mount.cifs //$localaddresszbox/Shared /media/zbox/Shared -o iocharset=utf8,credentials=/home/$mainboxuser/.smbcredentials,uid=$mainboxuser,gid=$mainboxgroup,noauto,sec=ntlm,nounix 0 0
  ssh -i /home/$zboxuser/.ssh/id_rsa $mainboxuser@$localaddressmain mount.cifs //$localaddresszbox/laptop /media/zbox/laptop -o iocharset=utf8,credentials=/home/$mainboxuser/.smbcredentials,uid=$mainboxuser,gid=$mainboxgroup,noauto,sec=ntlm,nounix 0 0
#else
#   echo "host not found"
fi

I symlinked this to /etc/rc2.d/S01MountSambaFromZBOXStartup and to /etc/rc5.d/MountSambaFromZBOXStartup

This works similarly. The idea is when I startup, I ping the local IP of my mainbox and if it is found, then I mount the shares over ssh. For some reason when going from Mint 17.3 to 18.1, I had to add the sleep 10. That was basically guesswork and trial and error. Not sure why 17.3 worked without the sleep command.

Then, for shutdown, I have a script /etc/init.d/UnmountSambaFromZBOXShutdown:

Code: Select all

#!/bin/bash
### BEGIN INIT INFO
# Provides:          UnmountSambaFromZBOXShutdown
# Required-Start:    network
# Required-Stop:
# Should-Start:     
# Default-Start:     5
# Default-Stop:
# Short-Description: Unmounts ZBOX samba share on shutdown
# Description:       Checks if PC is accessible and unmounts the shared and laptop folder if it is
### END INIT INFO

#address of local IP - zbox
localaddressmain=192.168.1.73
#localaddresszbox=192.168.1.74
sharedFolder=/media/zbox/Shared
laptopFolder=/media/zbox/laptop
mainboxuser=rick
zboxuser=rick

ping -c 1 $localaddressmain 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
   #echo "Host found"
   ssh -i /home/$zboxuser/.ssh/id_rsa $mainboxuser@$localaddressmain sudo /bin/umount -f $sharedFolder
   ssh -i /home/$zboxuser/.ssh/id_rsa $mainboxuser@$localaddressmain sudo /bin/umount -f $laptopFolder
   #else
#   echo "host not found"
fi

I have this symlinked to /etc/rc0.d/K01UnmountSambaFromZBOXShutdown and /etc/rc6.d/K01UnmountSambaFromZBOXShutdown

Similar concept, I ping the local IP of my mainbox, and if it's on then I umount the Samba shares.

Unfortunately, this stopped working after I upgraded from 17.3. I'm not sure why.

I also suspect that this is a completely stupid way of attempting to do what I want. I don't really know anything about using init.d scripts. I essentially guessed at the INIT INFO header. And getting this to work took a lot of trial and error. So if there is a smarter way of accomplishing these tasks, I'd be happy to know.

Return to “Scripts & Bash”