Logical Volume Management basics

I have explained how to activate an LVM logical volume before, but never talked much about LVM itself. Here is a brief introduction.

I have been playing with LVM for about a couple of years now and I must say I have found it very practical, even for managing simple home desktops and servers. The main feature I use is the ability to resize volumes online, but LVM is useful for other things too: creating snapshots, combining multiple disks into a single volume group, and adding redundancy through mirroring.

Physical Volumes, Volume Groups, Logical Volumes

LVM works in three layers:

  • Physical Volume (PV) — a disk or partition that LVM can use. Initialised with pvcreate.
  • Volume Group (VG) — a pool of storage made from one or more physical volumes.
  • Logical Volume (LV) — a virtual partition carved out of a volume group. This is what you format and mount.

Start by preparing a partition (use fdisk to set the type to 8e — Linux LVM):

sudo pvcreate /dev/sdb1
sudo vgcreate my_volume_group /dev/sdb1
sudo lvcreate -L 10G -n my_volume my_volume_group

Now format and mount it:

sudo mkfs.ext4 /dev/my_volume_group/my_volume
sudo mount /dev/my_volume_group/my_volume /mnt

Resizing

Need more space? If you have free space in the volume group:

sudo lvextend -L +5G /dev/my_volume_group/my_volume
sudo resize2fs /dev/my_volume_group/my_volume

The filesystem is resized online — no unmounting required.

Snapshots

LVM snapshots let you take a point-in-time copy of a volume. Useful for backups:

sudo lvcreate -L 1G -s -n my_snapshot /dev/my_volume_group/my_volume

The snapshot starts small and grows as changes are made to the original volume. Remove it when no longer needed:

sudo lvremove /dev/my_volume_group/my_snapshot

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *