Logical Volume Manager allows to create an abstraction layer over physical storage and therefore, provides more flexibility than using physical storage directly.

Some advantages are:

  • Flexible capacity: filesystems can be extended across multiple disks.
  • Resizeable storage pools: You can extend or reduce logical volumes with a single command, without reformatting and repartitioning the physical disk.
  • Disk stripping and mirroring volumes: you can create RAID volumes easily.
  • More: snapshots, online data relocation,…

But there are some disadvantages:

  • Windows does not recognize LVM disks. If you are going to install Windows and Linux, use physical partitions for Windows (or don’t use LVM at all).
  • It could be better to use a physical partition for the bootloader.

Table of Contents

Install required package

Install lvm2 if it is not already installed. You will need to be a root user or have superuser permissions to manage LVM.

LVM architecture

  • Physical Volume: under a Physical Volume (PV) there is a physical disk or partition. One PV corresponds to one disk or partition.

  • Volume Group: PVs are combined into a Volume Group (VG). A VG is a storage pool out of which Logical Volumes can be allocated.

  • Logical Volume: is the component that is used by file systems and applications.

Create a Physical Volume

You can create PVs from entire disks or from partitions, but using entire disks is the recommended option.

  1. Make sure the disk does not have partitions (nor a partition table).
  2. Create a PV: pvcreate <device>
    pvcreate /dev/sdb
    
  3. You may need to confirm that you want to erase any signature on the disk.
    WARNING: dos signature detected on /dev/vdb at offset 510. Wipe it? [y/n]: y
    

You can add several PVs at the same time by adding device paths separated by spaces.

pvcreate /dev/sdb /dev/sdc /dev/sdd

Create a Volume Group

  1. Add the PV to a new Volume Group.
    vgcreate <vg name> <physical volume>
    # vgcreate vg1 /dev/sdb
    

Create a Logical Volume

  1. You can create LVs with any size (inside VG limits). You define the size with the -L or -l parameters.
    lvcreate -L <size> -n <lv name> <volume group>
    # lvcreate -L 500M -n lv1 vg1
    
    • -L: allows to indicate an specific size (like 100M or 20G).
    • -l: allows to specify the LV size relative to a VG or PV. For example, -l 100%FREE create a LV that uses all the unallocated space of the VG. -l 50%VG uses 50% of the total space of the VG.

Format (create a filesystem) the Logical Volume

  1. You can use a LV like a physical partition (just take note of the VG and LV names). You can format it using standard tools like mkfs.ext4 (for EXT4 filesystems).
    mkfs.ext4 /dev/vg1/lv1
    

Adding a PV to an existing VG

  • Use vgextend to extend a Volume Group.
    vgextend <volume group> <physical volume>
    # vgextend vg1 /dev/sdc
    

Extend a LV

Use lvextend to grow a LV (remember to add the + before the size).

lvextend -L +<size> <logical volume full path>
# lvextend -L +20G /dev/vg1/lv1
  • You can use -l like in lvcreate.

Reduce a LV

You can use lvreduce (remember to add the - before the size). Before reducing a LV that contains a file system, ensure that the file system is not using the space that is going to be reduced. Also, use the parameter --resizefs to reduce the filesystem before reducing the LV.

lvreduce --resizefs -L -<size> <logical volume full path>
# lvreduce --resizefs -L -10G /dev/vg1/lv1

Remove a PV from a VG

First you need to ensure your PV is not being used by any LVs. If it’s the case, you need to run pvmove to move the contents from the PV you want to remove to other PV.

pvmove <physical volume>
# pvmove /dev/sdb
  • You can add the PV where you want the contents be moved. Otherwise, the contents are spread among the other PVs.
    pvmove /dev/sdb /dev/sdc
    

Now you can remove the physical volume.

vgreduce <volume group> <physical volume>
# vgreduce vg1 /dev/sdb

RAID

You can create any RAID (0/1/4/5/6/10) but, for this tutorial, I will show how to create a RAID0 (stripe) and a RAID1 (mirror).

RAID0

If your volume group has at least 2 physical volumes, you can create a logical volume that distributes the files between the PVs. This can improve data I/O. To create a stripped LV, use the parameters -i (for the number of physical volumes) and -l (for the size of the stripe in kilobytes).

lvcreate -L 100G -i 2 -l 64 -n lv1 vg1

RAID1

When using at least two PVs, you can use one of them for mirroring the data. Use the --type raid1 parameter to create a RAID1 and the -m parameter to specify how many copies of the data you want.

lvcreate --type raid1 -m 1 -L 50G -n lv1 vg1