Daily Linux Knowledge: LVM Logical Volume Management

In Linux systems, disks and partitions are the foundation of storage management, but traditional partitioning methods (such as MBR and GPT) have many drawbacks: they are difficult to expand online, challenging to resize flexibly, and cumbersome for disk replacement. To address these issues, Linux provides a more powerful and flexible mechanism—LVM (Logical Volume Manager).

LVM can be understood as a “scalable storage abstraction layer” built on top of physical disks, allowing disk management to no longer be constrained by traditional partitioning.

Daily Linux Knowledge: LVM Logical Volume Management

1. What is LVM?

LVM (Logical Volume Manager) is a logical volume management mechanism on Linux that builds “scalable logical volumes” on top of physical disks, enabling more flexible disk management.

In simple terms, it has three major advantages:

  1. Dynamic resizing (online expansion)

Partitions or file systems can be expanded without rebooting or affecting business operations.

  1. Cross-disk combination

Multiple physical disks can be combined into a single logical volume, maximizing storage space utilization.

  1. Snapshot capability

Snapshots can be taken of volumes for backup or temporary recovery.

These capabilities make LVM a core technology in server environments, virtual machines, databases, and scenarios requiring frequent expansion.

2. Structure of LVM

Understanding the architecture of LVM is the first step to mastering it.

LVM consists of three layers:

Physical Volume (PV) → Volume Group (VG) → Logical Volume (LV)

Let’s explain each one:

  1. PV (Physical Volume)

This is the LVM physical unit derived from physical disks or partitions.

For example:

  • /dev/sdb
  • /dev/sdc1

Create PV:

pvcreate /dev/sdb
  1. VG (Volume Group)

Multiple PVs can be combined into a VG, similar to combining several disks into a large storage pool.

Create VG:

vgcreate myvg /dev/sdb /dev/sdc1
  1. LV (Logical Volume)

Space in a VG can be divided into multiple LVs, similar to traditional partitions but more flexible.

Create LV:

lvcreate -L 20G -n data myvg

Once the LV is created, it can be formatted and mounted like a “partition”:

mkfs.ext4 /dev/myvg/data
mount /dev/myvg/data /mnt/data

3. Basic Operation Process

Below is the most commonly used workflow for LVM, suitable for disk expansion, creating storage space, and other common operations.

1. Create PV: Add disks to LVM

pvcreate /dev/sdb
pvdisplay     # View all PVs

2. Create VG: Turn multiple PVs into a volume group

vgcreate myvg /dev/sdb
vgextend myvg /dev/sdc  # Expand VG by adding a new disk
vgdisplay

3. Create LV: Allocate logical volume space

lvcreate -n data -L 50G myvg
lvdisplay

4. Format and mount

mkfs.ext4 /dev/myvg/data
mkdir /data
mount /dev/myvg/data /data

4. LVM Expansion

Expansion is the greatest advantage of LVM. Below is an example of expanding an LV by 10GB:

1. Expand LV

lvextend -L +10G /dev/myvg/data

2. Expand the file system (ext4)

resize2fs /dev/myvg/data

No need to reboot the system or unmount the volume (online expansion), making it very suitable for server environments.

5. Creating and Using Snapshots

Snapshots are a unique feature of LVM, useful for backups, temporary copies, and data recovery.

Create a snapshot:

lvcreate -s -L 5G -n data_snap /dev/myvg/data

Mount the snapshot:

mount /dev/myvg/data_snap /mnt/snap

Snapshots save the “differential data” of the volume and can be used for:

  • Creating a snapshot before data backup
  • Creating a snapshot before an upgrade
  • Restoring erroneous modifications

Delete the snapshot:

lvremove /dev/myvg/data_snap

Daily Linux Knowledge: LVM Logical Volume Management

Important! Operations and Maintenance Discussion Group Open to the Public!Scan to add the editor’s WeChat,apply to jointhe groupDaily Linux Knowledge: LVM Logical Volume Management▲ Long press to join the group

Leave a Comment