0% found this document useful (0 votes)
218 views

Creating and Mounting File System

A filesystem organizes files on a disk and keeps track of metadata like filenames, file sizes, and locations. Before a disk partition can be used to store files, it must be initialized by creating a filesystem on it. Common filesystem components include the superblock, inodes, data blocks, directories, and indirect blocks. Filesystems are created using the mkfs command and mounted using mount, making their contents accessible within the directory tree. Filesystems can be mounted read-only or read-write and must be unmounted with umount before removing media like floppies.

Uploaded by

Fausiya Salim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views

Creating and Mounting File System

A filesystem organizes files on a disk and keeps track of metadata like filenames, file sizes, and locations. Before a disk partition can be used to store files, it must be initialized by creating a filesystem on it. Common filesystem components include the superblock, inodes, data blocks, directories, and indirect blocks. Filesystems are created using the mkfs command and mounted using mount, making their contents accessible within the directory tree. Filesystems can be mounted read-only or read-write and must be unmounted with umount before removing media like floppies.

Uploaded by

Fausiya Salim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CREATING AND MOUNTING FILE SYSTEM

What are filesystems?

A filesystem is the methods and data structures that an operating system uses to
keep track of files on a disk or partition; that is, the way the files are organized
on the disk. The word is also used to refer to a partition or disk that is used to
store the files or the type of the filesystem.

Before a partition or disk can be used as a filesystem, it needs to be initialized,


and the bookkeeping data structures need to be written to the disk. This process
is called making a filesystem.

Most UNIX filesystem types have a similar general structure, although the
exact details vary quite a bit. The central concepts are superblock, inode , data
block, directory block , and indirection block. The superblock contains
information about the filesystem as a whole, such as its size (the exact
information here depends on the filesystem). An inode contains all information
about a file, except its name. The name is stored in the directory, together with
the number of the inode. A directory entry consists of a filename and the
number of the inode which represents the file. The inode contains the numbers
of several data blocks, which are used to store the data in the file. There is
space only for a few data block numbers in the inode, however, and if more are
needed, more space for pointers to the data blocks is allocated dynamically.
These dynamically allocated blocks are indirect blocks; the name indicates that
in order to find the data block, one has to find its number in the indirect block
first.
Which filesystem should be used?

There is usually little point in using many different filesystems.


Currently, ext3 is the most popular filesystem, because it is a journaled
filesystem. A filesystem that uses journaling is also called a journaled
filesystem. A journaled filesystem maintains a log, or journal, of what has
happened on a filesystem. In the event of a system crash, or if your 2 year old
son hits the power button like mine loves to do, a journaled filesystem is
designed to use the filesystem's logs to recreate unsaved and lost data. This
makes data loss much less likely and will likely become a standard feature in
Linux filesystems. However, do not get a false sense of security from this. Like
everything else, errors can arise. Always make sure to back up your data in the
event of an emergency.
Creating a filesystem

Filesystems are created, i.e., initialized, with the mkfs command. There is
actually a separate program for each filesystem type. mkfs is just a front end
that runs the appropriate program depending on the desired filesystem type.
The type is selected with the -t fstype option.
Mounting and unmounting

Before one can use a filesystem, it has to be mounted. The operating system
then does various bookkeeping things to make sure that everything works.
Since all files in UNIX are in a single directory tree, the mount operation will
make it look like the contents of the new
filesystem are the contents of an existing subdirectory in some already
mounted

filesystem.

The mounts could be done as in the following example:

$ mount /dev/hda2 /home $


mount /dev/hda3 /usr

The mount command takes two arguments. The first one is the device file
corresponding to the disk or partition containing the filesystem. The second
one is the directory below which it will be mounted. After these commands the
contents of the two filesystems look just like the contents of the /home and /usr
directories, respectively. One would then say that /dev/hda2 is mounted on
/home'', and similarly for /usr. To look at either filesystem, one would look at
the contents of the directory on which it has been mounted, just as if it were
any other directory. Note the difference between the device file, /dev/hda2, and
the mounted-on directory,/home. The device file gives access to the raw
contents of the disk, the mounted-on directory gives access to the files on the
disk. The mounted-on directory is called the mount point.

Linux supports many filesystem types. mount tries to guess the type of the
filesystem. You can also use the -t fstype option to specify the type directly;
this is sometimes necessary, since the heuristics mount uses do not always
work. For example, to mount an MS-DOS floppy, you could use the following
command:

mount -t msdos /dev/fd0

The mounted-on directory need not be empty, although it must exist. Any files
in it, however, will be inaccessible by name while the filesystem is mounted.
(Any files that have already been opened will still be accessible. Files that have
hard links from other directories can be accessed using those names.) There is
no harm done with this, and it can even be useful. For instance, some people
like to have /tmp and /var/tmp synonymous, and make /tmp be a symbolic link
to /var/tmp. When the system is booted, before the /var filesystem is mounted,
a /var/tmp directory residing on the root filesystem is used instead. When /var
is mounted, it will make the/var/tmp directory on the root filesystem
inaccessible. If /var/tmp didn't exist on the root filesystem, it would be
impossible to use temporary files before mounting /var.

If you don't intend to write anything to the filesystem, use the -r switch for
mount to do a read-only mount. This will make the kernel stop any attempts at
writing to the filesystem, and will also stop the kernel from updating file access
times in the inodes. Read-only mounts are necessary for unwritable media, e.g.,
CD-ROMs.

The alert reader has already noticed a slight logistical problem. How is the first
filesystem (called the root filesystem, because it contains the root directory)
mounted, since it obviously can't be mounted on another filesystem? Well, the
answer is that it is done by magic. The root filesystem is magically mounted at
boot time, and one can rely on it to always be mounted. If the root filesystem
can't be mounted, the system does not boot. The name of the filesystem that is
magically mounted as root is either compiled into the kernel, or set using LILO
or rdev.

The root filesystem is usually first mounted read-only. The startup scripts will
then run fsck to verify its validity, and if there are no problems, they will re-
mount it so that writes will also be allowed. fsck must not be run on a mounted
filesystem, since any changes to the filesystem while fsck is running will cause
trouble. Since the root filesystem is mounted read-only while it is being
checked, fsck can fix any problems without worry, since the remount operation
will flush any metadata that the filesystem keeps in memory.

When a filesystem no longer needs to be mounted, it can be unmounted with


umount. umount takes one argument: either the device file or the mount point.
For example, to unmount the directories of the previous example, one could
use the commands

umount /dev/hda2 $ umount

$ umount /dev/hda2 $
umount /usr
$

See the man page for further instructions on how to use the command. It is
imperative that you always unmount a mounted floppy. Don't just pop the
floppy out of the drive! Because of disk caching, the data is not necessarily
written to the floppy until you unmount it, so removing the floppy from the
drive too early might cause the contents to become garbled. If you only read
from the floppy, this is not very likely, but if you write, even accidentally, the
result may be catastrophic.

Mounting and unmounting requires super user privileges, i.e., only root can do
it. The reason for this is that if any user can mount a floppy on any directory,
then it is rather easy to create a floppy with, say, a Trojan horse disguised as
/bin/sh, or any other often used program. However, it is often necessary to
allow users to use floppies, and there are several ways to do this:

You might also like