\documentclass{ictlab}

\RCS $Revision: 1.7 $

\usepackage{alltt,key,xr,cols}
\externaldocument[lt-]%
{../../linux_training-plus-config-files-ossi/build/masterfile}

\ifx\pdftexversion\undefined \else
  \usepackage[pdfpagemode=None,pdfauthor={Nick Urbanik}]{hyperref}
\fi

\newcommand*{\labTitle}{The Automounter, and automating its
  Installation}
\renewcommand*{\subject}{Operating Systems and Systems Integration}

\providecommand*{\RPM}{\acro{RPM}\xspace}
\providecommand*{\CD}{\acro{CD}\xspace}

\begin{document}
%\Large
%\tableofcontents

\section{Aim}
\label{sec:aim}

After successfully working through this exercise, You will:
\begin{itemize}
\item understand what the automounter does;
\item be able to set up the automounter manually;
\item be able to write a shell script suitable for use with
  \texttt{kickstart} to automatically configure the automounter as
  part of an unattended installation.
\end{itemize}

\section{Background}
\label{sec:background}

The automounter provides a convenient way of accessing network
directories.  You use it when you log into your network account to
access your home directory.  It is often used with \NFS (Network File
System), but can also be used to mount local disks such as floppy
disks or removable media such as Zip disks and \CDROM{}s.

Currently your home directory is mounted by the automounter when you
log into your network account.  The configuration parameters for the
automounter are provided by our \LDAP directory server.  You may see
some information about the current automounter configuration by typing:
\begin{verbatim}
$ service autofs status
\end{verbatim}%$

\subsection{Advantages of the Automounter compared with putting
  entries in /etc/fstab}
\label{sec:advantages-of-autofs}

You can always put an entry into \texttt{/etc/fstab} that permanently
mounts a network partition on boot, like this:
\begin{verbatim}
nicku.org:/var/ftp/pub /mnt/ftp  nfs ro,defaults 0 0
\end{verbatim}
Every time the computer boots, the network directory will be mounted,
until the computer shuts down.

But \texttt{autofs} is much more flexible.
\begin{itemize}
\item Any user can mount the directory automatically when they need it
\item Does not use the system resources when it does not need to be
  mounted
\end{itemize}

\subsection{The way \texttt{autofs} is configured}
\label{sec:configuration}

There are two sets of configuration files used by \texttt{autofs}: the
master file, \texttt{/etc/auto.master}, described by \texttt{man
  auto.master}, and the \emph{map} files used to define what can be
mounted on individual entries listed in \texttt{/etc/auto.master}.
These are described in \texttt{man 5 autofs}.

The \texttt{/etc/auto.master} file has the format:
\begin{alltt}
\emph{directory}   \emph{mapfile} \emph{options}
\end{alltt}
The options are optional.

The directory is the top-level directory where automatic mounts
occur.  This entry points to another file (your map), which takes care
of individually defining these mounts and their file system types.
See \texttt{/etc/auto.misc} as an example of a map file.


%\clearpage
\section{Procedure}
\label{sec:procedure}

\begin{enumerate}
\item Identify the documentation that comes with the \texttt{autofs}
  package with the \RPM command:
\begin{verbatim}
$ rpm -qld autofs
\end{verbatim}%$
To read the manual pages for the \texttt{/etc/auto.master}
configuration file:
\begin{verbatim}
$ man auto.master
\end{verbatim}%$
and for the other \texttt{/etc/auto.*} configuration files:
\begin{verbatim}
$ man 5 autofs
\end{verbatim}%$
Read the documentation.

\item Change to the root directory of your file system, and create a
  directory called \texttt{.auto} there:
\begin{verbatim}
$ sudo mkdir /.auto
\end{verbatim}%$

\item create a dangling symbolic link;
\begin{verbatim}
$ sudo ln -s .auto/ftp /ftp
\end{verbatim}%$
  If you type \texttt{ls} here, you will see that the link is shown in
  red, perhaps blinking, to indicate that the link does not point
  anywhere.  The automounter will actually mount the network file
  system on this.


\item edit the master automounter file, \texttt{/etc/auto.master}:
\begin{verbatim}
$ xhost +localhost
$ sudo -v
$ sudo emacs /etc/auto.master
\end{verbatim}%$

\item Edit a line there to read:
\begin{verbatim}
/.auto    /etc/auto.ftp   --timeout 60
\end{verbatim}
and make sure there is a newline at the end, and save it.
\item Create a file \texttt{/etc/auto.ftp} that contains this line:
\begin{verbatim}
ftp     -ro,soft,intr   nicku.org:/var/ftp/pub
\end{verbatim}
and make sure there is a newline at the end, and save it.

\item Restart the automounter, and change to that directory:
\begin{verbatim}
$ sudo service autofs restart
$ cd /ftp/redhat-7.2
$ ls -l
\end{verbatim}%$

