Mounting and Unmounting Local Disks

an article added by: Daniel K. at 11282007


In: Categories » Computers and technology » Linux Commands » Mounting and Unmounting Local Disks

Linux’s strong points include its flexibility and the way it lends itself to seamless management of file locations. Partitions are mounted so that they appear as just another subdirectory. Even a substantial number of file systems look, to the user, like one large directory tree. This characteristic is especially helpful to the administrator, who can relocate partitions to various servers but can have the partitions still mounted to the same location in the directory tree; users of the file system need not know about the move at all. The file-system management process begins with the root directory (see Linux tutorial 7-1). The partition containing the kernel and core directory structure is mounted at boot time. This single partition needs to have all the required utilities and configuration files to bring the system up to single-user mode. Many of the directories on this partition are empty.

As the boot scripts run, additional partitions are mounted, adding to the structure of the file system. The mount process overlays a single subdirectory with the directory tree of the partition it is trying to mount. For example, let’s say that /dev/hda1 is the root partition. It has the directory /usr, which contains no files. The partition /dev/hda3 contains all the files that you want in /usr, so you mount /dev/hda3 to the directory /usr. Users can now simply change directories to /usr to see all the files from that partition. The user doesn’t need to know that /usr is actually a separate partition. Keep in mind that when a new directory is mounted, the mount process hides all the contents of the previously mounted directory. So in the /usr example, if the root partition did have files in /usr before mounting /dev/hda3, those /usr files would no longer be visible. (They’re not erased, of course—once /dev/hda3 is unmounted, the /usr files would become visible once again.)

Using the mount Command

Like many command-line tools, the mount command has myriad options, most of which you won’t be using in daily work. You can get full details on these options from the mount man page. In this section, you’ll explore the most common uses of the command. The structure of the mount command is as follows:

    mount [options] device directory
  

The options available for use with the mount –o parameter are shown in Image 7-2. The following mount command mounts the /dev/hda3 partition to the /usr directory with read-only privileges:

    [root@tedford /root]# mount -o ro /dev/hda3 /usr

Unmounting File Systems

To unmount a file system, use the umount command. Here’s the command format:

    umount [-f] directory

where directory is the directory to be unmounted. For example:

    [root@tedford /root]# umount /usr

unmounts the partition mounted on the /usr directory.

The command name is umount, with no “n.” What it does is “unmount,” but that’s not its name. When the File System Is in Use There’s a catch to umount: If the file system is in use (that is, when someone has a file open on that partition), you won’t be able to unmount that file system. To get around this, you have three choices:

- You can use lsof or fuser to determine which processes are keeping the files open, and then kill them off or ask the process owners to stop what they’re doing. (Read about the kill parameter in fuser on the fuser man page.) If you choose to kill the processes, be sure you understand the repercussions of doing so (read: don’t get fired for doing this).

- You can use the –f option with umount to force the unmount process. Any processes with open files on the partition will be left hanging, and data may be lost.

- The safest and most proper alternative is to bring the system down to single-user mode and then unmount the file system. In reality, of course, you don’t always get this luxury.

The /etc/fstab File

As mentioned earlier, /etc/fstab is a configuration file that mount can use. This file contains a list of all partitions known to the system. During the boot process, this list is read and the items in it are automatically mounted. Here’s the format of entries in the /etc/fstab file: /dev/device /dir/to/mount fstype parameters fs_freq fs_passno Here is a sample /etc/fstab file:

    /dev/hda2 / ext3 defaults 1 1
    /dev/hda8 /home ext3 defaults 1 2
    /dev/hda7 /tmp ext3 defaults 1 2
    /dev/hda5 /usr ext3 defaults 1 2
    /dev/hda6 /var ext3 defaults 1 2
    /dev/hda1 /usr/local ext3 defaults 1 2
    /dev/hda3 swap swap defaults 0 0
    /dev/fd0 /mnt/floppy ext3 noauto 0 0
    /dev/cdrom /mnt/cdrom iso9660 noauto,ro 0 0
    /dev/hdc /mnt/cdrom2 iso9660 noauto,ro 0 0
    none /proc proc defaults 0 0
    none /dev/pts devpts mode=0622 0 0

Let’s take a look at a few details of /etc/fstab that haven’t been mentioned yet, most notably the entry of swap for /dev/hda3, and the none for /proc and /dev/pts. In general, you’ll never have to touch these file systems once the system is installed, so don’t worry about them.

- The /dev/hda3 partition is where virtual memory resides. Unlike in Microsoft Windows and similar systems, in Linux the virtual memory can be kept on a separate partition from the root partition. This is done to improve performance, since the swap partition can obey rules different than a normal file system. Since the partition doesn’t need to be backed up or checked with fsck at boot time, the last two parameters on it are zeroed out. (Note that a swap partition can be kept in a normal disk file, as well. See the man page on mkswap for additional information.)

