*Cube-Host– full cloud services!!

Configuring and Managing LVM Partitions on Linux

LVM (Logical Volume Manager) is a Linux storage layer that lets you treat multiple disks (or partitions) as one flexible pool of space. With LVM you can extend volumes online, create snapshots, build mirrors, and redesign storage without reinstalling the OS.

LVM is built into the Linux kernel (via device-mapper) and is one of the most useful sysadmin tools when you manage VPS, dedicated servers, databases, file storage, backups, and growing projects.

Why LVM is used

  • Flexibility: resize logical volumes without rebuilding partitions
  • Scalability: add new disks into the same storage pool
  • Snapshots: create point-in-time copies (useful for backups and migrations)
  • Mirroring: improve fault tolerance (similar to RAID1)
  • Abstraction: your filesystem works with LVM volumes, not “raw disks”

LVM layers (simple explanation):

  • PV (Physical Volume): a disk or partition initialized for LVM
  • VG (Volume Group): a pool of space created from one or more PVs
  • LV (Logical Volume): a “virtual partition” inside a VG where you create a filesystem
LVM layers: PV, VG, LV

Installing the lvm2 utility

Install LVM tools (package lvm2) using your distribution’s package manager:

# Ubuntu / Debian
apt update
apt install -y lvm2

# RHEL / CentOS Stream / Rocky / Alma
dnf install -y lvm2

# Older CentOS
yum install -y lvm2

Check that LVM commands are available:

pvs
vgs
lvs

Creating LVM partitions (PV → VG → LV)

Example setup: you have two extra disks attached to a server, for example /dev/vdb and /dev/vdc.

Step 1 — identify disks:

lsblk
fdisk -l
List disks with fdisk -l

Important: be 100% sure you target the correct disks. On production servers, a wrong command can destroy data.

Step 2 — initialize disks as Physical Volumes (PV):

pvcreate /dev/vdb /dev/vdc
pvcreate initialization

Check PV status:

pvdisplay
pvs
pvdisplay output

Step 3 — create a Volume Group (VG). Here we name it test:

vgcreate test /dev/vdb /dev/vdc

Check the VG:

vgdisplay
vgs

Step 4 — create a Logical Volume (LV). Example: 5 GB LV named test1 inside VG test:

lvcreate -L 5G -n test1 test

Useful examples:

# Use 40% of VG capacity:
lvcreate -l 40%VG -n data test

# Use all free VG space:
lvcreate -l 100%FREE -n data test

Check LVs:

lvdisplay
lvs
lsblk

Create a filesystem and mount an LV

Create a filesystem. Choose ext4 for general purpose. (If you use XFS, note: XFS cannot be shrunk.)

# ext4 example:
mkfs.ext4 /dev/test/test1
mkfs.ext4 on LVM volume

Create mount point and mount it:

mkdir -p /var/www/home
mount /dev/test/test1 /var/www/home

Make the mount persistent after reboot by adding an entry into /etc/fstab:

nano /etc/fstab
/dev/test/test1  /var/www/home  ext4  defaults  0  2

Apply and check:

mount -a
df -h
lsblk -f
lsblk shows LVM mount

Extend an LVM logical volume

There are two common growth scenarios:

  • Scenario A: the VG already has free space → extend LV and filesystem
  • Scenario B: VG has no free space → add a new disk (PV) → extend VG → extend LV

Extend LV (example: add 10 GB):

lvextend -L +10G /dev/test/test1

Then resize the filesystem:

# ext4:
resize2fs /dev/test/test1

# xfs (if you used XFS):
# xfs_growfs /var/www/home

If you need to add a new disk first:

pvcreate /dev/vdd
vgextend test /dev/vdd

Shrink an LVM logical volume (safe method)

Warning: shrinking is risky. Do it only if you have backups. Also remember: XFS cannot be shrunk. The safe shrink procedure below is for ext4.

1) Unmount:

umount /var/www/home

2) Check filesystem integrity:

e2fsck -fy /dev/test/test1

3) Shrink filesystem first (example: to 4G):

resize2fs /dev/test/test1 4G

4) Shrink LV to match (example: reduce to 4G):

lvreduce -L 4G /dev/test/test1

5) Mount back and check:

mount /dev/test/test1 /var/www/home
df -h

Remove LVM groups and volumes

To remove LVM cleanly:

  • Unmount the filesystem
  • Remove its fstab entry
  • Remove LV → remove VG → remove PV labels

umount /var/www/home

lvremove /dev/test/test1
vgremove test
pvremove /dev/vdb /dev/vdc

Create mirrored volumes

Mirroring in LVM stores the same data on multiple disks (similar to RAID1). This increases fault tolerance but reduces usable space.

Classic example (two disks):

pvcreate /dev/sdb /dev/sdc
vgcreate vgmirror /dev/sdb /dev/sdc

# Mirror LV (LVM mirror)
lvcreate -L 5G -m1 -n lvMirr1 vgmirror

On modern systems you may also use LVM RAID types (for example RAID1), depending on your distro and needs.

Extra tips: snapshots and best practices

  • Use snapshots for backups/migrations (but remember snapshots are not “forever storage”).
  • Name volumes clearly: vg_data/lv_mysql, vg_data/lv_www etc.
  • Monitor free space in VG to avoid sudden “out of space” incidents.
  • Prefer extending over shrinking. Shrinking is always more dangerous.

VPS Linux

Prev
Menu