mitchellroe.dev

LVM guide (with linear and striped volumes)

LVM offers several advantages over a traditional, single volume. There are essentially two schemes one can use:

What is LVM?

LVM stands for Logical Volume Management, and is a system built into most modern Linux distributions. It allows you to combine disks of various sizes, capacities, and types into different types of “logical volumes”, allowing you to distribute and manage your data much more easily than traditional partitions.

There are essentially three layers to LVM:

  1. Physical volumes (PV)
  2. Volume groups (VG)
  3. Logical volumes (LV)

Let’s look at each one in turn.

Physical volumes (PV)

Physical volumes (PV’s) are any sort of physically-bound disk space. PV’s are usually something like a disk or a partition on a disk, but could also be a RAID or an iSCSI target.

There’s not much abstraction yet at this level, as they represent the physical representation and not any sort of logical representation. You can add a disk (or partition, etc.) to LVM by using the pvcreate command. This will format the device so LVM can use it.

Volume groups (VG)

Volume groups (VG’s) combine one or more physical volumes (PV’s) together into a logical grouping. A PV can only be a member of one VG at a time. Volume groups allow you to combine disks of varying capacities, types, etc., just as long as they are formatted as LVM physical volumes, into a single pool of disk resources.

Logical volumes (LV)

A logical volume (LV) is sort of like a “partition” that you create within a volume group (VG). It can be as big or as small as you want, just as long as you don’t exceed the capacity of the VG*.

You can then create a filesystem on a logical volume in much the same way that you would a traditional disk partition. The filesystem behaves just like any other filesystem, but with the added flexibility of being stored on multiple physical volumes.

*You can bypass this restriction by creating a “thin” logical volume, which will dynamically expand as needed.

Why use LVM?

Logical volume management (LVM) provides several advantages, both for flexibility and performance.

LVM improves flexibility

Because LVM abstracts away the physical properties of the underlying disks, you are free to add, remove, or change the underlying disk structures without affecting the filesystem(s) that live on them. For example, you can attach new disks to the system, add them to a volume group, then expand the logical volume (and, optionally, filesystem) to take up that new space, all without needing to take the filesystem offline. You can also reduce or swap out disks as needed within the volume group without the filesystem ever knowing that something has changed.

LVM improves performance

By creating striped volumes, you can perform your reads and writes in parallel, by accessing two or more disks at a time. This is especially beneficial for something like and XCP-ng virtual machine, as disk I/O is performed in a single-threaded process called tapdisk. XCP-ng creates one tapdisk process per virtual disk, meaning the I/O, when using a single VDI, is limited to a single stream of data. Using striped volumes with LVM allows you to spin up multiple tapdisk processes and use them in parallel.

Creating and using an LVM volume

1. Attach disks / physical volumes (PVs) to the VM

2. Pool the PVs into a volume group (VG)

3. Create a logical volume (LV)

Create a logical volume (LV) within the volume group (VG).

4. Add a filesystem to the logical volume (LV)

Create an ext4 (or whatever) filesystem on the logical volume (LV).

5. Configure the mount point

See the output of blkid to get the UUID of the new filesystem. You can then add it to /etc/fstab as you would any other filesystem.

Cookbook

Add an additional disk to a linear LV

NOTE: This only applies to linear LV’s.

Remove a physical volume (PV) from a volume group (VG)

NOTE: This only applies to linear LV’s.

Let’s say you added too many disks to a system, and you need to reclaim some of that space. We’ll remove a PV at /dev/xvdo from the volume group vg0.

Assuming there’s a single logical volume (LV) which uses up the whole volume group (VG), shrink the filesystem and LV by the amount you need to remove. For example, if you need to remove a 64 GiB physical volume (PV) from the vg0 VG, you would use the following lvresize command:

lvresize --size -64G --resizefs vg0/srv

Ensure there is no data on the physical volume (PV) with pvmove:

pvmove /dev/xvdo

Now you can remove the physical volume (PV) from the volume group (VG) with vgreduce:

vgreduce vg0 /dev/xvdo

Now expand the logical volume (LV) to take up whatever leftover space there is in the volume group (VG) with lvresize:

lvresize --extents +100%FREE --resizefs vg0/srv