% Linux Installation and Getting Started    -*- TeX -*-
% filesystem.tex 
% Copyright (c) 1992, 1993 by Matt Welsh <mdw@sunsite.unc.edu>
%
% This file is freely redistributable, but you must preserve this copyright 
% notice on all copies, and it must be distributed only as part of "Linux 
% Installation and Getting Started". This file's use is covered by the 
% copyright for the entire document, in the file "copyright.tex".

\section{Managing Filesystems}\label{sec-manage-fs}
\index{filesystems|(}
Another task of the system administrator is taking care of filesystems.
Most of this job entails periodically checking the filesystems for
damage or corrupted files; many systems automatically check the 
filesystems at boot time. 

\subsection{Mounting filesystems}
\index{filesystems!mounting|(}
First, a few concepts about filesystems. Before a filesystem is 
accessible to the system, it must be {\bf mounted} on some directory.
For example, if you have a filesystem on a floppy, you must mount it
under some directory, say {\tt /mnt}, in order to access the files
on it (see Section~\ref{sec-floppy}). After mounting the filesystem,
all of the files in the filesystem appear in that directory. 
After unmounting the filesystem, the directory (in this case, {\tt /mnt})
will be empty. 

The same is true of filesystems on the hard drive. The system automatically
mounts filesystems on your hard drive for you at bootup time. The 
so-called ``root filesystem'' is mounted on the directory {\tt /}. If
you have a separate filesystem for {\tt /usr}, for example, 
it is mounted on {\tt /usr}. 
If you only have a root filesystem, all files (including
those in {\tt /usr}) exist on that filesystem.

\index{mount@{\tt mount}}
The command {\tt mount} is used to mount a filesystem. The command
\begin{tscreen}
mount -av
\end{tscreen}
is executed from the file {\tt /etc/rc} (which is the
system initialization file executed at boot time; see Section~\ref{sec-rc}). 
The {\tt mount -av}
command obtains information on filesystems and mount points from
the file {\tt /etc/fstab}. An example {\tt fstab} file appears below.
\index{filesystems!/etc/fstab@{\tt /etc/fstab}}
\index{/etc/fstab@{\tt /etc/fstab}}
\begin{tscreen}\begin{verbatim}
# device       directory      type     options
/dev/hda2       /             ext2     defaults
/dev/hda3       /usr          ext2     defaults
/dev/hda4       none          swap     sw
/proc           /proc         proc     none
\end{verbatim}\end{tscreen}

The first field is the device---the name of the partition to mount. The
second field is the mount point. The third field is the filesystem
type---such as {\tt ext2} (for ext2fs) or {\tt minix} (for Minix 
filesystems). Table~\ref{table-fs-types} lists the various filesystem
types available for Linux.\footnote{This table is current as of
kernel version 1.1.37.} Not all of these filesystem types may be
available on your system; your kernel must have support for them
compiled in. See Section~\ref{sec-sysadm-upgrade} for information
on building the kernel.
\begin{table}[ht]\begin{center}
\small\begin{tabular}{lll}
\hline
Filesystem		& Type name 	& Comment \\
\hline
Second Extended Filesystem & {\tt ext2} & Most common Linux filesystem. \\
Extended Filesystem	& {\tt ext} 	& Superseded by {\tt ext2}.\\
Minix Filesystem 	& {\tt minix} 	& Original Minix filesystem; rarely used.\\
Xia Filesystem		& {\tt xia}	& Like {\tt ext2}, but rarely used.\\
UMSDOS Filesystem	& {\tt umsdos}	& Used to install Linux on an MS-DOS partition.\\
MS-DOS Filesystem	& {\tt msdos} 	& Used to access MS-DOS files. \\
{\tt /proc} Filesystem  & {\tt proc}	& Provides process information for {\tt ps}, etc.\\
ISO 9660 Filesystem	& {\tt iso9660} & Format used by most CD-ROMs.\\
Xenix Filesystem 	& {\tt xenix}	& Used to access files from Xenix.\\
System V Filesystem	& {\tt sysv}	& Used to access files from System V variants for the x86.\\
Coherent Filesystem	& {\tt coherent} & Used to access files from Coherent.\\
HPFS Filesystem		& {\tt hpfs} & Read-only access for HPFS partitions (DoubleSpace).\\
\end{tabular}\normalsize\rm
\caption{Linux Filesystem Types}
\label{table-fs-types}
\end{center}\end{table}

The last field of the {\tt fstab} file contains {\tt mount} 
options---usually, this is set to ``{\tt defaults}''.

\index{swap partition!in /etc/fstab@in {\tt /etc/fstab}}
\index{swapon@{\tt swapon}}
As you can see, swap partitions are included in {\tt /etc/fstab}
as well. They have a mount directory of {\tt none}, and type {\tt swap}.
The {\tt swapon -a} command, executed from {\tt /etc/rc} as well,
is used to enable swapping on all swap devices listed in {\tt /etc/fstab}.

\index{/proc@{\tt /proc}!mounting of}
The {\tt fstab} file contains one special entry---for the {\tt /proc}
filesystem. As mentioned in Section~\ref{sec-processes}, the {\tt /proc}
filesystem is used to store information about system processes, available
memory, and so on. If {\tt /proc} is not mounted, commands such as {\tt ps}
will not work.

\blackdiamond The {\tt mount} command may only be used by root. This is
to ensure security on the system; you wouldn't want regular users mounting
and unmounting filesystems on a whim. There are several software packages
available which allow regular users to mount and unmount filesystems
(floppies in particular) without compromising system security. 

The {\tt mount -av} command actually mounts all filesystems other
than the root filesystem (in the table above, {\tt /dev/hda2}). The
root filesystem is automatically mounted at boot time by the 
kernel.

Instead of using {\tt mount -av}, you can mount a filesystem by 
hand. The command
\begin{tscreen}
\# {\em mount -t ext2 /dev/hda3 /usr}
\end{tscreen}
is equivalent to mounting the filesystem with the entry
{\tt /dev/hda3} in the {\tt fstab} example file above.

\index{filesystems!unmounting!with shutdown or halt@unmounting with {\tt shutdown} or {\tt halt}}
\index{unmounting filesystems}
In general, you should never have to mount or unmount filesystems by hand.
The {\tt mount -av} command in {\tt /etc/rc} takes care of mounting
the filesystems at boot time.
Filesystems are automatically unmounted by the {\tt shutdown} or 
{\tt halt} commands before bringing the system down. 
\index{filesystems!mounting|)}

\subsection{Checking filesystems}
\index{filesystems!checking}
It is usually a good idea to check your filesystems for damage or
corrupt files every now and then. Some systems automatically check their
filesystems at boot time (with the appropriate commands in {\tt /etc/rc}).

\index{e2fsck@{\tt e2fsck}}
The command used to check a filesystem depends on the type of the
filesystem in question. For ext2fs filesystems (the most commonly used
type), this command is {\tt e2fsck}. For example, the command
\begin{tscreen}
\# {\em e2fsck -av /dev/hda2}
\end{tscreen}
will check the ext2fs filesystem on {\tt /dev/hda2} and automatically
correct any errors. 

\index{umount@{\tt umount}}
\index{filesystems!unmounting}
\index{unmounting filesystems}
It is usually a good idea to unmount a filesystem before checking
it. For example, the command
\begin{tscreen}
\# {\em umount /dev/hda2}
\end{tscreen}
will unmount the filesystem on {\tt /dev/hda2}, after which you
can check it. The one exception is that you cannot unmount the root
filesystem. In order to check the root filesystem when it's
unmounted, you should use a maintenance boot/root diskette (see
Section~\ref{sec-maint-diskette}). You also cannot unmount a filesystem
if any of the files in it are ``busy''---that is, being used by 
a running process. For example, you cannot unmount a filesystem if
any user's current working directory is on that filesystem. You will
receive a ``{\tt Device busy}'' error if you attempt to unmount a
filesystem which is in use.

\index{efsck@{\tt efsck}}
\index{xfsck@{\tt xfsck}}
\index{fsck@{\tt fsck}}
Other filesystem types use different forms of the {\tt e2fsck} command,
such as {\tt efsck} and {\tt xfsck}. On some systems, you can simply use
the command {\tt fsck}, which will determine the filesystem type
and execute the appropriate command. 

\blackdiamond It is important that you reboot your system immediately
after checking a mounted filesystem if any corrections were made to that
filesystem. (However, in general, you shouldn't check filesystems while they
are mounted.) For example, if {\tt e2fsck} reports that it corrected
any errors with the filesystem, you should immediately {\tt shutdown -r}
in order to reboot the system. This is to allow the system to 
re-sync its information about the filesystem when {\tt e2fsck} 
modifies it. 

The {\tt /proc} filesystem never needs to be checked in 
this manner. {\tt /proc} is a memory filesystem, managed directly
by the kernel.
\index{filesystems|)}
