Linux Mounting and Unmounting a Filesystem 🐧📂

🔄 Mounting a Filesystem

  • Objective: To interact with a disk, you need to mount its filesystem to a directory.

  • Why Not Direct Access?:

    • You can't cd into a device file (e.g., /dev/sdb) because it's not a directory but a device node.

  • Steps:

    1. Create a Mount Point:

      • Example: sudo mkdir /mnt/my_usb

    2. Mount the Filesystem:

      • Command: sudo mount /dev/sdb1 /mnt/my_usb

    3. This allows you to read from and write to the disk through /mnt/my_usb.

🔄 Unmounting a Filesystem

  • Objective: To safely disconnect a disk or remove its filesystem.

  • Steps:

    • Command: sudo umount /mnt/my_usb or sudo umount /dev/sdb1

    • It's crucial to unmount before physically disconnecting the drive to avoid file system errors.

⚙️ Automatic Mounting

  • Temporary Mount Points:

    • Created with the mount command; they disappear after reboot.

  • Permanent Mounting:

    • Modify /etc/fstab to automatically mount filesystems at boot.

    • Steps:

      1. Get UUID: Use sudo blkid to find the UUID of your device.

      2. Edit /etc/fstab:

        • Add an entry with the UUID, mount point, filesystem type, and options.

        • Example entry: UUID=your-uuid /mnt/my_usb ext4 defaults 0 2

⚠️ Cautions

  • Unmounting: Always unmount before disconnecting to prevent data corruption.

  • Temporary Mount Points: Disappear on reboot; use /etc/fstab for permanent mounts.

🛠️ Summary

  1. Mounting: Create a directory and use mount to access the filesystem.

  2. Unmounting: Use umount to safely disconnect.

  3. Automatic Mounting: Modify /etc/fstab to ensure automatic mounting on boot.

By following these steps, you can manage disk filesystems efficiently and avoid common issues related to data access and integrity. 🚀🖥️


Supplemental Reading: Mounting and Unmounting a Filesystem in Linux 🐧📁

🔄 Mounting and Unmounting Filesystems

  • Mounting: Connects a physical storage device (e.g., hard drives, CD/DVD drives) to a directory in the filesystem, known as the mount point.

  • Unmounting: Safely disconnects the filesystem from the mount point.

🗃️ File System Table (fstab)

The fstab file simplifies the process of mounting and unmounting filesystems by automating the mounting of partitions during boot. It contains the following columns:

  1. Device:

    • UUID or device name (e.g., sda1, sdb1).

  2. Mount Point:

    • Directory where the device is mounted (e.g., /mnt/my_usb).

  3. File System Type:

    • Linux file systems (e.g., ext4, swap, ntfs).

  4. Options:

    • Mounting options (e.g., rw, noexec, auto).

  5. Backup Operation:

    • Historically used for backups (now deprecated; use 0 for off and 1 for on).

  6. File System Check Order (fsck):

    • 0: Do not check.

    • 1: Check first (root filesystem).

    • 2: Check after root filesystem.

Example:

<File System>  <Mount Point>  <Type>  <Options>  <Dump>  <Pass>
/dev/sda1      /              ext3    nouser     0       1
/dev/sda2      swap           swap   defaults   0       0
/dev/hda1      /mnt/shared    nfs    rw,noexec  0       2

⚙️ fstab Options

  • sync/async: Synchronous/asynchronous operations.

  • auto/noauto: Auto-mount at boot.

  • dev/nodev: Device driver use.

  • exec/noexec: Binary execution.

  • ro/rw: Read-only/read-write.

  • user/users/nouser: User permissions for mounting.

  • defaults: Use default settings (rw, suid, dev, exec, auto, nouser, async).

✍️ Editing the fstab Table

Steps:

  1. Format and Partition the Drive:

    • Use fdisk to format the drive with a compatible file system (e.g., ext4).

  2. Identify Block Devices:

    • Use lsblk to list block devices and partitions.

    • Example output:

      NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
      sda     8:0    0  512G  0 disk 
      └─sda1  8:1    0  1G    0 part /boot
      sdb     8:16   0  1T    0 disk 
      └─sdb1  8:17   0  128G  0 part
  3. Edit the fstab File:

    • Add new entry for the partition.

    • Example:

      <File System>  <Mount Point>  <Type>  <Options>  <Dump>  <Pass>
      /dev/sda1      /              ext3    nouser     0       1
      /dev/sdb1      /mnt/mystorage ext4    defaults   0       2
  4. Reboot:

    • Check the mount point to ensure the new partition is available.

By understanding and using the fstab file effectively, IT professionals can manage disk mounting and unmounting tasks efficiently in Linux systems. 🔧💻

Last updated