# Logical Volume Management (LVM)

> Chapter 10 of the Courseiva LF-LFCS curriculum — https://courseiva.com/learn/lf-lfcs/logical-volume-management-lvm

**Official objective:** 4.2 — Configure and manage Logical Volume Manager (LVM) volumes

## Introduction

If you don't understand LVM, you could easily lose all your data when a disk runs out of space or when you need to upgrade storage on a Linux server. LVM is the tool that lets you carve up and resize disk space on a running system, making it essential for anyone who wants to pass the LFCS exam and manage real-world Linux servers.

## The Apartment Building Storage Analogy

3 friends share a house. Each has their own bedroom, but the kitchen, living room, and bathroom are shared. One friend's bedroom has a small closet, another has a built-in wardrobe, and the third has a large walk-in. This is like traditional disk partitioning: each partition (room) is fixed in size and separate. Now imagine they move into an apartment building. The building has 100 identical storage lockers in the basement. Each friend gets a key to a certain number of lockers. Friend A gets 10 lockers, Friend B gets 20, Friend C gets 30. They can combine their lockers into one big virtual storage space. If Friend C needs more lockers, the building manager can reassign some unused lockers from Friend A. This is Logical Volume Management (LVM). The physical lockers are Physical Volumes (PVs). The combined pool of all lockers is the Volume Group (VG). Each friend's assigned set of lockers is a Logical Volume (LV). LVM lets you resize, move, and combine storage without breaking anything, just like reassigning lockers without demolishing walls.

## Core explanation

Logical Volume Management (LVM) is a system for managing disk storage on Linux that gives you incredible flexibility. Instead of being stuck with a fixed-size partition that you can never change without wiping the drive, LVM lets you treat physical hard drives (or partitions on them) as a big pool of storage. You can then carve virtual disks out of that pool, grow them, shrink them, or move them between physical drives, all while the system is still running.

Let's start with the basic building blocks. A Physical Volume (PV) is any storage device that LVM can use. This could be a whole hard drive (like /dev/sdb), a single partition (like /dev/sda3), or even a RAID array. You mark a drive as a PV using the pvcreate command. Think of PVs as individual bricks of storage.

Next, a Volume Group (VG) is a collection of one or more Physical Volumes. You create a VG with the vgcreate command. The VG is like a big bucket of combined storage. The total size of the VG is the sum of all the PVs you add to it. You can add more PVs to a VG later if you need more space.

Finally, a Logical Volume (LV) is a virtual partition that you carve out of a Volume Group. You create one with lvcreate. This is what you actually format with a filesystem (like ext4 or XFS) and mount to a directory so you can store files. An LV can be smaller than the total VG, and you can expand it later if you have free space in the VG.

Why does this matter? Imagine you have a server with a single 500GB hard drive. You partition it into a 200GB root partition (/) and a 300GB data partition (/data). Over time, the data partition fills up. Without LVM, you are in trouble. You would have to back up all the data, repartition the drive (which wipes everything), restore the data, and hope you guessed the new sizes correctly. This takes the server offline, costs hours of work, and risks data loss.

With LVM, you simply add a second 500GB hard drive to the server. You mark the new drive as a PV with pvcreate /dev/sdc. You add it to the existing VG with vgextend. Then you extend your Logical Volume with lvextend -L +500G /dev/vg_name/lv_data. Finally, you resize the filesystem with resize2fs (for ext4) or xfs_growfs (for XFS). The whole process takes minutes, does not require a reboot, and does not touch any existing data.

LVM also supports snapshots, which let you take a point-in-time copy of a Logical Volume without halting the system. This is brilliant for backups or testing changes before applying them permanently.

Key commands you must know for LFCS:
- pvcreate / pvdisplay / pvs: manage Physical Volumes
- vgcreate / vgdisplay / vgs / vgextend: manage Volume Groups
- lvcreate / lvdisplay / lvs / lvextend / lvreduce: manage Logical Volumes
- lvresize: can both extend and reduce LVs
- resize2fs (ext4) or xfs_growfs (XFS): resize the filesystem inside the LV after you resize the LV itself

Remember: the order of resizing matters. First resize the LV, then resize the filesystem. For shrinking, you must defragment and resize the filesystem first, then shrink the LV. This is a common trap in the LFCS exam.

## Real-world context

