LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-12-2007, 02:04 PM   #1
calande
Member
 
Registered: Oct 2005
Distribution: Ubuntu
Posts: 165

Rep: Reputation: 15
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
 
Old 02-13-2007, 06:49 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.

Last edited by acid_kewpie; 02-13-2007 at 06:50 AM.
 
Old 02-13-2007, 07:10 AM   #3
calande
Member
 
Registered: Oct 2005
Distribution: Ubuntu
Posts: 165

Original Poster
Rep: Reputation: 15
Thanks Chris, how do I get started to set that up?
 
Old 02-13-2007, 08:40 AM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.)
 
Old 02-13-2007, 09:01 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 02-13-2007, 09:23 AM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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>

Last edited by PTrenholme; 02-13-2007 at 09:36 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mounting samba shares at startup somebodycr Linux - Software 7 09-17-2010 06:20 AM
mounting shares with samba darkleaf Linux - Software 1 02-13-2005 06:13 AM
SAMBA auto sync shares KaiserSoze860 Linux - Software 0 05-16-2004 01:35 PM
Auto-mounting drives at startup MandrakeDave Linux - General 7 09-29-2003 05:19 PM
Mounting Samba shares and Samba Share Login time112852 Linux - Software 1 09-14-2003 02:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration