\documentclass{ictlab}

\RCS $Revision: 1.2 $

\usepackage{alltt,key,xr,answer2,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 boot process, \texttt{init} and runlevels}
\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 be familiar with standard runlevels in a Linux system

\item be able to rescue a system without the main dynamic link
  libraries using an alternative \texttt{init}

\item be able to change through runlevels, and understand their
  purpose

\item be familiar with the main parts of the boot sequence
\end{itemize}

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

\subsection{\texttt{init} and runlevels}
\label{sec:init-and-runlevels}

\begin{itemize}
\item  Linux has seven modes of operation
\item  Referred to as \emph{runlevels}
  
\item The Linux Standards Base
  (\url{http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/runlevels.html})
  defines the following standard runlevels that all distributions should
  follow to be compliant:

  \noindent%
  \begin{tabular}[t]{ll}
    0 & halt\\
    1 & single user mode \\
    2 & multiuser with no network services exported \\
    3 & normal/full multiuser \\
    4 & reserved for local use, default is normal/full multiuser \\
    5 & multiuser with a display manager or equivalent \\
    6 & reboot
  \end{tabular}

\item The \texttt{init} process governs runlevels.  \texttt{init} is
  \emph{the first program that the kernel runs}.

\item To change from one runlevel to another, you can use
  \texttt{telinit} or simply \texttt{init}:
\begin{alltt}
$ \textbf{sudo init \emph{level}}
\end{alltt}%$

For example, if you are currently in runlevel 5, you can change to
runlevel 3 by executing the command:
\begin{alltt}
$ \textbf{sudo init 3}
\end{alltt}%$
\end{itemize}

\subsection{Runlevel 1, or Single-user Mode}
\label{sec:single-user-mode}

You can change to runlevel 1 in this way too.
\begin{itemize}
  \item  Mainly used for diagnostic purposes
  \item  Starts only a subset of the possible services, e.g.
  \begin{itemize}
    \item  No networking
    \item  No mail services
    \item  No name lookup services
    \begin{itemize}
      \item Except \texttt{/etc/hosts}
    \end{itemize}
    \item  No file-sharing services etc
  \end{itemize}
\end{itemize}

You can also select a runevel when the kernel boots.  When you see the
\acro{LILO} screen (the one with the red hat logo, before the system
boots), press \key{Ctrl-x} and then, at the \texttt{boot:} prompt:
\begin{alltt}
boot: \textbf{linux 1}
\end{alltt}

\subsection{An alternative \texttt{init}, and when you might need it}
\label{sec:init-alternative}


Normally the \texttt{init} program (\texttt{/sbin/init}) is started
automatically by the kernel; the location is known by the kernel.  But
sometimes you need to specify an alternative \texttt{init} program,
such as when the main dynamic link libraries do not correspond with
the executables on the system.  In that case, nothing works until they
match.  A useful tool is a \emph{statically linked} shell, which does
not depend on other dynamically loaded libraries.  There is one called
\texttt{sash}, which you will install today.  The \texttt{sash}
package installs \texttt{sash} as \texttt{/sbin/sash}.

To start the computer using \texttt{/sbin/sash} as the \texttt{init}
process, you can type the following at a \acro{LILO} prompt:
\begin{alltt}
boot: \textbf{linux 1 init=/sbin/sash}
\end{alltt}
and the system will provide you with the limited capabilities of the
stand-alone shell, definitely better than no access at all!
                                
%\clearpage
\section{Procedure}
\label{sec:procedure}

\begin{enumerate}
\item Make sure you have sash installed:
  \begin{alltt}
$ \textbf{rpm -q sash}
  \end{alltt}%$
  If it tells you it's not installed, then go and install it:
  \begin{alltt}
$ \textbf{cd /ftp/rh-7.1-updated/RedHat/RPMS}
$ \textbf{sudo rpm -Uhv sash-3.4-8.i386.rpm}
  \end{alltt}

\item Verify that you have sash there in the right place:
  \begin{alltt}
$ \textbf{ls -l /sbin/sash}
-rwxr-xr-x    1 root     root       508268 Aug 14  2001 /sbin/sash
  \end{alltt}%$
(The date and size will be differnent, but similar).

\item Use the \texttt{ldd} program to verify that \texttt{sash} is not
  dynamically linked.  Then compare the result with running on
  \texttt{bash}:
  \begin{alltt}
$ \textbf{ldd /sbin/sash}
$ \textbf{ldd /bin/bash}
  \end{alltt}%

\item Create a boot disk for Red Hat 7.2 like this (assuming that the
  automounter mounts \texttt{ictlab:/var/ftp/pub} on \texttt{/ftp}).
  Start by putting a formatted floppy disk (that has no bad sectors)
  into the drive (all contents will be replaced), and:
  \begin{alltt}
$ \textbf{cd /ftp/redhat-7.2/updates/images/i386}
$ \textbf{dd if=bootnet.img of=/dev/fd0}
  \end{alltt}
and wait until the data copying and conversion program \texttt{dd} has
finished creating a boot floppy.

\item boot your system into single user mode, as described in
  section~\vref{sec:single-user-mode}.

\item Change to the \texttt{/lib} directory, and examine the important
  dynamic library loader, \texttt{ld-linux.so.2}:
  \begin{alltt}
# \textbf{ls -l /lib/ld-linux.so.2}
lrwxrwxrwx    1 root     root           11 Dec 15 08:44
/lib/ld-linux.so.2
-> ld-2.2.4.so
  \end{alltt}
  
\item Now you will do something that you will probably never do again:
  rename \texttt{ld-linux.so.2}!
  \begin{alltt}
# \textbf{mv ld-linux.so.2 ld-linux.so.2-orig}
  \end{alltt}

\item Now examine this file:
  \begin{alltt}
# \textbf{ls -l ld-linux.so.2-orig}
  \end{alltt}
Oh dear, it doesn't work!  What to do?
\end{enumerate}

\section{Getting out of Disaster}
\label{sec:disaster-recovery}

Read section~\vref{sec:init-alternative}, and using the procedure
there, recover from this situation, and when finished, make sure that
you can have the normal \texttt{ls} command work.

Hints:
\begin{enumerate}
\item Type \texttt{help} when you have sash running

\item type \texttt{-ls} within \texttt{sash}
\end{enumerate}


\section{Now again!}
\label{sec:again}

\begin{enumerate}
\item rename the file \texttt{/lib/libc.so.6}:
  \begin{alltt}
# mv /lib/libc.so.6 /lib/libc.so.6-orig
  \end{alltt}

\item This time, boot your computer using the linux installation disk,
  but this time, at the \acro{LILO} prompt, enter:
  \begin{alltt}
boot: \textbf{linux rescue}
  \end{alltt}

\item Select \texttt{NFS}, \texttt{nicku.org} as the
  \NFS server, and the \NFS directory:
  \texttt{/var/ftp/pub/redhat-7.2}.

\item Agree to mount your Linux file systems.

\item Make sure that you are working on \texttt{your} hard disk, and
  not \texttt{/dev/hdc}, the one that Samson has installed on the
  internal hard disks!

\item Fix the problem again, and verify that it all works.

\item Show your lecturer.
\end{enumerate}

\subsection{More about runlevels}
\label{sec:more-about-runlevels}

Runlevels give great flexibility to the system administrator.  Have a
look in the \texttt{/etc/rc.d} directory.  First, make your terminal
window nice and wide, then:
\begin{alltt}
$ \textbf{cd /etc/rc.d}
$ \textbf{ls -lR rc?.d}
\end{alltt}
This will list the contents of the dirctories
\texttt{/etc/rc.d/rc0.d}, \texttt{/etc/rc.d/rc1.d},
\texttt{/etc/rc.d/rc2.d}, \texttt{/etc/rc.d/rc3.d},
\texttt{/etc/rc.d/rc4.d}, \texttt{/etc/rc.d/rc5.d} and
\texttt{/etc/rc.d/rc6.d}.

Each directory contains lots of symbolic links to scripts in
\texttt{/etc/rc.d/init.d}.  Some start with \texttt{K$nn$}, and others
start with \texttt{S$xx$}, where \texttt{$xx$} is a two-digit number.

Under the control of \texttt{/sbin/init}, these scripts are run with
the argument \texttt{start} if they start with \texttt{S$xx$}, and
\texttt{stop} if they start with \texttt{K$nn$}.

\begin{enumerate}
\item Look at the links in \texttt{/etc/rc.d/rc0.d}.  Do most of them
  start with \texttt{S$xx$} or \texttt{K$xx$}?\answerbox{K}

\item Why do you think this is so?\\\answerbox[0.9\linewidth]{stop processes in
    runlevel 0 to halt the computer.}

\item Look at the links in \texttt{/etc/rc.d/rc3.d}.  Do most of them
  start with \texttt{S$xx$} or \texttt{K$xx$}?\\ \answerbox{S}

\item Why do you think this is so?\\\answerbox[0.9\linewidth]{We want to
    start most services in runlevel 3.}

\item Try running a few of these scripts by hand:
  \begin{alltt}
$ \textbf{sudo /etc/init.d/autofs stop}
$ \textbf{sudo /etc/init.d/autofs start}
  \end{alltt}

\item What do you think the \textbf{chkconfig} program does when you
  type:
  \begin{alltt}
$ \textbf{sudo chkconfig --level 345 autofs off}
  \end{alltt}%$
and
  \begin{alltt}
$ \textbf{sudo chkconfig --level 345 autofs on}
  \end{alltt}%$

Do each of these things, and look for any change in the symbolic links
before and after each of the two
steps.\\\answerbox[0.9\linewidth]{\texttt{chkconfig} links the init
    scripts to \texttt{K$xx$autofs} in the directories for runlevels
    3, 4 and 5 when you run \texttt{chkconfig
      --level 345 autofs off}, and to \texttt{S$xx$autofs} when you
    tell \texttt{chkconfig} to turn it on.}
\end{enumerate}
\end{document}
