DOC_TYPE: RESEARCH_LOG

Linux Disk Management

#Linux#Disk Management#LVM#Troubleshooting#SysAdmin

Linux Disk Management

Linux Level Hands-on SysAdmin


Purpose

This guide teaches Linux disk management from fundamentals to real system administration scenarios. The goal is for the reader to understand disks, partitions, filesystems, mount points, persistent mounts, LVM, swap, disk usage analysis, and production troubleshooting.

This is not only a command list. Each operation is explained from the point of view of what it changes on the system and why it is performed.


Learning Objectives

After completing this guide, the reader should be able to:

  • Inspect disks and block devices
  • Create partitions
  • Format partitions with filesystems
  • Mount and unmount filesystems
  • Define persistent mounts with /etc/fstab
  • Analyze disk usage and inode usage
  • Use LVM for flexible disk management
  • Extend logical volumes safely
  • Create and manage swap space
  • Check disk health and performance
  • Troubleshoot common disk-related failures

Lab Environment

A test virtual machine is enough for practice. Ubuntu Server, Debian, Rocky Linux, AlmaLinux, or CentOS Stream can be used.

Example lab layout:

/dev/sda    # Operating system disk
/dev/sdb    # Empty disk added for lab work
Disk TypeExample
SATA/SCSI disk/dev/sda, /dev/sdb, /dev/sdc
NVMe disk/dev/nvme0n1, /dev/nvme1n1
SATA partition/dev/sda1, /dev/sdb1
NVMe partition/dev/nvme0n1p1

Practice disk operations in a lab first. Running commands against the wrong disk can destroy data.


1. Linux Disk Logic

Linux represents disks as device files under /dev.

/dev/sda
/dev/sda1
/dev/sdb
/dev/sdb1
/dev/nvme0n1
/dev/nvme0n1p1
ObjectMeaning
/dev/sdaPhysical or virtual disk
/dev/sda1Partition on the disk
/dev/sdbSecond disk
/dev/nvme0n1NVMe disk
/dev/nvme0n1p1Partition on an NVMe disk

Basic relationship:

Disk → Partition → Filesystem → Mount Point

Typical workflow:

  1. Add the disk.
  2. Create a partition.
  3. Create a filesystem.
  4. Mount it to a directory.
  5. Add it to /etc/fstab if it must survive reboot.

2. Viewing Disks and Partitions

The first step in disk management is always reading the current state.

lsblk
lsblk -f
df -h
sudo fdisk -l
sudo blkid

lsblk shows block devices, partitions, and mount points. df -h only shows mounted filesystems. blkid is useful for UUID values, especially for persistent mount definitions.


3. Partition Management

Create a partition with fdisk:

sudo fdisk /dev/sdb

Typical flow:

n       # new partition
p       # primary
1       # partition number
Enter   # default start
Enter   # default end, use full disk
w       # write changes

For GPT partitioning with parted:

sudo parted /dev/sdb
mklabel gpt
mkpart primary ext4 1MiB 100%
quit

4. Filesystems and Formatting

FilesystemUsage
ext4General purpose, stable, widely used
XFSLarge files and high-performance systems
btrfsSnapshots and advanced features
vfatEFI or portable compatibility
sudo mkfs.ext4 /dev/sdb1
sudo mkfs.xfs /dev/sdb1
lsblk -f

XFS can be grown but not shrunk, so production planning matters.


5. Mount and Persistent Mount

sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data
df -h
sudo umount /mnt/data

If unmount fails because the target is busy:

sudo lsof +D /mnt/data
fuser -vm /mnt/data

Persistent mount with /etc/fstab:

UUID=aaaa-bbbb /mnt/data ext4 defaults 0 2

Always test before reboot:

sudo mount -a

6. Disk Usage Analysis

df -h
df -i
sudo du -h --max-depth=1 / | sort -hr | head -20
sudo du -h --max-depth=1 /var | sort -hr | head -20
sudo find / -type f -size +500M -exec ls -lh {} \; 2>/dev/null
sudo find / -type f -exec du -h {} + 2>/dev/null | sort -hr | head -20

If disk space looks available but files cannot be created, check inode usage with df -i.


7. LVM

Disk/Partition → Physical Volume → Volume Group → Logical Volume → Filesystem → Mount
ConceptDescription
PVPhysical Volume, disk or partition added to LVM
VGVolume Group, pool of PVs
LVLogical Volume, allocated space from VG
sudo apt install lvm2 -y
sudo pvcreate /dev/sdb1
sudo vgcreate vg_data /dev/sdb1
sudo lvcreate -L 10G -n lv_appdata vg_data
sudo mkfs.ext4 /dev/vg_data/lv_appdata
sudo mkdir -p /appdata
sudo mount /dev/vg_data/lv_appdata /appdata

8. Extending LVM

df -h /appdata
sudo vgs
sudo lvs
sudo lvextend -L +5G /dev/vg_data/lv_appdata
sudo lvextend -l +100%FREE /dev/vg_data/lv_appdata
sudo resize2fs /dev/vg_data/lv_appdata
sudo xfs_growfs /appdata
sudo lvextend -r -L +5G /dev/vg_data/lv_appdata

9. Swap Management

swapon --show
free -h
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Persistent entry:

/swapfile none swap sw 0 0

10. Health, Cleanup, and Troubleshooting

lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,MODEL
sudo apt install smartmontools -y
sudo smartctl -a /dev/sda
sudo smartctl -H /dev/sda
journalctl --disk-usage
sudo journalctl --vacuum-time=7d
docker system df
docker system prune
sudo lsof | grep deleted

New disk is not visible:

lsblk
sudo fdisk -l
dmesg | tail -50
echo "- - -" | sudo tee /sys/class/scsi_host/host0/scan

Mount fails:

lsblk -f
sudo blkid
sudo dmesg | tail -50
sudo fsck /dev/sdb1

Do not run fsck on a mounted filesystem.


Production Notes

  • Confirm the target disk before any operation.
  • mkfs, fdisk, parted, and pvcreate can destroy data.
  • Test /etc/fstab changes with mount -a before reboot.
  • Take backup or snapshot before LVM extension where possible.
  • XFS cannot be shrunk.
  • Use resize2fs for ext4 and xfs_growfs for XFS.
  • If deleted files do not release space, check lsof | grep deleted.
  • Monitor /var/lib/docker on Docker hosts.
  • Disk fullness can interrupt Elasticsearch, MongoDB, PostgreSQL, and other stateful services.