Last modified: June 11, 2024

This article is written in: πŸ‡ΊπŸ‡Έ

Mounting and Unmounting

Mounting is the process of making a file system, disk, DVD, or USB drive accessible to the operating system so that you can read and write data on it. In Linux, you need to mount these devices before using them.

Unmounting is the process of disconnecting a mounted file system from the operating system. This should be done before physically disconnecting a device to prevent data loss or corruption.

Use the mount and umount commands to mount and unmount file systems.

+-------------------------------------+        +--------------------------------------+
|          Operating System           |        |            File System              |
|          (Linux Environment)        |        | (e.g., Disk, DVD, USB Drive)        |
+-------------------------------------+        +--------------------------------------+
        | ^                                         | ^
        | | Mounting                                | | Data Access
        | | (make accessible)                       | | (read/write)
        | |                                         | |
        v |                                         v |
+-------------------------------------+        +--------------------------------------+
|            mount command            | <----> |           Mounted State             |
+-------------------------------------+        +--------------------------------------+
        | ^                                         | ^
        | | Unmounting                              | | Data Protection
        | | (disconnect safely)                     | | (prevent loss/corruption)
        | |                                         | |
        v |                                         v |
+-------------------------------------+        +--------------------------------------+
|           umount command            | <----> |          Unmounted State            |
+-------------------------------------+        +--------------------------------------+

Verifying Drive Visibility

When working with different drives on a Linux-based system, it's important to verify whether these drives are visible to the operating system. Checking drive visibility helps confirm that the drive is properly connected and recognized by the system, which is a prerequisite for actions like mounting or partitioning.

Drive visibility indicates that the system can interact with the drive, access its metadata, and perform operations such as reading or writing data.

One common method to check drive visibility is by using the fdisk command, which provides various disk management tasks. When used with the -l option, fdisk lists all the accessible disk drives, regardless of their mount status.

Here is the command:

sudo fdisk -l

You need superuser or root privileges to execute this command, hence the sudo prefix.

The output will display a list of all the disk drives, their partitions, and relevant details like size, type, and partition scheme. Drives are usually named in the format of /dev/sdX or /dev/nvmeXnY, where X and Y are letters or numbers corresponding to the drive and partition number respectively.

If the drive you're interested in appears in this list, it means it's visible to the operating system and ready for further operations like mounting.

However, keep in mind that visibility doesn't necessarily mean the drive is in a healthy state. Tools like smartctl from the smartmontools package can be used for checking drive health and SMART (Self-Monitoring, Analysis, and Reporting Technology) status.

Mounting File Systems

Mounting a file system is an essential process in Linux, making the file system or a storage device (like a hard disk, CD-ROM, or USB drive) accessible for reading and writing data. Once a file system is mounted, it's integrated into the system's directory tree and can be accessed from the assigned mount point (a directory on your system).

File System /dev/sdb1
         |
         |  mount /dev/sdb1 /mnt/mydrive
         |
         v
 +-------------------------------+
 |  Linux System's Directory Tree |
 |                               |
 |   /                           |
 |   β”œβ”€β”€ home                    |
 |   β”œβ”€β”€ var                     |
 |   β”œβ”€β”€ etc                     |
 |   β”œβ”€β”€ ...                     |
 |   β”œβ”€β”€ mnt                     |
 |   β”‚   β”œβ”€β”€ ...                 |
 |   β”‚   β”œβ”€β”€ mydrive  <----------|------ Mounted here
 |   β”‚   β”œβ”€β”€ ...                 |
 |   β”œβ”€β”€ ...                     |
 +-------------------------------+

How to Mount a File System

The basic syntax for mounting a file system in Linux is as follows:

mount -t file_system_type source_location target_location

For example, to mount an ext4 file system located at /dev/sdb1 to the /mnt/shared directory, you would use:

mount -t ext4 /dev/sdb1 /mnt/shared

If you are unsure of the file system type, you can omit the -t option and Linux will attempt to determine the type automatically:

mount /dev/sdb1 /mnt/shared

Viewing All Mounted File Systems

To see a list of all currently mounted file systems, you can use the mount command with no parameters. This command will output information about all mounted file systems including their type, mount point, and mount options:

