Mounting and Unmounting Local Disks

an article added by: Daniel K. at 11282007


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

French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic

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 disclaimer

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.

related articles

1. The df program displays the amount of free space
Disk Free: df The df program displays the amount of free space, partition by partition. The drives/partitions must be mounted in order to get this information. NFS information can be gathered this way, as well. Some parameters for df are listed here; additional (rarely used) options are listed in the df manual page. To show the free space in a human-readable format for the file system on which /tmp is located, type this command: [michael@workbox michael]$ df -h /tmp ...

2. The top command is an interactive version of ps
Showing an Interactive List of Processes: top The top command is an interactive version of ps. Instead of giving a static view of what is going on, top refreshes the screen with a list of processes every two to three seconds (user adjustable). From this list, you can reprioritize processes or kill them. Linux tutorial 6-1 shows a top screen. The top program’s main disadvantage is that it’s a CPU hog. On a congested system, this program tends to complicate system management issues. Users start running...

3. Lunux who command who is logged in
Learning Who Is Logged In: who On systems that allow users to log in to other users’ machines or special servers, you will want to know who is logged in. You can generate such a report by using the who command: [michael@workbox michael]$ who The who report looks like this: michael tty1 Jun 14 18:22 rwhite pts/9 Jun 14 18:29 (:0) root pts/11 Jun 14 21:12 (:0) root pts/12 Jun 14 23:38 (:0) A Variation on who: w The w command di...

4. X offers the ability to display content on other hosts
Remote X Displays In Linux tutorial 1, I said that X offers the ability to display content on other hosts’ screens. This makes it possible to log in to a server that has a special program on it and have the display redirected to the screen in front of you. Allowing this to happen is a two-step process. The first step is telling your machine that it should allow connections from another host for X displays. This is done with the xhost command: [michael@workbox michael]$ xhost +ungerer...

5. 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. ...

6. 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...

7. 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 ...

8. 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...