http://www.greenfly.org/tips/autofs.htmlHow to set up autofsWhen I was trying to figure out the best way to set up my removable usb drive, I ended up stumbling upon autofs as an excellent solution. After realizing the potential of it, and how configurable it is, I started setting it up to manage other mountpoints on my system. Here, I will discuss everything I set up, and how, including dynamically mounted smb, ftp, and ssh mountpoints. What is it? First of all, what is autofs? Basically, it is a kernel option that lets you automatically mount filesystems to your computer when you access them, and then automatically umount them when they are no longer used. For some basics, you can read the Quick autofs Tutorial or for some more specific setup information you can also check the Autofs HOWTO. Okay, I know what it is. So what? To illustrate some of the simple niceties of autofs, here is an example of reading the contents of a CDROM, and ejecting it, pre and post-autofs. Before autofs: *insert CD into CDROM mount /cdrom ls /cdrom umount /cdrom *remove CD from CDROM With autofs: *insert CD into CDROM ls /cdrom *remove CD from CDROM Upon accessing the /cdrom directory, autofs automatically mounted it for me, and when I pressed the eject button (or waited for the timeout) it automatically umounted it. This is a fairly straightforward example, but it presents a lot of possibilities. For instance, you can set up SMB and NFS mounts that only mount as you access them, then umount later. Or, as in my usb drive example, mount the usb drive only when you access it, then quickly umount it when you aren't using it, so it will be safe to remove. Okay okay, fine. How did you set it up? First, install autofs. The autofs howto I linked to earlier goes
over this, for me I just needed to make sure autofs support was in my
kernel, and then I ran Removable drives Okay, so first I added a line to my /etc/auto.master file. This file keeps track of where the autofs filesystems will be eventually mounted, along with pointing to the specific configuration file (or script, I'll get to that later) for it to read from, and then a column for any other options you would like to add. Here's the line I added for my removable drives: /var/autofs/removable /etc/auto.removable --timeout=2 This line tells autofs to create a new directory under /var/autofs/removable, to then read from /etc/auto.removable to find out about any mountpoints that might exist under there, and to timeout (and therefore umount the filesystem) after 2 seconds of idling. The 2-second idling isn't as important for CDROMs as it is for usb drives, so if you want your CDROM to have a longer timeout, just create a new configuration file for it, and a new line in auto.master. You may also add extra options to be passed to the mount command in this column, options which will be applied to any mountpoint listed under this directory. With /etc/auto.master edited, I then created a new autofs configuration file, called auto.removable, and put it in /etc. The file looks like the following: cdrom -fstype=iso9660,ro,sync,nodev,nosuid :/dev/cdrom floppy -fstype=auto,sync,nodev,nosuid :/dev/fd0 usbdrive -fstype=vfat,uid=1002,gid=1002,umask=002 :/dev/sda1 The first column is the name of the directory mountpoint I want to use, so these lines would automount their devices when /var/autofs/removable/cdrom, /var/autofs/removable/floppy, and /var/autofs/removable/usbdrive were accessed, respectively. The second column sets up any mounting options you want to use for these individual filesystems, separated by commas. The third column is to list what device you wish to mount. IMPORTANT: if you are not listing an NFS mount, you need to prepend a colon to the beginning of the device, so /dev/cdrom becomes :/dev/cdrom. Once you create this file, simply restart autofs with With these settings in place, I put the Fedora Core 1 cd in my cdrom, and typed ls: [EMAIL PROTECTED]:~$ ls /var/autofs/removable/ [EMAIL PROTECTED]:~$ ls /var/autofs/removable/cdrom Fedora RPM-GPG-KEY TRANS.TBL GPL RPM-GPG-KEY-beta autorun README RPM-GPG-KEY-fedora dosutils README-Accessibility RPM-GPG-KEY-fedora-rawhide eula.txt RELEASE-NOTES RPM-GPG-KEY-fedora-test images RELEASE-NOTES.html RPM-GPG-KEY-rawhide isolinux [EMAIL PROTECTED]:~$ ls /var/autofs/removable/ cdrom [EMAIL PROTECTED]:~$ ls /var/autofs/removable/ Notice that the cdrom directory didn't exist in /var/autofs/removable until I actually tried to access it. Then it only existed until it timed out, which, with my configuration, is 2 seconds, and then it umounted. You can also check /var/log/syslog to watch autofs work:
I have highlighted a few lines of interest. You can see autofs detect the access of /var/autofs/removable/cdrom, then watch it mount the filesystem, and then, seconds later, watch it time out and expire the mount point. Beyond local filesystems Autofs can manage basically any filesystem that you could normally mount by hand. This means you can use it to manage your NFS and ssh mounts. To do this, I added another line to my /etc/auto.master for smb connections: /var/autofs/smb /etc/auto.smb --timeout=60 And then created the /etc/auto.smb file: backup -fstype=smbfs,username=joey,password=god,uid=1000,gid=1000 ://gibson/backup html -fstype=smbfs,username=joey,password=god,uid=1000,gid=1000 ://gibson/html mp3 -fstype=smbfs,guest,uid=1000,gid=1000 ://gibson/mp3 This file sets up three mountpoints, /var/autofs/smb/backup, /var/autofs/smb/html, and /var/autofs/smb/mp3, respectively. Notice that I specify the mountpoints with a colon first, and then the normal //servername/share syntax used for smb. With this set up, I then restart autofs, and: [EMAIL PROTECTED]:~$ ls /var/autofs/smb/ [EMAIL PROTECTED]:~$ ls /var/autofs/smb/backup 010901 rawtext 0211ay.jpg 1.40.2.zip 1.42.3.zip 2003 Web Analysis.xls ... url_200210.txt url_200210.xls url_200211.txt various_artists [EMAIL PROTECTED]:~$ ls /var/autofs/smb/ backup [EMAIL PROTECTED]:~$ ls /var/autofs/smb/ Again the mountpoint isn't created until I try to access it, then it mounts the smb share, and finally times out a minute later:
It works pretty much the same way that mounting the cdrom worked, only with different mount options. If you just want basic autofs mounting to work with your removable devices and network shares, this is really all you need to do. Now, if you want to push the envelope... Getting fancy Writing your own autofs scripts Upon reading up on the autofs options, I noticed that you can specify multiple nested mountpoints under a server. The example in the autofs[5] manpage is: server -rw,hard,intr / -ro myserver.me.org:/ \ /usr myserver.me.org:/usr \ /home myserver.me.org:/home The above would not only create server directory, but also nest usr and home directories (each potentially with their own options) under that mount point, simply by accessing the server directory. I used that fact, and the knowledge that you can specify scripts instead of configuration files in /etc/auto.master, to write my own dynamic script to allow me to browse smb shares dynamically. All a script you write has to do, is generate the second and third columns of a configuration file. The script will be passed the first column as an argument. I realized I could write a perl script that would receive the name of an smb server, and then scan that server for guest mountpoints, and output the proper autofs configuration. After some tweaking, I came up with this autofs script. (Note, this script uses the Filesys::SmbClient perl module, check the docs in the script itself for instructions on how to install that if you don't have it already.) You can test out the script by running it by itself, passing the name of an smb server on your network. For instance: [EMAIL PROTECTED]:~$ /etc/auto.smbbrowse gibson -fstype=smbfs,guest /mp3 //gibson/mp3 /print$ //gibson/print$ The script discovered two guest shares on that machine, mp3 and print$, and set up a multi-map entry for them. This would be exactly the same as having a text config file with the following line: gibson -fstype=smbfs,guest /mp3 //gibson/mp3 /print$ //gibson/print$ With this script written, I simply edit my /etc/auto.master file and add an entry for smb browsing: /var/autofs/smbbrowse /etc/auto.smbbrowse --timeout=60,-nonstrict I put the script in /etc/ and made sure it was executable. Now, whenever I browse to /var/autofs/smbbrowse, instead of reading a config file, it will run /etc/auto.smbbrowse and take its output for the configuration. For this script to work, however, it is very important to include the -nonstrict option. This tells the script to not fail if it can't seem to mount a share (ie, it is password-protected). Otherwise, if you run it and even a single share is password protected, it will error out and not try to mount any of them. With this line added, I restart autofs, and then try it out: [EMAIL PROTECTED]:~$ ls /var/autofs/smbbrowse [EMAIL PROTECTED]:~$ ls /var/autofs/smbbrowse/gibson mp3 [EMAIL PROTECTED]:~$ ls /var/autofs/smbbrowse/gibson/mp3 311 nofx Reef Madness! offspring Surf Rider operation_ivy anti-flag pennywise ... nin various_artists [EMAIL PROTECTED]:~$ ls /var/autofs/smbbrowse/ Note that I didn't actually have a configuration file in place for the gibson server, the script scanned the network and asked gibson what shares it had available on the fly, then the script passed any guest-mountable ones to autofs, which then decided to mount the mp3 share. In case you are curious, here's the syslog output:
You can notice that the logs are a bit different this time. It notices it gets a multi-map as input, and tries to mount each of the mountpoints it is given, only the mp3 mountpoint actually worked, but since we specified -nonstrict, it didn't completely error out. Now, if I want to access a guest share anywhere on my network, I simply need to browse to /var/autofs/smbbrowse/servername and see what is available. Note that this script is only configured to scan and try to configure mountpoints as guest. Dynamic ftp and ssh mounts with autofs and lufs One of the things that actually inspired me to write the smb browsing script, is the similar scripting that the lufs project has included for autofs. LUFS lets you mount remote ftp and ssh servers as though they were NFS or SMB fileservers on your filesystem. With their autofs tools you can simply browse around your filesystem with stragetically-named folders and browse ftp.kernel.org, or even your own ssh or ftp servers. The setup Okay, so the first thing to do, is to install and setup lufs on your system. This will vary per distribution, but for me it was a matter of running apt-get install lufs-source lufs-utils cd /usr/src/ tar xfvj lufs.tar.bz2 cd /usr/src/linux make-kpkg modules_image dpkg -i ../lufs-module*deb This builds the lufs kernel modules I needed and installed them with the rest of the modules for my kernel. If you don't use make-kpkg to manage your modules and kernel in debian... what's wrong with you? Just kidding, if you aren't using make-kpkg, then you will need to download the lufs source from their site and build it against your kernel according to their INSTALL docs. At this point, you will be able to use lufsmount and lufsumount to mount and umount remote ftp and ssh filesystems. Read the manpages for those repesctive commands for more information on how to run them manually. Configuring lufs with autofs Mounting things manually? That's not why we are using autofs! So, how do we get autofs and lufs to work together? It's actually rather easy, and just requires two extra entries in your /etc/auto.master file: /var/autofs/ssh /usr/bin/auto.sshfs uid=1000,gid=1000,--timeout=30 /var/autofs/ftp /usr/bin/auto.ftpfs uid=1000,gid=1000,--timeout=30 Notice I added some extra options to pass along to mount, so that any remote filesystem I mount would have the UID of my current user (if you had multiple users you wanted to have access you could always create a group for them all and then just use the gid= option.) At this point I simply restart autofs and browse to /var/autofs/ftp: [EMAIL PROTECTED]:~$ ls /var/autofs/ftp [EMAIL PROTECTED]:~$ ls /var/autofs/ftp/ftp.kernel.org/pub/linux/kernel/v2.6/ ChangeLog-2.6.0 linux-2.6.1.tar.bz2 patch-2.6.0.sign ChangeLog-2.6.1 linux-2.6.1.tar.bz2.sign patch-2.6.1.bz2 LATEST-IS-2.6.1 linux-2.6.1.tar.gz patch-2.6.1.bz2.sign README-2.6 linux-2.6.1.tar.gz.sign patch-2.6.1.gz linux-2.6.0.tar.bz2 linux-2.6.1.tar.sign patch-2.6.1.gz.sign linux-2.6.0.tar.bz2.sign patch-2.6.0.bz2 patch-2.6.1.sign linux-2.6.0.tar.gz patch-2.6.0.bz2.sign pre-releases linux-2.6.0.tar.gz.sign patch-2.6.0.gz snapshots linux-2.6.0.tar.sign patch-2.6.0.gz.sign testing [EMAIL PROTECTED]:~$ ls /var/autofs/ftp ftp.kernel.org [EMAIL PROTECTED]:~$ ls /var/autofs/ftp Once again, notice that the directory doesn't exist until you actually access it, then autofs creates it. You can treat this like any other mounted network filesystem and copy files to your local system. With the ftp and ssh modules, you can also specify user:[EMAIL PROTECTED] under the /var/autofs/ftp/ directory if you don't want to log in as anonymous. SSH mounting works basically the same was as ftp, except that
everything works best if already have set up passwordless ssh logins
with rsa keys for your root user. I just copied over my public root rsa
key to any remote ssh server I wanted to connect to, confirmed that I
could ssh to the server without requiring a password, and then I could
simply browse to One caveat, the LUFS project is still in the experimental stage, so don't be surprised if everything doesn't work exactly as stably as other network filesystems. I have noticed some caching issues when running vim on a file for minutes at a time, with file writes not always going through perfectly. For filesystem copying though, it should work fairly well. Conclusion So, at this point I have been using autofs for a bit, and have done
a few things to make it easier to manage. For one, the You can use this to create many different symlinks for different
ssh or smb accounts. I even ended up deleting my /cdrom directory, and
replacing it with a symlink to |
