LVM

LVM (Logical Volume Manager) is a software which uses physical devices abstract as PVs (Physical Volumes) in storage pools called VG (Volume Group). Whereas physical volumes could be a partition, whole hard drives grouped as JBOD (Just a Bunch Of Disks) or complete RAID systems.

Installation
First we need to enable the device mapper in our kernel:

is available on Gentoo Linux and supports the following useflags:
 * clvm = Allow users to build clustered lvm2
 * cman = Cman support for clustered lvm
 * lvm1 = Allow users to build lvm2 with lvm1 support
 * nolvmstatic = Allow users to build lvm2 dynamically
 * readline = Enables support for libreadline, a GNU line-editing library that almost everyone wants
 * static-libs = Build static libraries

You can install LVM2 using the following command:

The package comes with a start-stop-daemon, which we start and add to the boot runlevel:

Usage
LVM organizes storage in three different levels as follows:
 * hard drives, partitions, RAID systems or other means of storage are initialized as PV (Physical Volume)
 * Physical Volumes (PV) are grouped together in Volume Groups (VG)
 * Logical Volumes (LV) are managed in Volume Groups (VG)

PV (Physical Volume)
Physical Volumes are the actual hardware or storage system LVM builds up upon.

Partitioning
The partition type for LVM is 8e(Linux LVM):

In fdisk, you can create MBR partitions using the n key and then change the partition type with the t key to 8e. We will end up with one primary partition /dev/sdX1 of partition type 8e (Linux LVM).

Create PV
The following command creates a Physical Volume (PV) on the two first primary partitions of /dev/sdX and /dev/sdY:

List PV
The folloing command lists all active Physical Volumes (PV) in the system:

You can scan for PV in the system, to troubleshoot not properly initialized or lost storage devices:

Remove PV
LVM automatically distributed the data onto all available PV, if not told otherwise. To make sure there is no data left on our device before we remove it, use the following command:

This might take a long time and once finished, there should be no data left on /dev/sdX1. We first remove the PV from our Volume Group (VG) and then the actual PV:

VG (Volume Group)
Volume Groups (VG) consist of one or more Physical Volumes (PV) and show up as /dev// in the device file system.

Create VG
The following command creates a Volume Group (VG) named vg0 on two previously initialized Physical Volumes (PV) named /dev/sdX1 and /dev/sdY1:

List VG
The folloing command lists all active Volume Groups (VG) in the system:

You can scan for VG in the system, to troubleshoot not properly created or lost VGs:

Extend VG
With the following command, we extend the exisiting Volume Group (VG) vg0 onto the Physical Volume (PV) /dev/sdZ1:

Reduce VG
Before we can remove a Physical Volume (PV), we need to make sure that LVM has no data left on the device. To move all data off that PV and distribute it onto the other available, use the following command:

This might take a while and once finished, we can remove the PV from our VG:

Remove VG
Before we can remove a Volume Group (VG), we have to remove all existing Snapshots, all Logical Volumes (LV) and all Physical Volumes (PV) but one. The following command removes the VG named vg0:

LV (Logical Volume)
Logical Volumes (LV) are created and managed in Volume Groups (VG), once created they show up as /dev// and can be used like normal partitions.

Create LV
With the following command, we create a Logical Volume (LV) named lvol1 in Volume Group (VG) vg0 with a size of 150MB:

There are other useful options to set the size of a new LV like:
 * -l 100%FREE = maximum size of the LV within the VG
 * -l 50%VG = 50% size of the whole VG

List LV
The folloing command lists all Logical Volumes (LV) in the system:

You can scan for LV in the system, to troubleshoot not properly created or lost LVs:

Extend LV
With the following command, we can extend the Logical Volume (LV) named lvol1 in Volume Group (VG) vg0 to 500MB:

Once the LV is extended, we need to grow the file system as well (in this example we used ext4 and the LV is mounted to /mnt/data):

Reduce LV
Before we can reduce the size of our Logical Volume (LV) without corrupting existing data, we have to shrink the file system on it. In this example we used ext4, the LV needs to be unmounted to shrink the file system:

Now we are ready to reduce the size of our LV:

LV Permissions
Logical Volumes (LV) can be set to be read only storage devices.

The LV needs to be remounted for the changes to take affect:

To set the LV to be read/write again:

Remove LV
Before we remove a Logical Volume (LV) we should unmount and deactivate, so no further write activity can take place:

The following command removes the LV named lvol1 from VG named vg0:

Links

 * lvm2
 * LVM tldp.org