Samba Wake On Demand Script

Recently , I purchased a raspberry pi and I have to say that it is an amazing device. It now functions as my 24/7 server, however I have already a file server whereby I use to keep important files (such as family vacation photos , important documents , backup of my phone contacts and my save game files if any). The problem was leaving the server 24/7 as it draws about 60W (idle) as opposed to the pi which is about 5W. Of course this could solved by putting the server to sleep and waking it up with a Wake On Lan tool. It’s great if it was me that want to access the shared folder , but how about my less tech savvy family-members

Of course there are many methods such as using Apple Airport Router with Bonjour service to automatically wake the PC and serve. But then I was not willing to spend a lot of money on a new router for that functionality. The other option was to write a bash script and place in their PC whenever they wish to access the shared folder (basically wake the server up and serve the files).

After deep-google-searching , I’ve found the idea on how to resolve the problem all together.I found it was possible to execute scripts on Samba when a user connects and disconnects . This is exactly what I want , when the user connects to my raspberry pi , it would automatically wake my file serve and serve the files , as if it was coming from raspberry pi. All transparent. It takes care of everything and at the same time I want to reduce my electricity consumption as well

SAMBA supports the ability to execute a script when the user connects and when the user disconnects. So whenever the user authenticates to the server and accesses this share , it would trigger the script to wake up my file server , serve the files and when the user disconnects , it would just disconnect the file server (I have a cron script that actually puts the server to sleep after 30 minutes when there is no samba connection).

on_mount_script

[code language=”bash”] #!/bin/sh
#Revision v0.1 Pradeesh
hostsrv=192.168.1.107 #soon
ping -c 1 -w 1 -q $hostsrv &amp;gt;/dev/null<status=$?
if [ $status -ne 0 ] then
/usr/sbin/etherwake XX:XX:XX:XX:XX:XX
sleep 20
fi
mount -t cifs -o user=USERNAME,password=PASSWORD,rw,file_mode=0775,dir_mode=0775 //SERVER/PATH /location/of/mountpoint[/code]

on_unmount_script

This script is optional but I use it to indicate to close connection on my file server so that when my cron script runs if it is ready to sleep , it would just sleep gracefully

[code language=”bash”]<br />!/bin/sh<br />umount /location/of/mountpoint<br />[/code]

It is somewhat a messy script at this moment. I am thinking of improvising in the future. Just be sure to replace the XX:XX with the MAC address of the PC that you want to wake.

Be sure to also replace USERNAME,PASSWORD , //SERVER/PATH , /location/of/mountpoint with the right values , username,password,server path refers to the remote Samba server that you want to mount and /location/of/mountpoint refers to the place where it should be mounted

To copy these scripts , simply run nano , copy and paste the content and name it as on_mount_script.sh and on_unmount_script.sh . Be sure to make it executable (chmod +x)

Once you are done with this , head over to the SAMBA configuration file , be sure to add a new point and include the following additional lines

i.e sudo nano /etc/samba/smb.conf

[code language=”text”] [file_server] root postexec = /bin/on_umount_script.sh
comment = File Server
root preexec = /bin/on_mount_script.sh
path = /mnt/file_server<br />[/code]

You would be needing to execute it as root as generally mount and un-mount command requires root previlages and on top of that ether wake also requires root permissions.

Once it is done , you may start connecting to your new SAMBA relay server ,in my case my raspberry pi . You’ll notice a new share , the moment you click it , you would notice that it takes some time – this is because it would first wake up the server , wait for 20 seconds and then execute the mount command to connect to the remote SAMBA server and mount it. As you can see below , both of it are actually pointing to the same source.

192.168.1.132 being my pi and 192.168.1.107 being my file server. Since I am not able to access my file server mount directly when it is asleep at least I know that it would be possible to wake up my file server and serve files by visiting my pi directly 🙂

Capture

Hope this helps. Also if you are not able to run the script – be sure to have etherwake installed (install it either using dpkg or yum or any package manager that your distro uses) – in my case I am using Debian . Also in my next tutorial , I’ll be sharing a nifty on demand sleep script that checks if anyone is connected or any important process is running . If not , it puts the server to sleep

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge