Back

What is LVM?

LVM (Logical Volume Manager) is a device mapper framework for the Linux kernel that provides logical volume management, originally written by Heinz Mauelshagen in 1998 while at Sistina Software. It acts as a thin software layer on top of physical hard disks and partitions, creating an abstraction that allows dynamic storage resizing, snapshots, and flexible disk management without downtime.

LVM organizes storage into three layers: Physical Volumes (PVs), Volume Groups (VGs), and Logical Volumes (LVs). Logical volumes can be resized online by adding or removing physical extents (PEs), and most modern Linux distributions support having the root filesystem on a logical volume.

Common Use Cases

  • Combine physical disks: Merge multiple hard drives into a single logical volume (similar to JBOD) to create large storage pools without hardware RAID.
  • Resize filesystems online: Grow or shrink filesystem sizes without unmounting or rebooting the server, adapting to changing storage needs.
  • Snapshots for backups: Create LVM copy-on-write snapshots for consistent backups of production volumes without service interruption.
  • Migrate data without downtime: Use the pvmove command to relocate data between physical drives while the logical volume stays online and accessible.

Frequently Asked Questions

How to create a physical volume (pvcreate) in LVM step by step?

To create a physical volume in LVM, first identify the disk with `lsblk` or `fdisk -l`. Then run `pvcreate /dev/sdb`. This writes the LVM header on the disk, marking it as available for volume groups. If the disk has existing partitions use `pvcreate /dev/sdb1`. Verify with `pvs` or `pvdisplay`. You can create multiple PVs: `pvcreate /dev/sdb /dev/sdc`. LVM can combine disks of different sizes, though matching disk types (HDD or SSD) is recommended.

What is a Volume Group (vgcreate) in LVM and how to set it up?

A Volume Group (VG) pools one or more physical volumes (PV) into a single storage reservoir from which logical volumes (LV) are allocated. To create a VG: `vgcreate vg_data /dev/sdb /dev/sdc`. This joins both disks into one group. Verify with `vgs` or `vgdisplay`. Extend an existing VG with `vgextend vg_data /dev/sdd`. The VG enables snapshots, online volume resizing, and data migration between physical disks without service interruption.

How to create a logical volume (lvcreate) and format it in LVM?

To create a logical volume: `lvcreate -n lv_data -L 100G vg_data`. `-n` sets the name, `-L` defines size (use `-l 100%FREE` to consume all remaining space). Then format: `mkfs.ext4 /dev/vg_data/lv_data`. Mount with `mount /dev/vg_data/lv_data /mnt/data`. To extend later: `lvextend -L +50G /dev/vg_data/lv_data` followed by `resize2fs /dev/vg_data/lv_data` for ext4 or `xfs_growfs` for XFS. Logical volumes can be resized online without unmounting.

How to extend an LVM logical volume without losing data?

To extend an LV without data loss: 1) Add physical space: `pvcreate /dev/sdd` then `vgextend vg_data /dev/sdd`. 2) Extend the LV: `lvextend -L +100G /dev/vg_data/lv_data`. 3) Resize the filesystem: for ext4 use `resize2fs /dev/vg_data/lv_data`, for XFS use `xfs_growfs /mnt/mount_point`. With ext4 the entire operation is hot and requires no unmounting. To shrink an LV use `lvreduce -L -50G` but only if the filesystem supports shrinking and always with a recent backup.