Skip to content

Linux Logical Volume Manager (LVM): A Comprehensive Guide

Introduction

Linux Logical Volume Manager (LVM) is a powerful storage management system that provides flexibility and scalability for managing disk space. LVM allows administrators to create, resize, and manage logical volumes, making it easier to manage disk space and ensure data availability.

LVM Architecture

The LVM architecture consists of several layers:

1. Physical Volumes (PVs)

Physical volumes are the underlying storage devices, such as hard drives or solid-state drives.

2. Volume Groups (VGs)

Volume groups are collections of physical volumes. A volume group can contain multiple physical volumes.

3. Logical Volumes (LVs)

Logical volumes are created from volume groups and are the actual storage units used by the system.

Key Concepts and Terminologies

  • Physical Extent (PE): The smallest unit of storage in LVM, typically 4MB.
  • Logical Extent (LE): A mapping of a physical extent to a logical volume.
  • Volume Group Descriptor Area (VGDA): Stores metadata about the volume group.
  • Logical Volume Manager (LVM): The software that manages logical volumes.

LVM Commands

Physical Volume Management

pvcreate

Initializes a physical volume for use with LVM.

Usage: pvcreate /dev/sda1

Options:

  • --metadatacopies: Sets the number of metadata copies.
  • --metadatasize: Sets the metadata size.

pvdisplay

Displays information about physical volumes.

Usage: pvdisplay /dev/sda1

Options:

  • --verbose: Displays detailed output.
  • --brief: Displays brief output.

pvremove

Removes a physical volume from LVM.

Usage: pvremove /dev/sda1

Options:

  • --force: Forces removal.
  • --verbose: Displays detailed output.

pvscan

Scans all supported LVM block devices for physical volumes.

Usage: pvscan

Options:

  • --cache: Updates the LVM cache.
  • --verbose: Displays detailed output.

Volume Group Management

vgcreate

Creates a new volume group.

Usage: vgcreate vg_name /dev/sda1

Options:

  • --physicalextentsize: Sets the physical extent size.
  • --maxphysicalvolumes: Sets the maximum number of physical volumes.

vgdisplay

Displays information about volume groups.

Usage: vgdisplay vg_name

Options:

  • --verbose: Displays detailed output.
  • --brief: Displays brief output.

vgextend

Adds physical volumes to a volume group.

Usage: vgextend vg_name /dev/sdb1

Options:

  • --physicalextentsize: Sets the physical extent size.

vgreduce

Removes physical volumes from a volume group.

Usage: vgreduce vg_name /dev/sdb1

Options:

  • --force: Forces removal.
  • --verbose: Displays detailed output.

vgremove

Removes a volume group.

Usage: vgremove vg_name

Options:

  • --force: Forces removal.
  • --verbose: Displays detailed output.

Logical Volume Management

lvcreate

Creates a new logical volume.

Usage: lvcreate -L 10G -n lv_name vg_name

Options:

  • --size: Sets the logical volume size.
  • --name: Sets the logical volume name.

lvdisplay

Displays information about logical volumes.

Usage: lvdisplay vg_name/lv_name

Options:

  • --verbose: Displays detailed output.
  • --brief: Displays brief output.

lvextend

Resizes a logical volume.

Usage: lvextend -L +5G vg_name/lv_name

Options:

  • --size: Sets the new logical volume size.
  • --resizefs: Resizes the file system.

lvreduce

Resizes a logical volume.

Usage: lvreduce -L -5G vg_name/lv_name

Options:

  • --size: Sets the new logical volume size.
  • --resizefs: Resizes the file system.

lvremove

Removes a logical volume.

Usage: lvremove vg_name/lv_name

Options:

  • --force: Forces removal.
  • --verbose: Displays detailed output.

lvrename

Renames a logical volume.

Usage: lvrename vg_name/lv_name new_name

Options:

  • --verbose: Displays detailed output.

Increase or Decrease Size of LVM logical volumes

Root / OS logical Partitions

Since these partitions are already mounted, you'd need a live/recue cd (Ubunutu ISO works well too). Prerequisites 1. Boot into rescue mode or a live CD/USB to avoid modifying the root filesystem while it's in use. 2. Verify the current size and usage of the root LV: df -h and lvdisplay /dev/mapper/vg-root

Steps

#Step 1: Unmount the Root Filesystem
umount /dev/mapper/vg-root

#Step 2: Resize the Root Filesystem
resize2fs -p /dev/mapper/vg-root 10G

#Step 3: Reduce the LV Size
#Reduce the LV size by the desired amount (e.g., 5G). The -L option specifies the new size.
lvreduce -L -5G /dev/mapper/vg-root

#Extend all available space, replace +100% to percent you want to extend.
lvextend -l +100%FREE /dev/mapper/vg-root

#Step 5: Update the Filesystem. Update the filesystem to reflect the new size. (works for extend or reduce)
resize2fs -p /dev/mapper/vg-root

#Note: Steps 2 & 3 can be combined with following command
lvreduce --fs resize_fsadm --size -70G /dev/mapper/vg-root
#or
lvreduce --resizefs --size -70G /dev/mapper/vg-root

Additional Commands to get details on LVM

1
2
3
4
lvdisplay /dev/mapper/vg-root
lvscan
lvs
pvs

Argument Details resize2fs - -p: Prints progress. - /dev/mapper/vg-root: Specifies the device to resize. - 10G: Specifies the new size (adjust to your needs).

lvreduce - -L: Specifies the new size. - -5G: Reduces the LV size by 5G (adjust to your needs). - /dev/mapper/vg-root: Specifies the LV to reduce.

Advanced LVM Concepts

1. Thin Provisioning

Thin provisioning allows for the creation of logical volumes that are larger than the available physical storage.

2. Snapshots

Snapshots create a point-in-time copy of a logical volume.

3. Mirroring

Mirroring creates a duplicate copy of a logical volume for data redundancy.

4. Striped Volumes

Striped volumes distribute data across multiple physical volumes for improved performance.

LVM Configuration Files

  • /etc/lvm/lvm.conf: The main LVM configuration file.
  • /etc/lvm/archive: Stores metadata about volume groups and logical volumes.
The content provided is generated with the help of artificial intelligence (AI) and may contain inaccuracies or outdated information due to the limitations of AI. While I strive to review and validate the content, some errors or inaccuracies may still be present in the final output. Please use this content as a general guide only and verify any critical information through reputable sources before relying on it. I appreciate your understanding and feedback in helping us improve the accuracy and quality of our AI-generated content."