mount

Example Output:

/dev/sda1 on / type ext4 (rw,relatime,data=ordered)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
/dev/sdb1 on /mnt/external type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,errors=remount-ro)

Output Explanation:

Why It's Useful:

Unmounting File Systems

Unmounting a file system is the process of detaching it from the system's directory tree. Once a file system is unmounted, files cannot be accessed from that file system until it is mounted again.

This is an essential process because it ensures that all pending read/write operations are completed and all data cached in memory is written to disk. This helps prevent potential data loss or corruption.

How to Unmount a File System

To unmount a file system, you use the umount command followed by the mount point or the device name:

umount /mnt/shared

In this command, /mnt/shared is the mount point of the file system. This command will disconnect the file system from the directory tree.

Troubleshooting Unmounting Issues

Sometimes, you may encounter an error message indicating that the device is busy when you try to unmount a file system. This typically means some processes are still using the file system, preventing it from being unmounted.

To find out which processes are using the file system, you can use the lsof command (short for "list of open files") and filter the results with grep:

lsof | grep /mnt/shared

This command will list all processes currently accessing /mnt/shared. If a process appears with, for example, the ID 3528, you can stop this process with the kill command:

kill 3528

Then, you can retry the umount command.

Lazy Unmounting

If you still can't unmount the file system, you can use a "lazy" unmount with the -l option. This tells the system to unmount the file system as soon as it is not busy:

umount -l /mnt/shared

This is a powerful option and should be used with caution. When used, it might appear as if files have been unmounted, but in reality, their unmounting is only deferred until they are no longer in use.

Mounting an ISO Image

An ISO image is a disk image of an optical disc. In other words, it is a file that contains the exact contents, including the file system, of an optical disc such as a CD, DVD, or Blu-ray Disc. ISO images are often used for archival purposes, distribution of media, or for creating a backup copy of a disc.

In Linux, you can mount an ISO image to make its contents accessible just as you would with a physical disc.

ISO File (file.iso)
         |
         |  mount -o loop file.iso /mnt/iso
         |
         v
+-------------------------------+
|  Linux System's Directory Tree |
|                               |
|   /                           |
|   β”œβ”€β”€ home                    |
|   β”œβ”€β”€ var                     |
|   β”œβ”€β”€ etc                     |
|   β”œβ”€β”€ ...                     |
|   β”œβ”€β”€ mnt                     |
|   β”‚   β”œβ”€β”€ ...                 |
|   β”‚   β”œβ”€β”€ iso  <--------------|------ Mounted here
|   β”‚   β”œβ”€β”€ ...                 |
|   β”œβ”€β”€ ...                     |
+-------------------------------+

How to Mount an ISO Image

The process of mounting an ISO image is a bit different from mounting physical devices. The loop device is a pseudo-device that makes a file accessible as a block device, and it's used to mount files like ISO images that contain a file system within them.

Here's the general command for mounting an ISO image:

mount -o loop file.iso /mnt/iso

In this command:

For example, to mount an ISO image called image.iso in the current directory to the mount point /mnt/iso, you would use:

mount -o loop image.iso /mnt/iso

Checking the Contents of the ISO Image

After the ISO image is mounted, you can navigate to the mount point and inspect its contents just like a regular directory. For instance:

cd /mnt/iso
ls

This command will display the list of files and directories stored in the ISO image.

Remember to unmount the ISO image once you're done with it using the umount command followed by the mount point:

umount /mnt/iso

Challenges

I. Recognize Devices

II. Manual Mounting

III. Accessing Mounted Files

IV. Unmounting

V. Create a Virtual Disk File

Table of Contents

  1. Mounting and Unmounting
  2. Verifying Drive Visibility
  3. Mounting File Systems
    1. How to Mount a File System
    2. Viewing All Mounted File Systems
  4. Unmounting File Systems
    1. How to Unmount a File System
    2. Troubleshooting Unmounting Issues
    3. Lazy Unmounting
  5. Mounting an ISO Image
    1. How to Mount an ISO Image
    2. Checking the Contents of the ISO Image
  6. Challenges