204.3 Logical Volume Manager (weight: 3)

Tech Tutorial: 204.3 Logical Volume Manager (LVM) #

Introduction #

Logical Volume Manager (LVM) is a flexible and advanced option in Linux for disk management. It allows for the resizing of disks, the creation of snapshots, and the combining of multiple physical disks into a single logical volume. LVM is particularly useful in environments where disk space requirements are constantly changing. This tutorial will guide you through the essentials of managing logical volumes using LVM on a Linux system.

Key Knowledge Areas #

  • Create and manage physical volumes, volume groups, and logical volumes
  • Extend logical volumes
  • Resize logical volumes
  • Use LVM snapshots
  • Recover physical volumes, volume groups, and logical volumes

Utilities #

  • pvcreate, pvremove, pvchange, pvscan
  • vgcreate, vgchange, vgextend, vgreduce, vgremove, vgscan
  • lvcreate, lvextend, lvreduce, lvchange, lvremove, lvscan
  • lvs, vgs, pvs

Step-by-Step Guide #

Initial Setup #

Before using LVM, ensure the lvm2 package is installed on your system.

sudo apt-get install lvm2  # Debian/Ubuntu
sudo yum install lvm2      # CentOS/RHEL

Managing Physical Volumes (PV) #

  1. Creating a Physical Volume:

    To create a physical volume, you first need a free disk or partition. Use fdisk or lsblk to identify a free disk.

    sudo pvcreate /dev/sdb
    
  2. Displaying Physical Volume Information:

    sudo pvscan
    sudo pvs
    
  3. Removing a Physical Volume:

    sudo pvremove /dev/sdb
    

Managing Volume Groups (VG) #

  1. Creating a Volume Group:

    sudo vgcreate vg1 /dev/sdb
    
  2. Extending a Volume Group:

    Add another physical volume to the existing volume group.

    sudo vgextend vg1 /dev/sdc
    
  3. Reducing a Volume Group:

    Remove a physical volume from the volume group.

    sudo vgreduce vg1 /dev/sdc
    
  4. Removing a Volume Group:

    sudo vgremove vg1
    

Managing Logical Volumes (LV) #

  1. Creating a Logical Volume:

    sudo lvcreate -n lv1 -L 10G vg1
    
  2. Extending a Logical Volume:

    Extend the logical volume by an additional size.

    sudo lvextend -L +5G /dev/vg1/lv1
    
  3. Reducing a Logical Volume:

    Reduce the logical volume by a specified size.

    sudo lvreduce -L -5G /dev/vg1/lv1
    
  4. Removing a Logical Volume:

    sudo lvremove /dev/vg1/lv1
    

Using LVM Snapshots #

  1. Creating a Snapshot:

    sudo lvcreate -L 1G -s -n snap_lv1 /dev/vg1/lv1
    
  2. Restoring from a Snapshot:

    sudo lvconvert --merge /dev/vg1/snap_lv1
    

Conclusion #

LVM is a powerful tool that offers considerable flexibility in managing disk space on Linux systems. By mastering LVM, administrators can easily adjust their storage architecture as needs evolve. This tutorial has covered the basics of creating, managing, and manipulating physical volumes, volume groups, and logical volumes, along with practical examples to help you get started with LVM. Whether you are managing a single server or a complex multi-disk system, LVM provides the tools necessary to manage your storage effectively and efficiently.