Linux - Logical Volume Manager

LVM - Logical Volume Manager provides a method of addressing a pool of space to manage storage and thus allows for very flexible disk space operations rather than addressing individual disks.

It provides features like the ability to add disk space to a logical volume and its filesystem while that filesystem is mounted and active and it allows for the collection of multiple physical hard drives and partitions into a single volume group which can then be divided into logical volumes.

LVM Structure

LVM Structure

LVM is a layered structure: A Logical Volume sits inside a Volume Group that can span Physical Volumes. A Volume Group can contain a number of Logical Volumes.

When a Physical Disk is set up for LVM, metadata is written at the beginning of the disk for normal usage, and the end of the disk for backup usage.

  1. Logical Volume
  2. Volume Group
  3. Physical Volume

Physical Volume

# Get an overview of the current physical volumes:
pvscan

# Create a new physical volume (in this case 3 new volumes):
pvcreate /dev/xvdf{1,2,3}

# See Physical Extends:
pvdisplay /dev/xvdf1

Volume Group

# View information about volume groups:
vgdiplay <VG_NAME>

# Create a Volume Group:
vgcreate myvol \
    /dev/xvdf1 \
    /dev/xvdf2 \
    /dev/xvdf3

When running pvdisplay Physical Extent’s will be visible.

# Create a Volume Group with a given Physical Extent size:
vgcreate myvol /dev/xvdf1 /dev/xvdf2 /dev/xvdf3 -s 2M

Note: This changes the default PE size from 4MB to whatever you set it to. It has no effect on normal I/O performance - large numbers of extents can slow down LVM tools, but for actual usage it doesn’t matter as of LVM2.

Leftover space in the volume is used as metadata.

Logical Volume

Create a Logical Volume (allocate 12 PE’s evenly distributed across the 3 physical volumes):

lvcreate -l 12 -n 12ext myvol /dev/xvdf1:1-4 /dev/xvdf2:1-4 /dev/xvdf3:1-4

Note:

  • -l uses nr of PE’s for calculating the size of the logical volume
  • -L uses a specified PE size for calculating the size of the logical volume
  • -n allows you to set the logical volume’s name
# See Volume status:
lvdisplay myvol

# Extend a logical volume:
lvextend -L +10M /dev/myvol/mirror

Reduce a logical volume:

  • Cannot be done while LV mounted
  • Shrink the filesystem before LV –> else: Corrupted files!!
lvreduce -L -10M /dev/myvol/mirror

RAID

# Create mirrors:
lvcreate -L 20M -m2 mirror myvol

# Create stripes:
lvcreate -L 20M -i3 -I64 -n stripe myvol

LVM Snapshots

mkfs.xfs /dev/myvol/stripe
mount /dev/myvol/stripe /mnt
touch /mnt/myfile
df -Th

# Create a snapshot:
lvcreate -s -n snap -L 5M /dev/myvol/stripe

Snapchot size can be extended automatically when it is filled up to a certain point (extesion size can be set also).

LVM Thin Provisioning

Thin Provisioning is a concept that you’re able to overallocate resources to serve the needs of the clients while you work on the backend (works at the Logical Volume level of LVM).

# Setup a pool for Thin Provisioning:
lvcreate -l 290 --thinpool mythinpool myvol

# Create something in the pool
lvcreate -V 5G --thin -n client1 myvol/mythinpool

Extending LVM Disks

# 1. Add new physical disk space
pvcreate /dev/<DEVICE_NAME>

# 2. Extend the volume group
vgextend <VG_NAME> /dev/<DEVICE_NAME>

# 3. Extend the logical volume
lvextend -r -L +100%FREE /dev/<VG_NAME>/<LV_NAME>

Note: The -r flag will resize the filesystem if possible.