Kahibaro
Discord Login Register

Logical volumes

Understanding Logical Volumes in Linux (LVM)

Logical volumes are the core abstraction provided by LVM (Logical Volume Manager). In the parent chapter you’ve seen LVM concepts in general; here we focus specifically on logical volumes (LVs): how they are structured, created, managed, and used.

This chapter assumes you already know the relationship:

and focuses on the LV layer.


What Is a Logical Volume (in Practice)?

A logical volume is the “virtual disk” that you actually put filesystems on and mount. From the kernel’s point of view, a logical volume looks like a block device (similar to a normal partition).

Typical device paths:

You can:

The key advantage: LVs are flexible: sizes and layout can change without touching the underlying physical disks directly.


Types of Logical Volumes

LVM supports several kinds of logical volumes. The most common in everyday use:

1. Linear Logical Volumes

This is the default and simplest type:

Use case: Most general-purpose filesystems, root filesystem, /home, etc.

You typically get a linear LV if you don’t specify otherwise when creating it.

2. Striped Logical Volumes

Striped LVs distribute data in stripes across multiple PVs:

Benefits:

Drawbacks:

Creation example (conceptual):

lvcreate -i 2 -I 64 -L 100G -n lvfast vgdata

3. Mirrored Logical Volumes (Classic LVM Mirroring)

Mirrored LVs maintain multiple copies of data across PVs:

Key points:

Note: Many setups now use hardware RAID or MD RAID for mirroring and let LVM sit on top. Mirrored LVs are still useful but not as common as they once were.

4. Thin and Thin-Pool Logical Volumes (Overview Only)

Thin provisioning is powerful and deserves its own deep dive, but from the LV perspective:

Useful when:

Managing thin LVs uses lvcreate --type thin-pool and lvcreate --thin options; we’ll touch on generic LV operations here and leave advanced thin-provisioning behavior for a dedicated chapter or practice.


Creating Logical Volumes

Logical volumes live inside an existing volume group. You must have a VG ready first.

Basic Linear LV Creation

Common patterns:

  1. Create by size:
lvcreate -L 20G -n lvhome vgdata
  1. Create by percentage of VG:
lvcreate -l 100%FREE -n lvbackup vgdata
  1. Create by percentage of total VG size:
lvcreate -l 50%VG -n lvdata vgdata

Creating a Striped LV

You need at least as many PVs as stripes:

lvcreate -L 50G -i 2 -I 128 -n lvvideo vgfast

Key options:

Choose stripe size according to workload (larger for big sequential I/O, smaller for many small I/Os).

Creating a Mirrored LV (Classic)

Example with two-way mirror:

lvcreate -L 20G -m 1 -n lvsafe vgdata

Inspecting Logical Volumes

Several tools let you see LVs and how they are laid out.

The `lvs` Command (Summary View)

lvs

Typical output:

  LV      VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvroot  vgroot  -wi-ao----  40.00g
  lvhome  vgroot  -wi-ao---- 200.00g

Key columns:

You can customize columns, e.g.:

lvs -o lv_name,vg_name,lv_size,segtype

The `lvdisplay` Command (Detailed View)

lvdisplay /dev/vgroot/lvhome

Shows:

Using Logical Volumes as Filesystem Devices

The basic workflow:

  1. Create the LV.
  2. Put a filesystem on it.
  3. Mount it (manually or via /etc/fstab).

Example: Creating and Mounting an LV for `/data`

  1. Create LV:
lvcreate -L 100G -n lvdata vgdata
  1. Make filesystem:
mkfs.ext4 /dev/vgdata/lvdata
  1. Create mount point:
mkdir /data
  1. Mount:
mount /dev/vgdata/lvdata /data
  1. Add to /etc/fstab (persistent mount):
/dev/vgdata/lvdata  /data  ext4  defaults  0  2

Resizing Logical Volumes

One of the biggest advantages of LVs is that you can change their size. You must always consider both:

They are separate layers, and each has its own tools and limits.

Growing (Extending) a Logical Volume

Common situation: partition runs out of space; you want to give it more.

General steps:

  1. Ensure the VG has free space (vgs or vgdisplay).
  2. Extend the LV.
  3. Grow the filesystem.

Example: Extend lvdata from 100G to 150G and grow an ext4 filesystem:

  1. Extend the LV:
