A Comprehensive Guide to Disk and Partition Management in Linux Systems, Covering Core Operations like Viewing, Managing, and Optimizing

1. Viewing Basic Disk Information

1. List All Disk Devices

lsblk           # Tree view of disks and partitions
lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT  # Custom columns
sudo fdisk -l   # Detailed partition table information (requires root)

Example Output:

NAME        SIZE FSTYPE MOUNTPOINT
sda         100G        
├─sda1      512M vfat   /boot
└─sda2      99.5G ext4   /
nvme0n1     500G
├─nvme0n1p1 100G ext4   /data
└─nvme0n1p2 400G swap

2. Check Disk Space Usage

df -hT          # Human-readable format (-h), show filesystem type (-T)
df -i           # Check inode usage
du -sh /*       # Analyze directory sizes under root

3. Disk Model and SMART Information

sudo hdparm -I /dev/sda     # View detailed disk information
sudo smartctl -a /dev/nvme0n1  # Check SSD health status (requires smartmontools)

2. Partition Management Operations

1. Create a New Partition

sudo fdisk /dev/sdb   # Interactive partitioning tool
Common operations:
  n → Create new partition
d → Delete partition
  p → Print partition table
  w → Save and exit

2. GPT Partition Table Operations (for large capacity disks)

sudo gdisk /dev/sdc   # Similar to fdisk but supports GPT
sudo parted /dev/sdd  # More powerful partitioning tool

3. Format a Partition

# Common filesystems
sudo mkfs.ext4 /dev/sdb1      # Create ext4 filesystem
sudo mkfs.xfs /dev/sdb2       # Create XFS filesystem
sudo mkswap /dev/sdb3         # Create swap partition

# Format with label
sudo mkfs.ext4 -L "DATA" /dev/sdb1

4. Mount/Unmount Partitions

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

# Permanent mount (edit /etc/fstab)
echo '/dev/sdb1  /data  ext4  defaults  0 2' | sudo tee -a /etc/fstab
sudo mount -a  # Test fstab configuration

# Unmount
sudo umount /mnt/data

3. Disk Performance Testing and Optimization

1. Benchmark Testing

# Test sequential read/write (1G file, block size 1M)
sudo hdparm -tT /dev/sda

# Comprehensive test using fio (requires installation)
fio --name=test --filename=/mnt/test.file --size=1G --rw=read --direct=1 --bs=4k

2. I/O Monitoring

iotop -oPa           # Real-time disk I/O process ranking
iostat -xz 1         # Extended statistics, refresh every second
vmstat -d            # Disk activity statistics

3. Optimization Suggestions

  • SSD Optimization:

    sudo tune2fs -o discard /dev/nvme0n1p1  # Enable TRIM
    sudo fstrim -v /                         # Manually execute TRIM

  • Mechanical Hard Drive Scheduler:

    echo 'deadline' | sudo tee /sys/block/sda/queue/scheduler

4. RAID Management

1. Create Software RAID

# Create RAID1 (mirroring)
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

# View RAID status
cat /proc/mdstat
sudo mdadm --detail /dev/md0

2. RAID Monitoring

watch -n 1 cat /proc/mdstat  # Real-time monitoring of rebuild progress

5. LVM Logical Volume Management

1. Basic Operation Flow

# Create physical volume
sudo pvcreate /dev/sdb

# Create volume group
sudo vgcreate vg_data /dev/sdb

# Create logical volume
sudo lvcreate -L 50G -n lv_www vg_data

# Extend logical volume
sudo lvextend -L +10G /dev/vg_data/lv_www
sudo resize2fs /dev/vg_data/lv_www  # Adjust filesystem

2. Common Commands

pvdisplay       # View physical volume
vgdisplay       # View volume group
lvdisplay       # View logical volume
vgs --units g   # Display volume group space in GB

6. Data Security Operations

1. Disk Cloning

sudo dd if=/dev/sda of=/dev/sdb bs=4M status=progress
# Or use a safer tool
sudo apt install clonezilla

2. Data Erasure

# Securely erase entire disk
sudo shred -v -n 1 /dev/sdb

# Quickly clear disk
sudo blkdiscard /dev/sdb  # Only available for SSDs

3. Bad Sector Detection

sudo badblocks -v /dev/sdb > bad-blocks.txt
sudo smartctl -t long /dev/sda  # Deep SMART test

7. Troubleshooting Guide

1. Filesystem Repair

# Check ext4 filesystem
sudo fsck -y /dev/sdb1

# Force unmount damaged partition
sudo umount -l /mnt/corrupt

2. Disk Space Anomaly Troubleshooting

# Find large files
sudo find / -type f -size +100M -exec ls -lh {} 

# Find deleted files still occupying space
lsof | grep deleted

3. Handling Mount Failures

# View mount error logs
dmesg | grep mount
journalctl -b | grep mount

# Fix fstab errors
sudo mount -o remount,rw /

8. Common Command Quick Reference

Scenario

Command

View All Disks

<span>lsblk -d -o NAME,SIZE,RO,TYPE,MOUNTPOINT</span>

View Partition Table

<span>sudo parted -l</span>

Test Disk Speed

<span>sudo hdparm -tT /dev/sda</span>

Monitor Real-time I/O

<span>iostat -xmdz 1</span>

Extend LVM Partition

<span>lvextend -r -L +10G /dev/vg/root</span>

Repair Filesystem

<span>fsck -y /dev/sdb1</span>

9. Precautions

  1. Backup Before Operations: Partition/formatting operations are irreversible, always back up important data in advance

  2. Caution in Production Environment: Avoid performing disk maintenance during peak business hours

  3. Special Handling for SSDs: Avoid frequent writes, regularly execute TRIM

  4. RAID/LVM Planning: Choose the appropriate solution based on business needs (RAID10 is suitable for high I/O, LVM is convenient for expansion)

By mastering these commands, you can:

  • Efficiently manage server disk resources

  • Quickly identify storage performance bottlenecks

  • Safely implement disk expansion and maintenance

  • Build a reliable storage architecture

Leave a Comment