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!

Load LVM partition manually

Sometimes you have to load a logical volume that is not part of the main filesystem. Other times you might have to mount a volume from a live CD, perhaps to recover data from a system that is no longer booting.

In both cases it needs to be done manually.

Install the LVM tools

If lvm2 is not already installed:

sudo apt-get install lvm2

Scan for and activate volumes

If you can see the disk with fdisk but LVM does not recognise the volumes, the kernel may not have scanned for them yet. Start by scanning for physical volumes and volume groups:

sudo pvscan
sudo vgscan
sudo lvscan

pvscan detects LVM physical volumes, vgscan finds the volume groups, and lvscan lists the logical volumes within them.

Activate and mount

If the volume group is inactive (common after booting from a live CD or moving a disk to another machine), activate it:

sudo vgchange -ay

This activates all available volume groups. Now mount the logical volume:

sudo mount /dev/VolumeGroupName/LogicalVolumeName /mnt

Replace VolumeGroupName and LogicalVolumeName with the names shown by lvscan. If you are not sure what the volume is called, check /dev/mapper/ for device-mapper entries.

Troubleshooting

If lvscan shows your volumes but vgchange reports “Volume group not found”, check the output of vgscan carefully. Sometimes the volume group name contains a hyphen (e.g. my-server) which can confuse the command — try using the full path from /dev/mapper/ instead:

sudo mount /dev/mapper/my--server-myvol /mnt

Note the double hyphen — LVM represents the hyphen in the volume group name as — in the mapper path.

Enjoy!