A Beginners Guide on Linux LVM

Written by: Linuxopsys   |   Last updated: August 11, 2022

Traditional storage capacity used individual disk space. Logical Volume Manager brings a better storage strategy by aggregated storage capacity. This brings more flexibility for storage needs.

What is LVM Linux

LVM stands for Logical Volumes Management, a tool for logical volume management that includes allocating disks, striping, mirroring, and resizing logical volumes. It allows flexible disk space management compared to traditional disk partitioning.

LVM creates a layer of abstraction over the physical storage allowing the creation of logical storage volumes. The physical storage is hidden from the software; that way, it can be resized and moved without unmounting the file systems or stopping the application.

Advantages of using logical volume management:

  • Flexible storage capacity: add or reduce storage capacity by adding or removing physical disks, configuring them to the pool of physical volumes and defining them in logical volumes; logical volumes can be dynamically extended and reduced without reformatting the disk.
  • Increase storage performance: configuring the storage as striped logical volumes that stripes data across multiple disks can dramatically increase disk throughput performance.
  • Increase storage resiliency: capability to configure the storage to have mirrored logical volumes, which means if one of the mirrors fails, the logical volumes can still be accessed.

In Linux, Logical Volume Manager (LVM) is a device mapper framework that provides logical volume management for the Linux kernel. Most modern Linux distributions are LVM-aware to have their root file systems on a logical volume.

Logical Volume Management (LVM) manages three main components:

  • Physical Volume (PV) is the partition of the physical disks.
  • Volume Group (VG) is a combination of multiple individual hard drives/or disk partitions into a single volume group.
  • Logical Volume (LV) is a portion of the volume group (VG) used as a single volume where regular filesystems such as ext3 or ext4 can be created on the new logical volume.
LVM Diagram
LVM diagram

Create physical volume (PV), volume group (VG), logical volume (LV)

Let's learn about the three main components of LVM and how to create it.

Create a physical volume (PV)

Physical volume is the actual storage device, it can be the entire disk, a partition on disk, or can be a LUN (Logical Unit Number) from the storage area network (SAN) setup. It should be created then only you can create a volume group on top of it.

First, use fdisk -l command to check all the available partitions in the system.

Choose disk to create the physical volume (PV). Now, to create physical volumes, use pvcreate command.

For example to create PV on /dev/sdb.

pvcreate /dev/sdb
physical volume created using pvcreate command

Use pvdisplay command to show an overview of physical volumes.

pvdisplay
show physical volumes

Alternatively, use pvs command to show physical volumes information one per line.

Create Volume Group (VG)

Physical volumes can be combined into a volume group., Basically, this creates a logical name for a pool of disks. Then only logical volumes can be configured on top of the volume group.

The disk space allocated in the volume group is called extents. In the physical volume (PV), extends are referred as physical extents.

The volume group maps the physical extents to logical extents used by logical volume (LV), therefor the total LVs allocated with logical extents is always the same size as the physical extents.

To create a volume group, use vgcreate command.

For example to create a volume group with the name vg02 using the pv which we created earlier /dev/sdb, type:

vgcreate vg02 /dev/sdb
create volume group using vgcreate command

Use vgdisplay command to display volume group information.

show volume group using vgdisplay command

Alternatively use vgs command to list display volume groups one per line.

show volume group using vgs command

Create logical volume (LV)

Logical volumes are created on top of the volume group. There are three types of logical volumes: Linear Volume, Striped Volume and Mirrored Volume.

Linear logical volume - Linear volume aggregates space from one or more physical volume into one logical volume. For example, we can create 2GB logical volume from two 1GB disks. The application will see one device with 2GB in size, linear volume is the most common way to create the logical volume.

Striped logical volume - We can configure the logical volume to write across the underlaying physical volumes, this way we can control the way the data is written to the physical volumes by creating striped logical volume. The striped logical volume can improve the performance of data I/O throughput for large sequential read and writes, because the read and write can be executed in parallel to both disk.

Mirrored logical volume - Mirrored logical volume maintain identical copies of the data on different device, when data is written to one device, another copy is written to the second device. This provides the protection of the volume from device failures. An LVM mirror typically copy into devices in block of 512KB and maintain a log that is used to keep track of which block are in sync with the mirror.

Logical volumes are created using lvcreate command.

For example, let's create a linear logical volume named data from the volume group vg02.

lvcreate -L 1G -n data vg02

To display information about logical volumes use lvs or vgs o +<column_name1,column_name2>.

lvs
or
vgs -o +lv_size,lv_name

Create a filesystem

We have created logical volume and it will be available under /dev as a normal block device. Use ls /dev/volume-group-name to list it.

list logical volume in /dev

To format the logical volume with Ext4 filesystem, type:

mkfs.ext4 /dev/vg02/data

Mount the file system

Finally, you can mount the filesystem to an appropriate directory.

Example:

mount /dev/vg02/data /mnt/projects

Remember to add the mount point to /etc/fstab file to have LVM volumes available after system reboot.

Resize Logical Volume

There may be situations when there is no space on your logical volume. The option you have is to resize LVM or shrink the LVM.

To resize you can simply install a new hard. Create a PV for this disk and extend the existing Volume group using vgextend command. Then use lvextend to expand the logical volume and finally use resize2fs to expand the filesystem.

Conclusion

Hope you now got a good overview of LVM in Linux. Hope you give it a try.

Thanks for reading, please leave your feedback and suggestions in the below comment section.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment