Disk Management: Partitioning and Expansion Guide
Linux disk management: analyze usage with df/du, partition with fdisk/parted, manage volumes with LVM, extend partitions, and configure persistent mounts.
Table of Contents
Disk Management: Partitioning and Expansion Guide
Disk management is critical for server health and performance. This guide covers monitoring disk usage, partitioning tools, LVM (Logical Volume Manager) basics, partition expansion, and mounting.
Disk operations can be irreversible. Back up important data before proceeding.
Monitoring Disk Usage
df — Filesystem Usage
# Show all filesystems
df -h
# Show filesystem for a specific directory
df -h /var
# Show inode usage
df -i
# Include filesystem type
df -hT
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 28G 42% /
/dev/sdb1 100G 45G 50G 47% /data
tmpfs 2.0G 0 2.0G 0% /dev/shm
du — Directory Usage
# Show directory size
du -sh /var/log
# Show subdirectories
du -h --max-depth=1 /var
# Find the largest directories
du -h /var | sort -rh | head -20
# Find files larger than 100 MB
find / -size +100M -type f 2>/dev/null
Viewing Disk and Partition Information
# List all block devices
lsblk
# With detailed info
lsblk -f
# Show disk information
sudo fdisk -l
# For a specific disk
sudo fdisk -l /dev/sda
# Show disk UUIDs
blkid
Partitioning with fdisk
fdisk is an interactive tool for MBR (Master Boot Record) disk partitioning.
# Start fdisk
sudo fdisk /dev/sdb
Commands inside fdisk:
m — Help menu
p — Print partition table
n — New partition
d — Delete partition
t — Change partition type
w — Write changes and exit
q — Quit without saving
Creating a New Partition (fdisk)
sudo fdisk /dev/sdb
# n → new partition
# p → primary partition
# 1 → partition number
# Enter → default start sector
# +50G → 50 GB size
# w → write
# Format the partition
sudo mkfs.ext4 /dev/sdb1
# Create mount point
sudo mkdir /mnt/data
# Mount it
sudo mount /dev/sdb1 /mnt/data
Partitioning with parted
parted works with both MBR and GPT disks. GPT is required for disks larger than 2 TB.
# Start parted
sudo parted /dev/sdb
# Create GPT partition table
(parted) mklabel gpt
# Create partition
(parted) mkpart primary ext4 0% 100%
# Show partition table
(parted) print
# Exit
(parted) quit
# Format
sudo mkfs.ext4 /dev/sdb1
One-liner parted Usage
# Create GPT table and use entire disk as one partition
sudo parted -s /dev/sdb mklabel gpt mkpart primary ext4 0% 100%
# Format
sudo mkfs.ext4 /dev/sdb1
LVM Basics
LVM (Logical Volume Manager) abstracts physical disks for flexible storage management. You can resize volumes online and combine multiple disks.
LVM Concepts
| Concept | Abbreviation | Description |
|---|---|---|
| Physical Volume | PV | Physical disk or partition |
| Volume Group | VG | Pool created from PVs |
| Logical Volume | LV | Logical partition allocated from VG |
Setting Up LVM
# Install LVM tools
sudo apt install lvm2 -y
# Create physical volume
sudo pvcreate /dev/sdb
# Create volume group
sudo vgcreate vg_data /dev/sdb
# Create logical volume (50 GB)
sudo lvcreate -L 50G -n lv_data vg_data
# Format
sudo mkfs.ext4 /dev/vg_data/lv_data
# Mount
sudo mkdir /mnt/data
sudo mount /dev/vg_data/lv_data /mnt/data
Viewing LVM Information
# Physical volume info
sudo pvs
sudo pvdisplay
# Volume group info
sudo vgs
sudo vgdisplay
# Logical volume info
sudo lvs
sudo lvdisplay
Extending a Partition with LVM
# Add new disk and create PV
sudo pvcreate /dev/sdc
# Add to VG
sudo vgextend vg_data /dev/sdc
# Extend LV (use all free space)
sudo lvextend -l +100%FREE /dev/vg_data/lv_data
# Resize filesystem (ext4)
sudo resize2fs /dev/vg_data/lv_data
# For xfs
sudo xfs_growfs /mnt/data
Persistent Mount Settings (/etc/fstab)
Edit /etc/fstab to automatically mount disks on reboot:
# Get UUID
blkid /dev/sdb1
# Edit /etc/fstab
sudo nano /etc/fstab
# /etc/fstab format:
# UUID=xxxx /mount/point filesystem options dump pass
UUID=a1b2c3d4-... /mnt/data ext4 defaults 0 2
# Test fstab (shows errors)
sudo mount -a
# Show all mounts
mount | grep /mnt
A mistake in /etc/fstab can prevent the system from booting. Back up first: sudo cp /etc/fstab /etc/fstab.bak
Disk Health Check
# Check SMART status
sudo apt install smartmontools -y
sudo smartctl -a /dev/sda
# Run a short test
sudo smartctl -t short /dev/sda
# Check filesystem for errors (unmounted disk)
sudo fsck -n /dev/sdb1
Conclusion
Disk management is a core component of server maintenance. Regular usage monitoring with df/du, flexible volume management with LVM, and proactive disk health monitoring with SMART tools will keep your server's storage infrastructure healthy.
Related Articles
How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
How to Connect to Windows VDS via RDP: Complete Guide
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
How to Change VDS Server OS: Step-by-Step Reinstall Guide
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.