Unix - Filesystem Mounting and Unmounting in UNIX

 

1. Introduction

Filesystem mounting and unmounting are important concepts in UNIX operating systems. They allow users and administrators to access storage devices, disk partitions, removable media, and remote filesystems through a common directory structure.

In UNIX, all files and directories are organized into a single hierarchical structure that begins with the root directory, represented by /. Unlike operating systems that assign separate drive letters to storage devices, UNIX integrates different filesystems into this existing directory hierarchy.

Mounting is the process of attaching a filesystem to a specific directory so that its contents become accessible. Unmounting is the process of safely detaching that filesystem when it is no longer required.

For example, when a USB drive is connected to a UNIX computer, its filesystem may need to be mounted at a directory such as /mnt/usb. Once mounted, users can access the files stored on the USB drive through that directory.

2. What Is Filesystem Mounting?

Filesystem mounting is the process of making a filesystem accessible at a particular location within the UNIX directory tree. This location is called the mount point.

A filesystem may exist on a hard disk partition, USB drive, optical disc, network server, or another storage device. The operating system uses mounting to connect that filesystem to the directory hierarchy.

Consider a UNIX system with the following directory structure:

/
├── home
├── etc
├── usr
├── var
└── mnt
    └── usb

Suppose a USB drive contains files named notes.txt and report.pdf. When the USB filesystem is mounted at /mnt/usb, users can access these files through the paths /mnt/usb/notes.txt and /mnt/usb/report.pdf.

The files on the USB drive become part of the accessible directory hierarchy without requiring a separate drive letter.

Mounting does not normally copy the files into the mount-point directory. Instead, it connects the existing filesystem to that location so that the operating system can access its contents.

3. What Is a Mount Point?

A mount point is a directory in the existing filesystem where another filesystem is attached. It acts as the access location for the mounted storage.

For example, /mnt/data can be used as a mount point for an additional disk partition.

Before mounting, /mnt/data is simply a directory on the existing filesystem. After mounting, accessing /mnt/data displays the contents of the newly mounted filesystem.

A mount point should generally be an existing directory, and the user performing the operation must have the necessary permissions.

Common mount-point locations include:

  • /mnt — traditionally used for temporarily mounted filesystems.

  • /media — commonly used for removable media on many Linux distributions.

  • /home — may be used as a mount point for a separate filesystem containing user directories.

  • /backup — may be used to access a dedicated backup disk.

  • /data — may be used for application data stored on a separate partition.

The exact directory conventions depend on the UNIX or UNIX-like operating system being used.

An important point is that mounting a filesystem over a directory temporarily hides the directory's previous contents while the filesystem remains mounted. Those underlying files are not automatically deleted; they become visible again after unmounting.

4. Types of Filesystems That Can Be Mounted

UNIX supports different types of filesystems depending on the operating system, available drivers, and storage configuration.

4.1 Local Filesystems

Local filesystems are stored on devices directly connected to the computer, such as internal hard drives, solid-state drives, and USB storage devices.

Examples include UFS, commonly associated with BSD and other UNIX systems, and ext4, commonly used on Linux systems. The supported filesystem types vary by platform.

4.2 Removable Filesystems

Removable filesystems are stored on devices such as USB drives and memory cards. They can be mounted when needed and unmounted before the device is physically removed.

4.3 Network Filesystems

Network filesystems allow users to access files stored on remote computers through a network.

For example, Network File System (NFS) allows a UNIX machine to mount a directory exported by another server. After mounting, remote files can be accessed through a local directory path, subject to network availability and permissions.

4.4 Special and Virtual Filesystems

Some filesystems provide access to operating-system information rather than ordinary files stored on a disk.

For example, Linux provides virtual filesystems such as procfs and sysfs for exposing process and kernel information. These are platform-specific examples rather than universal features of every UNIX implementation.

5. The mount Command in UNIX

The mount command is used to attach a filesystem to a directory in the filesystem hierarchy. Depending on the UNIX variant, the command may require administrator privileges.

A general form of the command is:

mount device mount_point

Here:

  • mount is the command used to attach a filesystem.

  • device identifies the storage device or filesystem source.

  • mount_point specifies the directory where the filesystem should become accessible.

The precise syntax differs between UNIX variants. Some systems infer the filesystem type automatically, while others may require additional options.

Example 1: Mounting a Filesystem

sudo mount /dev/sdb1 /mnt/data

In this Linux-style example, /dev/sdb1 represents a disk partition, and /mnt/data is the mount point.

After a successful mount, the contents of that partition can be accessed through /mnt/data.

The device name is only an example. Users should identify the correct device before executing a mount command because device names vary between systems.

Example 2: Specifying a Filesystem Type

sudo mount -t ext4 /dev/sdb1 /mnt/data

The -t option specifies the filesystem type. In this example, the filesystem is identified as ext4.

This option is useful when the filesystem type cannot be inferred automatically or when explicit specification is required.

Example 3: Checking Mounted Filesystems

mount

Running this command without arguments commonly displays information about currently mounted filesystems, including their sources, mount points, and filesystem types.

On Linux, the following command is another useful option:

findmnt

It displays mounted filesystems in a structured format that makes the relationship between devices and mount points easier to understand.

6. What Is Filesystem Unmounting?

Unmounting is the process of detaching a mounted filesystem from the directory hierarchy. It is generally performed when a storage device is no longer needed or before removing a removable storage device.

Unmounting helps ensure that pending filesystem operations are completed and that the operating system can safely stop accessing the device.

For example, after copying documents to a USB drive mounted at /mnt/usb, the user should unmount it before physically disconnecting it.

