Linux Disk Management: A Complete Process from Mounting to Daily Maintenance

Physical Device Recognition

In Linux, everything is a file. When you connect a new disk to the system, the Linux kernel detects this device through the udev event mechanism:

# View the disk devices recognized by the system
$ lsblk
$ ls -l /dev/sd*

Disk devices are typically represented by paths such as /dev/sda, /dev/sdb, or /dev/nvme0n1, depending on the interface type (SATA, SAS, NVMe, etc.).

Partition Planning

1. Choosing Partition Table Type

First, determine the type of partition table, which mainly includes two types:

  • MBR (Master Boot Record) – Traditional partition table, supports up to 4 primary partitions, with a maximum size of 2TB per partition.

  • GPT (GUID Partition Table) – Modern partition table, supports more than 128 partitions and partition sizes exceeding 2TB.

For modern systems, I strongly recommend using the GPT partition table.

2. Partitioning Tools

Commonly used partitioning tools include:

  • fdisk – Traditional tool suitable for MBR.

  • gdisk – Designed specifically for GPT.

  • parted – A powerful partitioning tool that supports both MBR and GPT.

  • cfdisk – Provides a TUI interface, making it easier to use.

Using parted as an example for partitioning:

# Create a GPT partition table
$ parted /dev/sdb mklabel gpt

# Create a partition (using 0% to 100% of the space)
$ parted /dev/sdb mkpart primary 0% 100%

3. Underlying Partition Implementation

When performing partitioning operations, the system writes partition table information at the beginning of the disk, and for GPT, it also writes a backup partition table at the end of the disk. The kernel identifies partition boundaries and types by reading this information.

Formatting (Creating File System)

After partitioning, a file system needs to be created on the partition:

1. Choosing a File System

Linux supports various file systems, each with its characteristics:

  • ext4 – Stable and reliable, the default choice for most Linux distributions.

  • XFS – High performance, suitable for large files and high IO scenarios.

  • Btrfs – A modern file system that supports advanced features like snapshots and data checksums.

  • ZFS – An enterprise-level file system that supports data compression and deduplication.

2. Formatting Operations

# Format as ext4 file system
$ mkfs.ext4 /dev/sdb1

# Format as XFS file system
$ mkfs.xfs /dev/sdb1

# Format as Btrfs file system
$ mkfs.btrfs /dev/sdb1

3. Underlying Implementation of Formatting

The formatting operation will:

  1. 1. Create a superblock on the partition to store file system metadata.

  2. 2. Create an inode table to store file metadata.

  3. 3. Create a data block bitmap to manage free and used space.

  4. 4. Create a journal area (for journaling file systems) to improve reliability.

Mounting

After formatting, the file system needs to be mounted into the directory tree:

1. Temporary Mounting

# Create a mount point
$ mkdir -p /mnt/data

# Temporary mount
$ mount /dev/sdb1 /mnt/data

# View mount information
$ mount | grep sdb1
$ df -h

2. Permanent Mount Configuration

Edit the /etc/fstab file and add:

# Device Identifier   Mount Point     File System Type   Mount Options        dump fsck
/dev/sdb1    /mnt/data   ext4          defaults,noatime  0    2

Modern systems recommend using UUID or partition labels to identify devices to avoid issues caused by device name changes:

# View device UUID
$ blkid /dev/sdb1

# Mount using UUID
UUID=1234-5678-90ab-cdef  /mnt/data  ext4  defaults,noatime  0  2

3. Mount Options

Common mount options and their effects include:

  • defaults – Default options (rw, suid, dev, exec, auto, nouser, async).

  • noatime – Do not update file access time, improving performance.

  • nodiratime – Do not update directory access time.

  • relatime – Update access time only when necessary.

  • data=ordered/journal/writeback – Journaling modes for ext file systems.

  • nofail – Do not report errors if the device does not exist at startup.

4. Underlying Implementation of Mounting

The mounting operation essentially associates the root directory of the file system with the mount point. The kernel abstracts different file system implementations through the VFS layer (Virtual File System), using a unified interface for access.

