LVM is Dyn-o-mite

LVM is Dyn-o-mite

Published Dec. 15, 2024, 7:07 p.m. by dwest

This topic was the inspiration for starting a tech blog. Unless you are a Systems Administrator or a Linux Geek, then you probably never paid much attention to LVM, since standard partitioning is “good enough” for most cases. However, I am often asked for help when someone runs out of disk space and their system was setup with logical volumes. This is often the case for Red-Hat based distro’s. I started out by creating a wiki page to both help guide newer users when configuring it, but also as a helper tool that I can reference if I can’t remember the syntax for a specific operation. One day, a former co-worker contacted me and said that at their new job, one of their servers ran out of disk space on their root partition due to log data filling it up. The previous administrator had set it up using LVM, but none of the Systems Administrators were familiar with it, so they did not know how to fix it. My former co-worker had made a copy of my LVM wiki page and used that to save the day. If that page can make that big of a difference at just one company, then maybe creating a blog about it can help even more users.

LVM Overview

Logical Volume Manager (LVM) is an abstraction layer that sits on top of normal disk partitions. It provides additional features, such as dynamic resizing and snapshots, that standard disk partitioning does not offer. It also lets you combine multiple disks together to form a single continuous volume, which is similar to some types of RAID configurations. One of the most useful features is that it lets you resize your filesystem without requiring a that you restart your system or unmount the existing volume. This can save you some downtime if a virtual disk is in danger of filling up and you need to increase the space it uses. However, it can also be confusing since there are many steps required to set it up correctly. This document will outline the basic steps to create and resize LVM volumes.

Volume Types

LVM consists of a few layers that provide certain functions to the storage volumes. Since we will be adjusting these layers, it is important to understand what each of them does.

Physical Volume (PV) - This is the base storage volume of the LVM and usually sits on top of a physical disk or partition. You can think of it as being a disk that has been initialized with LVM. Each new disk or partition added will contain their own physical volumes and can be combined using volume groups

Volume Group (VG) - The Volume Groups layer sits on top of the physical volume layer and can be used to aggregate multiple physical volumes together. This allows you to add additional disks to a storage pool that the Volume Group can scale across. It also holds the logical volumes

Logical Volume (LV) - The Logical Volumes are similar to a traditional partition and is where you will create your filesystems. Unlike standard partitions, logical volumes have the advantage of being easily resized or moved within a volume group, which might span multiple disks. Each logical volume will utilize a slice of the same volume group for disk space, so you can readjust your partitions after installation if your original scheme did not provide adequate space.

Commands

Below is a list of common commands that you will need to use when interacting with LVM. I highly recommend reading their MAN pages, but here is a quick bulleted list for reference:

  • fdisk - Manipulate MBR disk partition table
  • gdisk - Interactive GUID partition table (GPT) manipulator
  • pvscan - scan all disks for physical volumes
  • pvdisplay - display attributes of a physical volume
  • pvcreate -initialize a disk or partition for use by LVM
  • vgscan - scan all disks for volume groups and rebuild caches
  • vgdisplay - display attributes of volume groups
  • vgcreate - create a volume group
  • lvscan - scan (all disks) for Logical Volumes
  • lvdisplay - display attributes of a logical volume
  • lvcreate - create a logical volume in an existing volume group
  • lvextend - extend the size of a logical volume
  • lvreduce - reduce the size of a logical volume
  • resize2fs - ext2/ext3/ext4 file system resizer

Partitioning Guides

Create an LVM Partition

