Autofs

From Devipedia

Jump to: navigation, search

These days gnome will automagically mount most removable drives when you plug them in. But what about servers that run without a UI?

I recently needed to add the ability to auto-mount a few esata drives. My udev solution worked just fine until we added a second identical drive. I needed a solution that would uniquely identify each of the drives and the naming convention needed to be consistent. I could not just depend on the drive names (/dev/sda, /dev/sdb) because the assignment of those names depends on the order the drives are plugged in. The drive for log file backups might show as /dev/sdd one day and /dev/sde the next.

I discovered that, since Linux 2.6, unique device names are automatically assigned under /dev/disk. (How did I miss this?) In fact, multiple names are given.

[root@server ~]# ls /dev/disk/
by-id  by-label  by-path  by-uuid

This is good stuff. You can find your device by a generated persistent ID, a UUID, a path or a label. I opted to use the by-id naming convention.

[root@server tmp]# ls /dev/disk/by-id/
scsi-1AMCC_3QJ03TMQD8CDE3009A0C
scsi-1AMCC_3QJ03TMQD8CDE3009A0C-part1
scsi-1AMCC_3QJ03TMQD8CDE3009A0C-part2
scsi-SATA_Hitachi_HDT7210_STF603MS0G4LRK
scsi-SATA_Hitachi_HDT7210_STF603MS0G4LRK-part1
scsi-SATA_Hitachi_HDT7210_STF607MS07U6RK
scsi-SATA_Hitachi_HDT7210_STF607MS07U6RK-part1

The SATA Hitachi drives are the removable drives I'm interested in.

Part of my udev script was discovering when a device is plugged in by examining a set of attributes. Now my job is easier because I can now simply check for the existence of my drive in /dev/disk/by-id. The remainder of my udev script simply mounted the drive. autofs seems to be a better approach at this point than using udev.

autofs is a daemon and may already be installed on your distribution. If not, go ahead and install it.

There are a few main files of concern, and you will likely only modify one of them. On Centos:

[root@server ~]# ls /etc/auto*
/etc/autofs_ldap_auth.conf  /etc/auto.misc  /etc/auto.smb
/etc/auto.master            /etc/auto.net


auto.master contains a template that includes the other auto.* files.

# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#
misc	/etc/auto.misc --ghost


For my case I added the following to /etc/auto.misc:

removable1                 -fstype=auto    :/dev/disk/by-id/scsi-SATA_Hitachi_HDT7210_STF603MS0G4LRK-part1
removable2                 -fstype=auto    :/dev/disk/by-id/scsi-SATA_Hitachi_HDT7210_STF607MS07U6RK-part1

Upon restart of the autofs service, my devices were auto-mounted.

The auto.master indicates that the devices listed in the auto.misc should be mounted under /etc/auto.misc. The --ghost option will cause the mount point to appear regardless of whether anything was mounted, and this may be useful while testing. Each entry in auto.misc includes mount name, mounting options, and unique device name following a colon.

Personal tools