Autofs

The purpose of this guide is to provide detailed instructions for setting up on-demand automounting of filesystems using Autofs. It can be used to mount network shares, extra system partitions, and removable drives (CD/DVD, USB, floppy, etc) when they are accessed and automatically unmount them when they are no longer needed. This guide assumes that your system is already capable of mounting the desired filesystems manually.

How it Works
Autofs is a special filesystem in which mount points are created and removed as filesystems are mounted and unmounted to them. The autofs daemon performs these operations when it detects a process reading from or writing to the defined directories. After a given period of inactivity, the daemon will automatically unmount the filesystem.

Kernel Configuration
Your kernel must have automounter support compiled in or as a module:

Necessary Software
Install Autofs:

Defining Mount Directories
Modify the Autofs master configuration file. This file lists one or more directories in which the Autofs daemon will create and delete mount points as you access them. These directories (the "mount points") will be controlled by Autofs and it is therefore strongly recommended that you do not use the "mount" command to explicitly mount filesystems there.

Each line in the file gives Autofs a directory in which to place its mounts, and the name of a configuration file containing the file systems to auto-mount there. You can add any number of mount points; this example will only define one.

All config files must have a new line at the end.

The --timeout option in auto.master specifies a period (in seconds) of inactivity after which the filesystem will be automatically unmounted. There are many discussions on the Internet about whether this should be very short (several seconds) or long (a minute or more). That discussion is outside the scope of this document.

The --ghost option creates "ghost" versions (i.e. empty directories) of all mount points defined in the configuration file (in this case /etc/autofs/auto.misc) regardless of whether or not any of the file systems are currently mounted there. Without it, unmounted filesystems will not appear at all and you will have to look in /etc/auto.misc if you forget the name of a mount point.

Defining Mount Points
This is a configuration file with examples for several kinds of filesystems. Mount point names are arbitrary. Mount options and device names can be pulled from fstab (if you've defined the filesystems there).

Russian charset and codepage (example): XPcomputer    -fstype=cifs,iocharset=utf8,codepage=cp866,dmask=0700,fmask=sk=0600      ://150.150.50.111/Tranzit

Credentials files for Samba shares take the following form:

Be sure to secure your credentials file:

Directory and File Permissions
Now we have configured Autofs basic mounting we must restrict permissions.

The automounted filesystems usually have undesired permissions or you might want to restrict permissions for certain users. This can be globally controlled by using the mounting instructions fmask and dmask.

See below for an example limiting the resource to the mounted user only:

NTFS permissions
When mounting an NTFS partition, a few extra steps are necessary to get the permissions right. The kernel NTFS drivers are read-only (ntfs-3g has write support if you need it), so never set the 'write' bit. Additionally, for proper access, the 'execute' bit needs to be cleared for files but set for directories. By default, each partition has a group of 'disk', so members of this group can be given 'read' access. Finally, since Windows is case-insensitive (e.g. C:\Windows = C:\WINDOWS = C:\windows), it doesn't hurt to disable case-sensitivity.

As an example:

Directory Creation and Linking
Now that Autofs is configured to manage /mnt/auto, create this directory.

For accessibility's sake, you can create links to the filesystems' pre-autofs mount points. Be sure to unmount them first if they already are.

Remove the original directories

Create interesting links:

Startup and System Boot
After this is complete, start the Autofs daemon by issuing:

If you wish to have this behavior on default startup, issue:

Problems
One user encountered a problem using Autofs to mount remote NFS shares. The problem was that the server's log reports successful authentication whereas the client told "permission denied by server". The workaround is to put "/etc/init.d/portmap restart" into "/etc/conf.d/local.start". That problem seems to be related to how portmap and NFS interact...

Also, by default, gnome (without gnome-volume-mounter) won't display the icons for floppy and CD etc. Re-installing gnome-volume-mounter as a workaround requires pmount, which fails to install once Autofs is running ( [Errno 13] Permission denied: '/media/.keep#new' ), of course because Autofs is managing /media. As a workaround to this problem, this user stopped Autofs, renamed media to mediaauto, so Autofs can manage its mediaauto directory with the appropriate floppy and cdrom symlinks from /media. This way you can have Autofs (for automounting floppies and cdrom), hal, dbus and gnome-volume-manager happily installed together.

If you experience sluggish transfer speeds between a Linux client and a Samba server, change -fstype from smbfs to cifs. Be sure to have cifs support compiled into [or as modules] your kernel.

-fstype=smbfs -> -fstype=cifs

UUID
Added by Turbomaniac_7 2007-04-10 This for the ones to wish to auto-mount by UUID instead:

First we have to figure out the UUID for the device we wish Autofs to work with. 'tune2fs -l ' can be used but only if the partition is a EXT2/EXT3, therefore the 'blkid' is more universal.

Lets see what we got with the 'blkid' command:

/dev/hda1: UUID="1b50a494-c9e4-4d92-91a6-31b6662b7231" TYPE="ext2" /dev/hda2: UUID="3eb9c4b5-fb87-4728-a5ae-5f06d40493bd" TYPE="swap" /dev/hda3: UUID="0f19ba2a-8506-49a1-b23f-f8676727e54a" SEC_TYPE="ext2" TYPE="ext3" /dev/sde1: LABEL="USB-120GB" UUID="3212-4c62" TYPE="vfat"
 * 1) blkid

Inside '/etc/autofs/auto.auto' instead of: ':/dev/hdaX' we have to write 'UUID="3212-4c62"'. Like this:

That's it.

UDEV
added by Pillarsdotnet 08:24, 4 December 2007 (UTC)

Here is a udev-only solution, that doesn't depend on Autofs, hald, uuid or device name:

Find an existing udev rule that is launched after the usb partition node is created
For me, the rule was in /etc/udev/rules.d/60-persistent-storage.rules, and it looked like this:

Activate the rules
Make the newly created files executable:

chmod 755 /lib/udev/usb_mount /lib/udev/usb_unmount

Reload the rules: udevadm control --reload-rules

UDEV Examples
This is a simple example on how to automount a particular usb hard disk using udev.

1.- Identify the disk:

udevadm info -a -p $(udevadm info -q path -n /dev/sdb1)

2.- Write the rule in /etc/udev/rules.d/10-local.rules creating a symlink to the device and running mount:

KERNEL=="sd?1", SUBSYSTEMS=="scsi", ATTRS{model}=="HD400LD", GROUP="users", SYMLINK+="PeekBox", RUN+="/bin/mount /dev/PeekBox"

3.- Define the drive in /etc/fstab

/dev/PeekBox   /mnt/PeekBox    vfat    noauto,users,gid=100,umask=007  0 0

When you plug the usb hard disk it gets automounted:

$ mount /dev/sdb1 on /mnt/PeekBox type vfat (rw,noexec,nosuid,nodev)

UDEV with Autofs
added by docbill, 29 Feb 2008 - modified 11 Sep 2008 by Anaphyis

If you want to want to combine the advantages of UDEV (that is mounting a medium when added and removed) and Autofs (unmounting when not in use) you can use this approach.

Start everything off
You should now see your removable devices not mentioned in fstab automatically added to /media - unless of course you used the line to restrict Autofs to USB devices only.