You are a junior Linux administrator at a mid-size e-commerce company. The company runs its online store on a single Linux server. The server has a 1TB hard drive split into three partitions: / (50GB), /var (200GB, where logs and databases live), and /data (750GB, where product images and customer uploads are stored). The /var partition is filling up fast because the database keeps growing. The server is critical, and downtime costs the company £10,000 per hour.

Without LVM, you would be panicking. With LVM, you have a plan. The server was set up with LVM during initial deployment, so all three partitions are Logical Volumes inside a single Volume Group called 'vg_store'. You check the Volume Group free space using the vgs command. You see there is 50GB free in the VG. You can immediately add that 50GB to the /var LV using lvextend -L +50G /dev/vg_store/var and then grow the filesystem with resize2fs /dev/vg_store/var. The server never goes offline, and the database keeps running the whole time.

A few months later, the company decides to add a new product feature that requires storing high-resolution videos. The /data partition is now 95% full, and the VG has no free space left. You have been authorised to buy a new 2TB hard drive. You install it in the server, run pvcreate /dev/sdb to make it a Physical Volume, then vgextend vg_store /dev/sdb to add the 2TB to your storage pool. Now you have plenty of free space. You extend the /data LV with lvextend -r /dev/vg_store/data -L +1.5T. The -r flag automatically resizes the filesystem as well.

In practice, an IT professional uses LVM for:
- Live storage expansion without rebooting
- Moving data between physical drives without downtime (using pvmove)
- Creating snapshots before applying risky updates or configuration changes
- Combining multiple small disks into one large volume
- Easily resizing partitions when requirements change

The key tools you use daily are:
- lvs to check current Logical Volume sizes and names
- vgs to see free space in your Volume Group
- lvextend with -L (absolute size) or -l (in extents) to grow LVs
- pvmove to relocate data from one failing drive to a healthy one

This flexibility is why nearly every production Linux server uses LVM. The LFCS exam tests your ability to read and understand command output, know the correct order of operations, and recognise the right commands for each task.

## Exam focus

The LFCS exam tests Logical Volume Management (LVM) extensively. Expect at least 3-5 questions specifically on LVM concepts and commands. The exam focuses on practical application: you will be given a scenario and asked to choose the correct command or set of commands.

Topics the exam loves to test:
- The correct command to create a Physical Volume (pvcreate)
- The command to display all Physical Volumes (pvs or pvdisplay)
- How to add a new disk to an existing Volume Group (vgextend)
- The command to create a Logical Volume (lvcreate) with specific size (-L or -l)
- The command to extend a Logical Volume (lvextend)
- The command to rescan for new LVM devices (pvscan or partprobe)
- The exact order of steps to extend a Logical Volume and its filesystem
- The difference between -L (absolute size) and -l (number of extents)
- How to create a Volume Group (vgcreate)
- The command to remove a Physical Volume from a Volume Group (vgreduce)

Common traps and wrong answers:
- They ask 'Which command adds a new disk to LVM?' and the options include fdisk, mkfs, or mount. The correct answer is pvcreate then vgextend.
- They ask 'After extending a Logical Volume, what must you do?' and the options include nothing, reboot, or run fsck. The correct answer is resize the filesystem with resize2fs or xfs_growfs depending on the filesystem type.
- They ask 'Which of the following is NOT a valid LVM command?' and include nonsense commands like lvformat or vgformat. You need to recognise the real commands: pvcreate, vgcreate, lvcreate, lvextend, lvreduce, lvresize, pvmove, pvs, vgs, lvs, pvdisplay, vgdisplay, lvdisplay, vgextend, vgreduce.
- They give you a list of steps in random order and ask you to put them in the correct sequence for extending a Logical Volume.

Key definitions to memorise:
- Physical Volume (PV): a hard disk, partition, or RAID array that LVM can use
- Volume Group (VG): a pool of storage made from one or more PVs
- Logical Volume (LV): a virtual partition carved from a VG, can be formatted and mounted
- Extent: the smallest unit of allocation in LVM (typically 4 MiB)
- pvresize: command to resize a PV if the underlying disk has changed size
- lvresize: a unified command that can both extend and reduce LVs (unlike lvextend and lvreduce which are more specific)

The exam also expects you to know that you can have multiple Volume Groups, and Logical Volumes can be mounted via standard mount commands once formatted. You should also understand that you need to activate LVM volumes with vgchange -ay before they can be used if they are from a different system.

## Step by step

