Table of Contents

Logical Volume Management (LVM)

General Information

LVM provides a way to create virtual disks and containers in order to provide flexibility with parition sizes and extending/shrinking space without shutting a system down.

Checklist


Expanding or New

Whether expanding or creating new storage, the summarized steps are:


Add/Expand Disk

Add or expand the disk to the system either physically or virtually if a VM.


Add Disk: Scan SCSI Bus

If adding a disk to a virtual machine and the new disk does not show up:


One Liner Method

echo "- - -" > /sys/class/scsi_host/$(grep mpt /sys/class/scsi_host/host?/proc_name | grep -o -E '(host[0-9])')/scan


Manual Method

Expand Disk: Re-Scan SCSI Bus

If you have expanded an existing disk, rescan the scsi bus

echo 1 > /sys/class/scsi_device/<device>/rescan

Where “<device>” is your disk such as “sda”.


Format the disk

/dev/sdb is used in this example.

Use gdisk in order for GPT instead of MBR. (seriously, don't use fdisk…let MBR die already)

gdisk /dev/sdb

Create a New Empty GUID Partition Table

Once in gdisk:

Create the LVM Partition


Create a Physical Volume

About: Think of physical volumes as your “virtual disks” to use in LVM.

Create a physical volume using the parition that the LVM is on.

pvcreate <device1-path> [device2-path] [deviceX-path]

Example:

pvcreate /dev/sdb1

Continue on to either Expanding Storage or Creating New Storage.


Expanding Storage

Expanding storage steps.

Expand: Summarized Steps

To expand storage for a logical volume, the summarized steps are:

Performed above in “Expanding or New”:

Remaining Steps:


Extend the Volume Group

  1. Add the new physical disk to the existing volume group
    vgextend vglocal /dev/sdb1

Extend Logical Volume

  1. Extend the logical volume that needs the space (/home will be used in this example)
    1. Option 1(Preferred): Move the physical extents of the logical volume to the new disk, so all of it resides on the same VMDK, and then use all of that new disk's space.
      1. There may be a performance hit if a logical volume is spanned across multiple VMDKs, so this method is preferred.
        pvmove --name lvhome /dev/sda2 /dev/sdb1
        lvextend --resizefs /dev/vglocal/lvhome /dev/sdb1
    2. Option 2: Give the logical volume all of the space from the newly added physical volume
      lvextend --resizefs --extents +100%PVS /dev/vglocal/lvhome /dev/sdb1
    3. Option 3: Give the logical volume a specific amount of additional free space from a specific physical volume
      lvextend --resizefs --size +10G /dev/vglocal/lvhome /dev/sdb1
    4. Option 4: Give the logical volume all of the free space available to the volume group (potentially across multiple physical volumes)
      lvextend --resizefs --extents +100%FREE /dev/vglocal/lvhome

Extending a Logical Swap Volume

Extending a swap volume is slightly different, as the “–resizefs” flag will not work.

  1. Example: Specify the total size of the logical swap volume
    lvextend --size 8G /dev/vglocal/lvswap /dev/sdb1
  2. Disable all swap
    swapoff -a
  3. Create a new swap area, overwriting the original
    mkswap /dev/vglocal/lvswap
  4. Enable swap
    swapon -a
  5. Verify
    swapon -s

Verify New LVM Layout

  1. Verify LVM Allocation
    1. Logical Volume:
      lvs
    2. Volume Group:
      vgs
    3. Physical Volumes:
      pvs

Logical Volume Location(s) on Physical Volumes

If you would like to see what logical volumes reside where, a nice command to view physical extent to logical volume mappings is:

pvdisplay -m

Grow File System

Skip if you used the “–resizefs” flag to lvextend above.

  1. Grow the file system
    1. Ext2/3/4
      resize2fs /dev/vglocal/lvhome
    2. XFS
      xfs_growfs /dev/vglocal/lvhome
  2. Verify filesystem space
    df -h

Cleanup

If you have just moved all of the data of a logical volume off of an old physical volume, the old physical volume can go away if it is no longer in use.

Examples below: You have just moved everything off of /dev/sdc1 to a new disk and can now remove the old disk (/dev/sdc).

Verify Old Physical Volume Not Used

pvs -o +pv_used

Reduce Volume Group

vgreduce vgname olddisk

vgreduce vglocal /dev/sdc1

Remove Physical Volume

pvremove olddisk

pvremove /dev/sdc1

Delete Virtual Disk

Login to the virtualization user interface and delete the old disk from inventory.


Errors During LVM Commands


Creating New Storage

Create New: Summarized Steps

To create new storage for a logical volume, the summarized steps are:

Performed above in “Expanding or New”:

Remaining Steps:


Create a Volume Group

About: Volume groups aggregate physical volumes into a usable pool of disk space.

vgcreate <vgname> <physical-device>

Example:

vgcreate vgstorage /dev/sdb1

Create a Logical Volume

About: Logical volumes use the disk space made available via a volume group.

lvcreate –size <size> –name <lv-name> <vg-name>

Example: Create a 100G logical volume

lvcreate --size 100G --name lvbackups vgstorage

Example: Create a logical volume that uses 100% of the volume group free extents

lvcreate --extents 100%FREE --name lvbackups vgstorage

Create the file system on the logical volume

About: So far, the only things being created were pretty much containers. Create the actual file system below.

mkfs -t <fstype> -L <label> /dev/mapper/<vgname>-<lvname>

Example:

mkfs -t ext4 -L Backups /dev/mapper/vgstorage-lvbackups

Mount the logical volume by uuid

Find the UUID with blkid

blkid
/dev/mapper/vgstorage-lvbackups: UUID="d89744ad-133c-452b-98d8-9480ef18fe77" TYPE="ext4"

Add entry to /etc/fstab by UUID

vim /etc/fstab
UUID=d89744ad-133c-452b-98d8-9480ef18fe77 /opt/backups ext4 defaults 0 2

Mount the fstab entries and check to see if it mounted with df

mount -a
df -h

LVM Renaming

We don't always get a name right the first time. Luckily, LVM allows for easy renaming of logical volumes and volume groups.

Rename a Volume Group (vgrename vg-oldname vg-newname)

vgrename vgbackupmirror vgredusb

Rename a Logical Volume (lvrename vgname lv-oldname lv-newname)

lvrename vgredusb lvbackupmirror lvredbackups

LVM Snapshots

LVM allows you to “freeze” the metadata of files. This allows for things like rsync to complete successfully even if files it is trying to sync are modified during the operation.

{TODO}