1. Why use autofs and not just use NFS?
NFS is great when you have one dedicated server and it's up all the time. If like me you have a few PCs with Linux and you want to share the mounted file systems. The order you boot your machines up can be important and you could even get deadlocks where two machines are waiting for each other. NFS can also appear to hang the booting process if a remote server is not available. If this happens then you have to manually mount the NFS systems, I find that a pain. 
With autofs, the file systems are mounted as required or on demand, and then automatically unmounted when they have not been used for some time. This means you can have two machines both of which mount file systems from the other and the order you boot them does not matter as long as the remote system is not required during the boot. For example your HOME area in on a remote NFS file system. To mount the remote system just access it. Change directory to it, list the contents of it or execute a script or program from it.
Also for removable devices you don't have the bother of mounting and unmounting them. They just work. Insert a cdrom, access it, when you are done wait for the timeout and eject it.
Note: Automounted systems are not checked with fsck (or whatever your filesyem uses) as you boot. This may need to be sorted out else where.
2. Stuff you may need to read up on.
- The mount command, how to mount stuff and its options.
- fstab how to mount file systems auto matically at boot time
- nfs & exports How to setup exports (shares) in NFS
- hosts file IP lookups
- hosts.allow and hosts.deny A bit od security
- Setting up Samba/Cifs
3. Configuring autofs a simple example.
I'm going to do this back to front from everyone else. Do not do anything yet just read, I know it is hard not to jump right in. If you want to understand what you are doing though...
. We are going to start with how to use the files that configure autofs. Followed by stopping and starting the autofs daemon.
Autofs config files are simple text files where blank lines are ignored and lines beginning with a '#' are considered a comment. White space is one or more <SPACE> or <TAB> characters. The usual stuff. You can therefore use your favorite text editor gedit, nano, vi, emacs, dave, kate, scite the list just keeps getting longer.
When you install autofs four files are added to the /etc directory.
$ ls -l /etc/auto*
-rw-r--r-- 1 root root 380 2008-04-09 18:32 auto.master
-rw-r--r-- 1 root root 380 2008-04-09 18:32 auto.misc
-rwxr-xr-x 1 root root 380 2008-04-09 18:32 auto.net
-rwxr-xr-x 1 root root 380 2008-04-09 18:32 auto.smb
We are going to be talking about auto.master and auto.misc to start with. These are called map files, as they are going to map how autofs finds and mounts file systems and devices. The /etc/auto.master map is read in when you run the autofs script. Each line in made up of a key, the path and name to a map file and lastly some options that will be aplied to all entries in the map file. This can also be blank. Each of these three bits of data are seperated by one or more white space characters. Whenever /etc/auto.master is changed the autofs daemon must be restarted to pick up the changes. BUT for the othe map files such as /etc/auto.misc these are read when a filesystem is accessed again so you do not have to restart autofs for these.
Note: Options are cumulative. This is not the same as with the SunOS autofs software.
For a simple example see below:-
- key is /misc
- map file is /etc/auto.misc
- option --timeout=300 is to time out or unmount after 300 seconds (5 minutes) of inactivity.
/misc /etc/auto.misc --timeout=300
So now lets look at this /etc/auto.misc map file and how it's put together. Again this has three bits of data per line. A key, the mount options, and a location. Again each bit of data is seperated by one or more white space characters.
A simple line looks like this:
- key is data
- mount options are -fstype=ext3
- the location being :/dev/sda2
data -fstype=ext3 :/dev/sda2
Take note that the location has a colon ':' in front of it thus indicates that /dev/sda2 is local. If it was on another remote server we would see
data -fstype=ext3 somehost:/sda2
Ok, back to our simple example. Now to put those two entries together and make sense of what is going no here. The autofs script reads the /etc/auto.master file finds our entry. it then reads the entries in the file it finds /etc/auto.misc. To generate the automount it puts them together and mounts
- File system /dev/sda2 from the local file system :
- Which is an ext3 file system
- mounted at /misc/data
The above example can be used to mount a local ext3 file system at /misc/data. The /etc/fstab entry to do the same would be along the lines of
/dev/sda2 /misc/data ext3 defaults 0 2
Note: The directory in our example data will be created and removed automatically. Unless you use the --ghost option but more of that later.
As you can see the /misc comes from /etc/auto.master and the data subdirectory comes from the map file /etc/auto.misc. Pretty simple now that's all explained. But the data directory will be created and removed by autofs.
4. Stopping starting and restarting your autofs daemon
There is the usual stop, start, restart script called /etc/init.d/autofs. There is also a forth option status
sudo /etc/init.d/autofs stop
sudo /etc/init.d/autofs start
sudo /etc/init.d/autofs restart
sudo /etc/init.d/autofs status
The stop start and restart options do "what they say on the tin". The status option is nice and lets you see what the automounter is up to. Once you have something automounted try it and see. You can also see any mounted file systems with the mount command with no options and the df commmand.
5. For Local file systems and devices
For local file systems you only need the autofs package everything else is already installed.
sudo apt-get install autofs
From the simple example above you know how to mount a local ext3 file system. Although, It's probably better for local systems to be mounted via fstab as they will get checked for corruption and duff files periodicially at boot time.
We can mount cdroms, and DVDs locally
. With the way Ubuntu is setup I'm not sure why you you want to do this but hey, you can!!. So here's how.
To keep the autofs tidy and organised we can create a new auto map file called removable and put all our removable devices in that. So to start with we need to update the auto master file and add the line to read the new map file. We will give all mounts a 5 second timeout so it can simply be ejected when we are finished. You may be able to use a smaller timeout. Try it out and see. We will also need to stop the cdrom from being mounted in /etc/fstab.
sudo gedit /etc/fstab
Now find the line that mounts the cdrom and comment it so it can be simply put back again if this gets screwed up. My line now looks like this. Make a note of the first option in my file is /dev/scd0 yours may well be different. Remember this as you are going to need it. Save the edit.
#/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0
Now to add the new map file to auto.master. see above for how this all fits together.
sudo gedit /etc/auto.master
Now add the line and save the changes.
/mnt /etc/auto.removable --timeout=5
I'm going to cheat with the auto.removable file. The line we want to mount cdroms is actually an example in the default map file auto.misc. Remember the first option from fstab you need that now, My cdrom gets mounted at /dev/scd0 yours might be different. So load up /etc/auto.removable in an editor it should get created if it does not exist. If not you can always use touch to create an empty file first.
sudo gedit /etc/auto.removable
Now add the line remembering to change the :/dev/scd0 to whatever the device name is for your cdrom.
cd -fstype=iso9660,ro,sync,nosuid,nodev :/dev/scd0
Save the changes and set the permissions for the new map file. As the auto.master changed we also need to bounce or restart the autofs server.
sudo chmod 644 /etc/auto.removable
sudo /etc/init.d/autofs restart
Now when you put a cdrom in your drive it will be automatically mounted and 5 seconds after you stop using it, autofs will unmount it. You can even see this happening as the directory gets created and destroyed. Start off with a CD in the drive. Do not access it in any way. Wait for longer than the timeout period. the do a listing of the /mnt directory. Nothing there? thats what we expect. Now ls the cdrom itself and then look at /mnt again.Wiat forthe time out and look again.
$ls /mnt
<Nothing returned>
$ls /mnt/cd
autorun.inf dists pics README.diskdefines ubuntu
bin install pool start.bmp ubuntu.ico
casper isolinux preseed start.exe wubi-cdboot.exe
disctree md5sum.txt programs start.ini
$ls /mnt
cd
$ls /mnt
<Nothing returned>
6. CDroms and DVD with a twist.
I have two DVD players on one of my machines, not sure why now but it was probably a good idea at the time. I like to access them as 0 or 1 rather than cdrom0 and cdrom1. With a little bit of tweaking it was done like this.
Add a new entry to /etc/auto.master to read the map file, add the line and save the changes.
/mnt /etc/auto.dvd --timeout=5
We need to create the map file this will setup the cd directory with autofs and call a second mapfile with the configuration of the two dvd palyers. /etc/auto.dvd has just the one line as below.
cd -fstype=autofs file:/etc/auto.dvdsub
Finally to setup the lines that will be used to mount the two dvd players
0 -fstype=iso9660,ro,sync,nosuid,nodev :/dev/hda
1 -fstype=iso9660,ro,sync,nosuid,nodev :/dev/hdb
The usual check that the files have the correct permissions already. As we changed /etc/auto.master we need to bounce the daemon.
sudo chmod 644 /etc/auto.dvd /etc/auto.dvdsub
sudo /etc/init.d/autofs restart
We can now place a cd/dvd into one of the devices and it will be mounted when we use ls or cd /mnt/cd we will see a further subdirectory 0 or 1 depending on the device we used.
7. File systems and devices over NFS
7.1. Installing and Configuring the software
Had I known it was this simple I would have done this years ago. To use autofs over NFS to access a file system on remote host you'll need the NFS and portmap packages.
sudo apt-get install autofs nfs-common portmap
To share NFS drives with other machines you'll need to add nfs-kernel-server as well which gives us an install line like this.
sudo apt-get install autofs nfs-kernel-server nfs-common portmap
Once you have the above packages installed it is time to get them configured. Start with portmap if you didn't get asked during the installation. When configuring portmap do not bind loopback. If you do you can either edit /etc/default/portmap by hand or run the command below. If you were not asked to configure portmap during the installation then run the commands below.
sudo dpkg-reconfigure portmap
sudo /etc/init.d/portmap restart
7.2. Very quick NFS HOWTO: (and I mean quick).
We will assume you have two machines linux32 and linux60. Linux60 has a large /data file system and you want to be able to access it from linux32 as /net/data.
On linux60 install and configure the four packages above. To tell NFS that you want to share /data on linux60 you have to add it to the the /etc/exports file.
sudo gedit /etc/exports
/data 192.168.0.0/255.255.255.240(rw,no_subtree_check,sync)
This line exports the directory /data from linux60 to a limited range of IP addresses with read/write access. See man exports for more details. After making your edits save and restart the NFS daemon.
sudo /etc/init.d/nfs-kernel-server restart
sudo exportfs -ra
Wow, that was quick :-) We now have the directory linux60:/data exported. We now need to add this share to an auto map file and add a line to the /etc/auto.master file and restart the autofs daemon.
7.3. Setting up Autofs with NFS
Now to add the new map file to /etc/auto.master. See above for how this all fits together.
sudo gedit /etc/auto.master
Now add the line and save the changes.
/net /etc/auto.nfs --timeout=300
So load up /etc/auto.nfs in an editor, it should get created if it does not exist. If not you can always use touch to create an empty file first.
sudo gedit /etc/auto.nfs
We want to mount this NFS file system on /net/data. We have already told autofs in the /etc/auto.nfs file that it will be mounted on /net and in the line below we give autofs the last part of the mount data. This is the first parameter on that line data. The second parameter is pretty straight forward it is a mount to a NFS file system so we use the -fstype=nfs. Mind blowing how hard that was
. The thrid parameter is the location of the real file system. It is on linux60 and its mounted as /data which gives us linux60:/data. Remember for local file systems there was nothing in front of the colon ':'.
data -fstype=nfs linux60:/data
Save the changes and set the permissions for the new map file. As the auto.master changed we also need to bounce or restart the autofs server.
sudo chmod 644 /etc/auto.nfs
sudo /etc/init.d/autofs restart
Now you can access the file system on linux60:/data on the remote machine with cd /net/data and it will auto mount as if by magic. Job done! Well almost. 
8. Autofs and Cifs/Samba shares.
I'm assuming you have Samba/Cifs already setup. If not there are loads of HOWTOs out there and this one is getting way too long already. 
Setting up the shares is really really simple. The default install of autofs supplies a script /etc/auto.smb This is really good, although I have modified it slightly to get it to work the way I wanted. I do not want to automatically mount the hidden samba shares. Those that end with a '$'. I also added an extra check for a default credentials file when a specific server file does not exist. Get my updated version here save it as /etc/auto.smb, but backup your original file first. Just to save confusion remove the executable flags from the file too.
cd /etc
sudo mv auto.smb auto.smb.org
sudo chmod 644 /etc/auto.smb.org
sudo gedit auto.smb
<paste the contents and then save the file>
sudo chmod 755 /etc/auto.smb
You can test this file is working simply by running it from the command line. I'm using a VMware machine that is running Windows 2K and is called imaginatively vmwin2ksamba. It has four shares, The entire C: and E: drives along with the downloads and Temp directeries.
$ sudo /etc/auto.smb vmwin2ksamba
-fstype=cifs,credentials=/etc/auto.smb.credentials \
/Downloads ://vmwin2ksamba/Downloads \
/C_DRIVE ://vmwin2ksamba/C_DRIVE \
/E_DRIVE ://vmwin2ksamba/E_DRIVE \
/Temp ://vmwin2ksamba/Temp
The script acutally creates on the fly, the second and thrid parameters for a map file in multimap mode. See the autofs(5) man page for a very brief explaination of multimap mode.
To use /etc/auto.smb add a line to the /etc/auto.master and then restart the autofs daemon and try it out. I also add the nonstrict option to the map line, without it if any one of the connections or shares is wrong or you do not have permission then the whole thing breaks. Also I have a user samba who's gid=1001 and uid=1001. This user cannot run a login shell and is therefore a little more secure. See the Samba docs, they explain it much better than I do.
/winshare /etc/auto.smb --timeout=300,nonstrict,gid=1001,uid=1001
sudo /etc/init.d/autofs restart
You can now look at the dictory winshare/vmwin2ksamba and see all the available shares.
$ ll /winshare/vmwin2ksamba/
total 0
drwxrwxrwx 1 samba samba 0 2008-04-13 08:42 C_DRIVE/
drwxrwxrwx 1 samba samba 0 2007-12-20 10:42 Downloads/
drwxrwxrwx 1 samba samba 0 2008-04-13 08:52 E_DRIVE/
drwxrwxrwx 1 samba samba 0 2008-04-13 12:31 Temp/
You may have to setup a credentials file to get access to all the shares on your network. The credintials file is from the default file /etc/auto.smb.<windows host > or with my modified version you can use a fall back file /etc/auto.smb.credentials. The file format is one key = value pair per line as in the following. I don't use the domain value and it still works ok.
username = <name>
password = <password>
domain = <domain>
9. Advanced methods, automount all NFS shares on your network.
The above is good and it works but means for every file system you want to mount you have to add a line to the map file /etc/auto.net. But what if you could have autofs set up so that whenever you added a file system to an /etc/export to a host you could automatically access the file system remotely without changing anything else! Well you can and as the genius says "Once you understand it, It is really easy"!
Now as an example you have two machines as before linux32 and linux60. From both machines you want to export the local /data. linux60 has /ubuntu_cd_iso directory and linux32 has /music. These directories are already exported as we did before, that part is the same. It is only the autofs part that is different. To make things interesting we add a third machine, which we use to play around with stuff, and it's called linux12. It had very limited disk and therefore has no file systems exported.
See the section File systems and devices over NFS above and install the the following packages and configure them.
sudo apt-get install autofs nfs-common portmap
What we will end up with is an automatic system to allow access to any exported NFS file systems that are on any of the accessable hosts on our network. They will automagically appear in a logically structured directory tree. /net/<host>/<sub-dir>. Remembering our example setup we will be able to simply cd to one of the directories below and it will be there.
/net/linux60/data
/net/linux60/ubuntu_cd_iso
/net/linux32/data
/net/linux32/music
This is almost exciting! Nah, I need to get a life
. Pay attention this gets complex real quick. In the /etc/auto.master file we still access the map file /etc/auto.nfs as before, which means it should contain the following line. This line will make the top level directory of the mount poiunt /net look at the file /etc/auto.nfs for the mappings and add an extra flag to set the timeout to 1 minute.
/net /etc/auto.nfs --timeout=300
The entrire contents of /etc/auto.nfs map file will be replaced with a single line as all exported systems will be automounted. The magic line will be
* -fstype=autofs,-Dhost=& file:/etc/auto.nfssub
Wow, that's a lot of Linux style scary stuff! To explain, autofs has two patterns that can be used in map files, '*' and '&'.
- The '*' is used in the key position and means 'forall keys'.
- in our example it will be replaced with the host names of the servers exporting file systems.
- linux60 and also linux32.
- The '&' is expanded to mean the value in the key field.
This makes our line expand into two lines once for each server.
linux60 -fstype=autofs,-Dhost=linux60 file:/etc/auto.nfssub
linux32 -fstype=autofs,-Dhost=linux32 file:/etc/auto.nfssub
The -Dhost=& is creating a variable with a name of host that can be used in the file /etc/auto.nfssub. Moving on to the last map file the one that actually creates the configuration for the mount points. This map file is called /etc/auto.nfssub as named in the previous map /etc/auto.nfs. This file also only has one line.
* ${host}:/& This looks more cryptic than the last one,
but fear not it's not hard to get your head round after all I did! This line again uses the pattern '*' for all keys which will be the exported directory name this time. The location is then made up with the contents of the variable host, which has the host name in it, followed by :/ and the expaned key which is the directory. giving us the following lines when they are expanded by autofs.
linux60 linux60:/data
linux60 linux60:/ubuntu_cd_iso
linux32 linux32:/data
linux32 linux32:/music
Having amended and/or created the files from above, make sure /net exists and also set the permissions on the map files as before we can now restart autofs.
Save the changes and set the permissions for the new map file. As the auto.master changed we also need to bounce or restart the autofs server. The directories for hosts and the subdirectories where the file systems are mounted will automatically be created and deleted as required.
sudo chmod 644 /etc/auto.nfs /etc/auto.nfssub
sudo /etc/init.d/autofs restart
Now go check it all worked, if you do not see the message "No such file or directory" all worked.
ls /net/linux60/data
ls /net/linux32/data
When you add more exported directories to either linux32 or linux60 they will just be available on linux12 with no further changes to the configuration on linux12. That's got to be good. One bouns is that any filesystems that are exported can also be accessed in the same way on the local machine. For example on linux60 there is an NFS export on /data this can be accessed on linux60 as /data or as /net/linux60/data.
One little annoyance it that if you use a graphical file browser setting up a bookmark to the automounted file systems they seem to dissapear whenthe the automounted file system is unmounted. But not to worry there is a simple solution.
Create a symbolic link that points to the automount directory and make that your bookmarked directory. For the example above we can create a sym link from the users home directory to point at /net/linux60/data
ln -s /net/linux60/data ~/linux60_data
10. To use or not use the --ghost option
The --ghost option tells the automounter to create empty or "ghost" sub-directories of all the mount points listed in /etc/auto.master. The directories always exist regardless of whether the file systems is mounted or not. This can be really useful and very handy as it shows you all of the auto-mountable file systems. Without the --ghost option, you'll have to remember the names of the directories as they are removed when the file system is umounted.
The porblem comes when the friendly system cron job that runs once a day or so, and looks over your local harddisk. It is looking for junk/temp files, and cleaning up other stuff like compressing manual pages and other system stuff. So what is the problem with that? This cron job will often run the whole file tree and when it comes to a directory like "/misc" it will scan all the subdirectories of it as the auto mount directoirs will exist those filesystem will be scanned. Since they possible on a reomte host this is not a good idea. The sub-directory could also be from a local removable drive such as a DVD and there would be little point checking for trash files, nothing could be done with them anyway. DVD are read only!
One way round this problem is the symbolic link, which is just some text, so it would not mount the file system as it is reading the contents of the file as in the cron job. BUT if it is accessed for example with ls or cd the the symbolic link is followed and the magic of the automounter happens. The filesystem gets mounted and you can access it.
This all says, that if you want the functionally that the --ghost option gives you simulate it with symbolic links.
11. Links that were useful to me and some further reading if you feel lucky.
The man pages for
- auto.master(5)
- autofs(5)
- automount(8)
- exports(5)
- fstab(5)
- hosts.allow(5)
- portmap(8)
- mount(8)
- nfs(5)