- The none entry in conjunction with /proc is for the /proc file system. This is a special file system that provides an interface to kernel parameters through what looks like any other file system. Although it appears to exist on disk, it really doesn’t—all the files represent something that is in the kernel. Most notable is /dev/kcore, which is the system memory abstracted as a file. People new to the /proc file system often mistake this for a large unnecessary file and accidentally remove it, which will cause the system to malfunction in many glorious ways. Unless you are sure you know what you are doing, it’s a safe bet to leave all the files in /proc alone.

- The last entry in a /etc/fstab file, /dev/pts, is for a new mechanism to improve implementation for network terminal support (ptys). This entry is necessary if you intend to allow remote login to your host via rsh, telnet, rlogin, or ssh.

When mounting partitions with the /etc/fstab configured, you can run the mount command with only one parameter: the directory you wish to mount to. The mount command checks /etc/fstab for that directory; if found, mount will use all parameters that have already been established there. For example, here’s the command to mount a CD-ROM given the /etc/fstab shown earlier:

    [root@tedford /root]# mount /mnt/cdrom

legal notice

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

Useful tools and features

Link to this article from your page    Send this article to you or to a friend
If you like this article (tutorial), please link to it from your web page using the information above.

related articles

1. Editing text filesin Linux Red Hat
Editing Text Files Editors are by the far the bulkiest of common tools, but they are also the most useful. Without them, making any kind of change to a text file would be a tremendous undertaking. Regardless of your Linux distribution, you will have gotten a few editors. You should take a few moments to get comfortable with them before you’re busy fighting another problem. NOTE Although all of the editors listed here come with Red Hat 8, not all of them are installed by default. ...

2. Reviewing Linux File System Standards
Reviewing Linux File System Standards One argument you hear regularly against Linux is that there are too many different distributions, and that multiple distributions lead to fragmentation. This fragmentation will eventuate in different, incompatible Linux versions. This is, without a doubt, complete nonsense that plays on “FUD” (Fear, Uncertainty, and Doubt). These types of arguments usually stem from a misunderstanding of the kernel and distributions. However, the Linux community has realized that it h...

3. Linux is built upon the foundation of file systems
File Systems Linux is built upon the foundation of file systems. They are the mechanisms by which the disk gets organized, providing all of the abstraction layers above sectors and cylinders. In this module, you’ll learn about the composition and management of these abstraction layers supported by the default Linux file system, ext2, and its more robust counterpart, ext3. This module covers the many aspects of managing disks. This includes creating partitions, establishing file systems, automating the process ...

4. The fsck tool short for File System ChecK
Using fsck The fsck tool, short for File System ChecK, is used to diagnose and repair file systems that may have become damaged in the course of daily operations. Such repairs are usually necessary after a system crash in which the system did not get a chance to fully flush all of its internal buffers to disk. (Although this tool’s name bears a striking resemblance to one of the expressions often uttered after a system crash, that this tool is part of the recovery process is strictly coincidence.) Us...

5. The process of adding a disk under Linux on the Intel
Adding and Partitioning a Disk The process of adding a disk under Linux on the Intel (x86) platform is relatively easy. Assuming you are adding a disk that is of similar type to your existing disks (e.g., adding an IDE disk to a system that already has IDE drives or adding a SCSI disk to a system that already has SCSI drives), the system should automatically detect the new disk at boot time, and all that is left is partitioning it and creating a file system on it. If you are adding a new type of disk (...

6. With the partitions created you need to put file systems on them
Syncing disks. WARNING: If you have created or modified any DOS 6.x partitions, please see the fdisk manual page for additional information. [root@tedford /root]# If you needed to write an /etc/fstab file yourself for this configuration, it would look something like this: /dev/hdb1 / ext3 defaults 1 1 /dev/hdb2 swap swap defaults 0 0 /dev/hdb3 /usr ext3 defaults 1 2 /dev/hdb5 /home ext3 defaults 1 2 /dev/hdb6 /var ext3 defaults 1 2 /dev/hdb...

7. Mounting NFS partitions works much the same way as mounting local partitions
Mounting NFS Partitions Mounting NFS partitions works much the same way as mounting local partitions. The only difference is in how the partition is addressed. On local disks, partitions are addressed by their device name, such as /dev/hda1. In NFS mounts, partitions are referenced by their hostnames and export directories. Thus, if the server named ungerer is allowing your host to mount the directory /export/SL1200/MK2 and you want to mount this to /projects/topsecret1, you would use this command:...

8. Using autofs to mount partitions across the network
Starting the autofs Service Because the way you’re using autofs to mount partitions across the network relies on NFS, before using it you’ll need to be sure you can do normal NFS mounts. Once you have this working, just make a simple change to your startup scripts to deploy the autofs service. The easiest way to do this is to run the redhat-config-services utility to enable the daemon. Simply start the configuration tool and mark the autofs check box as shown. If you need to start autofs by hand...