My configs / samba automounter

[ <- main ]

samba automounter

My samba automounter setup allows for automounting any share on any host available on the LAN (just as smbmount does manually). To work, autofs v4 support is required both in the kernel and usermode software. Other than that, it's really simple to set up.

Basic installation

The installation goes to three files in the /etc directory. First, add a line to /etc/auto.master:

/samba /etc/auto.smbhost --timeout=300

Then, create a new file called /etc/auto.smbhost:

* -fstype=autofs,-Dhost=& file:/etc/auto.smbshare

Finally, create a new file called /etc/auto.smbshare:

* -fstype=smb,dmask=777,fmask=666,user,username=,password= ://${host}/&

To make it work, we now restart the autofs daemon. On redhat, mandrake & similar systems, it goes like:

/etc/rc.d/init.d/autofs reload

.. and you are almost ready to go. We'll just create one simple script to ease up listing shares available on a machine. Call it whatever you like and place it somewhere in your PATH. For example ~/bin/. This is how it looks:

#!/bin/sh

# check we are in a dir below the samba dir
pwd | grep -q /samba/ || exit

# determine hostname
host=`pwd | awk -F/ '{print tolower($3)}'`

# which config file should we use
file=/etc/auto.smbshare.$host
[ -f $file ] || file=/etc/auto.smbshare

# get username & check if we have a password
user=`perl -e 'while(<>) { if(/^\*.*username=([^, ]*)/) { print $1; exit }}' < $file`
pass=`awk '/password=[^ ,]/{v=1}END{if(!v)print "-N"}' < $file`

# List the shares for the computer in question:
smbclient -L $host -U $user $pass | awk '/[A-Za-z0-9] *Disk/ { if(!n++ && !length("'$pass'")) print ""; print $1 }' | tr '[A-Z]' '[a-z]'

Now, you are ready to take off. Now imagine you want to go to the \\FOO\MUSIC share. Just do cd /samba/foo/music and you are there. Or, to check out what other shares are available on the FOO machine, you go to /samba/foo and run the script you just created (no parameters required).

Supporting usernames & passwords

If you want to access shares that need specific usernames / passwords, first make a copy of auto.smbshare, named by the host, like auto.smbshare.HOST. Then, add a new line to auto.smbhost, replacing the * with the hostname and auto.smbshare with auto.smbshare.HOST. Finally similarly modify the host's smbshare file with suitable values. You'll get the picture by looking at the examples below. The script above should work as long as you use the same conventions as in the examples below.

Example 1: The shares on the FOO host is only accessible with the username xyz (typical win2k situation). You could then have the following setup:

-- auto.smbhost:

foo -fstype=autofs,-Dhost=& file:/etc/auto.smbshare.foo
* -fstype=autofs,-Dhost=& file:/etc/auto.smbshare

-- auto.smbshare:

* -fstype=smb,dmask=777,fmask=666,user,username=,password= ://${host}/&

-- auto.smbshare.foo:

* -fstype=smb,dmask=777,fmask=666,user,username=xyz,password= ://${host}/&

NOTE: The auto.smb* files are automatically reloaded, so you don't need to restart the autofs for these changes to take effect.

Example 2: In addition to the username in example 1, the share \\FOO\PRIVATE needs the password abcd to be accessible. You could then have the following setup:

-- auto.smbhost:

foo -fstype=autofs,-Dhost=& file:/etc/auto.smbshare.foo
* -fstype=autofs,-Dhost=& file:/etc/auto.smbshare

-- auto.smbshare:

* -fstype=smb,dmask=777,fmask=666,user,username=,password= ://${host}/&

-- auto.smbshare.foo:

private -fstype=smb,dmask=777,fmask=666,user,username=xyz,password=abcd ://${host}/&
* -fstype=smb,dmask=777,fmask=666,user,username=xyz,password= ://${host}/&

Known bugs

The automatic unmounting doesn't seem to work right now. I don't know if it's a problem of the autofs module or of my scripts. If somebody fixes this, please tell me.