Daily Maintenance

1. Disk Monitoring

# View disk usage
$ df -h

# View directory size
$ du -sh /path/to/dir

# View disk IO status
$ iostat -x 1
$ iotop

# View file system status
$ tune2fs -l /dev/sdb1    # ext series
$ xfs_info /dev/sdb1      # XFS

2. File System Check and Repair

# Check ext4 file system (must unmount before executing)
$ umount /mnt/data
$ e2fsck -f /dev/sdb1

# Check XFS file system
$ xfs_repair /dev/sdb1

# Force check (add to the last field of fstab)
# ...  0  2   # Second priority check

3. Adjusting File System Parameters

# Adjust ext4 file system parameters
$ tune2fs -o journal_data_writeback /dev/sdb1

# Adjust XFS file system parameters
$ xfs_io -c "chattr +c" /mnt/data   # Enable compression

4. Disk Quota Management

# Enable quota
$ mount -o remount,usrquota,grpquota /mnt/data
$ quotacheck -cugm /mnt/data
$ quotaon /mnt/data

# Set quota
$ edquota -u username

5. Disk Expansion

After physical expansion, update the partition table:

# Update partition table
$ partprobe /dev/sdb

# Resize partition
$ parted /dev/sdb resizepart 1 100%

# Resize file system
$ resize2fs /dev/sdb1    # ext4
$ xfs_growfs /mnt/data   # XFS

6. Performance Optimization

File system level optimizations include:

# Adjust IO scheduler
$ echo deadline > /sys/block/sdb/queue/scheduler

# Adjust read-ahead size
$ blockdev --setra 8192 /dev/sdb

# Optimize mount options
$ mount -o remount,noatime,nodiratime,data=writeback /mnt/data

Advanced Techniques

1. LVM (Logical Volume Management)

LVM provides more flexible storage management:

# Create physical volume
$ pvcreate /dev/sdb1

# Create volume group
$ vgcreate data_vg /dev/sdb1

# Create logical volume
$ lvcreate -n data_lv -L 100G data_vg

# Format and mount
$ mkfs.ext4 /dev/data_vg/data_lv
$ mount /dev/data_vg/data_lv /mnt/data

2. RAID Configuration

Example of soft RAID configuration:

# Create RAID 1 array
$ mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1

# Format and mount
$ mkfs.ext4 /dev/md0
$ mount /dev/md0 /mnt/data

3. File System Snapshots

LVM snapshots:

# Create snapshot
$ lvcreate -s -n data_snap -L 10G /dev/data_vg/data_lv

# Mount snapshot
$ mount /dev/data_vg/data_snap /mnt/snap

Troubleshooting and Recovery

1. Data Recovery Tools

# Create disk image
$ dd if=/dev/sdb of=/path/to/backup.img bs=4M

# Use TestDisk to recover partition table
$ testdisk /dev/sdb

# Use PhotoRec to recover lost files
$ photorec /dev/sdb1

2. Common Problem Solutions

  • High Disk IO: Use iotop to identify processes and check ionice priority.

  • Disk Full: Use ncdu to locate large files, and lsof | grep deleted to find deleted but still open files.

  • Disk Errors: Check for errors in dmesg and /var/log/messages, and run smartctl -a /dev/sdb to check hardware health.

Best Practice Summary

  1. 1. Always use UUID: Device names may change, UUID is more reliable.

  2. 2. Avoid single partition with excessive capacity: Split into multiple logical volumes for easier management.

  3. 3. Regular backups: Especially for critical data and metadata.

  4. 4. Monitor disk health: Set up SMART monitoring and perform regular checks.

  5. 5. Balance performance and security: For example, noatime can improve performance but may sacrifice some security features.

  6. 6. Document all operations: Record all disk management operations, including partition schemes and mount options.

Disk management is a core aspect of Linux system administration. A deep understanding of its principles and best practices can help you manage systems more effectively, improving performance and reliability.

Leave a Comment