Most of the time, new LVM partitions will get created during the OS installation process. However, as you add new disks to a system, you might need to create fresh volumes, so we will walk through this process. It is also worth noting that you can create a physical volume on a bare disk instead of a partition. However, there are some pro’s and con’s with doing this, with the main one being that you lose some flexibility when resizing a physical volume. For this walk-thru, I am going to assume you are adding LVM to a partition with a disk named like this “/dev/sd“. Substitute the “” part with the actual drive letter and partition number. Depending on the OS and hardware you are using, you might get drive names like “ens160”, “eno1”, “nvme0n1”, etc. Just substitute your actual drive names anywhere you see “sd“.

  1. Create a standard partition to hold the physical volumes. Use whichever partitioning tool that is appropriate for your environment:
  • Using gdisk - Handles both GPT and MBR partition tables. Required if the partition is greater than 2TB

    gdisk /dev/sd<drive>
    o create a new empty GUID partition table (GPT)
    n add a new partition
    
    Enter the Hex Code for Partition Type (8e00 Linux LVM)
    Hex code or GUID (L to show codes, Enter = 8300): 8e00
    
    Write changes to disk
    w write table to disk and exit
    
  • Using fdisk - Older partitioning tool that usually can only do MBR partitions. It is sometimes symlinked to gdisk, so it might function the same.

    fdisk /dev/sd<drive>
    o create a new empty partition table (MBR)
    n add a new partition
    
    Enter the Hex Code for Partition Type (8e00 Linux LVM)
    Hex code or GUID (L to show codes, Enter = 8300): 8e00
    
    Write changes to disk
    w write table to disk and exit
    
  • Using parted - Provides more partitioning features than gdisk and fdisk, but is also more complicated to use without a GUI (aka gparted)

    parted /dev/sd<drive> -- mklabel <msdos|gpt>  # Pick msdos for MBR or gpt for GPT partition tables
    parted /dev/sd<drive> -- mkpart primary <512MiB> - <8GiB>  # This is the partition size, so adjust these numbers to fit your requirements
    
  1. Create a physical volume

    pvcreate /dev/sd<device><part#>
    
  2. Create a Volume Group. Replace “” with whatever name you want to name it (ex. vg01)

    vgcreate <vg_name> <device><part#>
    
  3. Create a Logical Volume. You can use a specific size or use the volume groups free disk space.

    lvcreate -L<size> -n <label> <vg_name>
    
    ex. lvcreate -L25G -n home vg01

    lvcreate -l 100%FREE -n <label> <vg_name>
    
    ex. lvcreate -l 100%FREE -n home vg01

  4. Format the Volume with your filesystem of choice.

    mkfs -t <type> /dev/<vg_name>/<label>
    
    ex. mkfs -t ext4 /dev/vg01/home

For some filesystems, you might need to install some extra tools. However, EXT4 should be available for most modern Linux OS’s.

Resize an Existing LVM Partition

This will resize an existing LVM partition with the assumption that there is available space. This is often done in a virtual environment, using shared storage, when a volume has run out of space. In this scenario, make sure you have expanded the disk size using a partition manager, such as fdisk, gdisk, etc. One thing to note before you try this, check the filesystem type you are using. If you are using XFS, know that you can NOT shrink the filesystem. You can only grow it.

  1. Scan and display all physical volumes. This will also display if a PV has unused, “Free”, space.

    pvscan
    

  2. Scan and display all the Volume Groups. This will also display if the VG has unused, “Free”, space. This will automatically change when you resize the logical volume

    vgscan
    vgdisplay
    

  3. Scan and display all Logical Volumes. Make note of the “LV Path”

    lvscan
    lvdisplay
    

  4. Adjust the size of the Logical Volume using one of the following methods

  • Extend Logical Volume to a fixed amount

    lvextend -L<size> <LV Path>
    
    ex. lvextend -L12G /dev/vg01/root

  • Append additional space to the logical volume

    lvextend -L+<size> <LV Path>
    
    ex. lvextend -L+12G /dev/vg01/root

  • Grow the logical volume using all remaining space

    lvextend -r -l +100%FREE <LV Path>
    
    ex. lvextend -r -l +100%FREE /dev/vg01/root

    Note:
    Adding ‘-r’ to any of the previous commands will resize the filesystem to match the volume size. You can then skip the next step if you used it.
  1. Resize the Filesystem
  • EXT4 and similar files systems

    resize2fs <lvname> <size> 
    

  • XFS Filesystem

    • XFS Grow using a specific size
      xfs_growfs -D <size> <lvname> 
      
    • Grow using all available space
      xfs_growfs -d <lvname> 
      
    WARNING:
    Take care when shrinking a volume. Not all filesystems support shrinking and will get corrupted if you try. If you need to make more room on a volume by reducing the volume size of another, then you can do so with the following:
    lvreduce -L-<size> <LV Path>
    

    ex. lvreduce -L-12G /dev/vg1/root

    Note:
    Unless you are using the -r flag in the previous commands, you should first resize the filesystem before reducing the logical volume. Otherwise, you will corrupt the filesystem. Check the next step for instructions.

So there you have it. You have been shown step-by-step how to create and resize an LVM volume. Please let me know if you have any questions or find any errors. Also, if you have suggestions to make it better, then I would be happy if you left a comment for me.

Much of this was compiled using the following sources:

  • http://www.rootusers.com/how-to-increase-the-size-of-a-linux-lvm-by-expanding-the-virtual-machine-disk/
  • http://www.linux.com/community/forums/red-hat/adjusting-rescanning-iscsi-drive-size
  • http://www.linuxuser.co.uk/features/resize-your-disks-on-the-fly-with-lvm
  • https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Online_Storage_Reconfiguration_Guide/iscsiconfig.html
  • https://www.centos.org/docs/5/html/Cluster_Logical_Volume_Manager/VG_create.html
  • http://www.thegeekstuff.com/2010/08/how-to-create-lvm/
  • https://www.centos.org/docs/5/html/Cluster_Logical_Volume_Manager/LV_create.html

Share this post

Similar posts

There are no similar posts yet.

0 comments

There are no comments.

Add a new comment