Stage 3 · Build
Storage & Filesystems
LVM & Device Mapper
Logical volumes, thin provisioning, dm-cache, and dm-crypt — flexible storage management.
LVM Overview
LVM (Logical Volume Manager) adds a layer of abstraction between physical disks and filesystems. It lets you create flexible volumes that can be resized, moved, and snapshotted without unmounting. LVM is built on the device mapper framework.
# Physical Volumes (PV) → Volume Groups (VG) → Logical Volumes (LV)
# /dev/sdb (PV) ─┐
# ├─ myvg (VG) ─┬─ lv_data (LV) ─ /data
# /dev/sdc (PV) ─┘ ├─ lv_logs (LV) ─ /logs
# └─ lv_swap (LV) ─ swap
# View LVM structure
sudo pvs
# PV VG Fmt Attr PSize PFree
# /dev/sdb myvg lvm2 a-- 500.00g 200.00g
sudo vgs
# VG #PV #LV #SN Attr VSize VFree
# myvg 2 3 0 wz--n- 1000g 200.00g
sudo lvs
# LV VG Attr LSize Pool Origin Data%
# data myvg -wi-ao---- 500.00g
# logs myvg -wi-ao---- 200.00gLVM layers: Physical Volumes (raw disks) → Volume Groups (storage pools) → Logical Volumes (usable partitions). This abstraction enables flexible management.
LVM Operations
# Create physical volume
sudo pvcreate /dev/sdb /dev/sdc
# Create volume group
sudo vgcreate myvg /dev/sdb /dev/sdc
# Create logical volume
sudo lvcreate -L 100G -n lv_data myvg
sudo lvcreate -l 50%VG -n lv_logs myvg
# Extend a logical volume (online!)
sudo lvextend -L +50G /dev/myvg/lv_data
sudo resize2fs /dev/myvg/lv_data # ext4
sudo xfs_growfs /dev/myvg/lv_data # XFS
# Reduce a logical volume (UNMOUNT FIRST!)
sudo umount /data
sudo resize2fs /dev/myvg/lv_data 50G
sudo lvreduce -L 50G /dev/myvg/lv_dataLVM volumes can be extended online without unmounting. Shrinking requires unmounting and resizing the filesystem first. XFS cannot be shrunk.
Thin Provisioning
Thin provisioning allocates storage on demand rather than upfront. You create a large virtual volume but only consume physical space as data is written. This allows overcommitting storage — create more virtual space than physical space exists.
# Create a thin pool
sudo lvcreate -L 500G -n thin_pool myvg
sudo lvconvert --type thin-pool myvg/thin_pool
# Create thin volumes
sudo lvcreate -V 1TB --thin -n thin_vol1 myvg/thin_pool
sudo lvcreate -V 1TB --thin -n thin_vol2 myvg/thin_pool
# Both volumes are 1TB but share 500GB physical space
sudo lvs
# LV VG Attr LSize Pool Data%
# thin_pool myvg twi-a-tz-- 500.00g 45.00
# thin_vol1 myvg Vwi-a-tz-- 1.00T thin_pool 22.00
# thin_vol2 myvg Vwi-a-tz-- 1.00T thin_pool 23.00Thin provisioning is useful for VMs and containers where most volumes are not fully utilized. Monitor Data% to avoid running out of physical space.
dm-cache: SSD Caching
dm-cache uses a fast SSD as a read/write cache for a slower HDD. Hot data is automatically promoted to the SSD, providing near-SSD performance with HDD capacity.
# Create cache device on SSD
sudo lvcreate -L 100G -n cache_pool myvg /dev/nvme0n1
# Create cache metadata
sudo lvcreate -L 1G -n cache_meta myvg /dev/nvme0n1
# Convert to cache pool
sudo lvconvert --type cache-pool myvg/cache_pool --poolmetadata myvg/cache_meta
# Attach cache to existing LV
sudo lvconvert --type cache --cachepool myvg/cache_pool myvg/lv_data
# Check cache status
sudo lvs -o+cache_read_hits,cache_read_misses
# LV CacheReadHits CacheReadMisses
# data 12345678 987654dm-cache automatically promotes hot data to the SSD cache. The 'writeback' mode writes to cache first (faster), 'writethrough' writes to both (safer).
dm-crypt: Encryption
# Encrypt a partition
sudo cryptsetup luksFormat /dev/sdb1
# Enter passphrase: ****
# Open (decrypt) the partition
sudo cryptsetup luksOpen /dev/sdb1 encrypted_vol
# Enter passphrase: ****
# Create filesystem
sudo mkfs.ext4 /dev/mapper/encrypted_vol
sudo mount /dev/mapper/encrypted_vol /mnt/encrypted
# Check encryption status
sudo cryptsetup status encrypted_vol
# /dev/mapper/encrypted_vol is active and is in use.
# type: LUKS2
# cipher: aes-xts-plain64
# keysize: 512 bits
# Close (unmount and encrypt)
sudo umount /mnt/encrypted
sudo cryptsetup luksClose encrypted_volLUKS (Linux Unified Key Setup) provides a standard format for encrypted volumes. Use LUKS2 for new deployments — it supports Argon2 key derivation and authenticated encryption.
LVM Snapshots
# Create snapshot
sudo lvcreate -L 10G -s -n data_snap /dev/myvg/lv_data
# Mount snapshot
sudo mount -o ro /dev/myvg/data_snap /mnt/snapshot
# Restore from snapshot (destructive!)
sudo umount /dev/myvg/lv_data
sudo lvconvert --merge /dev/myvg/data_snap
# Remove snapshot
sudo lvremove /dev/myvg/data_snapLVM snapshots use CoW and grow over time as the origin changes. They are good for short-term backups but not for long-term retention.
LVM makes it easy to resize, move, and snapshot volumes. Always use LVM on servers so you can extend storage without downtime.
Thin pools can run out of space if overcommitted. Monitor Data% with lvs. When the pool is full, writes fail and data loss is possible.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.