Understanding LVM (Logical Volume Manager) in Linux

Understanding LVM (Logical Volume Manager) in Linux

LVM (Logical Volume Manager) is a tool used for managing disk storage in Linux. It allows administrators to manage disk partitions more flexibly, providing capabilities to resize, extend, or even remove volumes without much hassle. Unlike traditional partitioning schemes, LVM enables the pooling of storage from multiple physical devices into a single volume group (VG), which can then be subdivided into logical volumes (LVs). These logical volumes can be formatted, mounted, and used just like regular partitions.

Key Concepts in LVM

  • Physical Volume (PV):

    • This is the physical storage device or partition that is added to LVM. It can be a hard disk, SSD, or partition.
  • Volume Group (VG):

    • A collection of physical volumes (PVs) that create a pool of storage. Multiple physical volumes can be combined into a single volume group.
  • Logical Volume (LV):

    • A partition created from the volume group. These are the "virtual partitions" that the operating system uses to store data.

LVM Workflow Example

Step-by-Step Example of Adding and Extending Space Using LVM

Let's assume you have two new applications, app1 and app2, and you need separate partitions and space for each application.

Install a New Hard Disk

First, you need to install a new hard disk on the server, which will be used for LVM storage

After that Start the Linux Environment

sudo su - 
fdisk -l

2. Create Partitions on the New Disk

You can partition the new disk using fdisk or parted. This step involves creating partitions that will later be used as Physical Volumes (PVs).

fdisk /dev/sdb

Select t for , Change a Partition type

Select L for , list known partition types

Select 8e , Linux LVM

Select w for , Save and Continue

fdisk -l , to check , if /dev/sdb1 has been created.

3. Create Physical Volumes (PV)

After creating partitions, you will designate them as Physical Volumes (PV). Use the pvcreate command to do this.

pvcreate /dev/sdb1
pvdisplay

4. Create a Volume Group (VG)

Next, group the physical volume into a Volume Group (VG). You can have multiple physical volumes in a single volume group.

vgcreate demoapp /dev/sdb1
vgdisplay

5. Create Logical Volumes (LV)

Once the volume group is created, you can create Logical Volumes (LV) for your applications. A logical volume is similar to a partition, and you can define its size based on your needs.

lvcreate -L 1000M -n lvapp1 demoapp 
lvcreate -L 1000M -n lvapp2 demoapp
lvdisplay

6. Apply a Filesystem

After creating the logical volumes, you'll need to format them with a file system such as ext4 or xfs. This will allow the logical volume to be used for storing data.

mkfs.ext4 /dev/demoapp/lvapp1
mkfs.ext4 /dev/demoapp/lvapp2

7. Set Mount Points

Finally, you can set the mount points for the logical volumes. For example, you might want to mount lvapp1 to /app-1 and lvapp2 to /app-2.

mkdir /app-1
mkdir /app-2

mount /dev/demoapp/lvapp1 /app-1
mount /dev/demoapp/lvapp2 /app-2

To ensure that the volumes are mounted automatically after reboot, add the following lines to the /etc/fstab file:

Conclusion

LVM is a powerful tool for managing storage in Linux. It allows you to efficiently manage disk space, create logical volumes on-the-fly, and expand storage as needed without much disruption. Whether you're managing multiple partitions on a single disk or combining storage from multiple disks, LVM offers flexibility and scalability for modern server environments.

With the steps outlined above, you can easily add and extend space on your Linux system, ensuring your server can accommodate growing applications like app1 and app2 without running into disk space limitations.