LVM (Logical Volume Manager)

Posted on 29 March 2020. Last updated on 01 February 2021.


LVM (Logical Volume Manager) allows administrators to create virtual partitions spanning 2 or more physical hard-drives.
It is also possible to create software raid systems, with mirrored partitions.

On gentoo you can install LVM with the following command.
emerge --ask sys-fs/lvm2

Debian and ubuntu users can use this command:
apt-get install lvm2


Create PV (Physical Volume)
First we have to select wich partition we want to add.
We do this with the create physical volume.
pvcreate /dev/sd[bc]1

To see wich physical volumes are available:

If more physical volumes should be displayed, then pvscan can detect inactive physical volumes and activate those.

VG (Volume Group)
The next step is to create a volume group (VG), a volume group roups a number of physical volumes and show up as /dev/VG_NAME in the device file system.
vgcreate vg0 /dev/sd[bc]1

With vgdisplay you get a list of all active volume groups.

vgscan will search for any volume group:

You can add additional physical volumes
vgextend vg0 /dev/sdd1

Or take one out of it. There is a risk of data loss, so move the data out first.
pvmove -v /dev/sdc1
vgreduce vg0 /dev/sdc1

When a volume group is no longer needed, it can be removed with:
vgremove vg0

LV (Logical Volume)
The logical volumes are the partitions we are going to use. They show up as /dev/VG_NAME/LV_NAME.

To create a logical volume, the lvcreate command is used. The parameters to the command consist out of the requested size for the logical volume (which cannot be larger than the amount of free space in the volume group), the volume group from which the space is to be claimed and the name of the logical volume to be created.
lvcreate -L 150M -n lvol1 vg0
Or if you want to use the full free disk space:
lvcreate -l 100%FREE -n lvol1 vg0

To list all logical volumes:

The lvscan command can be used to scan for logical volumes on all available volume groups:

When a logical volume needs to be expanded, then the lvextend command can be used to grow the allocated space for the logical volume.
If there is still room in the Volume Group.
lvresize --resizefs --size +350GB /dev/vg0/lvol1
Note: the command does not work on all file systems, sometimes it need to be done in 2 steps:
lvextend -L500M /dev/vg0/lvol1
resize2fs /dev/vg0/lvol1 500M

It is also possible to reduce a logical volume. First shrink the file system itself. Not all file systems support online shrinking.
ext4 does not support online shrinking; you have to umount it first. It is also recommended to do a file system check to make sure there are no inconsistencies.
umount /mnt/data
e2fsck -f /dev/vg0/lvol1
resize2fs /dev/vg0/lvol1 150M

LVM supports permission states on the logical volumes.
For instance, a logical volume can be set to read only using the lvchange command:
lvchange -p r /dev/vg0/lvol1
mount -o remount /dev/vg0/lvol1

And ofcourse it is possible to remove a logical volume.
First you have to unmount the volume and then deactivate it, before you can remove the logical volume:
umount /dev/vg0/lvol1
lvchange -a n /dev/vg0/lvol1
lvremove /dev/vg0/lvol1

To create a RAID logical volume, you specify a raid type as the --type argument of the lvcreate command. Usually when you create a logical volume with the lvcreate command, the --type argument is implicit. For example, when you specify the -i stripes argument, the lvcreate command assumes the --type stripe option. When you specify the -m mirrors argument, the lvcreate command assumes the --type mirror option. When you create a RAID logical volume, however, you must explicitly specify the segment type you desire.

The following command creates a 2-way RAID1 (mirroring) array named my_lv in the volume group my_vg that is 1G in size.
lvcreate --type raid1 -m 1 -L 1G -n my_lv my_vg

You can create RAID1 arrays with different numbers of copies according to the value you specify for the -m argument. Although the -m argument is the same argument used to specify the number of copies for the previous mirror implementation, in this case you override the default segment type mirror by explicitly setting the segment type as raid1. Similarly, you specify the number of stripes for a RAID 4/5/6 logical volume with the familiar -i argument, overriding the default segment type with the desired RAID type. You can also specify the stripe size with the -I argument.

Or 500Mb volume in 1 command.
lvcreate -L 500M -m1 vg1 /dev/sda1 /dev/sdb1

To add a partition to a raid (as additional mirror)
first create the Physical Volume (PV), It has to be at least the same size as the first partition. And if you want the logs on it, larger, the logs can be big.
pvcreate /dev/sde1

Add it to the Volume Group (VG)
vgextend vg1 /dev/sde1

And finally add it as mirror:
lvconvert -m1 --type mirror --corelog /dev/vg1/raid1 /dev/sde1
-m1 is the first mirror.
--corelog using system memory for the mirror log. No disk space used for logs.

Some site with a more complete explanation:
Arch Linux