[Solved] Mounting Samba Shares on Startup and Unmounting on Shutdown

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

[Solved] Mounting Samba Shares on Startup and Unmounting on Shutdown

Post by 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.
Last edited by ugly on Wed Nov 21, 2018 4:19 am, edited 1 time in total.

User avatar
T_Characht3r
Level 1
Level 1
Posts: 34
Joined: Fri Sep 21, 2018 8:58 pm

Re: Mounting Samba Shares on Startup and Unmounting on Shutdown

Post by T_Characht3r » Mon Nov 12, 2018 3:51 pm

I'm not very familiar with terminal, but i'm guessing the issue may be that 18.3 (or whatever it is) terminal has different commands. Make sure all the commands are correct.
Don't 'Wine' about emulating Windows.
if your problem has been solved, please edit your original post and add [SOLVED] to the beginning of the Subject Line.
*NOT SO ACTIVE, PM ME IF I FORGET A DISCUSSION .*

ugly
Level 3
Level 3
Posts: 170
Joined: Thu Nov 24, 2016 9:17 pm

Re: Mounting Samba Shares on Startup and Unmounting on Shutdown

Post by ugly » Mon Nov 12, 2018 7:29 pm

Wow, I don't know where you managed to dig up this topic from. I completely forgot about it.

I don't know what was causing the issue with the particular setup I had back then. But I did manage to find an alternate solution (using similar scripts).

I'll make a note to post a more comprehensive update when I have more time (probably this weekend), just in case someone else finds it useful. But the basic answer is that I changed to using systemd to handle these scripts at startup and shutdown. They've worked flawlessly since.

User avatar
T_Characht3r
Level 1
Level 1
Posts: 34
Joined: Fri Sep 21, 2018 8:58 pm

Re: Mounting Samba Shares on Startup and Unmounting on Shutdown

Post by T_Characht3r » Tue Nov 13, 2018 12:54 am

ugly wrote:
Mon Nov 12, 2018 7:29 pm
Wow, I don't know where you managed to dig up this topic from. I completely forgot about it.
I frequently, when checking the forums, will go and seek out any topic that hasn't got a response yet, and I will also check the second page. :mrgreen:
Don't 'Wine' about emulating Windows.
if your problem has been solved, please edit your original post and add [SOLVED] to the beginning of the Subject Line.
*NOT SO ACTIVE, PM ME IF I FORGET A DISCUSSION .*

ugly
Level 3
Level 3
Posts: 170
Joined: Thu Nov 24, 2016 9:17 pm

Re: Mounting Samba Shares on Startup and Unmounting on Shutdown

Post by ugly » Sat Nov 17, 2018 3:17 pm

I'll post my current implementation just in case it helps anyone else. Here's how I accomplish automatically mounting samba shares on startup and automatically unmounting samba shares on shutdown:

The mounting script is the same as I had above. I run this script on my main PC (the PC that I want to have the drives mounted on when I boot). In this script, I refer to it as the 'mainbox'.

First, to the end of my /etc/fstab file I added:

Code: Select all

//192.168.1.74/Shared /media/zbox/Shared cifs _netdev,guest,users,noauto,nofail,credentials=/home/rick/.smbcredentials
//192.168.1.74/laptop /media/zbox/laptop cifs _netdev,guest,users,noauto,nofail,credentials=/home/rick/.smbcredentials
Then, this is the script that I want to run on startup:

Code: Select all

#!/bin/sh
#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,nounix 0 0
  mount.cifs //$localaddress/laptop /media/zbox/laptop -o iocharset=utf8,credentials=/home/$mainboxuser/.smbcredentials,uid=$mainboxuser,gid=$mainboxgroup,noauto,nounix 0 0
fi
I call this bash script zbox-mount, copy it to /usr/bin/ and make it executable (sudo chmod +x /usr/bin/zbox-mount). I also take ownership: (sudo chown -R rick:nogroup /usr/bin/zbox-mount)

For my setup there are two different shared folders I'm mounting that are shared by my other PC (I call it zbox because it's a Zotac ZBOX mini-pc). The zbox has the IP address 192.168.1.74. On my zbox I have a samba share called Shared and one called laptop.

The basic idea is that when I boot my PC, I ping the zbox, and if the zbox responds then I use mount.cifs to mount the two shared folders. So //192.168.1.74/Shared gets mounted to /media/zbox/Shared on my mainbox.

The use case for this is when the samba share (my zbox computer) is already on, and I boot or reboot my main PC.

Obviously, if someone else wanted to use this script, they'd need to change the IP address, the user and group name, and the paths. I'm also using a credentials file at ~/.smbcredentials for permission.

To get this script to run at startup I use systemd. To do this I made a service file called zbox-mount.service

Code: Select all

[Unit]
Description=zbox mount
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=oneshot
RemainAfterExit=false
ExecStart=/usr/bin/zbox-mount

[Install]
WantedBy=multi-user.target
I have a note that under [Service] to add a user and group (e.g., User=rick and Group=nogroup on separate lines), but it doesn't seem to be needed.

Copy the service file to /etc/systemd/system/

Then to run it at startup, open a terminal and run: sudo systemctl enable zbox-mount

And, finally, to enable mount.cifs and umount to work without a password, open a terminal and run sudo visudo
And at the end of the file add (replacing rick with your own username):
rick ALL= NOPASSWD: /sbin/mount.cifs, /bin/umount

I also have scripts for the other scenario (when my main PC is on and I turn on the zbox afterwards).

I'll try to give a shorter description. The key point is that these scripts are run on the zbox (the PC with the shared folders). The previous script was run on my main PC.

Also, this involves ssh commands, so you'd have to have ssh set up for this to work.

To keep this short, here's the mounting script:

Code: Select all

#!/bin/bash
#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

#Keeps terminal open
#$SHELL
This file will be located at: /usr/bin/zbox-mount

The basic idea is that this will ping my main PC, and if a response is received, it will use ssh to run the same mount.cifs commands as in the previous script.

To get this to run when I boot my zbox, the process is similar. Make a service file for systemd:

Code: Select all

[Unit]
Description=zbox mount
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=oneshot
RemainAfterExit=false
ExecStart=/usr/bin/zbox-mount

[Install]
WantedBy=multi-user.target
Save the file as: /etc/systemd/system/zbox-mount.service

And run: sudo systemctl enable zbox-mount

The last set of scripts, that will unmount the shared folders on the main PC when the zbox is shutdown is this:

Code: Select all

#!/bin/bash
#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

#Keeps terminal open
#$SHELL
Save this file as /usr/bin/zbox-samba-unmount

The basic idea is that the script will ping the main PC, and if a response is received, run some ssh commands to unmount the mounted shares. This uses /bin/umount

Then, to have this run automatically when the zbox is shutdown make another service file:

Code: Select all

[Unit]
Description=zbox samba unmount
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/bin/zbox-samba-unmount

[Install]
WantedBy=multi-user.target
Save the file as: /etc/systemd/system/zbox-samba-unmount.service

And run: sudo systemctl enable zbox-samba-unmount

Hopefully that is everything. These scripts would have to be edited a decent amount to suit someone else's needs, but hopefully it's a decent template.

Post Reply

Return to “Scripts & Bash”