LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Auto-mounting samba shares at startup (https://www.linuxquestions.org/questions/linux-software-2/auto-mounting-samba-shares-at-startup-527884/)

calande 02-12-2007 02:04 PM

Auto-mounting samba shares at startup
 
I'm in an office where samba shares are always different depending on the time, for instance at this time, jack and josh are connected, but jane is out of the office. Later, jane is here but jack has gone. You get the picture.

At the moment, I mount my samba shares manually from the terminal, because I use Gnome. I'd like a script to run each minute with a Cron job to check available samba shares on the network, and to mount or unmount them silently, depending if the share is available or not. Does some one have a script like this to create dynamically mount points under /mnt ? How could we do it? Thanks ;)

acid_kewpie 02-13-2007 06:49 AM

oh no no no you don't want to do that. you want to use autofs, which does exactly what you want... mounting on demand. centos does contain a script to mount smb shares, but afaik it's actually horribly horribly borked, unliek the nfs one which always works fine. what i'd suggest is just defining a few specific entries for autofs, rather than the cleverer approach the defective scripts in /etc/ try to use.

calande 02-13-2007 07:10 AM

Thanks Chris, how do I get started to set that up?

PTrenholme 02-13-2007 08:40 AM

You need an auto.master and auto.<service_name> file, usually in /etc/. Both files should be executaable.

Here's mine, from my Fedora system:
Code:

$ cat /etc/auto.master | grep -v ^#
/smb    /etc/auto.cifs --timeout=0
+auto.master

$ cat /etc/auto.cifs | grep -v ^#
#!/bin/bash
key="$1"
credfile="/etc/auto.smb.$key"
mountopts="-fstype=cifs,file_mode=0775,dir_mode=0775,uid=$USER,gid=smb"
smbclientopts=""
SMBCLIENT=$(whereis smbclient  | cut -f 2 -d " ")
if ! [ -x $SMBCLIENT ]
then
    echo No cmbclient executable found. Have you installed samba? >2
    exit 1
fi
if [ -r "$credfile" ]
then
        mountopts=$mountopts",credentials=$credfile"
        smbclientopts="-A "$credfile
else
        smbclientopts="-N"
fi
$SMBCLIENT $smbclientopts -gL $key\
  | awk -v key="$key" -v opts="$mountopts" -F'|' -- '
        BEGIN  { ORS=""; first=1 }
        /^Disk[^$]*$/  { if (first) { print opts; first=0 };
                  sub(/ /, "\\ ", $2);
                  if (length($2)==1) sub(/ ^/, "", $2);
                  print " \\\n\t /" tolower($2), "://" key "/" $2 }
        END    { if (!first) print "\n"; else exit 1 }
        '

This scrip mounts the shares under a root directory, /smb visable to everyone connected to the Linux system. (Most useres are using are running Linux on a laptop, with remote login disabled, so that no problem for us.) In a multi-user system, a mount point in each user's home directory would probably make more sense.

Notice that the connection script also looks for a "credentials" file containing the remote login information. Since my systems are open to everyone on the local net, I put them (the credentials) in /etc, but a better solution would be to look in, say, ~/.smbpasswrd for the user's credentials.

The autofs mounts the shares when they are first needed. The timeout=0 setting, above, keeps them mounted "forever," but other timeouts can be specified (in minutes).

Here's a script that could be called from a user's login to connect for them :
Code:

$ cat Scripts/MountSmb
#! /bin/bash
running=$(nm-tool eth1 | grep -r "*T.S.S.")
while [ -z "$running" ];do
    sleep 5
    running=$(nm-tool eth1 | grep -r "*T.S.S.")
done
ls /smb/$1/* > /dev/null

(The stuff before the last line is just to make sure the connection - to my AP, "T.S.S." - is established, since most users have laptops using WiFi, and they may not have established a connection when the script is run, or may be connected to a different AP.)

acid_kewpie 02-13-2007 09:01 AM

yeah, it's the awk statements in the script there that have always been totally screwed up. as above, if that is the case, i would strongly suggest just adding manual shares yourself, instead of this automated approach.

PTrenholme 02-13-2007 09:23 AM

Quote:

Originally Posted by acid_kewpie
yeah, it's the awk statements in the script there that have always been totally screwed up. as above, if that is the case, i would strongly suggest just adding manual shares yourself, instead of this automated approach.

Chris, that awk code works fine for me. It's easy enough to test it -- just run it from a command line, piping the output from smbclient into the program. You'll notice that I've changed the code slightly to force all the generated share names to lower case, and to skip all the NT-based default share names -- the ones ending in dollar signs.

<edit>
As an alternative to the script, run it (the script) by hand, capture the output to a file, and write a bash script to cat the file when called. Then you can "tweak" the file to your hearts content before it's used, and, of course, have a different file for each user. The point in the script is to mount all shares available from $key, so "tweaking" might be desirable in some cases.

The point of the script is just to get the automount information into the correct format.
</edit>


All times are GMT -5. The time now is 05:08 AM.