1. **Prepare the physical disk** — First, you need a physical storage device. This could be a new hard drive, an SSD, or even a partition. You identify the device (e.g., /dev/sdb) and optionally partition it using fdisk or parted. The whole disk or a partition can become a Physical Volume (PV).
2. **Create the Physical Volume** — Run pvcreate /dev/sdb (or /dev/sdb1 if you partitioned it). This writes LVM metadata onto the device, marking it as available for LVM. You can verify with pvs or pvdisplay. Once a PV is created, it is reserved for LVM and cannot be used directly by the system.
3. **Create the Volume Group** — Run vgcreate my_volume_group /dev/sdb. This creates a pool named 'my_volume_group' from the PV. You can add more PVs later with vgextend. Use vgs or vgdisplay to check the total size and free space of the VG.
4. **Create a Logical Volume** — Run lvcreate -L 10G -n my_logical_volume my_volume_group. This carves out a 10GB Logical Volume named 'my_logical_volume' from the Volume Group. The LV appears as a block device at /dev/my_volume_group/my_logical_volume or /dev/mapper/my_volume_group-my_logical_volume.
5. **Format and mount the Logical Volume** — Format the LV with a filesystem: mkfs.ext4 /dev/my_volume_group/my_logical_volume. Then create a mount point: mkdir /mnt/data. Mount the LV: mount /dev/my_volume_group/my_logical_volume /mnt/data. To make the mount permanent, add an entry to /etc/fstab.
6. **Extend the Logical Volume (when you need more space)** — When the LV runs out of space, first ensure there is free space in the VG (check with vgs). Then extend the LV: lvextend -L +5G /dev/my_volume_group/my_logical_volume. Finally, resize the filesystem: resize2fs /dev/my_volume_group/my_logical_volume. The LV is now 5GB larger without any downtime.

## Comparisons

### Physical Volume (PV) vs Standard Partition

**Physical Volume (PV):**
- Created using pvcreate command
- Can be part of a Volume Group and combined with other PVs
- Cannot be directly formatted with a filesystem

**Standard Partition:**
- Created using fdisk or parted
- Is independent and cannot be combined with others
- Can be directly formatted with a filesystem and mounted

### Volume Group (VG) vs Physical Disk

**Volume Group (VG):**
- A logical pool of storage from multiple PVs
- Can be dynamically extended by adding more PVs
- Has no corresponding /dev entry; managed with vgcreate

**Physical Disk:**
- A single physical device (sdX)
- Cannot be extended without replacing the disk
- Has a fixed /dev entry (e.g., /dev/sda)

### lvextend Command vs resize2fs Command

**lvextend Command:**
- Extends the Logical Volume (the block device)
- Does not affect the filesystem on top of the LV
- Requires free space in the Volume Group

**resize2fs Command:**
- Resizes the filesystem inside the Logical Volume
- Must be run after lvextend to use the new space
- Does not care about the Volume Group, only the filesystem

### LVM Snapshot vs Full Backup

**LVM Snapshot:**
- Point-in-time copy that shares data with the original
- Very fast to create and uses little space initially
- Not independent; requires original LV to remain unchanged

**Full Backup:**
- Independent copy of all data
- Slower to create and uses space equal to data size
- Does not depend on the original to restore

## Diagram

_This diagram shows how two physical disks become Physical Volumes, are combined into one Volume Group, and then three Logical Volumes are carved from that group and formatted with different filesystems._

```mermaid
flowchart TD
    A[Physical Disk /dev/sdb] --> B[Physical Volume (PV)]
    C[Physical Disk /dev/sdc] --> D[Physical Volume (PV)]
    B --> E[Volume Group (VG)]
    D --> E
    E --> F[Logical Volume 1: /dev/vg/root]
    E --> G[Logical Volume 2: /dev/vg/var]
    E --> H[Logical Volume 3: /dev/vg/home]
    F --> I[Filesystem ext4]
    G --> J[Filesystem XFS]
    H --> K[Filesystem ext4]
```

## Common misconceptions

