Creating and Managing LVM
LVM (Logical Volume Manager) adds an abstraction layer between physical storage and file systems. Instead of formatting partitions directly, you create a pool of storage (Volume Group) from one or more disks, then carve out Logical Volumes from that pool. The key advantage: you can resize, move, and snapshot volumes while they are online. This is why Ubuntu uses LVM by default in the server installer.
LVM architecture
Physical level:
/dev/sdb /dev/sdc
| |
pvcreate pvcreate
| |
↓ ↓
Physical Volumes (PV): /dev/sdb, /dev/sdc
|
vgcreate vg_data /dev/sdb /dev/sdc
|
↓
Volume Group (VG): vg_data [total: 1 TB]
/ | lvcreate lvcreate lvcreate
| | |
↓ ↓ ↓
Logical Volumes (LV):
/dev/vg_data/mysql [200 GB]
/dev/vg_data/logs [300 GB]
/dev/vg_data/backup [500 GB]
|
mkfs.ext4 or mkfs.xfs
|
mount /dev/vg_data/mysql /var/lib/mysqlCreating LVM from scratch
# Step 1: Install LVM tools
sudo apt install -y lvm2
# Step 2: Prepare disks — partition them with type "Linux LVM"
# Either: use whole disk directly as PV (simpler, common for dedicated storage)
# Or: partition first and use the partition as PV (required for mixed-use disks)
# Use whole disk as PV:
sudo pvcreate /dev/sdb /dev/sdc
# Or use a partition:
# sudo fdisk /dev/sdb → n → Enter → Enter → Enter → t → 30 (Linux LVM) → w
# sudo pvcreate /dev/sdb1
# Step 3: Create a Volume Group from the physical volumes
sudo vgcreate vg_data /dev/sdb /dev/sdc
# Verify
sudo pvs # Show physical volumes
sudo vgs # Show volume groups
pvs and vgs output
PV VG Fmt Attr PSize PFree
/dev/sdb vg_data lvm2 a-- 500.00g 500.00g
/dev/sdc vg_data lvm2 a-- 500.00g 500.00g
VG #PV #LV #SN Attr VSize VFree
vg_data 2 0 0 wz--n- 999.99g 999.99g
Creating logical volumes
# Create a logical volume with a specific size
sudo lvcreate -n mysql -L 200G vg_data
# Create a logical volume using percentage of free space
sudo lvcreate -n logs -l 30%FREE vg_data
# Create a logical volume using ALL remaining free space
sudo lvcreate -n backup -l 100%FREE vg_data
# Format and mount the logical volume (accessed via /dev/VG/LV or /dev/mapper/VG-LV)
sudo mkfs.xfs /dev/vg_data/mysql
sudo mkdir -p /var/lib/mysql
sudo mount /dev/vg_data/mysql /var/lib/mysql
# Add to /etc/fstab (use /dev/mapper name for reliability)
echo "/dev/mapper/vg_data-mysql /var/lib/mysql xfs defaults,noatime 0 2" | sudo tee -a /etc/fstab
# Verify
sudo lvs # Show logical volumes
df -h /var/lib/mysql
Managing volume groups
# Add a new physical disk to an existing volume group (expand the pool)
sudo pvcreate /dev/sdd
sudo vgextend vg_data /dev/sdd
sudo vgs # VSize should now be larger
# Remove a disk from a volume group (move data off it first)
sudo pvmove /dev/sdb # Moves all data from sdb to other PVs in VG (slow)
sudo vgreduce vg_data /dev/sdb
sudo pvremove /dev/sdb
# Rename a volume group
sudo vgrename vg_data vg_production
# Delete a volume group (WARNING: destroys all LVs and data)
# sudo lvremove /dev/vg_data/*
# sudo vgremove vg_data
# sudo pvremove /dev/sdb /dev/sdc
LVM display commands
# Quick overview of all LVM resources
sudo pvs # Physical volumes: PV name, VG, size, free
sudo vgs # Volume groups: name, PV count, LV count, size, free
sudo lvs # Logical volumes: name, VG, size, attributes
# Detailed info about a specific object
sudo pvdisplay /dev/sdb
sudo vgdisplay vg_data
sudo lvdisplay /dev/vg_data/mysql
# Show LVM configuration for all objects
sudo lvscan # List all LVs with active state
Conclusion
LVM is worth setting up whenever you need flexibility in storage management. The setup order is always: pvcreate on disks, vgcreate to pool them, lvcreate to carve out volumes, mkfs to format, mount to access. Expanding storage later is straightforward: add a disk with pvcreate + vgextend, then grow the LV with lvextend + resize2fs (or xfs_growfs for XFS). This is the standard storage management pattern for Ubuntu servers.
FAQ
Is Creating and Managing LVM important for Ubuntu administrators?+
Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.
Should I practice this on a live server?+
Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.
What should I do after reading this article?+
Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.
Need help with Ubuntu administration?
Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.
Hire Me for Support