Neat Trick, aka autofs

One of the things I learned about in my RHCE training was the autofs capabilities in Linux. I'd never used them before, and found they're pretty neat.

Consider the following:

-{ conrad@conrad-laptop }---{ 07:30 PM }-
-[ ~ ]-> df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda3             32993140   3939496  27377664  13% /
varrun                 1037908       128   1037780   1% /var/run
varlock                1037908         0   1037908   0% /var/lock
udev                   1037908        84   1037824   1% /dev
devshm                 1037908         0   1037908   0% /dev/shm
lrm                    1037908     16488   1021420   2% /lib/modules/2.6.22-14-generic/volatile
/dev/sda2             60725696  33124744  27600952  55% /media/sda2
-{ conrad@conrad-laptop }---{ 07:38 PM }-
-[ ~ ]-> cd /cifs/imp/data
-{ conrad@conrad-laptop }---{ 07:39 PM }-
-[ /cifs/imp/data ]-> ls
Ableton/                     Mov/  poser/         System Volume Information/
aecache/                     Movies/        RECYCLER/      Thumbs.db
dlmovies/                    msdownld.tmp/  render/        u/
fraps/                       Music/         renderoutput/  w/
blah/                        pagefile.sys
-{ conrad@conrad-laptop }---{ 07:39 PM }-
-[ /cifs/imp/data ]-> df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda3             32993140   3939504  27377656  13% /
varrun                 1037908       128   1037780   1% /var/run
varlock                1037908         0   1037908   0% /var/lock
udev                   1037908        84   1037824   1% /dev
devshm                 1037908         0   1037908   0% /dev/shm
lrm                    1037908     16488   1021420   2% /lib/modules/2.6.22-14-generic/volatile
/dev/sda2             60725696  33124744  27600952  55% /media/sda2
//imp/data           312568640 286684512  25884128  92% /cifs/imp/data
//imp/SharedDocs     146512768 122200528  24312240  84% /cifs/imp/SharedDocs
-{ conrad@conrad-laptop }---{ 07:39 PM }-
-[ /cifs/imp/data ]-> 

To do this, you need to use autofs. Install your distributions autofs packages.

Create the file /etc/auto.cifs with the following contents and edit the file for your environment.

#!/bin/bash
#
# Stolen from http://www.howtoforge.com/accessing_windows_or_samba_shares_using_autofs, and fixed up
# a bit.
# This file must be executable to work! chmod 755!
key="$1"
#
# To mount server named 'vader' with a username of 'bob' and a password of '4b0b', 
# create a file called /etc/auto.cifs.vader containing
# username=bob
# password=40b
#
# This file should have permissions of 600, by running 'chmod 600 /etc/auto.cifs.vader'
#
# SMB mounts tend to work better if you map them to a real user, change the uid and gid
# here.
MOUNT_UID=1001
MOUNT_GID=1001
#
credfile="/etc/auto.smb.$key"
# Note: Use cifs instead of smbfs:
mountopts="-fstype=cifs,file_mode=0644,dir_mode=0755,uid=${MOUNT_UID},gid=${MOUNT_GID}"
smbclientopts=""
for P in /bin /sbin /usr/bin /usr/sbin
do
        if [ -x $P/smbclient ]
        then
                SMBCLIENT=$P/smbclient
                break
        fi
done
[ -x $SMBCLIENT ] || exit 1
if [ -e "$credfile" ]
then
        mountopts=$mountopts",credentials=$credfile"
        smbclientopts="-A "$credfile
else
        smbclientopts="-N"
fi
$SMBCLIENT $smbclientopts -gL $key 2>/dev/null \
   | awk -v key="$key" -v opts="$mountopts" -F'|' -- '
        BEGIN   { ORS=""; first=1 }
        /Disk/  { if (first) { print opts; first=0 }; 
	          sub(/ /, "\\ ", $2); 
		  print " \\\n\t /" $2, "://" key "/" $2 }
        END     { if (!first) print "\n"; else exit 1 }
        '

You'll probably want to create a /etc/auto.cifs.SERVERNAME file as indicated in the file above to store the share authentication information.

Edit /etc/auto.master, and add the following line:

/cifs	/etc/auto.cifs --timeout=60

Start autofs -

/etc/init.d/autofs start

And, presto, it should work. Unfortunately autofs is still a bit of voo-doo to me, so I'm not sure what to check for troubleshooting. I monitored /var/log/daemon.log, and was able to look for errors there.