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:
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:
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:
- /var is the partition mainly storing logs;
- /tmp is where temporary files are stored;
- /home, this directory stores all user data;
- / is the root partition, on the basis of which all other directories in the Linux file system are attached.
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.
Let's install the required packages and pre-update the repositories using the package manager:
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:
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:
Let's choose the physical partition of the future LVM, in our case it is /dev/sda6, you can create unlimited physical volumes.
Next, you need to go higher up the scheme and create a volume group that will merge the already created physical volumes.
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:
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:
Let's write the command to create a file system:
Once the area is cleaned up and the system is installed, we can proceed to mount the volume and verify:
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:
Next, let's move on to deleting the logical volume with the command:
After that we will remove the shared group and physical partition:
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.