Cyber Services / All lessons / CentOS / Linux Administration / Creating Logical Volumes (LVM)

Creating Logical Volumes (LVM)

Module 18 of 33 · 4 min read · Free to read

MODULES ↓ Command blocks have a COPY button

The Professional's Toolkit for Flexible Storage Management

Traditional disk partitioning is rigid. Once you create a partition, resizing it is difficult and risky. In an enterprise environment where storage needs are constantly changing, this is a major limitation. The Logical Volume Manager (LVM) is a powerful abstraction layer that sits between your physical disks and your operating system, providing an incredibly flexible and scalable way to manage storage. Mastering LVM is a key skill for any professional Linux system administrator.

The LVM Philosophy: The "Bucket of Storage" Analogy

The best way to understand LVM is to think of it as a way to create a large, flexible "bucket" of storage out of your physical disks.

The LVM Architecture

Physical Volumes (PVs)

These are your raw building blocks. A PV can be an entire disk (/dev/sdb) or a single partition (/dev/sdc1).

More Physical Volumes

You can add multiple PVs together to create a larger pool.

Volume Group (VG)

This is the "bucket." You combine one or more PVs into a single, large pool of storage called a Volume Group.

Logical Volume (LV)

A "slice" you carve out of the VG bucket. This is what you format with a file system and mount.

Another Logical Volume

You can create multiple LVs from the same VG, like creating different partitions.

Pros and Cons of LVM

Pros (The Power of Flexibility)

  • Easy Resizing: You can grow or shrink Logical Volumes on a live system without downtime. This is LVM's killer feature.
  • Spanning Disks: A single file system can be larger than any single physical disk by combining multiple disks into one Volume Group.
  • Snapshots: You can create instantaneous, point-in-time "snapshots" of a logical volume, which is invaluable for consistent backups.
  • Thin Provisioning: Allocate a large logical volume to a user but have it only consume physical space as they actually use it.

Cons (Complexity and Recovery)

  • Increased Complexity: The additional abstraction layer (PV -> VG -> LV) adds steps to the setup process and has a steeper learning curve than simple partitioning.
  • Difficult Recovery: If a disk in a multi-disk volume group fails, or if the LVM metadata becomes corrupted, data recovery can be significantly more complex than with a standard partition.

Implementation on CentOS: A Real-World Scenario

Scenario: You've added two new 20GB disks (/dev/sdb and /dev/sdc) to your server. You need to create a flexible 30GB file system for application data, leaving some space for future growth.

Step 1: Create the Physical Volumes (PVs)

First, we initialize our raw disks as LVM Physical Volumes. This marks them as available for LVM to use.

# Initialize the first disk
$ sudo pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

# Initialize the second disk
$ sudo pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.

# Verify the PVs
$ sudo pvs
  PV         VG   Fmt  Attr PSize   PFree 
  /dev/sdb        lvm2 ---  <20.00g <20.00g
  /dev/sdc        lvm2 ---  <20.00g <20.00g

Step 2: Create the Volume Group (VG)

Now, we combine our two PVs into a single "bucket" of storage. We'll name it `app_vg`.

# Create the VG using both PVs
$ sudo vgcreate app_vg /dev/sdb /dev/sdc
  Volume group "app_vg" successfully created

# Verify the VG
$ sudo vgs
  VG      #PV #LV #SN Attr   VSize  VFree 
  app_vg    2   0   0 wz--n- <39.99g <39.99g

We now have a single storage pool of nearly 40GB.

Step 3: Create the Logical Volume (LV)

Next, we carve out a 30GB "slice" from our `app_vg` bucket. We'll name this logical volume `data_lv`.

# Create a 30GB logical volume named 'data_lv' from the 'app_vg' group
$ sudo lvcreate -L 30G -n data_lv app_vg
  Logical volume "data_lv" created.

# Verify the LV
$ sudo lvs
  LV      VG      Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data_lv app_vg  -wi-a----- 30.00g

The new logical volume is now available as a block device at /dev/app_vg/data_lv.

Step 4: Format and Mount the Logical Volume

The final step is to treat our new LV like any other partition: format it with a file system and mount it persistently.

# 1. Format the LV with XFS
$ sudo mkfs.xfs /dev/app_vg/data_lv

# 2. Create a mount point
$ sudo mkdir /appdata

# 3. Edit /etc/fstab to add the persistent mount
# (Use 'sudo blkid /dev/app_vg/data_lv' to get the UUID)
# Add this line to /etc/fstab:
UUID="your-lv-uuid"  /appdata   xfs   defaults   0 0

# 4. Mount and verify
$ sudo mount -a
$ df -h /appdata
The Power of LVM in Action: Extending a Live Filesystem

Scenario: The /appdata filesystem is getting full. We need to add the remaining 10GB from our volume group to it, without any downtime.

  1. Extend the Logical Volume: Use lvextend to add the remaining free space in the VG to our LV.
    # The -r flag resizes the filesystem at the same time
    # The -l +100%FREE uses all remaining free space in the VG
    $ sudo lvextend -r -l +100%FREE /dev/app_vg/data_lv
  2. Verify the Change: The command should complete in seconds. Now, re-run the df -h command.
    $ df -h /appdata
    Filesystem                    Size  Used Avail Use% Mounted on
    /dev/mapper/app_vg-data_lv    40G   25G   15G  63% /appdata

You have just seamlessly expanded a live filesystem from 30GB to 40GB with zero downtime. This incredible flexibility is why LVM is the standard for enterprise Linux storage.

Modules (33) ← CentOS / Linux Administration