LVM on Linux (volume manager)
The disk partitioning scheme you just saw is useful for dividing the data into an organizational structure that meets and makes some sense. However, once the partition is created, making changes is complicated without involving a possible loss of data.
At best, it is possible to move the data to the beginning of the disk and repartition it “hot”. Worst case scenario, you need a backup, recreate the partitions, and restore the backup to the new layout.
To avoid all this hassle and possible data loss, the staff decided to create a logical volume manager, called LVM - Logical Volume Manager.
LVM on Linux is a Disk Manager integrated with the Linux Kernel. It allows you to:
- Disks are exchanged without service interruption (hotswap);
- Change volume size;
- Create volume images (snapshots);
- Back up volume images;
- Create a single volume from multiple disks (similar to RAID 0);
- Create mirrored volumes on more than one disk (similar to RAID 1).
In this way, LVM was created to expand the file system that is traditionally viewed as a set of physical disks and partitions. Its purpose is to allow great flexibility for the administrator in managing the disks.
Imagine that the user has the following partition scheme without LVM:
**Mount Point** | **Partition** | **Size** |
/boot | /dev/sda1 | 500 Megabytes |
Swap | /dev/sda2 | 1 Gigabyte |
/ | /dev/sda3 | 6 Gigabytes |
/home | /dev/sda4 4 | Gigabytes |
Whereas, if the user uses LVM, he could simply decrease the size of /home and increase the root, or even add another disk and increase the root, without having to back up the data, format the partition, and copy the data back. Look at the same example using volumes:
**Mount Point** | **Partition** | **Size** |
/boot | /dev/sda1 | 500 Megabytes |
Swap | /dev/vg00/swap | 1 Gigabyte |
/ | /dev/vg00/root | 6 Gigabytes |
/home | /dev/vg00/home | 4 Gigabytes |
It’s important that you know that there are two versions of LVM: 1 and 2. Version 2 is supported by Kernel 2.6 and 3.x, and by Kernel 2.4 with the application of some patches.
Kernels versions 4.x and 5.x already support LVM version 2.
The only partition that cannot be used with the volume manager is /boot.
LVM terminology
Before showing the LVM commands, you need to understand the terminology that LVM uses:
- Physical Volume: It is a disk or some hardware that behaves like a disk (such as storage that uses RAID);
- Volume Group: It is an abstraction of LVM that brings together logical volumes and physical volumes in the same administrative unit;
- Logical Volume: It is the equivalent of a partition in a non-LVM system.
Step by step to work with LVM
To work with LVM, you need to initialize the disks for LVM, then create at least one Volume group, create at least one logical volume in the group you just created, format the volume with the desired file system, and then mount it.
Briefly, it is necessary to take the following steps:
- If you are going to use only one disk partition, you must create it with fdisk or parted and change its type to 8e (LVM), otherwise, simply use the entire disk with pvcreate;
- Initialize physical volumes (partitions or disks) with the command
- pvcreate; Create a volume group with the vgcreate command;
- Activate a volume group with the vgchange command;
- Create a logical volume with the lvcreate command;
- Format the logical volume with the desired file system with mkfs .
- Mount the logical volume with the mount.
Creating LVM partitions
If you don’t want to use the entire disk as an LVM, you can use fdisk to create one or more LVM-like partitions. To create an LVM partition with fdisk, you create the partition normally and change the partition type to 8e.
For example, let’s imagine that the system was installed on the /dev/sda disk.
And for LVM, two more disks will be used: /dev/sdb and /dev/sdc that are not partitioned.
Before adding a disk or partition as a physical LVM volume, you must initialize it with the pvcreate command.
Initializing physical volumes
To initialize physical volumes from entire disks, the command is: pvcreate and the full path of the partition or disk:
Creating a volume group
After initializing the disks, you need to create a volume group with the disks with the vgcreate command:
Activating a volume group
After creating the volume group, it is necessary to activate it with the vgchange command:
After the system is rebooted it is necessary to activate the volume group again. Therefore, it is necessary to include this command in the system load scripts.
Creating logical volumes
The lvcreate command creates logical volumes. In the example, a 1GB logical volume called logic1 will be created on the volume myvolume:
Since the /dev/sdb and /dev/sdc disks each have 2GB in our example, it is possible to create up to 4 volumes of 1GB each, or a single logical volume of 4GB, as in the example below:
Activating logical volume
The lvchange command activates/deactivates the logical volume for use:
To ACTIVATE:
To DISABLE:
Formatting the logical volume
Any file system can be used to format the logical volume:
Once you have formatted the logical volume, you need to mount it:
After these steps, the logical volume will be ready for use.
You can also use LVM to increase or decrease the size of a volume.
Increasing volume size with a new disk
First you need to create the physical volume:
Assign it to the group:
Disassemble the logical volume:
Increase the logical volume group:
Search for error and repair:
Finally, we resized:
Now just put it back together:
Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it?
Share