Updated April 17, 2023
Introduction to Linux LVM
In today’s world, when the entire world is going to be built on the data it becomes a daunting task for system admins to manage the disk space. In earlier times, when one used to run out of space, a long cumbersome process used to start in order to increase the space available to a disk partition. Not only this the system had to be taken off-line. In Linux LVM or Logical Volume Manager, when introduced in Fedora Linux, was a savior as it was the first time some abstraction layer allowed flexible disk management. To formally define LVM, it is a device mapper framework that enables allocation of space on a mass storage device with a hint of flexibility.
Syntax:
In Linux, there are infinite possibilities of LVM and in this article, we will discuss the ones which are widely used when it comes to the utility of LVM. In this section we will come face to face with the syntax as in the next section we will take a dig at how LVM works.
1. Display Physical Volumes
Syntax:
pvs
OR
pvdisplay
2. Displaying Volume Groups
Syntax:
vgs
OR
vgdisplay <volume group name>
3. Creating Logical Volumes
Syntax:
lvcreate -L <size of logical volume> -n <Volume name> <VG name>
4. Listing the partition types in Linux
Syntax:
lsblk
5. Mount Logical Volumes
Syntax:
mount -t ext3 <Logical Volume path> <mount path>
6. Extending a Logical Volume
Syntax:
lvextend -L +<Size increase> <Logical volume path>
7. Removing a Logical Volume
Syntax:
lvremove <Logical Vol path name>
How LVM works in Linux?
Now that we syntactically have a brief idea of what a Logical Volume Manager looks like, it is time for us to understand how does LVM works in Linux. To begin with, LVM deals with the management of disk allocation, stripping of disks, mirroring disks, and resizing the logical volumes. Now, if you look at the syntax carefully, you will find that the first 3, has just few alphabets been different or in other words, understanding each of the 3 different chronological portions of the architecture.
The first one is the Physical Volume. This has a LVM utility prefix pv. These are block devices or disk-like devices that act like raw material for LVM to build a higher abstraction layer. The next item on the architecture is Volume Group which has utility prefix vg. LVM helps in a combination of physical volumes into pools of storage. These layer abstracts the characteristics of the underlying device. The third and the last is Logical Volume, which is prefixed as lv. This is analogous to partition on a physical disk, except having a bit more flexibility. All these three can be considered as a subset within the previous one, i.e. Logical Volume group is combined to Volume Group, Pool of volume groups make a physical volume.
Now that we have a complete understanding of LVM and its different components, let us start with some chronological order of steps. For using LVM, we would first need to create a partition on the unpartitioned disk. For the sake of this article, we will assume that the partition can be created on the single partitions. The other, by the way, is a raw and unpartitioned hard disk. For this, cfdisk command is used. Next is to have physical volumes created, for which pvcreate command is used. Once the physical volume is created it is time to create a volume group inside that, as this is the group which will act like a container for all of our storage. Now we need to create the most granular level of the architecture, i.e. Logical volume. It is like cutting smaller pieces of cake from a big piece of cake. This smaller logical volume can be of any number, but the net size of all these smaller pieces of cake should never exceed that of the volume group where these logical volumes are created.
At this point of time, our logical volume is ready to use. At this point, we can start creating file systems in our logical volumes. One should be aware of the fact that a small % of space is reserved for super-users (by default 5%).
Apart from this initial working of LVM, there are some advanced utilities of mounting, extending, removing local volumes as deemed fit for a use case. During mounting, one should be careful about creating a mount point before executing the mount command. One of the biggest advantages of extending the logical volume is to increase the space any time one runs out of space in the volume. When one runs lvextend only the size of the logical volume increases, whereas the size of the file system doesn’t and in order to do that, one needs to run the resize2fs command. Last but not the least, when a logical volume is removed, one should make sure that it doesn’t contain any valuable data, as this command can’t be undone and should be carefully executed!
In the next section, we will look at examples of the explained working of LVM.
Examples
Lets us discuss the examples of Linux LVM.
Example #1 – Display Physical Volumes
Syntax:
pvs
Output:
Example #2 – Displaying Volume Groups
Syntax:
vgs
Output:
Example #3 – Creating Logical Volumes
Syntax:
lvcreate -L 5G -n EduCBAVol1 EduCBADemoGrp
lvcreate -L 2G -n EduCBAVol2 EduCBADemoGrp
lvcreate -L 1G -n EduCBAVol3 EduCBADemoGrp
Post creating of Logical Volume:
Syntax:
vgs -o +lv_size,lv_name
Output:
Post creating of Logical Volume:
Example #4 – Listing the partition types in Linux
Syntax:
lsblk
Output:
Example #5 – Mount Logical Volumes
Syntax:
mount /dev/EduCBADemoGrp/EduCBAVol1 /mnt/EduCBAVol1
mount /dev/EduCBADemoGrp/EduCBAVol2 /mnt/EduCBAVol2
mount /dev/EduCBADemoGrp/EduCBAVol3 /mnt/EduCBAVol3
vi /etc/fstab
Output:
Example #6 – Extending a Logical Volume
Syntax:
lvextend -L +600 /dev/EduCBADemoGrp/EduCBAVol1
Output:
Example #7 – Removing a Logical Volume
Syntax:
lvremove /dev/EduCBADemoGrp/EduCBAVol2
Output:
Conclusion
With the set of examples and explanations to working of how LVM works and what different utilities the command caters to in Linux. We encourage you to experiment more with different parameters and get acquainted with some failure modes which acts like a block for using LVM in some scenarios!
Recommended Articles
We hope that this EDUCBA information on “Linux LVM” was beneficial to you. You can view EDUCBA’s recommended articles for more information.