If a filesystem is removed without proper unmounting, pending writes may not be completed. This can result in missing files, corrupted data, or filesystem inconsistencies.

Unmounting does not normally delete the data stored on the device. It simply disconnects the filesystem from its mount point.

7. The umount Command in UNIX

The umount command is used to detach a mounted filesystem. Notice that the command is spelled umount, without the letter n after the initial u.

Its general syntax is:

umount mount_point

Alternatively, the filesystem may be identified by its device name, depending on the system.

Example 1: Unmounting a USB Drive

sudo umount /mnt/usb

This command attempts to detach the filesystem mounted at /mnt/usb.

After a successful unmount, the USB drive's contents are no longer accessible through that mount point. The directory itself remains present, although its original contents become visible again if it contained any before mounting.

Example 2: Unmounting by Device Name

sudo umount /dev/sdb1

This example identifies the filesystem by its device name. The device must correspond to a mounted filesystem, and the command must be used according to the system's supported syntax.

Example 3: Checking Before Removal

findmnt /mnt/usb

On Linux, this command can be used to inspect whether a filesystem is mounted at the specified location. If the mount is no longer present, the command will not show an active mount at that path.

8. Step-by-Step Process of Mounting and Unmounting

The following example demonstrates a typical workflow for accessing a USB storage device on a Linux system. Actual device names and procedures vary across UNIX platforms.

Step 1: Identify the storage device.

Use a suitable system utility to identify the device and its partitions. For example:

lsblk

This Linux command lists block devices and helps distinguish disks from their partitions.

Step 2: Create a mount-point directory.

sudo mkdir -p /mnt/usb

This creates the /mnt/usb directory if it does not already exist.

Step 3: Mount the filesystem.

sudo mount /dev/sdb1 /mnt/usb

This attaches the filesystem on the selected partition to the specified directory, assuming the device and filesystem are valid.

Step 4: Access the files.

ls /mnt/usb

The command displays the directory contents of the mounted filesystem.

Step 5: Finish using the device.

Close files and applications that are using the mounted filesystem. Make sure that copying or writing operations have finished.

Step 6: Unmount the filesystem.

sudo umount /mnt/usb

This detaches the filesystem from the mount point.

Step 7: Remove the device safely.

After confirming that unmounting has succeeded, disconnect the removable device. Some systems also provide a separate power-off or safe-removal operation for external storage.

9. Common Problems During Mounting and Unmounting

Filesystem operations may fail for several reasons. Understanding these problems helps administrators troubleshoot storage access issues.

9.1 Mount Point Does Not Exist

A mounting operation may fail if the specified mount-point directory does not exist.

For example, attempting to mount a filesystem at /mnt/archive may fail if that directory has not been created.

The directory can be created using:

sudo mkdir -p /mnt/archive

The mounting command can then be attempted again with the correct device and filesystem details.

9.2 Permission Denied

Mounting and unmounting filesystems often require administrator privileges. If an ordinary user does not have the necessary permissions, the operation may fail.

Using sudo on systems that support it may resolve the issue, provided the user is authorized to perform the operation.

9.3 Device or Filesystem Not Found

A mounting operation may fail when the device name is incorrect, the device is disconnected, or the specified filesystem cannot be detected.

Administrators should verify the available devices and partitions before attempting to mount them.

9.4 Filesystem Is Busy

An unmounting operation may fail if a process is currently accessing the filesystem.

For example, a terminal session whose current working directory is /mnt/usb, or an application reading a file from that directory, may prevent a normal unmount.

The user should close applications accessing the filesystem and move terminal sessions outside the mount point before trying again.

On Linux, the following command can help identify processes using a mount point:

sudo fuser -vm /mnt/usb

The output can help locate processes that may be preventing unmounting. Processes should be stopped carefully rather than terminated indiscriminately.

9.5 Filesystem Errors

A damaged filesystem may fail to mount or may be mounted with restricted access. Administrators should examine the relevant system messages and use appropriate filesystem checking or repair utilities when necessary.

Filesystem repair should be performed using the correct tool for the filesystem type, and repair operations generally require the filesystem to be unmounted.

10. Difference Between Mounting and Unmounting

Mounting

Unmounting

Connects a filesystem to the directory hierarchy.

Detaches a filesystem from the directory hierarchy.

Makes the filesystem's contents accessible through a mount point.

Stops access to the filesystem through that mount point.

Commonly performed before using a storage device.

Commonly performed after finishing with a storage device.

Uses the mount command.

Uses the umount command.

May fail if the device, filesystem, or mount point is invalid.

May fail if processes are still using the filesystem.

11. Importance of Filesystem Mounting and Unmounting

Filesystem mounting and unmounting are essential for efficient storage management in UNIX environments.

First, mounting allows administrators to integrate multiple storage devices into a unified directory structure. This makes it easier to organize application data, user files, backups, and shared resources.

Second, mounting enables access to remote filesystems, allowing users and applications to work with data stored on other computers.

Third, unmounting helps protect data integrity by ensuring that the operating system can complete pending filesystem operations before a storage device is disconnected.

Fourth, these operations support flexible system administration. Administrators can attach additional storage when required and detach it when it is no longer needed.

Finally, understanding mounting and unmounting helps users diagnose storage-related problems, manage removable devices safely, and maintain reliable UNIX systems.

Conclusion

Filesystem mounting and unmounting are fundamental storage-management operations in UNIX. Mounting makes a filesystem accessible through a directory called a mount point, while unmounting safely detaches it from the directory hierarchy. The mount and umount commands are commonly used to perform these operations, although their exact options and behavior depend on the operating system.

A clear understanding of these concepts helps users access local and network storage, troubleshoot filesystem errors, and prevent data loss when disconnecting storage devices.