Linux Storage Management: Partitioning, LVM, Swap, and RAID

Modern enterprise Linux storage involves managing physical block devices, partition tables (GPT/MBR), Logical Volume Management (LVM) abstraction layers, dynamic filesystem resizing, and swap spaces. This guide covers storage diagnostics, partitioning tools (fdisk, parted), LVM administration, live filesystem growth, swap allocation, and software RAID.


⚡ Quick Dive

Storage & LVM Command Cheat Sheet

Layer Command Action Example
Inspection lsblk -f Tree view of block devices, filesystems, and UUIDs lsblk -f
Inspection fdisk -l Detailed partition table layout per disk sudo fdisk -l /dev/sdb
Partitioning parted <dev> Create/resize GPT partitions non-interactively parted -s /dev/sdb mklabel gpt
LVM: PV pvcreate <dev> Initialize block device as LVM Physical Volume sudo pvcreate /dev/sdb1
LVM: VG vgcreate <name> <pvs> Pool Physical Volumes into Volume Group sudo vgcreate data_vg /dev/sdb1 /dev/sdc1
LVM: LV lvcreate -L <sz> -n <n> <vg> Allocate Logical Volume from Volume Group sudo lvcreate -L 50G -n app_lv data_vg
LVM: Grow lvextend -r -L +20G <lv> Extend Logical Volume AND resize filesystem live sudo lvextend -r -L +20G /dev/data_vg/app_lv
LVM: Snapshot lvcreate -s -L <sz> -n <snap> <lv> Create instantaneous point-in-time snapshot sudo lvcreate -s -L 5G -n app_snap /dev/data_vg/app_lv
Swap swapon -s / swapon -a Inspect active swap spaces or enable all in fstab swapon --show
RAID cat /proc/mdstat Check software RAID array status cat /proc/mdstat

Complete LVM End-to-End Workflow

# 1. Initialize physical partitions as Physical Volumes
sudo pvcreate /dev/nvme0n1p1 /dev/nvme1n1p1

# 2. Group them into a Volume Group
sudo vgcreate vg_storage /dev/nvme0n1p1 /dev/nvme1n1p1

# 3. Create a 100GB Logical Volume
sudo lvcreate -L 100G -n lv_database vg_storage

# 4. Format with XFS filesystem
sudo mkfs.xfs /dev/vg_storage/lv_database

# 5. Mount and use
sudo mkdir -p /mnt/database
sudo mount /dev/vg_storage/lv_database /mnt/database

📖 Extended Guide

1. Block Device Naming & Partition Schemes

Device Naming Conventions

  • /dev/sdX: SATA, SCSI, or USB drives (e.g., /dev/sda, /dev/sdb). Partitions are /dev/sda1, /dev/sda2.
  • /dev/nvmeXn1: High-speed PCIe NVMe SSDs (e.g., /dev/nvme0n1). Partitions are /dev/nvme0n1p1, /dev/nvme0n1p2.
  • /dev/vdX: Virtual disk images on KVM/QEMU hypervisors (e.g., /dev/vda).
  • /dev/mapper/*: LVM logical volumes and LUKS encrypted containers.

MBR vs. GPT Partition Tables

  • MBR (Master Boot Record): Legacy standard. Limited to 2TB disk size and maximum 4 primary partitions.
  • GPT (GUID Partition Table): Modern UEFI standard. Supports disks up to 9.4 ZB and virtually unlimited partitions (default 128) with CRC32 integrity checksums.
# Create a GPT partition table and partition with parted
sudo parted -s /dev/sdb mklabel gpt
sudo parted -s /dev/sdb mkpart primary ext4 1MiB 100%

2. Logical Volume Manager (LVM) Architecture

LVM provides an abstraction layer between physical hardware disks and the filesystems mounted on the OS:

+-------------------------------------------------------------------+
|                     Filesystem (/data, /home)                    |
+-------------------------------------------------------------------+
|  Logical Volume (LV 1)            |  Logical Volume (LV 2)        |
+-----------------------------------+-------------------------------+
|                       Volume Group (VG)                          |
+-----------------------------------+-------------------------------+
|  Physical Volume (PV: /dev/sdb1)  |  Physical Volume (PV: /dev/sdc1)|
+-----------------------------------+-------------------------------+
|           Disk /dev/sdb           |           Disk /dev/sdc       |
+-------------------------------------------------------------------+

Physical Volumes (PV)

# Scan and list PVs
sudo pvs
sudo pvdisplay /dev/sdb1

Volume Groups (VG)

# Add another disk to expand existing Volume Group
sudo pvcreate /dev/sdd1
sudo vgextend data_vg /dev/sdd1

# View available free space in Volume Group
sudo vgs

Logical Volumes (LV) & Live Extension

Logical Volumes can be resized online without unmounting the filesystem:

# Allocate 100% of remaining free space in Volume Group
sudo lvcreate -l 100%FREE -n lv_data data_vg

# Extend LV by 50GB AND automatically grow ext4/XFS filesystem live (-r / --resizefs)
sudo lvextend -r -L +50G /dev/data_vg/lv_data

# For ext4 manual resize:
# sudo resize2fs /dev/data_vg/lv_data

# For XFS manual resize (must be mounted):
# sudo xfs_growfs /mnt/database

[!NOTE] XFS filesystems can be extended while mounted, but cannot be shrunk. ext4 filesystems can be extended live, but shrinking requires unmounting and running offline e2fsck.


3. LVM Snapshots for Backups

LVM snapshots use Copy-on-Write (CoW) to capture a frozen point-in-time view of data:

# 1. Create a 10GB snapshot of lv_data
sudo lvcreate -s -L 10G -n snap_data /dev/data_vg/lv_data

# 2. Mount snapshot in read-only mode to perform consistent backup
sudo mkdir -p /mnt/snapshot
sudo mount -o ro /dev/data_vg/snap_data /mnt/snapshot

# 3. Take backup using tar or rsync
tar -czf /backup/db_snapshot.tar.gz /mnt/snapshot/

# 4. Unmount and delete snapshot (critical: do not let snapshots fill to 100%)
sudo umount /mnt/snapshot
sudo lvremove -y /dev/data_vg/snap_data

4. Swap Space Management

Swap space acts as virtual memory overflow when physical RAM is fully utilized:

Creating a Swap File (Recommended over dedicated partition)

# 1. Allocate 4GB contiguous file
sudo fallocate -l 4G /swapfile
# If fallocate is not supported on filesystem:
# sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress

# 2. Set strict permissions (security critical)
sudo chmod 600 /swapfile

# 3. Format as swap space
sudo mkswap /swapfile

# 4. Enable swap
sudo swapon /swapfile

# 5. Persist in /etc/fstab
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

# 6. Verify active swap
swapon --show
free -h

5. Software RAID with mdadm

Linux Software RAID aggregates multiple disks for redundancy (RAID 1), performance (RAID 0), or both (RAID 5 / RAID 10).

# 1. Create a RAID 1 mirror across two disks
sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1

# 2. Monitor rebuilding progress
cat /proc/mdstat

# 3. Save RAID array configuration
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u

# 4. Format and mount RAID device
sudo mkfs.ext4 /dev/md0
sudo mount /dev/md0 /mnt/raid_storage