Auto Mount SMB shares when connected to WiFi with Tasker + Execute + scripts

mikeserv

Member
May 23, 2010
34
11
0
So I've really grown to like having my local SMB shares from my media server mounted to my Xoom so that I can stream video and/or music or whatever on the fly. I don't like the mountpoints getting all messed up when I leave the network though, and I had less than stellar experiences with both Mount Manager and CIFSManager.

CIFSManager worked, but didn't integrate with Tasker, so I couldn't automate it. Mount Manager (the paid version anyway) came with a Tasker plugin, but I don't think it was fully compatible with the Honeycomb OS and I had a lot of issues with it (called the developer and left a message a few hours after buying it, hopefully I can get a refund for my $2.99).

Then I found the (free!) Locale Execute plugin and decided to just script the behavior I wanted myself. If you have Tasker and local SMB shares as well this could be a good solution for you. Just install the Locale Execute plugin from the market and follow these instructions. I'll post them here because I can't post to the dev section yet, and I'm not really sure if this is dev worthy anyway.

First I setup a profile for my home WiFi network (WiFi Connected > SSID [my home WiFi SSID]. Then I added an Enter action with the Execute plugin:

Code:
@!sh /[path]/[to]/[chk_and_mnt_script]/cifs_chk_and_mnt.sh
But I needed to make sure the mount points are cleaned if I disconnect for any reason so I added an Exit action with the Execute plugin as well:

Code:
@!sh /[path]/[to]/[umnt_script]/cifs_umount.sh
In Execute the "@" symbol is used to keep the program from echoing commands to "toasts" (the little black notification boxes you get sometimes, like when an app is "granted SuperUser permissions") and the "!" symbol is used to grant the line of Execute code root access.

I removed the toasts but I still wanted some notification as to what was going on and a quick google revealed this xda post: http://forum.xda-developers.com/showthread.php?t=773232

In that post I found a little .apk that would allow me to call a toast from a bash script. I've attached it to this post.

In the mount script I run a little check to find out if the "cifs.ko" kernel module is already loaded. If it is I skip that block, but if not I go ahead and load it. This way I always know that the kernel module is available before I try to mount the SMB shares.

Here's the Enter bash script I wrote called "cifs_chk_and_mnt.sh":

Code:
#!/bin/bash
lsmod | grep -q 'cifs' #check if cifs module is loaded
if [[ $? -eq 1 ]] #if not
then
    am start -a android.intent.action.MAIN -e message 'cifs.ko kernel module not yet loaded. Loading now...' -n com.rja.utility/.ShowToast #displays toast
    insmod /system/lib/modules/cifs.ko #loads kernel module
fi
am start -a android.intent.action.MAIN -e message 'Mounting local SMB network shares...' -n com.rja.utility/.ShowToast #displays toast
mount -o username=[windows login],password=[your password] -t cifs //[host ip]/[share] /[path]/[to]/[mount]/[point] #actually mounts share
#replace "[]" info with your own with NO "[]" and copy and paste line for as many shares as you want to mount
And here's the Exit script called "cifs_umount.sh":
Code:
#!/bin/bash
am start -a android.intent.action.MAIN -e message 'Home network lost. Clearing local SMB share mount points...' -n com.rja.utility/.ShowToast #displays toast
umount /[path]/[to]/[mount]/[point] 
#replace "[]" info with your own with NO "[]" and copy and paste line for as many shares as you have mounted in the Enter script
Now, like I said, I used the linked (and attached) .apk to display toasts from the BASH scripts, but if you don't want it or care enough to use it then feel free to delete the toast lines or throw a "#" in front of them. If you do want the toasts displayed then you'll have to download it and install it. It won't show in your app drawer.

Anyway, here are the files (I had to add a ".txt" extension to the .sh files to comply with xda's attachment rules, but you can just rename them):
 

Attachments

richardkemp

Senior Member
Dec 12, 2005
162
18
0
Thanks for this, looks great! I literally just paid for one of the premium smb apps only to find it didn't do quite what I wanted. I'm sure this will be what I'm looking for, cheers.

Edit:
Something for users to note: The umount script wasn't working for me, after some investigation it turns out that though when mounting, '/sdcard/dir' is acceptable, though the mount command actually considers my sdcard to actually be at '/mnt/sdcard' , so *that* was the dir I had to use when unmounting afterwards. This may be true for others too. I am using CM7 on a Desire.
 
Last edited:

richardkemp

Senior Member
Dec 12, 2005
162
18
0
Looking into this, I found that on my CM7 Desire, mounting the shares worked even if "lsmod | grep cifs" returns nothing, and the file is not in the place mentioned in your script either. To streamline the whole process, I just removed all the complicated stuff from the script and ran the mount command directly, saving time. Just a heads up for others, try it and see if it works for you.
 
Sep 5, 2012
32
5
0
Thanks so much for this!
Unfortunately, I have a couple of problems... When the share mounts, it is not in utf-8 format, so folders with odd characters do not show, even though I have that preference set in Mount Manager!
In addition, the unmount script does not work for some reason. When disabling wifi, I recieve a toast that Tasker got the message and executed my exit task, but there is no toast for the unmount script. I've triple checked for grammatical or directory errors, but there are none. Any idea why the unmount script isn't working?
 

mikeserv

Member
May 23, 2010
34
11
0
Thanks so much for this!
Unfortunately, I have a couple of problems... When the share mounts, it is not in utf-8 format, so folders with odd characters do not show, even though I have that preference set in Mount Manager!
In addition, the unmount script does not work for some reason. When disabling wifi, I recieve a toast that Tasker got the message and executed my exit task, but there is no toast for the unmount script. I've triple checked for grammatical or directory errors, but there are none. Any idea why the unmount script isn't working?
To be honest, I completely forgot I ever wrote this. Still, the unmount issue could be due to your busybox version. Have you tried executing the relevant commands at a root enabled terminal? Do they work there?

Moreover, you should understand that I apparently wrote this to be completely independent of "Mount Manager" or any related app and these scripts are dependent only on: busybox, Tasker, and the Execute plugin.

-Mike
 
Sep 5, 2012
32
5
0
To be honest, I completely forgot I ever wrote this. Still, the unmount issue could be due to your busybox version. Have you tried executing the relevant commands at a root enabled terminal? Do they work there?

Moreover, you should understand that I apparently wrote this to be completely independent of "Mount Manager" or any related app and these scripts are dependent only on: busybox, Tasker, and the Execute plugin.

-Mike
I tried executing from terminal and the mount script works fine, but the unmount does not work. It gives me a "no closing quote" error. I'm pretty noobish when it comes to scripts, but it doesn't look like there are errors in the script itself...

Also, if it is independent of Mount Manager, is there a way to include utf-8 formatting in the script?


Sent from my Galaxy Nexus
 

Attachments

Last edited:

loogielv

Senior Member
Oct 15, 2010
463
88
0
So I've really grown to like having my local SMB shares from my media server mounted to my Xoom so that I can stream video and/or music or whatever on the fly. I don't like the mountpoints getting all messed up when I leave the network though, and I had less than stellar experiences with both Mount Manager and CIFSManager.

CIFSManager worked, but didn't integrate with Tasker, so I couldn't automate it. Mount Manager (the paid version anyway) came with a Tasker plugin, but I don't think it was fully compatible with the Honeycomb OS and I had a lot of issues with it (called the developer and left a message a few hours after buying it, hopefully I can get a refund for my $2.99).

Then I found the (free!) Locale Execute plugin and decided to just script the behavior I wanted myself. If you have Tasker and local SMB shares as well this could be a good solution for you. Just install the Locale Execute plugin from the market and follow these instructions. I'll post them here because I can't post to the dev section yet, and I'm not really sure if this is dev worthy anyway.

snip...
I dont care this post is 5 years old, i dont care that i'm bumping this thread...this little gold brick of information was FINALLY what i needed to figure out how to mount cifs without that "never-gonna-be-updated-ever-ever-again-cifsmanager" apk. I just threw the same basic code into tasker and finally got a mount. thank you! if i was more secure in my manhood, i just might embarrass myself.

the real reason for the bump, instead of mere word wasting, is to help anyone anyone searching for the cifs manager apk, or getting the error "no such device" when trying to mount. The issue is connected to the both the "mount name space separation" in SUPERSU, the old android sdcard permissions and who knows what else.

heres' the code I put into tasker:

su --mount-master -c busybox mount -o username=[USERNAME],password=[PASSWORD],rw,file_mode=0777,dir_mode=0777,iocharset=utf8,context=u:eek:bject_r:rootfs:s0 -t cifs //192.168.xxx.xxx/cifsync /sdcard/cifsync

no brackets of course.

that popped it right online, no issues but my kernel provides cifs.io support through synapse settings app. so it was easy to load the module at startup
 
Last edited:
  • Like
Reactions: DABugh

roizcorp2

Senior Member
Oct 25, 2015
69
25
0
I dont care this post is 5 years old, i dont care that i'm bumping this thread...this little gold brick of information was FINALLY what i needed to figure out how to mount cifs without that "never-gonna-be-updated-ever-ever-again-cifsmanager" apk. I just threw the same basic code into tasker and finally got a mount. thank you! if i was more secure in my manhood, i just might embarrass myself.

the real reason for the bump, instead of mere word wasting, is to help anyone anyone searching for the cifs manager apk, or getting the error "no such device" when trying to mount. The issue is connected to the both the "mount name space separation" in SUPERSU, the old android sdcard permissions and who knows what else.

heres' the code I put into tasker:

su --mount-master -c busybox mount -o username=[USERNAME],password=[PASSWORD],rw,file_mode=0777,dir_mode=0777,iocharset=utf8,context=u:eek:bject_r:rootfs:s0 -t cifs //192.168.xxx.xxx/cifsync /sdcard/cifsync

no brackets of course.

that popped it right online, no issues but my kernel provides cifs.io support through synapse settings app. so it was easy to load the module at startup
in code format so there will be no syntax issues -
Code:
su --mount-master -c busybox mount -o username=[USERNAME],password=[PASSWORD],rw,file_mode=0777,dir_mode=0777,iocharset=utf8,context=u:object_r:rootfs:s0 -t cifs //192.168.xxx.xxx/cifsync /sdcard/cifsync