Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

How to Set Up and Configure LVM in Linux: A Hands-On Tutorial

Set up LVM in Linux step by step: physical volumes, a volume group and logical volumes, then grow a volume without downtime, add a disk and take snapshots.

3 min readBy Swati

Reviewed by the AmazingRDP technical team

Setting up LVM in Linux

Several disks to combine and resize as you like? See our dedicated servers. See Cheap Dedicated Servers from $38.99

With plain partitions, a disk is cut into fixed pieces, and changing their sizes later is awkward. LVM, the Logical Volume Manager, puts a flexible layer in between. Volumes can grow while in use, span several disks, and be frozen in a snapshot.

The three layers

LayerShortWhat it is
Physical volumePVA disk or partition handed to LVM
Volume groupVGA pool made of one or more physical volumes
Logical volumeLVA piece cut from the pool, which you format and mount like a partition

1. Install the tools and find the disks

sudo apt install lvm2
lsblk

On Fedora, AlmaLinux and Rocky Linux: sudo dnf install lvm2. The examples use two empty disks, /dev/sdb and /dev/sdc. Everything on them is erased.

2. Create the physical volumes

sudo pvcreate /dev/sdb /dev/sdc
sudo pvs

3. Create a volume group

sudo vgcreate vgdata /dev/sdb /dev/sdc
sudo vgs

vgdata is a name of your choice. The pool now has the combined size of both disks.

4. Create a logical volume

sudo lvcreate -n files -L 50G vgdata
sudo lvs

-L 50G sets the size. To use all remaining space, write -l 100%FREE in its place. Leaving some of the pool free is wise: it is what snapshots and later growth use.

5. Format and mount

sudo mkfs.ext4 /dev/vgdata/files
sudo mkdir -p /mnt/files
sudo mount /dev/vgdata/files /mnt/files

To mount it at every start, add to /etc/fstab:

/dev/vgdata/files  /mnt/files  ext4  defaults,nofail  0  2

Test with sudo mount -a before restarting. More in mount and automount drives in Linux.

Grow a volume while it is in use

sudo lvextend -r -L +20G /dev/vgdata/files

-r grows the file system together with the volume, with the volume mounted and in use. This is the main reason people use LVM.

Add a disk to the pool

sudo pvcreate /dev/sdd
sudo vgextend vgdata /dev/sdd

The new space is available to every logical volume in the group.

The root volume that is smaller than the disk

Ubuntu Server's installer uses LVM and often gives the root volume only part of the disk. If df -h / shows less than you expected and sudo vgs shows free space, claim it:

sudo lvextend -r -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

After a provider has enlarged a virtual disk, the partition and the physical volume must grow first:

sudo growpart /dev/sda 3
sudo pvresize /dev/sda3
sudo lvextend -r -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

Use your own disk and partition number from lsblk.

Snapshots

A snapshot freezes the state of a volume at one moment, for a consistent backup or as a way back before a risky change.

sudo lvcreate -s -n files_snap -L 5G /dev/vgdata/files
sudo mount -o ro /dev/vgdata/files_snap /mnt/snap

The 5 GB is room for the changes made to the original while the snapshot exists; when it fills up, the snapshot becomes useless. To return the volume to the snapshot's state, or to drop the snapshot:

sudo lvconvert --merge /dev/vgdata/files_snap
sudo lvremove /dev/vgdata/files_snap

Remove snapshots when you are done; they slow writes down while they exist.

Shrinking

Possible with ext4, only while unmounted, and with a real risk of data loss if a number is wrong: sudo lvreduce -r -L 30G /dev/vgdata/files. XFS cannot be shrunk at all. Back up first, or better, start small and grow.

Remove everything

sudo umount /mnt/files
sudo lvremove /dev/vgdata/files
sudo vgremove vgdata
sudo pvremove /dev/sdb /dev/sdc

What LVM is not

A volume group across several disks is not protection. If one disk fails, the volumes that used it are lost. For protection, put LVM on top of a RAID array, and keep backups. Snapshots are on the same disks and are not backups either. For encrypted volumes, see encrypting a disk with LUKS.

Servers with several disks are where LVM pays off most: see our dedicated servers. Related: clone Ubuntu Server with Clonezilla.

Questions

What is LVM in Linux?

The Logical Volume Manager: a layer between disks and file systems that lets volumes be resized, span several disks and be snapshotted.

How do I extend an LVM volume?

Run sudo lvextend -r -L +20G with the volume's path. The -r grows the file system as well.

Can I extend an LVM volume without unmounting it?

Yes. Growing works while the volume is mounted and in use, with ext4 and XFS.

Is an LVM snapshot a backup?

No. It lives on the same disks as the original. Copy it elsewhere for a backup.