In Linux systems, disk partitioning and mounting are fundamental operations for managing storage space. Imagine your computer’s hard drive as a large wardrobe; partitioning divides it into smaller compartments, while mounting connects these compartments to a directory (like a wardrobe entrance), allowing the system to recognize and use the space. Let’s quickly master this skill with the simplest steps!

Step 1: Check Your Disk “Inventory”

Before starting, confirm the disks and existing partitions recognized by the system.

  • View Disk List: Use lsblk or fdisk -l (requires root privileges, e.g., sudo fdisk -l).
    In the output, /dev/sda represents the first SATA hard drive, and /dev/sda1 is its first partition (the number 1 denotes the partition number; primary/extended partitions are other types).

  • View Mounted Partitions: Run df -h to see currently mounted partitions (e.g., /dev/mapper/cl-root is the root partition).

Step 2: “Cut the Cake” for the Disk — Create a New Partition

Assume you have a new disk /dev/sdb (unpartitioned) or want to add a partition to /dev/sda. Follow these steps:

  1. Enter Partition Tool: Use fdisk (requires root):
    For example, sudo fdisk /dev/sdb (back up data before operating!).

  2. Create a New Partition:
    - Type n (New) → Press p to select “Primary Partition” (recommended for beginners; e is for extended partitions, which we can ignore for now) → Press Enter (accept default partition type) → Specify the partition size (or press Enter to use the remaining space).
    - To specify a size (e.g., 20GB), enter +20G (units: G/M/K).

  3. Save and Exit: Type w (Write) and press Enter to confirm. The partition is now created!

Step 3: “Dress the Partition” — Format the Partition

After creating a partition, the system cannot use it directly. Formatting (like painting the wardrobe compartment) is required.

  • Choose a File System: Linux commonly uses ext4 (similar to Windows NTFS). Run:
    sudo mkfs.ext4 /dev/sdb1 (replace /dev/sdb1 with your new partition).

  • Warning: Formatting will erase data! Back up data before formatting!

Step 4: “Hang” the Partition in the System — Temporary Mount

To make the system accessible, mount the partition to a directory (e.g., /mnt/mynewdisk):

  1. Create a Mount Point Directory:
    Create an empty directory as the mount point:
    sudo mkdir /mnt/mynewdisk.

  2. Mount the Partition:
    Use the mount command:
    sudo mount /dev/sdb1 /mnt/mynewdisk.
    - Now, accessing /mnt/mynewdisk will show the new partition’s contents.
    - Verify: Run df -h to confirm /dev/sdb1 is mounted at /mnt/mynewdisk.

Step 5: Permanent Mount — Remain Effective After Reboot

Temporary mounting only works during the current session. To make it persistent, modify the /etc/fstab file:

  1. Get the Partition UUID (more stable, avoiding device name changes):
    Run sudo blkid /dev/sdb1 and find the UUID="xxxx-xxxx-xxxx-xxxx" (e.g., UUID="a1b2c3d4-5678-90ef-ghij").

  2. Edit the fstab File:
    Open /etc/fstab with sudo nano /etc/fstab and add the following line at the end:

   UUID=a1b2c3d4-5678-90ef-ghij /mnt/mynewdisk ext4 defaults 0 0
  • Format Explanation:
    [UUID/Device Name] [Mount Point] [File System] [Mount Options] [dump] [fsck Order]
    • defaults: Default mount options (read/write, no compression, etc.). Adjust as needed.
  1. Verify Mounting:
    Run sudo mount -a; no errors mean the configuration is correct.

Summary & Notes

  • Key Steps: Partition (create) → Format (paint) → Mount (connect) → Persist (fstab).
  • Risk Warning: Always back up data before partitioning/formatting to avoid data loss from accidental deletion or formatting.
  • Unmount a Partition: Use sudo umount /mnt/mynewdisk (ensure the directory is not in use).

With these steps, you’ve successfully partitioned and mounted a Linux disk. Feel free to leave questions in the comments! See you next time!

Xiaoye