23 August 2012

Configuring AutoFS in Linux

This post details simple usage of AutoFS under Linux.  The purpose of
AutoFS is to mount volumes as they are needed and unmount them when
they are not in use.  This is useful for sharing out home directories,
documentation, or applications to clients from a NAS cluster as well
as presenting standardized directory structures wherein the directory
structure of origin may not be standard.  Both of which will be covered.
Our details for this are:
        HOST (client):          europa (10.0.7.90)
        HOST (NFS server):      humboldt (10.0.7.125)
        OS:                     CentOS 6.2 Linux (RedHat EL derivative)

To start things off, I have a group "sci" and 2 users, "rfeynman" and
"pcherenkov".  On this host, actual home directories are setup under
"/export/home", though our users' home directories are configured for
"/home":
        europa [0] /bin/grep sci /etc/group
        sci:x:500:
        europa [0] /usr/bin/tail -2 /etc/passwd
        rfeynman:x:500:500:R. Feynman:/home/rfeynman:/bin/bash
        pcherenkov:x:501:500:P. Cherenkov:/home/pcherenkov:/bin/bash
        europa [0] /bin/ls -ld /home /export/home
        drwxr-xr-x. 5 root root 4096 Aug 23 14:47 /export/home/
        drwxr-xr-x. 2 root root 4096 Aug 23 14:45 /home/
        europa [0] /bin/ls -ld /home/* /export/home/*
        /bin/ls: cannot access /home/*: No such file or directory
        drwx------. 2 root       root 16384 Aug 23 14:18 /export/home/lost+found/
        drwx------. 2 pcherenkov sci   4096 Aug 23 14:47 /export/home/pcherenkov/
        drwx------. 2 rfeynman   sci   4096 Aug 23 14:47 /export/home/rfeynman/

As seen above, nothing exists under "/home", only "/export/home".
We could move the directories for our users or we could simply use AutoFS
to handle it for us.  The default configuration options for AutoFS are
stored in "/etc/sysconfig/autofs".  Initially, there will only be a few
entries defined and they are fairly self-explanatory:
        europa [0] /bin/grep -v ^# /etc/sysconfig/autofs
        TIMEOUT=300
        BROWSE_MODE="no"
        MOUNT_NFS_DEFAULT_PROTOCOL=4
        USE_MISC_DEVICE="yes"

The "TIMEOUT" option above defines a value of 300 seconds, that being
how long the automounter will at least wait until unmounting an unused
mount under its control.  Additionally, setting "BROWSE_MODE" to "no"
will prevent directory listings of potential AutoFS managed directory
structures until those directories have been mounted.  Since AutoFS can
be manage by means other than local files (such as NIS maps), there is
a setting for it in "/etc/nsswitch.conf".  For our purposes, we'll be
using local files for AutoFS configuration:
        europa [0] /bin/grep automount /etc/nsswitch.conf
        automount:  files

Getting back to our users, below, we add new local maps for "auto.home"
and "auto.info" to "/etc/auto.master":
        europa [0] echo "/home  /etc/auto.home" >> /etc/auto.master
        europa [0] echo "/opt  /etc/auto.info" >> /etc/auto.master
        europa [0] /bin/grep -v ^# /etc/auto.master
        /misc   /etc/auto.misc
        /net    -hosts
        +auto.master
        /home   /etc/auto.home
        /opt    /etc/auto.info

In the above, we have our additions to the default entries.  The format of
"auto.master" is (see man page: auto.master(5)):
        mount_point     map     [options]

Next, we configure the "auto.home" map:
        europa [0] echo "*      -bind   :/export/home/&" >> /etc/auto.home
        europa [0] /bin/cat /etc/auto.home
        *       -bind   :/export/home/&

Taken together with the "auto.home" entry in "/etc/auto.master", our
"auto.home" map entry says: for any user directory under "/export/home",
mount it under "/home" using mount option "bind" (see loopback mounts).
Since "autofs" is currently disabled, we need to enable and start it:
        europa [0] /sbin/chkconfig --list autofs
        autofs          0:off   1:off   2:off   3:off    4:on    5:on    6:off
        europa [0] /sbin/chkconfig --level 3 autofs on
        europa [0] /sbin/service autofs start
        Starting automount:                                        [  OK  ]

The cool part about AutoFS is that it will only mount a volume as it
is needed.  Since no AutoFS mounts are currently needed, "/home" is
still empty:
        europa [0] /bin/ls -laF /home /export/home
        /export/home:
        total 32
        drwxr-xr-x. 5 root       root  4096 Aug 23 14:47 ./
        drwxr-xr-x. 3 root       root  4096 Aug 23 14:22 ../
        drwx------. 2 root       root 16384 Aug 23 14:18 lost+found/
        drwx------. 2 pcherenkov sci   4096 Aug 23 14:47 pcherenkov/
        drwx------. 2 rfeynman   sci   4096 Aug 23 14:47 rfeynman/

        /home:
        total 4
        drwxr-xr-x.  2 root root    0 Aug 23 15:31 ./
        dr-xr-xr-x. 26 root root 4096 Aug 23 15:31 ../

Since our users are configured to have their home directories under
"/home", by switching user to "rfeynman", "autofs" sees that the directory
for "/home/rfeynman" is needed and mounts it per the options we specified
in "auto.home":
        europa [0] /bin/su - rfeynman
        [rfeynman@europa ~]$ pwd
        /home/rfeynman
        [rfeynman@europa ~]$ /bin/df -ah
        Filesystem            Size  Used Avail Use% Mounted on
        /dev/sda1             6.0G  1.6G  4.2G  27% /
        proc                     0     0     0   -  /proc
        sysfs                    0     0     0   -  /sys
        devpts                   0     0     0   -  /dev/pts
        tmpfs                 467M     0  467M   0% /dev/shm
        /dev/sda3            1008M   98M  860M  11% /var
        none                     0     0     0   -  /proc/sys/fs/binfmt_misc
        /dev/sdb1            1020M   34M  935M   4% /export/home
        /export/home/rfeynman
                             1020M   34M  935M   4% /home/rfeynman
        [rfeynman@europa ~]$ /bin/ls -laF
        total 20
        drwx------. 2 rfeynman sci  4096 Aug 23 14:47 ./
        drwxr-xr-x. 3 root     root    0 Aug 23 15:33 ../
        -rw-r--r--. 1 rfeynman sci    18 Dec  2  2011 .bash_logout
        -rw-r--r--. 1 rfeynman sci   176 Dec  2  2011 .bash_profile
        -rw-r--r--. 1 rfeynman sci   124 Dec  2  2011 .bashrc
        -rw-r--r--. 1 rfeynman sci   121 Dec  7  2011 .kshrc
        [rfeynman@europa ~]$ /bin/ls -laF /home
        total 8
        drwxr-xr-x.  3 root     root    0 Aug 23 15:33 ./
        dr-xr-xr-x. 26 root     root 4096 Aug 23 15:31 ../
        drwx------.  2 rfeynman sci  4096 Aug 23 14:47 rfeynman/
        [rfeynman@europa ~]$ /bin/touch trashfile
        [rfeynman@europa ~]$ /bin/ls -li /home/rfeynman/trashfile /export/home/rfeynman/trashfile
        21 -rw-r--r--. 1 rfeynman sci 0 Aug 23 15:35 /export/home/rfeynman/trashfile
        21 -rw-r--r--. 1 rfeynman sci 0 Aug 23 15:35 /home/rfeynman/trashfile
        [rfeynman@europa ~]$ exit

In the output above, we see that "rfeynman" has full access to his
files and that his is the only directory currently under "/home".
Once we switch user to (or log in as) "pcherenkov", his directory is
also automatically mounted by the AutoFS 'automount' daemon.
        europa [0] /bin/su - pcherenkov
        [pcherenkov@europa ~]$ /bin/df -ah
        Filesystem            Size  Used Avail Use% Mounted on
        /dev/sda1             6.0G  1.6G  4.2G  27% /
        proc                     0     0     0   -  /proc
        sysfs                    0     0     0   -  /sys
        devpts                   0     0     0   -  /dev/pts
        tmpfs                 467M     0  467M   0% /dev/shm
        /dev/sda3            1008M   98M  860M  11% /var
        none                     0     0     0   -  /proc/sys/fs/binfmt_misc
        /dev/sdb1            1020M   34M  935M   4% /export/home
        /export/home/rfeynman
                             1020M   34M  935M   4% /home/rfeynman
        /export/home/pcherenkov
                             1020M   34M  935M   4% /home/pcherenkov
        [pcherenkov@europa ~]$ exit
        logout

While the volumes are mounted by "autofs", they are visible in
"/etc/mtab" and "/proc/mounts":
        europa [0] /bin/grep home /etc/mtab
        /dev/sdb1 /export/home ext4 rw 0 0
        /export/home/rfeynman /home/rfeynman none rw,bind 0 0
        /export/home/pcherenkov /home/pcherenkov none rw,bind 0 0
        europa [0] /bin/grep home /proc/mounts
        /dev/sdb1 /export/home ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0
        /etc/auto.home /home autofs rw,relatime,fd=19,pgrp=3136,timeout=300,minproto=5,maxproto=5,\
                indirect 0 0
        /dev/sdb1 /home/rfeynman ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0
        /dev/sdb1 /home/pcherenkov ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0

Remembering our default TIMEOUT option, even though "pcherenkov" has
logged out, the mounted volume (or filesystem in this case) will still
exist for a short while after it is no longer used.  Below, I've switched
user back to "pcherenkov" to illustrate this.  It takes about 330+ seconds
after logout for 'automount' to remove the "pcherenkov" home directory:
        europa [0] /bin/grep ^TIMEOUT /etc/sysconfig/autofs
        TIMEOUT=300
        europa [0] /bin/date ; /bin/ls /home ; /bin/su - pcherenkov -c "/usr/bin/whoami ; exit" ; \
        > sleep 301 ; /bin/date ; /bin/ls /home ; \
        > while : ; do /bin/date ; /bin/ls /home ; echo ; sleep 10 ; done
        Thu Aug 23 16:35:53 EDT 2012
        rfeynman/
        pcherenkov
        Thu Aug 23 16:40:54 EDT 2012
        pcherenkov/  rfeynman/
        Thu Aug 23 16:40:54 EDT 2012
        pcherenkov/  rfeynman/

        Thu Aug 23 16:41:04 EDT 2012
        pcherenkov/  rfeynman/

        Thu Aug 23 16:41:14 EDT 2012
        pcherenkov/  rfeynman/

        Thu Aug 23 16:41:24 EDT 2012
        rfeynman/
        ^C

One of the foremost reasons for using AutoFS is for dealing with NAS
volumes, such as NFS mounts.  Having previously enabled rpcbind on europa
and set up an NFS server on humboldt (see nfs.linux), we can configure
our AutoFS map for "auto.info"
        europa [0] /usr/bin/getent hosts humboldt-int
        10.0.7.125      humboldt-int
        europa [0] /bin/grep info /etc/auto.master
        /opt    /etc/auto.info
        europa [0] /bin/cat /etc/auto.info
        lhc-docs        -ro,bg,intr     humboldt-int:/export/lhc-docs
        man             -ro,bg,intr     humboldt-int:/export/man
        europa [0] /etc/init.d/autofs reload
        Reloading maps
        europa [0]

In the above "auto.info" configuration, both "lhc-docs" and "man" will
be automatically mounted by the AutoFS 'automount' daemon under "/opt"
as they are needed.  The second column is simply standard NFS mount
options with the third column detailing the shared filesystem from the
NFS server.  Switching back to user "rfeynman", we can see that nothing
initially exists under "/opt" until we need to access an AutoFS managed
directory structure:
        [rfeynman@europa ~]$ pwd
        /home/rfeynman
        [rfeynman@europa ~]$ /bin/ls -al /opt
        total 4
        drwxr-xr-x.  2 root root    0 Aug 23 17:31 .
        dr-xr-xr-x. 26 root root 4096 Aug 23 17:31 ..
        [rfeynman@europa ~]$ /bin/ls -al /opt/lhc-docs
        total 344
        drwxr-xr-x. 2 4294967294 4294967294   4096 Aug 23 16:32 .
        drwxr-xr-x. 3 root       root            0 Aug 23 17:40 ..
        -rw-r--r--. 1 4294967294 4294967294   1491 Aug 23 16:32 section.1
        -rw-r--r--. 1 4294967294 4294967294 109208 Aug 23 16:29 section.2
        -rw-r--r--. 1 4294967294 4294967294  45224 Aug 23 16:29 section.3
        -rw-r--r--. 1 4294967294 4294967294 106232 Aug 23 16:30 section.4
        -rw-r--r--. 1 4294967294 4294967294  76056 Aug 23 16:30 section.5
        [rfeynman@europa ~]$ /usr/bin/head -10 /opt/lhc-docs/section.1
        [http://public.web.cern.ch/public/en/lhc/lhc-en.html]

        The Large Hadron Collider
        Our understanding of the Universe is about to change...

        The Large Hadron Collider (LHC) is a gigantic scientific instrument near
        Geneva, where it spans the border between Switzerland and France about
        100m underground. It is a particle accelerator used by physicists to
        study the smallest known particles – the fundamental building blocks of
        all things. It will revolutionise our understanding, from the minuscule

Our man directory, shared from "humboldt", is also available under /opt:
        [rfeynman@europa ~]$ /usr/bin/gunzip -c /opt/man/man1/xxd.1.gz | \
        > /usr/bin/groff -Tascii -man - |  /usr/bin/head -20
        XXD(1)                                                                  XXD(1)

        NAME
               xxd - make a hexdump or do the reverse.

        SYNOPSIS
               xxd -h[elp]
               xxd [options] [infile [outfile]]
               xxd -r[evert] [options] [infile [outfile]]

        DESCRIPTION
               xxd  creates a hex dump of a given file or standard input.  It can a/bin/lso
               convert a hex dump back to its original binary form.  Like  uuencode(1)
               and  uudecode(1)  it allows the transmission of binary data in a `mail-
               safe' ASCII representation, but has the advantage of decoding to  stan-
               dard output.  Moreover, it can be used to perform binary file patching.

        OPTIONS

Since we've accessed both, they are now visible via a directory listing of
"/opt" as well as in a 'df' listing and a check of "/proc/mounts":
        [rfeynman@europa ~]$ /bin/ls -al /opt
        total 12
        drwxr-xr-x.  4 root       root          0 Aug 23 17:43 .
        dr-xr-xr-x. 26 root       root       4096 Aug 23 17:31 ..
        drwxr-xr-x.  2 4294967294 4294967294 4096 Aug 23 16:32 lhc-docs
        drwxr-xr-x. 20 4294967294 4294967294 4096 Aug 23 16:43 man
        [rfeynman@europa ~]$ /bin/df -h
        Filesystem            Size  Used Avail Use% Mounted on
        /dev/sda1             6.0G  1.6G  4.2G  27% /
        tmpfs                 467M     0  467M   0% /dev/shm
        /dev/sda3            1008M   98M  860M  11% /var
        /dev/sdb1            1020M   34M  935M   4% /export/home
        humboldt-int:/export/lhc-docs
                              6.0G  1.6G  4.1G  28% /opt/lhc-docs
        humboldt-int:/export/man
                              6.0G  1.6G  4.1G  28% /opt/man
        [rfeynman@europa ~]$ /bin/egrep 'info|humboldt' /proc/mounts
        /etc/auto.info /opt autofs rw,relatime,fd=25,pgrp=4933,timeout=300,minproto=5,\
                maxproto=5,indirect 0 0
        humboldt-int:/export/lhc-docs /opt/lhc-docs nfs4 ro,relatime,vers=4,rsize=131072,\
                wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,\
                clientaddr=10.0.7.90,minorversion=0,local_lock=none,addr=10.0.7.125 0 0
        humboldt-int:/export/man /opt/man nfs4 ro,relatime,vers=4,rsize=131072,wsize=131072,\
                namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,\
                clientaddr=10.0.7.90,minorversion=0,local_lock=none,addr=10.0.7.125 0 0


see also:
    Configuring AutoFS in FreeBSD
    Remounting Part of an FS Elsewhere (loopback mounts)
    Configuring NFS in Linux (nfs.linux)