Linux Logical Volume Manager (LVM) Deep Dive Tutorial

LVM, short for Logical Volume Manager, is awesome – and it doesn’t seem to get the attention it deserves. In this one-shot tutorial, I’ll show you why you should care about LVM, how to get started, some of the commands you can use to manage it, and more. You’ll even learn how to use it to take snapshots of your system!

Subscribe to LearnLinuxTV

Commands Used

Resizing a logical volume

First, get a tiny bit of space back for tmp files (this command clears apt caches and etc):

sudo apt clean

Extend the logical volume

lvextend --resizefs -l +100%FREE /dev/mapper/vg_ubuntu-lv_root

Adding a disk to LVM

After adding a physical or virtual disk to the server, run the following commands.

Convert the new disk to an LVM physical volume

Note: Make sure you change “sdb” to the identifier of your drive):

pvcreate /dev/sdb
Add the new physical volume to the volume group
vgextend vg_ubuntu /dev/sdb
Check the status
vgdisplay
Extend the physical volume by 10GB (or however many “GB” you want)
lvextend -L +10G /dev/mapper/vg_ubuntu-lv_root
Grow the logical volume with ALL of the available space, without specifying a particular size
lvextend --resizefs -l +100%FREE /dev/mapper/vg_ubuntu-lv_root
Grow the filesystem to match the newly available space
resize2fs /dev/mapper/vg_ubuntu-lv_root
Check current available space
 df -h

Creating a brand-new LVM setup

Add a new virtual or physical disk to the server.

Note: For all of the below commands, change “sdc” to match your disk’s identifier

Convert the new disk to be an LVM physical volume

pvcreate /dev/sdc

Create the volume group

vgcreate vg_extra /dev/sdc

Check the volume group

vgdisplay

Create a logical volume (named lv_logs in this example)

lvcreate vg_extra -L 5G -n lv_logs

Format the logical volume

mkfs.ext4 /dev/mapper/vg_extra-lv_logs

Create a directory to mount the new logical volume

mkdir /mnt/extra/logs

Mount the logical volume

mount /dev/mapper/vg_extra-lv_logs /mnt/extra/logs

Find the “block id” of the new logical volume

blkid /dev/mapper/vg_extra-lv_logs

Back up your fstab file to be safe

cp /etc/fstab /etc/fstab.bak

Edit the fstab file

nano /etc/fstab
Add a line to the fstab to mount the volume, similar to this
UUID=<BLOCK ID FOR LOGICAL VOLUME> /mnt/extra/logs ext4 defaults 0 2
Test the new mount, first making sure it’s not mounted
umount /mnt/extra/logs
Then test your fstab file (BEFORE rebooting)
mount -a

If no errors, then you’re all set.

Snapshots

Create a new snapshot

lvcreate /dev/mapper/<SOURCE VOLUMEGROUP NAME> -L 1G -s -n snapshot_name

View used space of snapshots

lvs

Mount a snapshot

mount /dev/mapper/vg_extra-web_snapshot_20200421 /mnt/extra/snapshot

Restore a snapshot

Umount the original volume:

umount /path/to/mounted/logical-volume

Restore the snapshot:

lvconvert --merge /dev/mapper/snapshot_name

Deactivate/reactivate to fresh it:

lvchange -an /dev/mapper/vg_extra-lv_web
lvchange -ay /dev/mapper/vg_extra-lv_web