Storage and LVMLesson 2 of 3
Logical volumes: PV, VG and LV
Turn partitions into physical volumes, pool them into a volume group with a chosen extent size, and carve out logical volumes.
pvcreatevgcreatelvcreatepvsvgslvspvdisplayvgdisplaylvdisplayvgcreate -slvcreate -l
After this lesson you can
- Explain how physical volumes, volume groups, physical extents and logical volumes relate.
- Create a volume group with a specified physical extent size and a logical volume sized in megabytes or in extents.
- Put a file system on a logical volume, mount it persistently and read the layout with the *s and *display commands.
The idea
Partitions are fixed slices. LVM (Logical Volume Manager) adds a flexible layer so storage can be pooled, resized and moved while the system runs. Three layers:
| Layer | Name | What it is |
|---|---|---|
| Bottom | Physical volume (PV) | A disk or partition that LVM has claimed |
| Middle | Volume group (VG) | A pool made of one or more PVs |
| Top | Logical volume (LV) | A slice of the pool that you format and mount |
A VG divides its space into equal chunks called physical extents (PE), 4 MiB by default. A logical volume is always a whole number of extents. That explains two exam favourites:
- The PE size is set when the VG is created:
vgcreate -s 8M. A task may demand a specific one. - Sizes can be given as
-L 100M(megabytes) or-l 10(extents). With 8 MiB extents, 100 MiB is not a multiple of 8, so LVM rounds up to 104 MiB.-l 10gives exactly 80 MiB. When a task says “approximately”, you’re allowed this rounding; if it says extents, give extents.
| Task | PV | VG | LV |
|---|---|---|---|
| Create | pvcreate DEV | vgcreate [-s SIZE] VG DEV... | lvcreate -n NAME -L SIZE VG |
| Short list | pvs | vgs | lvs |
| Detailed | pvdisplay | vgdisplay | lvdisplay |
| Remove | pvremove | vgremove | lvremove |
The result is a device node at /dev/VG/LV, and the same one at /dev/mapper/VG-LV. Hyphens in names are doubled inside /dev/mapper, so avoid hyphens in VG and LV names.
Try it
sudo pvcreate /dev/vdb2
sudo vgcreate -s 8M vgdata /dev/vdb2
sudo vgs vgdata
Physical volume "/dev/vdb2" successfully created.
Volume group "vgdata" successfully created
VG #PV #LV #SN Attr VSize VFree
vgdata 1 0 0 wz--n- 296.00m 296.00m
296 MiB rather than 300: LVM keeps some space for metadata and drops the remainder that does not fill an extent (37 × 8 MiB = 296 MiB).
sudo vgdisplay vgdata | grep -E 'PE Size|Total PE|Alloc PE|Free PE'
PE Size 8.00 MiB
Total PE 37
Alloc PE / Size 0 / 0
Free PE / Size 37 / 296.00 MiB
Two ways to size a volume
sudo lvcreate -n lvweb -L 100M vgdata
sudo lvcreate -n lvlogs -l 10 vgdata
sudo lvs vgdata
Rounding up size to full physical extent 104.00 MiB
Logical volume "lvweb" created.
Logical volume "lvlogs" created.
LV VG Attr LSize
lvlogs vgdata -wi-a----- 80.00m
lvweb vgdata -wi-a----- 104.00m
The “Rounding up” line is LVM telling you exactly what happened. -l 100%FREE uses all remaining space; -l +50%FREE takes half of what is left.
File system and permanent mount
sudo mkfs.xfs /dev/vgdata/lvweb
sudo mkdir -p /srv/web
echo '/dev/mapper/vgdata-lvweb /srv/web xfs defaults 0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo mount -a
findmnt /srv/web
meta-data=/dev/vgdata/lvweb isize=512 agcount=4, agsize=6656 blks
data = bsize=4096 blocks=26624, imaxpct=25
log =internal log bsize=4096 blocks=1368, version=2
/srv/web /dev/mapper/vgdata-lvweb xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota
mount -a mounts everything in /etc/fstab that is not yet mounted. If your new line has an error, it fails now, at the prompt, rather than at boot, where a bad fstab line sends the machine into emergency mode. That is why you always run mount -a after editing it (and why findmnt --verify is worth knowing).
Undo: in reverse order
sudo umount /srv/web
sudo sed -i '\#/srv/web#d' /etc/fstab
sudo lvremove -y /dev/vgdata/lvweb /dev/vgdata/lvlogs
sudo vgremove -y vgdata
sudo pvremove -y /dev/vdb2
Logical volume "lvweb" successfully removed.
Logical volume "lvlogs" successfully removed.
Volume group "vgdata" successfully removed
Labels on physical volume "/dev/vdb2" successfully wiped.
Going further
lvcreate --type can make mirrored, striped, thin and VDO volumes; the RHCSA needs only plain (linear) volumes. lvs -o +devices shows which PV holds each LV, and pvmove migrates extents from one PV to another online, so a disk can be replaced without downtime. vgrename and lvrename rename safely if the volume is unmounted. LVM metadata is backed up automatically in /etc/lvm/backup, and vgcfgrestore can recover a deleted volume group’s layout. vgscan and lvmdevices help when disks move between machines.
Practice
0 of 5 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Warm-up
Read the LVM layout of a system
On the lab VM list the physical volumes, volume groups and logical volumes with the short commands. State the volume group name that holds the root file system and how much free space it has.
Check your result
pvs,vgsandlvseach print a table.vgsshowsVFree.Hint
sudo pvs; sudo vgs; sudo lvs.lsblkshows the same tree.Solution
sudo pvs sudo vgs sudo lvs - Practice
A volume group with 8 MiB extents
Using partition
/dev/vdb2(300 MiB, from the previous lesson), create a physical volume, then a volume groupvgdatawith a physical extent size of 8 MiB. Verify the extent size and how many extents the group has.Check your result
sudo vgdisplay vgdatashowsPE Size 8.00 MiBandTotal PEof about 37 (300 MiB ÷ 8 MiB, minus metadata).Hint
pvcreate /dev/vdb2,vgcreate -s 8M vgdata /dev/vdb2.Solution
sudo pvcreate /dev/vdb2 sudo vgcreate -s 8M vgdata /dev/vdb2 sudo vgdisplay vgdata | grep -E 'PE Size|Total PE|Free PE' - Practice
Size a logical volume two ways
Create
lvwebof exactly 100 MiB using a size in megabytes, andlvlogsof 10 extents using extents. Compare their sizes withlvs. Explain why a request for 100 MiB can be rounded when the extent size is not a divisor of it.Check your result
lvs vgdatashowslvwebat104.00m(100 MiB rounded up to 13 extents of 8 MiB) andlvlogsat exactly80.00m(10 extents).Hint
lvcreate -n NAME -L 100M VGfor size,lvcreate -n NAME -l 10 VGfor extents. A logical volume is always a whole number of extents.Solution
sudo lvcreate -n lvweb -L 100M vgdata sudo lvcreate -n lvlogs -l 10 vgdata sudo lvs vgdata sudo vgs vgdata - Practice
File system, mount point, fstab
Put an XFS file system on
lvweb, mount it on/srv/webpermanently using its device-mapper path, and show thatmount -aand a reboot keep it.Check your result
findmnt /srv/webshowsxfson/dev/mapper/vgdata-lvweb.sudo mount -aprints nothing. After a rebootdf -h /srv/webstill shows it.Hint
mkfs.xfs /dev/vgdata/lvweb. Both/dev/vgdata/lvweband/dev/mapper/vgdata-lvwebname the same device. Add/dev/mapper/vgdata-lvweb /srv/web xfs defaults 0 0to/etc/fstab.Solution
sudo mkfs.xfs /dev/vgdata/lvweb sudo mkdir -p /srv/web echo '/dev/mapper/vgdata-lvweb /srv/web xfs defaults 0 0' | sudo tee -a /etc/fstab sudo systemctl daemon-reload sudo mount -a findmnt /srv/web df -h /srv/web - Challenge
Remove it in the right order
Undo everything: unmount the volume and remove its
fstabline, then remove the logical volumes, the volume group and the physical volume, in that order. Explain what error you get if you try to remove the volume group first.Check your result
lvs,vgsandpvsno longer listlvweb,lvlogs,vgdataor/dev/vdb2.vgremove vgdatawith LVs present asks for confirmation to remove them, or refuses without-f.Hint
Reverse order of creation:
umount, editfstab,lvremove,vgremove,pvremove.Solution
sudo umount /srv/web sudo sed -i '\#/srv/web#d' /etc/fstab sudo systemctl daemon-reload sudo lvremove -y /dev/vgdata/lvweb /dev/vgdata/lvlogs sudo vgremove -y vgdata sudo pvremove -y /dev/vdb2