Intermediate · Storage: partitions, file systems and LVM · topic 27 of 57 · 40 min
LVM: physical volumes, volume groups and logical volumes
Explanation
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.
The three layers
| Layer | What it is | Create | Show |
|---|---|---|---|
| PV (physical volume) | A disk or partition prepared for LVM | pvcreate | pvs, pvdisplay |
| VG (volume group) | A pool of space made from one or more PVs | vgcreate | vgs, vgdisplay |
| LV (logical volume) | A slice of the VG used like a partition | lvcreate | lvs, 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
# 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.00gAn 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.
# 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/wwwRemoving in the reverse order
# umount /var/www # and remove its fstab line
# lvremove /dev/vgdata/lvweb
# vgremove vgdata
# pvremove /dev/vdb3 /dev/vdc1Other 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
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.
# 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.60tPractice (0/4 done)
What is the correct order to build LVM storage?
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).
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Growing storage and adding swapStorage: partitions, file systems and LVM · same module
- Running containers: ports, volumes, networks and limitsDevOps: Git, Docker, Kubernetes and CI/CD · Docker
- Block and file storage: EBS, instance store and EFSCloud Computing (AWS, Azure, Google Cloud) · Compute and storage: EC2 and S3
- Docker Compose: multi-container appsDevOps: Git, Docker, Kubernetes and CI/CD · Docker
- HAVING versus WHERESQL and Databases · Aggregation: GROUP BY and HAVING
- OVER and PARTITION BYSQL and Databases · Window functions
Questions people ask about this
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
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
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.
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 more topic in “Storage: partitions, file systems and LVM” before its exam: Growing storage and adding swap.
- Module 5 exam: Storage · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, SELinux and SSH. 57 topics left in the course before the final exam.