LVM, RAID & Swap
Module 4 · Storage Management

LVM, RAID & Swap

Logical Volume Manager, software RAID with mdadm, device mapper, and swap space — the tools for storage that can grow, survive a drive failure, or extend RAM.

The Storage Setup Process, End to End

Every storage device generally goes through the same pipeline before an application can use it: identify the device → partition it → optionally layer LVM or RAID on top → create a filesystem → mount it (and persist that mount in /etc/fstab). LVM and RAID both sit between “partition” and “filesystem” — they change what a partition-like block device is, not how it’s eventually mounted.


Device Mapper and Multipath

The device mapper is the kernel framework that lets Linux create virtual block devices layered on top of real ones — LVM, software RAID, and encrypted volumes (covered in the Security module) are all built on it.

DM-Multipath uses device mapper to present multiple physical I/O paths to the same storage (common with SAN-attached storage that has redundant cabling/controllers) as a single, fault-tolerant device — if one path fails, I/O keeps flowing over another. Its configuration lives in /etc/multipath.conf.


Software RAID with mdadm

RAID combines multiple physical disks into one logical unit for redundancy, performance, or both. Linux’s software RAID is managed with mdadm:

mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
RAID levelBehavior
RAID 0Striping — performance, zero redundancy
RAID 1Mirroring — full redundancy, half usable capacity
RAID 5Striping with distributed parity — tolerates one disk failure
RAID 10Mirrored stripes — performance and redundancy, at the cost of capacity

Logical Volume Manager (LVM)

LVM adds a flexible layer between physical disks and filesystems, so you can resize storage, span a filesystem across multiple disks, and take snapshots — all without unmounting anything or being boxed in by fixed partition boundaries.

LVM’s tools fall into three categories, one per layer of its stack:

LayerWhat it isTool prefix
Physical Volume (PV)A raw disk or partition, initialized for LVM usepv*
Volume Group (VG)A storage pool combining one or more PVs — can span multiple physical disksvg*
Logical Volume (LV)A resizable “virtual partition” carved out of a VG — this is what actually gets formatted and mountedlv*

Building the stack:

TaskCommand
Create a physical volumepvcreate /dev/sdd2
Remove a physical volumepvremove /dev/sdd2
Create a volume group spanning two disksvgcreate vg_shared /dev/sdd1 /dev/sdd2
Create a 20 GB logical volume named sales inside a volume grouplvcreate -n sales -L 20G vg_sales

Inspecting the stack:

TaskCommand
List current logical volumes, brieflvs
Detailed info on a PV / VG / LVpvdisplay, vgdisplay, lvdisplay
Even more detail (verbose mode)add -v to any of the above, e.g. vgdisplay -v

Resizing:

lvextend -L +50G /dev/vg_sales/sales     # grow the logical volume
resize2fs /dev/vg_sales/sales            # then grow the ext4 filesystem to match
lvreduce -L 100G /dev/vg_sales/sales     # shrink the logical volume
⚠️ Common Pitfall: Shrink the Filesystem Before You Shrink the Logical Volume

Growing is safe in either order, but shrinking is not: lvreduce on ext4 must happen after you’ve already shrunk the filesystem with resize2fs to a size at or below the new target — do it in the wrong order and you truncate live data out from under the filesystem. (XFS sidesteps this entirely by refusing to shrink at all — see Lesson 2.)


Swap Space

Swap extends usable memory by giving the kernel somewhere on disk to page out data that doesn’t fit in physical RAM.

Swap typeDescription
Swap partitionA dedicated partition, formatted specifically for swap use
Swap fileA regular file used as swap space — more flexible, easily resized without repartitioning
zswap / zramCompressed swap held in RAM itself, trading CPU cycles for reduced disk I/O — useful on systems with little or no disk-backed swap
TaskCommand
Prepare a partition or file as swapmkswap /dev/sdb2
Activate a swap areaswapon /dev/sdb2
Deactivate a swap areaswapoff /dev/sdb2
View active swap areasswapon --show or free -h

🧠 Knowledge Check

1. In LVM's three-layer stack, which layer is the one you actually format with a filesystem and mount?

2. You need to shrink an ext4 logical volume from 200G to 100G. What's the correct order of operations?

3. Which RAID level provides redundancy by mirroring data across two disks, at the cost of half the total usable capacity?


What’s Next

Storage is set up before the system even fully boots — Module 5 walks through exactly what happens from power-on to login prompt.