Billing and payments
Cloud server volumes
Data centers and network
Data privacy and data portability
How it works
VPN to GPN
How to work with LVM
LVM (logical volume manager) is a special Linux OS subsystem that provides additional options for managing partitions on hard disks. Unlike the classical scheme, when partitions that are used for file systems are created on every disk, LVM allows for combining the space on different disks into a common logical space, which can then be used to create or expand partitions. Thus, with help of LVM, one can place one partition on multiple disks or place multiple partitions on one disk.
In this manual, we will consider the simplest example of basic LVM setup and use. As an example, we will use a system where there are 3 disks of 8 GB – /dev/sdc, /dev/sdd and /dev/sde. The space on those disks will be used to create two 10 GB partitions that will be formatted to the ext4 file system. In addition, we will consider the process of increasing the existing logical volume and its file system by adding disks.
In this manual, we use the commands the misuse of which can easily lead to complete loss of all data. Therefore, prior to performing each of them, one should fully understand the meaning of all the actions performed. In case of lack of understanding, please refer to the documentation for the relevant command.
All necessary packages are already installed by default on most Linux operating systems.
Generally, LVM configuration process can be divided into the following steps:
- Allocating disk space (creating partitions) for the LVM subsystem and its formatting. In LVM terminology, these partitions are referred to as physical volumes (PV).
- Combining previously allocated space into one logical space by adding physical volumes to a volume group (VG).
- Using available space in the group to create or expand logical volumes (LV).
- Formatting and mounting logical volumes using standard file systems.
When using LVM, consider the following features and limitations:
- Do not place the "/ boot" partition on LVM.
- Each physical volume can exist only in a single volume group.
- Each logical volume can use space from a single volume group.
- A volume group can consist of one or more physical volumes.
- One group of volumes is sufficient for full operation, however, several of them can be created for one’s convenience.
- Logical volumes are available as block devices with the names / dev / <VGName> / <LVName>, where <VGName> and <LVName>. Those are the names of the volume group and logical volume.
- The names of utilities for working with physical volumes begin with pv (pvcreate, pvchange, etc.), with volume groups – with vg (vgcreate, vgchange, etc.), with logical volumes – with lv (lvcreate, lvchange, etc.).
- Configuration commands are to be run as root or by using sudo.
To allow the LVM subsystem to use disk space, it must be partitioned and formatted in a special way. For this purpose, "Linux LVM" type partitions (type 8e in MBR, or 8e00 in GPT) are created on the disks, which are then formatted using the pvcreate command. If one wants to use the entire disk, one can create a single partition that will occupy the entire space.
Let’s set up the disk/dev/sdc to use as a physical volume.
Before performing the partitioning, make sure that there are no partitions on the disk:
Let’s create a MBR type partition table and a type 8e (Linux LVM) partition that occupies the entire free space:
Check the partition table again:
Let’s re-read the partition table for the system to record the changes:
Let’s format the /de/ sdc1 partition as a physical volume using the pvcreate command:
To get information about the existing physical volumes, run the pvdisplay command:
Perform the same actions for the disks /dev/sdd и /dev/sde, which will lead us to creating three physical volumes:
After creating physical volumes, they need to be grouped together.
Create a volume group named "storage" from the previously created physical volumes with the vgcreate command:
To get information about the existing volume groups, run the vgdisplay command:
The disk space in LVM is divided into blocks that called physical extent (PE). If we run the vgdisplay command, we will see the number of blocks used and unused by the logical volumes and the corresponding amount of allocated and available for allocation disk space in the group.
Let’s create two logical volumes named "files" and "db" in the "storage" volume group using the following command:
To get information about the existing logical volumes, run the lvdisplay command:
From an OS perspective, a logical volume is a block device. Let’s consider working with the logical volume /dev/storage/files. It can be formatted using the standard tools:
You can mount it using the standard procedure as well:
For automated mounting, one can add a record to /etc/fstab:
At this stage, basic LVM configuration can be considered completed.
LVM allows for resizing existing logical volumes at the expense of the free space available in a volume group.
For demonstration, an additional disk /dev/sdf, 16 GB in volume, is added to the system. It must be used to increase the logical volume /dev/storage/files.
First of all, one needs to create a partition on the disk and format it as a physical volume, similarly to the previously created physical volumes:
To increase the size of an existing volume group using the new physical volume, one can run the vgextend command:
As we see after running the vgdisplay command, the storage volume group has now more than 20Gb of available space:
Prior to any manipulations with the logical volume, it is necessary to unmount the file system placed on it to avoid data corruption:
When creating a file system, its metadata records its exact size. Increasing the size of a block device where it is located will not increase the available free space. Therefore, to increase the size of the ext4 file system on LVM, it is necessary to first increase the logical volume where it is located, and only then increase the size of the file system itself.
Increase the size of the logical volume /dev/storage/files by 18 gigabytes:
Before increasing the FS, it is necessary to check it:
Should there be no errors after the check, one can increase the FS up to the maximum size:
Prior to using the increased FS, it is recommended to run a second check:
Afterwards, the file system can be mounted and used as usual.
To obtain relevant reference information, it is recommended to use the system help (the man command).
Here is the list of the most common commands used for working with LVM:
Commands to manage physical volumes:
- pvcreate – creating physical volumes.
- pvdisplay – retrieving information about physical volumes.
- pvchange – changing the settings of physical volumes.
- pvresize – changing the size of physical volumes.
- pvremove – deleting physical volumes.
- pvscan – detecting physical volumes.
- pvmove – moving extents (PE) between physical volumes.
Commands to manage group volumes:
- vgcreate – creating volume groups.
- vgdisplay – retrieving information about volume groups.
- vgchange – changing the settings of volume groups.
- vgextend – increasing the size of volume groups.
- vgreduce – decreasing the size of volume groups.
- vgrename – renaming volume groups.
- vgremove – deleting volume groups.
- vgscan – detecting volume groups.
Commands to manage logical volumes:
- lvcreate – creating logical volumes.
- lvdisplay – retrieving information about logical volumes.
- lvchange – changing the settings of logical volumes.
- lvextend – increasing the size of logical volumes.
- lvreduce – decreasing the size of logical volumes.
- lvrename – renaming logical volumes.
- lvremove – deleting logical volumes.
- lvscan – detecting logical volumes.
You can use lsblk command to get information about all available block devices or any specific devices.