23.09.2024

LVM: creation and remove logical volume

As you dive into the depths of Linux, you begin to realise that it is not just an operating system with minimal requirements, but a huge Swiss knife. You go down to the level of namespaces and containers, eBPF capabilities in the kernel and realise how huge the system is. Today, however, let's talk about the more obvious and understandable layers of the operating system iceberg and look at flexible disc space management, using the LVM utility!

What is LVM?

LVM or Logical Volume Management is a software layer for managing disc space, and more specifically their logical volumes. The main problem that this layer solves is that disc management is not flexible enough. For example, we have a service that requires 3GB of free space, but there is only 2GB on one physical disc and 1GB on another, what should we do? After all, they are in different spaces?

That's right, this is where LVM can help! Pay attention to the architecture of the solution itself and the way the utility works:

Screenshot №1 — Schema

Physical disks have a logical space called partitions or in LVM Physical Volumes, just they are combined at the software level into Volume Groups. After that all space in the VM is allocated between the new logical spaces, as well as flexibly sharing free space with each other!

The only file system partition that can't be in LVM is /boot, because booting at the low level of the OS kernel is performed from it, and then LVM is connected at the software level to divide the space.

Classically, the partitioning scheme looks as follows:

Screenshot №2 — Lvm spaces

Each volume is allocated to a separate volume for a reason, the logical space concept allows you to flexibly define the space for each of them. Administration is easier with a logical volume, because you don't have to search for a specific area in the physical space to completely clean up your logs. The partitions represented are:

From words to deeds, let's create the first LVM space from scratch and look at the basic functions of the system!

How do I create an LVM volume?

If you don't have sufficient resources than you can perform actions on powerful cloud servers. Serverspace provides isolated VPS / VDS servers for common and virtualize usage.

Screenshot №3 — Create Server

Let's install the required packages and pre-update the repositories using the package manager:

apt update && apt upgrade -y && apt install lvm2 -y

Screenshot №4 — Installation

After the installation has taken place, we need to scan the current discs and logical volumes, relative to which we will build a logical LVM structure. Let's execute the command:

lsblk

Screenshot №5 — List of disks

As you can see on the image, there is a total of one disc with two logical volumes attached to the system. Unfortunately, it is not possible to hot-allocate vda1 space, as it is mounted in the root partition. That's why you need to connect to a machine with liveboot OS and already make changes on the disc or use another space that can be unmounted.

In this case study we will use the sda6 partition and unmount it with the command:

umount /dev/sda6

Let's choose the physical partition of the future LVM, in our case it is /dev/sda6, you can create unlimited physical volumes.

pvcreate /dev/sda6

Next, you need to go higher up the scheme and create a volume group that will merge the already created physical volumes.

vgcreate main /dev/sda6

After that you need to structure the total disc space into logical volumes as if it were a regular disc. Let's execute the command:

lvcreate -L 2GB main

Screenshot №6 — Create logical volume

Now we have a logical volume created and managed by LVM on the /dev/sda6 partition. But for the OS to be able to work with it we need to install a file system inside this logical volume of LVM itself. Let's run the logical volume scan command and select the required one:

lvscan

Let's write the command to create a file system:

mkfs.ext4 /dev/main/lvol0

Once the area is cleaned up and the system is installed, we can proceed to mount the volume and verify:

mount /dev/main/lvol0 /home && df -H

Screenshot №7 — Mount

Great, the volume is added and mounted to the /home directory, but what if I need to delete it? Go in reverse order.

How do I delete an LVM volume?

For this we will go the opposite way and write delete logical volumes, they can also be scanned by writing the command:

lvscan

Next, let's move on to deleting the logical volume with the command:

lvremove /dev/main/lvol0

After that we will remove the shared group and physical partition:

vgremove /dev/main && pvremove /dev/sda6

Screenshot №8 — Remove logical volume

After manipulations above on the machine remained on the logical volume of the disc sda6 without changes, it can also be deleted at will utility parted.