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
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
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
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
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.