Working with LVM

LVM is a disk space management system with an additional degree of abstraction from the physical layer. It allows flexible and efficient management of disk space, data fault tolerance, and also helps in solving various tasks on data backup, deployment of storage systems for virtualization environments, and so on.

In this article we will see how to install and use LVM in Linux.

Installation

LVM is usually installed with your Linux operating system, but if it is not, you can install it using your Linux distribution's package manager. For most distributions, the command to install LVM will look like this:

For Debian or Ubuntu:

sudo apt-get install lvm2

For CentOS, Fedora, and RHEL:

sudo yum install lvm2

Creating physical volumes

Before creating logical volumes, you must create physical volumes (pv). A physical volume is a virtual device, such as a hard disk or disk partition, that LVM uses to create logical volumes.

Note: It is recommended that you create physical volumes on top of the hard disk without using partitioning, because otherwise the process of dynamic lvm resizing becomes more complicated in the long run. 

To create a physical volume, follow these steps:

Use the pvcreate command to create a physical volume:

sudo pvcreate /dev/sdb

Where /dev/sdb is the path to the physical device.

2. Check the created physical volume using the pvdisplay command:

sudo pvdisplay

In the output, you will see information about the created physical volume.

Creating a physical volume group

A physical volume group (vg) is a logical container that can combine several physical volumes into one common group. However, nothing prevents it from being used within a single physical volume. To create a physical volume group, follow these steps:

1. use the vgcreate command to create a physical volume group:

sudo vgcreate vg0 /dev/sdb
# либо для нескольких pv
sudo vgcreate vg0 /dev/sdb /dev/sdc

Creating a logical volume

Now you can create logical volumes, which can be conventionally represented as partitions on a hard disk.

1. Create a logical volume in the physical volume group using the lvcreate command:

sudo lvcreate -n lv0 -L 10G vg0   # Создание логического тома lv0 размером 10 ГБ в группе физических томов vg0

Where -n is the name of the logical volume, -L is the size of the logical volume.

2. Check the created logical volume using the lvdisplay command:

sudo lvdisplay

You will see information about the created logical volume.

3. Create a file system on the logical volume using the mkfs command:

sudo mkfs.ext4 /dev/vg0/lv0

 4. Create a mount point for the new file system using the mkdir command:

sudo mkdir /mnt/lv0

 5.  Mount the new file system to the mount point using the mount command:

sudo mount /dev/vg0/lv0 /mnt/lv0

You can now use the new file system located on the logical volume.

Using LVM

LVM provides many commands for managing physical volumes, physical volume groups, and logical volumes. Some of the most commonly used commands are listed below:

  • pvcreate: Creates a new physical volume.
  • pvdisplay: Displays information about physical volumes.
  • vgcreate: Creates a new group of physical volumes.
  • vgextend: Adds a physical volume to a physical volume group.
  • vgdisplay: Displays information about physical volume groups.
  • lvcreate: Creates a new logical volume.
  • lvextend: Increases the size of a logical volume.
  • lvdisplay: Displays information about logical volumes.

LVM also allows you to use it to increase the size of physical volumes, physical volume groups and logical volumes while the system is running. There are corresponding commands for this purpose:

  • pvresize: Resizes a physical volume.
  • vgresize: Resizes a physical volume group.
  • lvresize: Changes the size of a logical volume.

By executing any of the above commands with the --help parameter you will be able to familiarize yourself with additional parameters of the command arguments.

Control the size of a logical volume:

For example, to increase the size of logical volume lv0 in physical volume group vg0 by 5 GB, use the command:

sudo lvresize -L +5G /dev/vg0/lv0

or all remaining free space on the physical volume:

sudo lvresize -l +100%FREE /dev/vg0/lv0
Note that the last example uses the uppercase letter l, while the previous example uses the uppercase letter L.
In this format, you have the flexibility to set a percentage of how much free space from the remaining space on the physical volume to add to the logical volume.

You can then resize the file system on the fly using the resize2fs command:

sudo resize2fs /dev/vg0/lv0

LVM Snaphots

LVM in Linux supports a snapshot mechanism that allows you to create exact copies of logical volumes at a specific point in time. This is a useful feature that allows you to save the state of data at a particular point in time and restore to that state if necessary.

LVM snapshot technology works as follows: a snapshot is created by saving the state of a logical volume at the point in time when the snapshot is created. After the snapshot is created, any changes that occur in the original logical volume are stored in additional differential disk space. This can help in backing up and restoring data at the time the snapshot is created.

Let's take a look at how to create and use the LVM snapshot mechanism.

 Creating an LVM snapshot

Creating an LVM snapshot is accomplished by using the lvcreate command. For example, to create a snapshot of the myvolume logical volume in the myvg volume group, use the following command:

sudo lvcreate --snapshot --name myvolume_snap --size 1G /dev/myvg/myvolume

This command will create a snapshot of the myvolume logical volume named myvolume_snap with a size of 1 GB.

Note that you must specify the snapshot size in order to create a snapshot.

Using an LVM snapshot

After you create a snapshot, you can use it to perform various tasks. For example, you can use the snapshot to restore data or to create a backup. Use the following command to mount the snapshot on the file system:

sudo mount /dev/myvg/myvolume_snap /mnt/snapshot

Here /mnt/snapshot is the mount point where the snapshot will be mounted. Once the snapshot is mounted, you can copy data from it or use it for data recovery.

Deleting an LVM snapshot

Once a snapshot has been used, it can be deleted. To delete a snapshot, use the lvremove command. For example, to remove the myvolume_snapshot in the myvg volume group, use the following command:

sudo lvremove /dev/myvg/myvolume_snap

Note that if you delete a snapshot, all data that was changed after the snapshot was created will be lost.

Thin pools

Thin pool is an LVM mechanism that allows you to create a logical volume that uses only the portion of the physical disk devices that actually contains data. This allows you to reduce disk space consumption and manage disk space more efficiently. This feature of LVM has become most common in virtualization systems as a convenient and economical solution for allocating disk space for virtual machines and containers. Let's take a look at how to create a thin pool in LVM.

Creating thin pool LVM

Thin pool is created using the familiar lvcreate command. For example, to create a thin pool mythinpool in the myvg volume group on the physical volumes /dev/sdb and /dev/sdc, use the following command:

sudo lvcreate -L 50G -T myvg/mythinpool /dev/sdb /dev/sdc

Here -L specifies the size of the thin pool in gigabytes and -T indicates that we are creating a thin pool. This command will create a thin pool mythinpool of 50 GB.

Creating a thin volume LVM

Thin volume is a logical volume that is created inside a thin pool. Thin volumes can be created using the lvcreate command. For example, to create a thin volume mythinvolume of size 10 GB in thin pool mythinpool in the myvg volume group, use the following command:

sudo lvcreate -V 10G -T myvg/mythinpool -n mythinvolume

Here -V specifies the size of the thin volume in gigabytes and -n specifies the name of the thin volume. This command will create a thin volume mythinvolume of 10 GB inside thin pool mythinpool.

Using thin volume LVM

Thin volume can be used as a regular logical volume. For example, to create an ext4 file system on a thin volume mythinvolume, use the following command:

sudo mkfs.ext4 /dev/myvg/mythinvolume

Once the thin volume file system is created, it can be mounted and used as a regular logical volume.

Thin pool allows you to create more flexible and efficient storage configurations in Linux. Thin pool is used in combination with thin volume for optimal disk space utilization.