lvextend -L 150G /dev/vgdata/lvdata
# or add +50G:
lvextend -L +50G /dev/vgdata/lvdata
  1. Grow the filesystem (online for ext4 on a mounted LV):
resize2fs /dev/vgdata/lvdata

Some filesystems (like XFS) use different tools (e.g. xfs_growfs) and may require specific procedures, but the logic is always:

Shrinking (Reducing) a Logical Volume

Shrinking is riskier than growing because data may be truncated if mistakes are made. General rule:

Shrink the filesystem first, then shrink the LV.

Example with ext4:

  1. Unmount the filesystem (most filesystems require this to shrink):
umount /data
  1. Check filesystem consistency:
e2fsck -f /dev/vgdata/lvdata
  1. Shrink filesystem to target size (must be >= used space):
resize2fs /dev/vgdata/lvdata 80G
  1. Now shrink the LV:
lvreduce -L 80G /dev/vgdata/lvdata
# or interactive:
lvreduce -r -L 80G /dev/vgdata/lvdata

The -r option (--resizefs) can automatically shrink the filesystem along with the LV for supported filesystems, but you should still have backups and understand what is happening.

  1. Mount again:
mount /data

Always have verified backups before shrinking LVs. A mistake can lead to irreversible data loss.


Moving Logical Volumes Between Physical Volumes

You can relocate the data backing an LV from one PV to another without unmounting, using pvmove. The LV itself doesn’t change from the filesystem’s perspective; its underlying extents are just moved.

Example: Move all data from /dev/sdb1 to /dev/sdc1 within the same VG:

pvmove /dev/sdb1 /dev/sdc1

Use cases:

From the logical volume’s view, nothing about its device path or size changes, only the physical location of its extents.


Renaming Logical Volumes

You might need more descriptive LV names later. You can rename without losing data.

Example:

lvrename vgdata oldname lvappdata

or specifying full paths:

lvrename /dev/vgdata/oldname /dev/vgdata/lvappdata

After renaming, remember to update references in:

Deleting Logical Volumes

To remove an LV:

  1. Ensure it is not in use or mounted.
  2. Optionally, wipe filesystem signatures if you want to reuse the space for something else (outside LVM).
  3. Remove the LV.

Example:

umount /data
lvremove /dev/vgdata/lvdata

LVM will ask for confirmation; you can bypass prompts with -y:

lvremove -y /dev/vgdata/lvdata

Warning: LV deletion destroys all data on that logical volume. Recovering data afterward is extremely difficult.


Logical Volume Snapshots (Overview)

Snapshots are a special kind of LV that captures the state of another LV at a point in time using copy-on-write.

Key ideas (from the LV perspective):

Example (classic snapshot):

lvcreate -L 10G -s -n lvsnap /dev/vgdata/lvdata

Modern setups typically use thin snapshots via thin pools, which handle space more efficiently. Regardless of type, when working with snapshots as an admin you treat them as additional LVs.


Practical Patterns and Use Cases

Separate LVs for Different Data Types

Common layout:

Benefits:

Growing Storage Over Time

Scenario:

This elasticity is exactly why logical volumes are central to modern Linux storage design.


Common Logical Volume Management Commands (Cheat Sheet)

Create LV:

lvcreate -L 20G -n lvname vgname

List LVs:

lvs          # summary
lvdisplay    # detailed

Extend LV:

lvextend -L +10G /dev/vgname/lvname
resize2fs /dev/vgname/lvname    # ext4 example

Reduce LV (with care):

umount /mountpoint
e2fsck -f /dev/vgname/lvname
resize2fs /dev/vgname/lvname 50G
lvreduce -L 50G /dev/vgname/lvname
mount /mountpoint

Move LV data from one PV to another:

pvmove /dev/sdX /dev/sdY

Rename LV:

lvrename vgname oldlv newlv

Remove LV:

umount /mountpoint
lvremove /dev/vgname/lvname

By mastering logical volumes—creating, resizing, moving, and snapshotting them—you gain fine-grained, flexible control over storage that traditional fixed partitions cannot offer. The next related topics naturally build on this layer: thin provisioning, snapshots and rollbacks, and integrating logical volumes with RAID and encryption.

Views: 20

Comments

Please login to add a comment.

Don't have an account? Register now!