Skip to content
Search RunEntLinux

Searches general documentation on RunEntLinux. Each distribution has its own documentation search.

    Browse this documentation

    LVM (Logical Volume Manager) is a powerful disk management tool in Linux. It adds an abstraction layer between physical disks and filesystems, making storage allocation and resizing much more flexible.

    LVM consists of three layers:

    LayerAbbreviationDescription
    Physical VolumePV (Physical Volume)The underlying disk or partition; the foundational building block of LVM
    Volume GroupVG (Volume Group)A storage pool composed of one or more PVs
    Logical VolumeLV (Logical Volume)A logical partition allocated from a VG; can be formatted and mounted

    The layer relationships are as follows:

    Disk/Partition --> PV --> VG --> LV --> Filesystem
    /dev/sdb1 --> PV1 -+
    +--> vg_data --> lv_app --> /app
    /dev/sdc1 --> PV2 -+ --> lv_backup --> /backup

    Most Enterprise Linux distributions come with LVM pre-installed. If it is not installed:

    Terminal window
    sudo dnf install lvm2 -y

    Physical volumes are the foundation layer of LVM. Disks or partitions must first be initialized as PVs.

    First, use fdisk or gdisk to create a partition and set its type to 8e (Linux LVM):

    Terminal window
    sudo fdisk /dev/sdb
    # n -> p -> 1 -> Enter -> Enter -> t -> 8e -> w
    sudo partprobe /dev/sdb
    Terminal window
    sudo pvcreate /dev/sdb1
    sudo pvcreate /dev/sdc1

    You can also create a PV directly on an entire disk (no prior partitioning needed):

    Terminal window
    sudo pvcreate /dev/sdd
    Terminal window
    # Brief listing
    sudo pvs
    # Detailed information
    sudo pvdisplay /dev/sdb1

    Volume groups pool multiple PVs into a unified storage pool.

    Terminal window
    sudo vgcreate vg_data /dev/sdb1 /dev/sdc1
    Terminal window
    # Brief listing
    sudo vgs
    # Detailed information
    sudo vgdisplay vg_data

    When new disks are added later, you can add new PVs to an existing VG:

    Terminal window
    sudo pvcreate /dev/sdd1
    sudo vgextend vg_data /dev/sdd1

    Removing a Physical Volume from a Volume Group

    Section titled “Removing a Physical Volume from a Volume Group”
    Terminal window
    # First migrate data to other PVs
    sudo pvmove /dev/sdc1
    # Then remove from the VG
    sudo vgreduce vg_data /dev/sdc1

    Logical volumes are usable storage space carved out from a volume group that can be formatted and mounted.

    Terminal window
    sudo lvcreate -L 50G -n lv_app vg_data

    Creating with a Percentage of Remaining VG Space

    Section titled “Creating with a Percentage of Remaining VG Space”
    Terminal window
    # Use 100% of the remaining VG space
    sudo lvcreate -l 100%FREE -n lv_backup vg_data
    Terminal window
    # Brief listing
    sudo lvs
    # Detailed information
    sudo lvdisplay /dev/vg_data/lv_app
    Terminal window
    # Format as XFS
    sudo mkfs.xfs /dev/vg_data/lv_app
    # Create a mount point and mount
    sudo mkdir -p /app
    sudo mount /dev/vg_data/lv_app /app
    # Verify
    df -hT /app
    Terminal window
    # Get the LV's UUID (you can also use the LV path directly)
    sudo blkid /dev/vg_data/lv_app
    # Edit fstab
    echo '/dev/vg_data/lv_app /app xfs defaults 0 0' | sudo tee -a /etc/fstab

    One of LVM’s most powerful features is online logical volume extension.

    Extending an LV and Its Filesystem Simultaneously

    Section titled “Extending an LV and Its Filesystem Simultaneously”

    The -r flag extends the filesystem automatically along with the LV:

    Terminal window
    # Add 20G of space
    sudo lvextend -r -L +20G /dev/vg_data/lv_app
    Terminal window
    # Extend the LV to 100G
    sudo lvextend -r -L 100G /dev/vg_data/lv_app
    Terminal window
    sudo lvextend -r -l +100%FREE /dev/vg_data/lv_app

    Step-by-Step Extension (manually extending the filesystem)

    Section titled “Step-by-Step Extension (manually extending the filesystem)”

    If you do not use the -r flag, you need to extend the filesystem manually:

    Terminal window
    # Step 1: Extend the LV
    sudo lvextend -L +20G /dev/vg_data/lv_app
    # Step 2: Extend the filesystem
    # For XFS filesystems
    sudo xfs_growfs /app
    # For EXT4 filesystems
    sudo resize2fs /dev/vg_data/lv_app

    These three commands are the most commonly used LVM status queries in day-to-day operations.

    Terminal window
    sudo pvs
    PV VG Fmt Attr PSize PFree
    /dev/sdb1 vg_data lvm2 a-- <50.00g 0
    /dev/sdc1 vg_data lvm2 a-- <50.00g 30.00g
    Terminal window
    sudo vgs
    VG #PV #LV #SN Attr VSize VFree
    vg_data 2 2 0 wz--n- 99.99g 30.00g
    Terminal window
    sudo lvs
    LV VG Attr LSize Pool Origin Data% Meta%
    lv_app vg_data -wi-ao---- 50.00g
    lv_backup vg_data -wi-ao---- 19.99g
    Terminal window
    # Show LV name, size, volume group, and device path
    sudo lvs -o lv_name,lv_size,vg_name,lv_path

    LVM snapshots create a read-only or read-write copy of a logical volume at a specific point in time. They are commonly used for backups and testing.

    Terminal window
    # Create a 10G snapshot of lv_app
    sudo lvcreate -s -L 10G -n lv_app_snap /dev/vg_data/lv_app

    Parameter descriptions:

    • -s: Create a snapshot
    • -L 10G: Snapshot space size (for storing changed data)
    • -n lv_app_snap: Snapshot name
    Terminal window
    sudo lvs

    Snapshot volumes display the Origin (source volume) and Data% (percentage of snapshot space used).

    Mounting a Snapshot (read-only access to historical data)

    Section titled “Mounting a Snapshot (read-only access to historical data)”
    Terminal window
    sudo mkdir -p /mnt/snap
    sudo mount -o ro /dev/vg_data/lv_app_snap /mnt/snap

    You can copy needed files from the snapshot:

    Terminal window
    sudo cp /mnt/snap/important-file.conf /app/important-file.conf

    Or merge the snapshot to roll back the entire logical volume:

    Terminal window
    # First unmount the source volume and the snapshot
    sudo umount /app
    sudo umount /mnt/snap
    # Merge the snapshot (roll back to the state at snapshot time)
    sudo lvconvert --merge /dev/vg_data/lv_app_snap
    # Remount
    sudo mount /dev/vg_data/lv_app /app
    Terminal window
    sudo umount /mnt/snap
    sudo lvremove /dev/vg_data/lv_app_snap

    When removing LVM resources, you must work in order: LV -> VG -> PV.

    1. Unmount and remove the logical volume:

      Terminal window
      sudo umount /app
      sudo lvremove /dev/vg_data/lv_app
    2. Remove the volume group:

      Terminal window
      sudo vgremove vg_data
    3. Remove the physical volumes:

      Terminal window
      sudo pvremove /dev/sdb1 /dev/sdc1

    The following demonstrates the complete process of setting up LVM storage using two new disks, /dev/sdb and /dev/sdc.

    1. Create partitions on both disks:

      Terminal window
      sudo parted /dev/sdb mklabel gpt
      sudo parted /dev/sdb mkpart primary 0% 100%
      sudo parted /dev/sdc mklabel gpt
      sudo parted /dev/sdc mkpart primary 0% 100%
      sudo partprobe
    2. Create physical volumes:

      Terminal window
      sudo pvcreate /dev/sdb1 /dev/sdc1
      sudo pvs
    3. Create a volume group:

      Terminal window
      sudo vgcreate vg_data /dev/sdb1 /dev/sdc1
      sudo vgs
    4. Create logical volumes:

      Terminal window
      sudo lvcreate -L 80G -n lv_app vg_data
      sudo lvcreate -l 100%FREE -n lv_logs vg_data
      sudo lvs
    5. Format and mount:

      Terminal window
      sudo mkfs.xfs /dev/vg_data/lv_app
      sudo mkfs.xfs /dev/vg_data/lv_logs
      sudo mkdir -p /app /var/applogs
      sudo mount /dev/vg_data/lv_app /app
      sudo mount /dev/vg_data/lv_logs /var/applogs
    6. Configure auto-mounting at boot:

      Terminal window
      sudo cp /etc/fstab /etc/fstab.bak
      cat <<EOF | sudo tee -a /etc/fstab
      /dev/vg_data/lv_app /app xfs defaults 0 0
      /dev/vg_data/lv_logs /var/applogs xfs defaults 0 0
      EOF
      sudo mount -a
    7. Verify the results:

      Terminal window
      df -hT /app /var/applogs
      sudo pvs && sudo vgs && sudo lvs

    The flexibility of LVM makes it the preferred solution for managing storage in Enterprise Linux environments. Mastering PV, VG, and LV operations along with online extension and snapshot capabilities will greatly improve storage management efficiency.