\item If the automounter is not working properly, try seeing what the
  status of it is.  Here is the output from a machine which is working
  correctly:
{\small
\begin{verbatim}
$ service autofs status
Configured Mount Points:
------------------------
/usr/sbin/automount --timeout 60 /.auto file /etc/auto.ftp
/usr/sbin/automount --timeout 60 /home ldap nicku.org:ou=auto.home,
dc=tyict,dc=vtc,dc=edu,dc=hk
/usr/sbin/automount --timeout 60 /usr/local ldap nicku.org:
ou=auto.practical,dc=tyict,dc=vtc,dc=edu,dc=hk

Active Mount Points:
--------------------
/usr/sbin/automount --timeout 60 /.auto file /etc/auto.ftp
/usr/sbin/automount --timeout 60 /home ldap nicku.org:ou=auto.home,
dc=tyict,dc=vtc,dc=edu,dc=hk
/usr/sbin/automount --timeout 60 /usr/local ldap nicku.org:
ou=auto.practical,dc=tyict,dc=vtc,dc=edu,dc=hk
\end{verbatim}}%$
\end{enumerate}
Please note that I have broken the long lines here into two so that
they can fit on the page.

Note too that anything which prevents \NFS, such as a firewall, will
stop you from mounting a network file system.  This should not be the
case with you.  Here is how to test if you have a firewall.
\begin{verbatim}
$ sudo ipchains -L -n
\end{verbatim}%$
Output that looks like this:
\begin{verbatim}
ipchains: Incompatible with this kernel
\end{verbatim}
or that looks like this:
\begin{verbatim}
Chain input (policy ACCEPT):
Chain forward (policy ACCEPT):
Chain output (policy ACCEPT):
\end{verbatim}
show that your firewall is turned off.

\subsection{Make sure you understand what it is doing}
\label{sec:understand-autofs}

Now test the system to make sure you can see the automounter working.
\begin{enumerate}
\item Type:
\begin{verbatim}
$ mount
\end{verbatim}%$
to see that the network directory is not mounted.
\begin{explanation}
  If it is, make sure that no processes have a current directory on
  the network directory, and wait 60 seconds till the automounter times
  out.
\end{explanation}

\item Now list the network directory:
\begin{verbatim}
$ ls /ftp
\end{verbatim}%$
If you see no files from the network directory, something is wrong.
Fix it before proceding.

\item Check that the mount command shows that it is mounted:
\begin{verbatim}
$ mount
\end{verbatim}%$

\item Finally, wait 60 seconds, and verify that the automounter has
  unmounted the network file system.
  
  Important note: if any process has its current directory on a
  mounted file system, it will be impossible to unmount that file
  system until every process has changed its current directory away
  from the mounted file system, or any remaining processes that have
  their current directory on the mounted file system are all
  terminated.  Example:
  \begin{alltt}
$ \textbf{cd /ftp/rh-7.2-updated}
$ \textbf{# Wait for a very long time}
  \end{alltt}
  \ldots the automounter will not unmount the file system from
  \texttt{/.auto/ftp} until you change directory from the network file
  system.
\end{enumerate}

\section{Automating this with a shell script}
\label{sec:scripting}

The next step is to automate this.  The aim is to use this script with
\texttt{kickstart}\footnote{Kickstart is a system for performing
  automated system installation.} in a future lab session so that you can
automatically configure the automounter when you perform an unattended
installation.


\subsection{Procedure for Scripting the automounter setup}
\label{sec:scripting-procedure}

Do this \emph{only} after you are certain that your automounter is
working properly.  You should build your shell script up piece by
piece, not try to write it all at once.
\begin{enumerate}
\item undo the setup completely:
  \begin{enumerate}
  \item delete the directory \texttt{/.auto}
  \item delete the file \texttt{/etc/auto.ftp}
  \item delete the line 
 \begin{verbatim}
/.auto    /etc/auto.ftp   --timeout 60
\end{verbatim}
from \texttt{/etc/auto.master}.
 \end{enumerate}

\item How do I get something like
  \begin{alltt}
$ \textbf{sudo echo "blah blah blah" >> /etc/testing}
bash: /etc/testing: Permission denied
  \end{alltt}%$
to work?  I just get: \texttt{bash: /etc/testing: Permission denied}

\item[\textbf{Solution:}]Although you executed \texttt{echo} as the
  user \texttt{root}, you did not perform the redirection with
  ``\texttt{>>}'' as \texttt{root}; the redirection was done by your
  shell, which is you.  To perform the redirection, you would need to
  start a shell process owned by root, which would then have
  permission to write to \texttt{/etc/testing}:
  \begin{alltt}
$ \textbf{sudo sh -c 'echo "blah blah blah" >> /etc/testing'}
  \end{alltt}%$
  which works fine.

\item Now write the code to create the directory and the symbolic
  link.  The code should work no matter where you run the script.
  Note: Do \emph{\textbf{not}} use \texttt{sudo} in your script.  Use
  \texttt{sudo} to run your script as a whole.
  
  You may find it helpful to refer to the notes about symbolic links
  on our subject web site.

  Make sure that this works before proceeding.  


\item The pseudo code for the whole system is:
\begin{verbatim}
if there is no line containing /etc/auto.ftp in /etc/auto.master
    add the appropriate line to /etc/auto.master
overwrite any file /etc/auto.ftp with the appropriate content
make the directory /.auto
if any file or directory exists with the name /ftp
    delete it.
create the symbolic link
turn on the autofs service with chkconfig in runlevels 3, 4 and 5
\end{verbatim}

\item Show your lecturer your working shell script.
\end{enumerate}

\end{document}
