Lab 11: LVM — Logical Volume Management

Time: 40 minutes | Level: Advanced | Docker: docker run -it --rm --privileged ubuntu:22.04 bash

LVM (Logical Volume Manager) adds a flexible abstraction layer between physical disks and filesystems. You can resize volumes online, create snapshots, and span data across multiple disks without repartitioning.


Prerequisites

docker run -it --rm --privileged ubuntu:22.04 bash
apt-get update -qq && apt-get install -y lvm2

Step 1: Create Loopback Devices (Virtual Disks)

In this lab we simulate physical disks using loopback devices — image files mounted as block devices.

# Create two 100 MiB disk images
dd if=/dev/zero of=/tmp/disk1.img bs=1M count=100
dd if=/dev/zero of=/tmp/disk2.img bs=1M count=100

# Create explicit loop device nodes (required in privileged Docker)
mknod /dev/loop60 b 7 60 2>/dev/null || true
mknod /dev/loop61 b 7 61 2>/dev/null || true

# Attach the images to loop devices
losetup /dev/loop60 /tmp/disk1.img
losetup /dev/loop61 /tmp/disk2.img

# Verify
losetup -a | grep loop6

📸 Verified Output:

💡 losetup -f --show /file.img auto-selects the next free loop device and prints it — useful in scripts.


Step 2: Initialize Physical Volumes (PVs)

📸 Verified Output:

💡 The "not usable 4.00 MiB" is reserved for LVM metadata (stored at the start of the PV).


Step 3: Create a Volume Group (VG)

A Volume Group pools multiple PVs into one storage reservoir.

📸 Verified Output:

💡 PE (Physical Extent) is LVM's allocation unit — default 4 MiB. All LV sizes are multiples of the PE size.


Step 4: Create a Logical Volume (LV) and Format It

📸 Verified Output:


Step 5: Mount and Use the Logical Volume

📸 Verified Output:

💡 Use lvs for a compact overview of all LVs: lvs vg_data


Step 6: Extend a Logical Volume Online

One of LVM's biggest advantages — resize without unmounting!

📸 Verified Output:

💡 For XFS filesystems, use xfs_growfs /mnt/lv_data instead of resize2fs. XFS only supports online growth.


Step 7: Extend the Volume Group (pvmove / vgextend)

When a VG runs out of space, add a new PV:

📸 Verified Output:

pvmove — Migrate data between PVs:

💡 pvmove is non-destructive and works while the filesystem is mounted — great for disk replacement with no downtime.


Step 8: Capstone — Thin Provisioning Concept

Thin provisioning lets you over-allocate storage — logical volumes appear larger than the actual physical backing.

⚠️ Thin pools require dm-thin-pool kernel module. In this Docker environment we demonstrate the concept:

Capstone Challenge: Simulate a disk being decommissioned:


Summary

Concept
Command
Purpose

Physical Volume

pvcreate /dev/loop0

Initialize disk for LVM

Volume Group

vgcreate vg_name PV...

Pool multiple PVs

Logical Volume

lvcreate -L size -n name VG

Create virtual disk

Inspect

pvdisplay, vgdisplay, lvdisplay

Show LVM info

Format

mkfs.ext4 /dev/mapper/VG-LV

Create filesystem

Extend LV

lvextend -L +size /dev/VG/LV

Grow logical volume

Grow FS

resize2fs / xfs_growfs

Expand filesystem

Add PV to VG

vgextend VG /dev/newdisk

Increase VG capacity

Migrate data

pvmove /dev/old /dev/new

Relocate extents

Thin pool

lvcreate --type thin-pool

Over-provision storage

Last updated