乐闻世界logo
搜索文章和话题

How to partition, format, mount, and manage disk space in Linux disk management and filesystem operations?

2月17日 23:38

Linux disk management and file systems are core skills for system administrators, involving disk partitioning, formatting, mounting, and maintenance operations.

Disk partitioning:

  • fdisk: traditional disk partitioning tool
    • View partitions: fdisk -l
    • Enter interactive mode: fdisk /dev/sdb
    • Common commands: n (new partition), p (print partition table), d (delete partition), w (save and exit), q (exit without saving)
  • parted: modern disk partitioning tool, supports GPT partition table
    • View partitions: parted -l
    • Enter interactive mode: parted /dev/sdb
    • Create partition: mkpart primary ext4 1MiB 100%
    • Set partition table: mklabel gpt
  • lsblk: list block device information
  • blkid: view block device UUID and filesystem type

File systems:

  • Common filesystem types: ext4, xfs, btrfs, ntfs, vfat
  • mkfs: create filesystem, e.g., mkfs.ext4 /dev/sdb1, mkfs.xfs /dev/sdb1
  • mkfs.ext4: create ext4 filesystem
  • mkfs.xfs: create xfs filesystem

Disk mounting:

  • mount: mount filesystem, e.g., mount /dev/sdb1 /mnt/data
  • umount: unmount filesystem, e.g., umount /mnt/data
  • View mount points: mount, df -h
  • /etc/fstab: automatic mount configuration file
    • Format: device mountpoint filesystem_type mount_options dump fsck
    • Example: /dev/sdb1 /mnt/data ext4 defaults 0 2
    • Use UUID: UUID=xxx /mnt/data ext4 defaults 0 2 (recommended)
    • View device UUID: blkid /dev/sdb1

Disk space management:

  • df: view disk space usage, df -h (human-readable format)
  • du: view directory or file size, du -sh directory (show total directory size)
  • du -h --max-depth=1 /: view size of directories under root
  • Clean disk space:
    • Clean package cache: apt clean (Debian/Ubuntu), yum clean all (CentOS/RHEL)
    • Clean old logs: logrotate
    • Find large files: find / -type f -size +100M

LVM (Logical Volume Management):

  • pvcreate: create physical volume, e.g., pvcreate /dev/sdb1
  • vgcreate: create volume group, e.g., vgcreate vgname /dev/sdb1
  • lvcreate: create logical volume, e.g., lvcreate -L 10G -n lvname vgname
  • lvextend: extend logical volume, e.g., lvextend -L +5G /dev/vgname/lvname
  • lvreduce: shrink logical volume, e.g., lvreduce -L -5G /dev/vgname/lvname
  • resize2fs: resize ext4 filesystem, e.g., resize2fs /dev/vgname/lvname
  • xfs_growfs: extend xfs filesystem
  • pvdisplay, vgdisplay, lvdisplay: view physical volume, volume group, logical volume information

RAID (Redundant Array of Independent Disks):

  • mdadm: manage software RAID
    • Create RAID 0: mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sdb1 /dev/sdc1
    • Create RAID 1: mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
    • Create RAID 5: mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1
    • View RAID status: cat /proc/mdstat, mdadm --detail /dev/md0

Disk performance optimization:

  • Use SSD: improve I/O performance
  • Adjust I/O scheduler: echo deadline > /sys/block/sda/queue/scheduler
  • Increase filesystem block size: mkfs.ext4 -b 4096 /dev/sdb1
  • Use noatime mount option: reduce disk writes
  • Enable filesystem journaling: improve data security

Disk troubleshooting:

  • View disk health: smartctl -a /dev/sda (requires smartmontools)
  • View disk I/O: iostat -x 1
  • View disk errors: dmesg | grep -i error
  • Repair filesystem: fsck /dev/sdb1 (requires unmounting first)
标签:Linux