LVM

From Segfault
Jump to navigation Jump to search

Introduction

Quoting from the LVM2 Resource Page:

LVM2 refers to the userspace toolset that provide logical volume management facilities on linux. 
It is reasonably backwards-compatible with the original LVM toolset.
To use LVM2 you need 3 things: device-mapper in your kernel, the userspace device-mapper support
library (libdevmapper) and the userspace LVM2 tools.

Usage

Create a volume group and a logical volume:

pvcreate /dev/sd{y,z}                                                                    # Use wipefs before if needed.
vgcreate vgtest /dev/sd{y,z}
lvcreate -L 100M -n foobar vgtest

We could have omitted the -L parameter to create the LV using all space left in the VG. Or we can use the --extents parameter to specify percentages:

lvcreate -l 50%FREE -n half vgtest                                                       # Or use 50%VG to allocate half the VG size instead.

Once can also specify RAID levels when creating logical volumes:

lvcreate -l 100%FREE --type raid0 -n lvraid vgtest

Resize logical volumes:

lvresize -L +100M vgtest/half                                                            # Add --resizefs to resize the file system in one go via fsadm

Snapshots

Create a snapshot, e.g. to process backups:

lvcreate -l 20%ORIGIN -s -n foobar_snap vgtest/foobar                                    # Allocate 20% of the origin volume for the snapshot.

If we want to discard[1] the changed data, we can merge the snapshot back into its origin device (and remove the snapshot afterwards):

$ lvconvert --mergesnapshot vgtest/foobar_snap
 Merging of volume vgtest/foobar_snap started.
 vgtest/foobar: Merged: 99.37%
 vgtest/foobar: Merged: 100.00%

If we want to keep[1] the changed data, we would just remove the snapshot:

lvremove vgtest/foobar_snap

Activate all LVs in a VG, which can be necessary if these LVs have not been activated by the boot scripts:

vgscan
vgchange -ay

Links

References

  1. 1.0 1.1 This was somewhat counterintuitive to me, see Talk:LVM for details.