- **Misconception:** LVM is only for enterprise servers with multiple drives. **Reality:** LVM works on a single disk as well. You can set up a single disk with LVM partitions and still get the flexibility to resize them later without repartitioning the entire disk. (Beginners often think LVM is overkill for a single drive, but it is actually useful on any system where storage needs might change over time.)
- **Misconception:** You must unmount a Logical Volume before you can extend it. **Reality:** You can extend a Logical Volume while it is mounted and in use. The filesystem resize step can also be done online with ext4 and XFS. (People confuse resizing with shrinking (which often requires unmounting) or with traditional partition resizing which does require unmounting.)
- **Misconception:** LVM adds a significant performance overhead compared to regular partitions. **Reality:** Modern LVM performance overhead is negligible (typically less than 1%) and is offset by the management flexibility it provides. (Older versions of LVM had more overhead, and some myths persist from those days. Also, any misconfiguration like mirroring across slow drives can create a bad impression.)
- **Misconception:** You need to reboot after creating or extending a Logical Volume. **Reality:** LVM operations do not require a reboot. Changes take effect immediately after running the appropriate commands. (This confusion comes from hardware changes like adding a new physical disk, which sometimes requires a rescan (but not a reboot) for the OS to recognise it.)
- **Misconception:** LVM snapshots are full backups and can replace regular backups. **Reality:** LVM snapshots are point-in-time copies that share data with the original volume. They are not independent backups and will become invalid if the original volume runs out of space or the snapshot itself fills up. (The word 'snapshot' sounds like a backup, but it is more like a 'save state' that depends on the original data not changing too much.)

## Key takeaways

- LVM stands for Logical Volume Manager and gives you flexible, resizeable disk storage on Linux.
- The three core components of LVM are Physical Volumes (PVs), Volume Groups (VGs), and Logical Volumes (LVs).
- A Physical Volume is any storage device (whole disk, partition, or RAID) that you initialise with pvcreate.
- A Volume Group is a pool of storage created by combining one or more Physical Volumes with vgcreate.
- A Logical Volume is a virtual partition carved from a Volume Group with lvcreate and can be formatted with a filesystem and mounted.
- You can extend a Logical Volume and its filesystem while the system is running, without any downtime.
- The lvextend command grows the LV, but you must separately grow the filesystem with resize2fs (ext4) or xfs_growfs (XFS).
- To shrink a Logical Volume, you must first shrink the filesystem (after unmounting it), then shrink the LV with lvreduce.
- LVM snapshots allow you to capture the state of a Logical Volume at a specific moment, useful for backups before risky changes.
- The -r flag in lvextend automatically resizes the filesystem, saving you an extra command step.

## FAQ

**Can I use LVM on the same disk where the operating system is installed?**

Yes. Many Linux distributions allow you to configure LVM during installation, placing the root filesystem, /var, /home, etc., all on separate Logical Volumes inside one Volume Group on a single disk.

**Can I remove a Physical Volume from a Volume Group without losing data?**

Yes, but only after moving the data off that PV to another PV in the same VG using the pvmove command. Once the PV is empty, you can remove it with vgreduce.

**Is there a maximum size for a Logical Volume?**

The maximum size depends on the filesystem you use, not LVM itself. ext4 has a 16TB per-filesystem limit on most systems, and XFS can handle up to 8 exabytes. LVM can create LVs up to 8 exabytes as well (with 64-bit architectures).

**Do I need to unmount the Logical Volume before creating a snapshot?**

No. One of the benefits of LVM snapshots is that they can be created while the filesystem is mounted and in use. The snapshot will capture a point-in-time image of the data.

**What command shows me the size and free space of my Volume Groups?**

Use vgs (short output) or vgdisplay (detailed output). vgs shows a one-line summary per VG including total size, allocated size, and free size.

**Can I move a Logical Volume to another system?**

Yes. You can export a Volume Group using vgexport, physically move the disks to another system, then import it with vgimport. This requires that all PVs in the VG are moved together.

**What is the difference between lvresize and lvextend?**

lvresize is a general command that can both extend and reduce a Logical Volume. lvextend is a more specific command for extending only. Both work, but lvextend is more explicit and preferred for extending.

## Check your understanding

1. **What is the correct command to mark a physical disk (e.g., /dev/sdb) as available for LVM?**

   Answer: pvcreate /dev/sdb

2. **After extending a Logical Volume with lvextend, what additional command is needed to make the extra space available in the filesystem?**

   Answer: You must resize the filesystem: resize2fs for ext4 or xfs_growfs for XFS.

3. **What LVM component is a collection of Physical Volumes that provides a pool of storage?**

   Answer: Volume Group (VG)

4. **Which command shows a short summary of all Logical Volumes on the system?**

   Answer: lvs

5. **Can you extend a Logical Volume while it is mounted and in use?**

   Answer: Yes, LVM allows you to extend Logical Volumes online without unmounting them, as long as the filesystem supports online resizing (ext4 and XFS do).

---

Interactive version with quiz and diagrams: https://courseiva.com/learn/lf-lfcs/logical-volume-management-lvm
