The Linux Logical Volume Manager version 2 (LVM2) was introduced in the Linux 2.6 kernel. The basic point behind it is that it splits and combines physical volumes into logical volumes, and introduced volume mirroring and volume clustering. LVM2 can have 4,370,464,768 physical or logical volumes, and each device can hold 8 Exabytes, provided other restrictions do not apply. For comparison, LVM1 devices could be 2 TB in size, and only 256 physical or logical volumes were allowed.
Logical volume management is a widely-used technique for deploying logical rather than physical storage. The basic process is illustrated below, but a physical disk is split into logical volumes.
Logical volumes are then assigned to logical volume groups, which can span physical volumes. This means that a logical volume group can be bigger than a physical volume. Logical volume groups are then split into logical volumes. The logical volumes consist of fixed size logical extents; the default size is 4 MB. Each logical extent maps to a physical extent on a physical volume and a physical extent must be the same size as a logical extent. These logical extents can map to physical extents that are held on different physical volumes. This leads to two different types of mapping between logical and physical extents.
In Linear Mapping the logical extents are mapped sequentially to extents on a physical volume until that volume is full, then the mapping starts on the next physical volume. This means that the logical volume can be bigger than a physical volume.
In Striped Mapping groups of contiguous physical extents called stripes are mapped from different physical volumes to a logical volume as show below. This has performance advantages as the IO workload is shared between different disk spindles.
LVM has the following advantages over raw physical partitions:
Logical volumes can be resized in-flight, even if applications are accessing the data. This means no downtime is needed to resize storage partitions.
Data can be migrated between storage devices in-flight, even if applications are accessing the data
Logical volumes can be made bigger than physical devices, and large logical volumes can perform well if they are constructed from several small physical volumes.
Data can be protected from failure with disk mirroring and I/O multi-pathing
Data copies can be created using logical volume snapshots and then used for non-disruptive backups or testing
Basic LVM commands
These commands assume you have physical volumes called vol1, vol2, vol3 and a logical disk group called volg_01
To create a physical volume use the pvcreate command
pvcreate /dev/vol1
to create a volume group use the vgcreate command
vgcreate volg_01 /dev/vol1 /dev/vol2
To add a volume to an existing volume group use the command
vgextend volg_01 /dev/vol3
To remove a volume from a volume group use
vgreduce volg_01 /dev/vol3
Note that any logical volumes using physical extents from volg_01 /dev/vol3 will be removed as well.
Assuming that your logical volumes are called LVOL1 and LVOL2;
to create a 150 GB logical volume use
lvgcreate -n lvol1 -- size 150G volg_01
This will be use linear mapping as that is the default. Each logical volume name within a volume group must be unique, but volumes is different volume groups can have the same name. A logical volume is stored in the device directory as /dev/vol_group_name/logical_volume_name
To create a 150 GB logical volume called LVOL2 that uses striped mapping the command is
lvcreate -i2 -I4 - -size 150G -n LVOL2 volg_01
The -i2 means use two stripes, the -I4 means the stripe size if 4 KB. You could put /dev/vol1 and /dev/vol2 at the end of the command to force the logical volume to use stripes from those two physical volumes.
To remove a logical volume first unmount with the command
unmount /dev/volg_01/LVOL1
Then remove it with the command
lvremove/dev/volg_01/LVOL1
To add an extra 50GB to the LVOL1 logical volume use the command
lvextend -L+50G /dev/volg_01/LVOL1
Once the logical volume is extended you need to expand the file system to use the additional space