Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 40

Dynamic Disk Management with LVM

Recommended resources

Hello! Welcome to the final lesson in our module on Linux Process and Performance Management.

In our last session, we developed a powerful workflow for diagnosing a slow server, using tools like vmstat and iostat to pinpoint resource bottlenecks. We saw how disk I/O can bring a system to its knees. That raises a critical question: what do you do when the problem isn't just how the disk is being used, but the fact that you're running out of space or need more flexibility than traditional partitions offer?

This lesson provides the answer. We will explore the Logical Volume Manager (LVM), a cornerstone of modern Linux storage administration. You will learn to create, extend, and resize logical volumes using LVM to manage disk storage dynamically. This skill is essential for managing servers where storage needs change over time, and it's a topic you can expect to discuss in a technical interview.

Let's get started.

1. The Problem with Static Partitions

Imagine you've partitioned a disk for a server. You give 50GB to /var for logs and application data. A few months later, the application becomes wildly popular, and the log files fill up the entire 50GB partition. The rest of the disk has plenty of free space, but /var is full and the application is crashing.

With traditional partitions, fixing this is a painful, risky process. With LVM, it's a routine, online operation. Let's see a powerful demonstration of why LVM is so valuable.

Linux Logical Volume Manager (LVM) Deep Dive Tutorial

The video "Linux Logical Volume Manager (LVM) Deep Dive Tutorial" from Learn Linux TV starts with a perfect demonstration of the problem we're solving. It shows what happens when a standard filesystem runs out of space versus an LVM-managed one.

Watch the first 10 minutes of the video (00:00 - 10:28). Focus on: The scenario where a log file fills up the disk on a non-LVM system. The limitations in recovering from this situation. How easily the same problem is solved on the LVM-enabled server by extending the filesystem online (without a reboot).

This demonstration makes the "why" of LVM crystal clear. It provides an abstraction layer over your physical disks, allowing you to treat them as a single, flexible pool of storage.


2. The Core Concepts of LVM

LVM achieves this flexibility through a three-tiered hierarchy. Understanding these layers is key to using LVM effectively.

  1. Physical Volumes (PVs): These are the raw building blocks. A PV can be a whole disk (e.g., /dev/sdb) or a disk partition (e.g., /dev/sdb1). You initialize a physical storage device as a PV to tell LVM it's available for use.

  2. Volume Groups (VGs): A VG is a pool of storage created by grouping one or more PVs together. For example, you could combine two 1TB disks (/dev/sdb and /dev/sdc) into a single 2TB Volume Group called datavg. This VG is your total available storage pool.

  3. Logical Volumes (LVs): An LV is what you actually use. It's "carved" out of a Volume Group and behaves like a regular partition. You can create a 200GB LV named webapp_data from your datavg pool, format it with a filesystem like ext4, and mount it at /srv/webapp. The beauty is that you can easily resize this LV later by adding more space from the VG.

This diagram illustrates the relationship perfectly.

LVM Setup Hierarchy
This diagram shows the LVM hierarchy. A physical disk partition (/dev/sdb1) is designated as a Physical Volume (PV). It's added to a Volume Group (Vol_Gp0), which acts as a storage pool. From this pool, Logical Volumes (LV_1, LV_2) are created, which are then formatted and mounted just like regular partitions.

Now that we have the theory, let's get our hands on the "katana" and put it into practice.


3. Setting Up Your Practice Lab

For this lesson, we need a system with extra disks to work with. The best way to practice LVM is in a safe virtual environment.

Complete Beginner's Guide to LVM in Linux [With Hands-on]

The article "Complete Beginner's Guide to LVM in Linux" provides an excellent Vagrant configuration that will automatically set up a virtual machine with three extra virtual disks, perfect for our exercises.

