The mount and umount Commands

Now that the file system has been created, you can mount the partition. Initially, it will be empty, since the system hasn't had access to the file system for files to have been added to it. The command to mount file systems is mount , and its syntax is as follows:

mount [options] <-t type> [-o mount options] <device> <mounting point>

In this case, we want to temporarily mount our partition on /mnt (or any other mount point you have chosen: remember that the mount point must exist). The command for mounting our newly created partition is:

$ mount -t ext3 /dev/hdb1 /mnt

The -t option is used to specify what type of file system the partition is supposed to host. The file systems you will most frequently encounter are ext2FS (the GNU/Linux file system) or ext3FS (an improved version of ext2FS with journaling capabilities), VFAT (for all DOS/Windows® partitions: FAT 12, 16 or 32) and ISO9660 (CD-ROM file system). If you don't specify any type, mount will try and guess which file system is hosted by the partition by reading the superblock.

The -o option is used to specify one or more mounting options. The options appropriate for a particular file system will depend on the file system being used. Refer to the mount(8) man page for more details.

Now that you've mounted your new partition, it's time to copy the entire /usr directory onto it:

$ (cd /usr && tar cf - .) | (cd /mnt && tar xpvf -)

Now that the files are copied, we can unmount our partition. To do this, use the umount command. The syntax is simple:

umount <mount point|device>

So, to unmount our new partition, we can type:

$ umount /mnt

or:

$ umount /dev/hdb1
[Tip]Tip

Sometimes it may happen that a device (usually the CD-ROM) is busy. If this happens, most users would solve this problem by rebooting their computer. For example, if umount /dev/hdc fails, then you could try the “lazyumount. The syntax is fairly simple:

umount -l <mount point|device>

This command disconnects the device and closes all open handles to the device when possible. Usually you can eject the disc using the eject <mount point|device> command. So... if the eject command does nothing and you don't want to reboot, use lazy unmounting.

Since this partition is going to “become” our /usr directory, we need to tell the system. To do this, we edit the /etc/fstab file. It makes it possible to automate the mounting of certain file systems, especially at system start-up. It contains a series of lines describing the file systems, their mount points and other options. Here's an example of such a file:

/dev/hda1   /           ext2    defaults        1 1
/dev/hda5   /home       ext2    defaults        1 2
/dev/hda6   swap        swap    defaults        0 0
none        /mnt/cdrom  supermount dev=/dev/scd0,fs=udf:iso9660,ro,-- 0 0
none        /mnt/floppy supermount dev=/dev/fd0,fs=ext2:vfat,--,sync,umask=0 0 0
none        /proc       proc    defaults        0 0
none        /dev/pts    devpts  mode=0622       0 0

Each line consists of:

There is always an entry for the root file system. The Swap partitions are special since they're not visible in the tree structure, and the mount point field for those partitions contains the swap keyword. As for the /proc file system, it will be described in more detail in Chapter 10, The /proc Filesystem. Another special file system is /dev/pts.

At this point, we have moved the entire /usr hierarchy to /dev/hdb1 and we want this partition to be mounted as /usr at boot time. To accomplish this, add the following entry to the /etc/fstab file:

/dev/hdb1        /usr         ext2    defaults  1 2

Now the partition will be mounted at each boot, and will be checked for errors if necessary.

There are two special options: noauto and user. The noauto option specifies that the file system should not be mounted at start-up, and is mounted only when you tell it to. The user option specifies that any user can mount and unmount the file system. These two options are typically used for the CD-ROM and floppy drives. There are other options, and /etc/fstab has a man page (fstab(5)) you can read for more information.

One advantage of using /etc/fstab is that it simplifies the mount command syntax. To mount a file system described in the file, you can either reference the mount point or the device. To mount a floppy disk, you can type:

$ mount /mnt/floppy

or:

$ mount /dev/fd0

To finish our partition moving example, let's review what we've already done. We copied the /usr hierarchy and modified /etc/fstab so that the new partition will be mounted at start-up. But for the moment, the old /usr files are still in their original place on the drive, so we need to delete them to free up space (which was, after all, our initial goal). To do so, you first need to switch to single user mode by issuing the telinit 1 command on the command line.

And that's it. Now, go back to multi-user mode (telinit 3 for standard text mode or telinit 5 for the X Window System), and if there's no further administrative work left, you should now log off from the root account.