Skip to content

Intermediate · Storage: partitions, file systems and LVM · topic 27 of 57 · 40 min

LVM: physical volumes, volume groups and logical volumes

Explanation

LVM (Logical Volume Manager) pools disks into a volume group and cuts flexible logical volumes from it. Disks or partitions become physical volumes (pvcreate), are pooled into a volume group (vgcreate), and logical volumes are made with lvcreate. Space is counted in extents, 4 MiB each by default.

The problem with plain partitions

A partition has a fixed size and lives on one disk. When /data fills up, you cannot just "add another disk to it". LVM adds a layer between disks and file systems that fixes this. It is used on most RHEL servers; the installer itself puts / and swap on LVM by default.

Think of it like this: Think of LVM as a joint family's bank account. Each earner (a disk) deposits money: these are physical volumes. All deposits go into one family account: the volume group. From it, the family makes separate budgets for food, school fees and travel: the logical volumes. If school fees need more, you move money from the shared pool, or a new earner adds a deposit. Nobody has to break open a fixed deposit.

The three layers

LayerWhat it isCreateShow
PV (physical volume)A disk or partition prepared for LVMpvcreatepvs, pvdisplay
VG (volume group)A pool of space made from one or more PVsvgcreatevgs, vgdisplay
LV (logical volume)A slice of the VG used like a partitionlvcreatelvs, lvdisplay

A VG is divided into physical extents (PE), small equal blocks. The default PE size is 4 MiB; vgcreate -s 8M chooses another. An LV is always a whole number of extents. Exam tasks sometimes say "make an LV of 20 extents in a VG with 16 MiB extents"; that is 320 MiB.

Building it, step by step

bash
# pvcreate /dev/vdb3 /dev/vdc1
  Physical volume "/dev/vdb3" successfully created.
  Physical volume "/dev/vdc1" successfully created.
# vgcreate -s 8M vgdata /dev/vdb3 /dev/vdc1
  Volume group "vgdata" successfully created
# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  vgdata   2   0   0 wz--n- <6.98g <6.98g
# lvcreate -n lvweb -L 2G vgdata         # by size
  Logical volume "lvweb" created.
# lvcreate -n lvlogs -l 64 vgdata        # by extents: 64 x 8 MiB = 512 MiB
# lvcreate -n lvrest -l 100%FREE vgdata  # everything left
# lvs
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%
  lvlogs vgdata -wi-a----- 512.00m
  lvrest vgdata -wi-a-----  <4.48g
  lvweb  vgdata -wi-a-----   2.00g
-L takes a size (M, G); -l takes a number of extents or a percentage.

An LV appears as two names that point to the same device: /dev/vgdata/lvweb and /dev/mapper/vgdata-lvweb. Use either in fstab. From here it is like any partition: make a file system, mount it, add it to fstab.

bash
# mkfs.xfs /dev/vgdata/lvweb
# mkdir -p /var/www
# echo "/dev/vgdata/lvweb /var/www xfs defaults 0 0" >> /etc/fstab
# systemctl daemon-reload && mount -a
# lsblk /dev/vdb3
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vdb3             252:19   0    2G  0 part
└─vgdata-lvweb   253:2    0    2G  0 lvm  /var/www

Removing in the reverse order

bash
# umount /var/www          # and remove its fstab line
# lvremove /dev/vgdata/lvweb
# vgremove vgdata
# pvremove /dev/vdb3 /dev/vdc1

Other storage layers (briefly)

RHEL also offers Stratis, a newer tool that manages pools and thin file systems with one command set, and VDO (Virtual Data Optimizer), which saves space by removing duplicate data and compressing it; in RHEL 9 VDO is used through LVM (lvcreate --type vdo). Both are useful in special cases. Learn plain LVM well first; it is what you will meet on almost every server and in the exam. Check the current exam objectives for whether Stratis or VDO are included.

Common mistakes

  • Running pvcreate on a disk that has data or a partition table you need. It overwrites the start of the device.
  • Mixing up -L (size) and -l (extents): lvcreate -l 2G fails, and lvcreate -L 64 gives 64 MiB, not 64 extents.
  • Choosing a PE size the task did not ask for. Some exam tasks specify it; set it with vgcreate -s, because it cannot easily be changed later.
  • Forgetting that an LV still needs a file system and a mount.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

LVM: physical volumes, volume groups and logical volumes · 1/7

disks → PV → VG → LV → file system

Plain partitions have a fixed size on one disk. LVM adds a flexible layer.

Prefer a video by a teacher? Find free videos on “lvm tutorial pvcreate vgcreate lvcreate rhel 9 rhcsa” on YouTube ↗

In real life: Storage for a fast-growing ed-tech start-up

A Noida ed-tech start-up stores lecture videos on a RHEL server. They do not know how fast storage will grow. The admin puts both data disks into one volume group, vgmedia, and creates a logical volume using only 60% of the space. When usage grows, he can extend it in minutes without downtime, or add another disk to the group.

bash
# pvcreate /dev/sdb /dev/sdc
# vgcreate vgmedia /dev/sdb /dev/sdc
# lvcreate -n lvvideos -l 60%VG vgmedia
# mkfs.xfs /dev/vgmedia/lvvideos
# vgs vgmedia
  VG      #PV #LV #SN Attr   VSize VFree
  vgmedia   2   1   0 wz--n- 3.99t 1.60t

Practice (0/4 done)

Task 1 · Question

What is the correct order to build LVM storage?

Task 2 · Code · Python

Write lv_extents(size_mib, pe_mib) that returns how many extents an LV of at least size_mib MiB needs, when each extent is pe_mib MiB. LVM rounds UP to a whole extent. Example: 500 MiB with 8 MiB extents needs 63 extents (504 MiB).

Task 3 · Code · Python

Write vg_free_after(vg_extents, pe_mib, lv_sizes_mib) that returns the free space in MiB left in a volume group after creating LVs of the given sizes (each rounded up to whole extents). Return -1 if they do not all fit.

Task 4 · Practice

Using the LVM partition from the first lesson plus your second spare disk: create a VG named "vgdata" with 16 MiB extents, an LV "lvdata" of exactly 20 extents with XFS mounted on /data permanently, and check every layer.

Quick quiz

Question 1

1.What is the default physical extent size?

Question 2

2.Which creates an LV using all remaining space in vg01?

Question 3

3.Which two paths name the same LV?

Question 4

4.A VG has 8 MiB extents. lvcreate -l 25 makes an LV of…

Question 5

5.Which command lists volume groups with their free space?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is the default physical extent size?

Answer: 4 MiB

The default PE size is 4 MiB.

Which creates an LV using all remaining space in vg01?

Answer: lvcreate -n lv1 -l 100%FREE vg01

Percentages go with -l, e.g. 100%FREE.

Which two paths name the same LV?

Answer: /dev/vg01/lv1 and /dev/mapper/vg01-lv1

Both are links to the same device-mapper device.

A VG has 8 MiB extents. lvcreate -l 25 makes an LV of…

Answer: 200 MiB

25 x 8 MiB = 200 MiB.

Which command lists volume groups with their free space?

Answer: vgs

vgs shows VSize and VFree.

Which command grows a mounted XFS file system to fill its LV?Related

Answer: xfs_growfs /data

xfs_growfs takes the mount point.

From Growing storage and adding swap

Two containers are on the default bridge network. Why can the api container not reach the database at the host name db?Related

Answer: The default bridge network has no DNS by container name; use a user-defined network

User-defined bridge networks provide name resolution; the default bridge does not.

From Running containers: ports, volumes, networks and limits

How many partitions does a GPT table allow by default?Related

Answer: 128

GPT reserves room for 128 entries.

From Disks and partitions: MBR, GPT, fdisk and parted

What is the usual default general purpose EBS volume type today?Related

Answer: gp3

gp3 is the general purpose SSD with a baseline of 3,000 IOPS and 125 MiB/s included.

From Block and file storage: EBS, instance store and EFS

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 1 more topic in “Storage: partitions, file systems and LVM” before its exam: Growing storage and adding swap.
  2. Module 5 exam: Storage · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, SELinux and SSH. 57 topics left in the course before the final exam.

← Previous: Permanent mounts with /etc/fstab and UUIDs