Follow the instructions in the "Lab preparation for hands-on" section to install Vagrant and VirtualBox (if you haven't already) and to download and start the provided virtual machine. Once it's running, SSH into it with vagrant ssh. This will give you the perfect sandbox to safely run all the commands in this lesson.

Once you are inside the VM, you can verify your disks with lsblk. You should see /dev/sdc, /dev/sdd, and /dev/sde ready for you to use.


4. Hands-On: Create, Extend, and Resize

We'll now walk through the most common LVM operations.

Part 1: Creating an LVM Setup from Scratch

First, let's create a new LVM setup using two of our spare disks. We'll create PVs, a VG, an LV, format it, and mount it.

Complete Beginner's Guide to LVM in Linux [With Hands-on]

The same "Beginner's Guide to LVM" article provides a clear, command-by-command walkthrough of this exact process. We will follow its steps.

Read and execute the commands in these sections: "1. Physical Volumes": Focus on the pvcreate and pvs/pvdisplay commands. Use /dev/sdc and /dev/sdd to create your PVs. "2. Volume Groups": Follow the vgcreate instructions to create a VG named my_storage using the two PVs you just made. "3. Logical Volumes": Use lvcreate to make a 3GB Logical Volume named my_data inside your my_storage VG. Then, format it with mkfs.ext4 and mount it to a directory like /mnt/my_data (you'll need to create this directory first with sudo mkdir -p /mnt/my_data).

After completing these steps, you will have a fully functional LVM volume mounted and ready to use. You can check your work with the pvs, vgs, lvs, and df -h commands.

Part 2: Extending a Logical Volume (The Most Common Task)

This is the scenario from our introduction. Your application is running out of space on my_data, and you need to give it more room. We have a spare disk, /dev/sde, ready to be added to the pool.

LVM | Logical Volume Management | Combining Drives Together

Chris Titus Tech's video, "LVM | Logical Volume Management," offers a very concise and practical demonstration of extending a volume. It's a great, fast-paced guide to this core task.

Watch the segment from 08:25 to 16:36. Follow along in your own VM to perform the same steps on your LVM setup: Create a new PV: Use sudo pvcreate /dev/sde to initialize your last spare disk. Extend the VG: Use sudo vgextend my_storage /dev/sde to add the new PV to your volume group. Extend the LV: Use a command like sudo lvextend -L +4G /dev/my_storage/my_data to add 4GB to your logical volume. The -L +4G syntax means "add 4 gigabytes to the current size". Resize the Filesystem: This is the crucial final step! LVM has given the volume more space, but the filesystem on top doesn't know about it yet. Run sudo resize2fs /dev/my_storage/my_data to make the filesystem expand to fill the new space. After you're done, run df -h again. You'll see that /mnt/my_data is now larger, all without unmounting it or rebooting!

This ability to seamlessly add storage to a live system is the primary reason LVM is so widely used in server environments.

Part 3: Reducing a Logical Volume

While less common, you sometimes need to shrink a logical volume to reclaim space for other uses. This process is more complex and carries more risk. If you get the steps wrong, you will destroy the data on your filesystem. Always have a backup before attempting to shrink a volume.

The key steps are:

  1. Unmount the filesystem.
  2. Run a filesystem check to ensure it's clean.
  3. Resize the filesystem first, shrinking it to a size smaller than your target LV size. This is the most critical step.
  4. Reduce the logical volume size.
  5. Remount the filesystem.

Power of LVM in Linux: A Comprehensive & Practical Guide - Harsh

The article "Power of LVM in Linux" provides a detailed and safe procedure for reducing a logical volume.

Carefully read and follow the steps in the "LV REDUCTION STEPS" section. Apply them to the my_data volume you created. For example, let's try to shrink it by 2GB. Unmount: sudo umount /mnt/my_data Check: sudo e2fsck -f /dev/my_storage/my_data Resize FS: sudo resize2fs /dev/my_storage/my_data 5G (assuming it's currently 7G; you're resizing it to the new target size). Reduce LV: sudo lvreduce -L 5G /dev/my_storage/my_data (This reduces the LV to match the new filesystem size). Mount: sudo mount /dev/my_storage/my_data /mnt/my_data Run df -h to confirm the new, smaller size.


Conclusion

Excellent work. You've now mastered the fundamentals of dynamic storage management in Linux. This skill moves you beyond simple setup and into the realm of scalable, flexible system administration. You can now confidently manage storage on a server, adapting to changing needs without requiring downtime or risky partition manipulations.

Key Takeaways:

  • LVM provides a flexible abstraction layer over physical disks using Physical Volumes (PVs), Volume Groups (VGs), and Logical Volumes (LVs).
  • The primary benefit of LVM is the ability to dynamically resize filesystems.
  • Extending a logical volume is a safe, online process: vgextend -> lvextend -> resize2fs.
  • Reducing a logical volume is a risky, offline process that requires a specific order of operations: unmount -> check fs -> resize2fs (shrink fs) -> lvreduce (shrink lv).

This lesson concludes our module on process and performance management. You've learned to monitor processes, diagnose performance bottlenecks, and now, to manage the underlying storage.

In our next module, "Linux System Troubleshooting and Recovery," we will dive even deeper into system internals, starting with how to trace the Linux boot process stages (BIOS -> GRUB -> Kernel -> systemd) to identify where a failure occurs.