Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Examendoelen in dit hoofdstuk
- Fysieke volumes maken en verwijderen
- Fysieke volumes aan volumegroepen toevoegen
- Logische volumes maken en verwijderen
- Bestaande logische volumes groter maken
Understand the LVM layers
LVM has three layers: disks become physical volumes (PV), PVs are joined into a volume group (VG), and from the VG you cut logical volumes (LV). The file system goes on the LV.
file systems: /data (xfs) /web (ext4)
logical vols: lvdata 5G lvweb 3G
└────────── vgdata (VG, 12G) ──────────┘
physical vols: /dev/vdb1 (PV) /dev/vdc (PV)| Term | Meaning |
|---|---|
| PV | A disk or partition prepared for LVM |
| VG | The storage pool made of one or more PVs |
| LV | A "partition" cut from the VG; it gets a file system |
| PE / extent | The unit of space in a VG (default 4 MiB) |
Know what LVM can do
The main reasons to use LVM.
- Resize: grow (and for ext4 also shrink) volumes, add disks to a VG while it runs.
- Join disks: one LV can use space from many disks.
- Snapshots: a frozen view of an LV for backups.
- Move data:
pvmovemoves data off a disk while it is in use, so you can replace the disk. - Thin provisioning, RAID and cache LVs (more advanced).
Notitie
RHEL installs the root and swap on LVM by default (VG rhel).
Create physical volumes
A PV can be a whole disk or a partition. With partitions, set the type to lvm first.
- (Partition way) Create a partition with type lvm.
fdisk /dev/vdb Command (m for help): n (+5G) Command (m for help): t (lvm) Command (m for help): w - Turn the partition (or a whole disk) into a PV.
- Check the PVs.
Create a volume group
Join one or more PVs into a VG. You can choose the extent size with -s (default 4 MiB).
vgcreate vgdata /dev/vdb1 /dev/vdc
Volume group "vgdata" successfully created
vgs
VG #PV #LV #SN Attr VSize VFree
vgdata 2 0 0 wz--n- <14.99g <14.99g
vgdisplay vgdata
vgcreate -s 8M vgbig /dev/vddExamentip
A task may ask for an extent size ("extents of 8 MiB") and an LV size in extents ("20 extents"): use vgcreate -s 8M and lvcreate -l 20. Check with vgdisplay (PE Size).
Create logical volumes and file systems
lvcreate cuts an LV from the VG: with -L a size, with -l a number of extents or a percentage.
- Create the LVs.
- Put a file system on them and mount them for good.
| lvcreate size | Means |
|---|---|
-L 5G | 5 GiB |
-L 500M | 500 MiB |
-l 100 | 100 extents |
-l 50%FREE | Half of the free space in the VG |
-l 100%FREE | All free space |
-l 20%VG | 20 % of the whole VG |
Name and find LVM devices
An LV has several device names. All point to the same device-mapper device (/dev/dm-N).
| Name | Example |
|---|---|
/dev/VG/LV | /dev/vgdata/lvdata |
/dev/mapper/VG-LV | /dev/mapper/vgdata-lvdata |
/dev/dm-N | /dev/dm-3 (can change; do not use it) |
Onthoud
In fstab, /dev/VG/LV or /dev/mapper/VG-LV are stable names; you do not need the UUID for LVs.
Add space to a volume group
When the VG is full, add a new PV with vgextend.
Grow a logical volume and its file system
lvextend -r grows the LV and the file system in one step, while it is mounted.
lvextend -r -L +2G /dev/vgdata/lvdata
lvextend -r -l +100%FREE /dev/vgdata/lvweb
lvextend -r -L 10G /dev/vgdata/lvlog
df -h /data| Meaning | |
|---|---|
-L +2G | Add 2 GiB |
-L 10G | Make it 10 GiB in total |
-l +100%FREE | Add all free space of the VG |
-r | Also resize the file system (xfs_growfs or resize2fs) |
- Without
-r, grow the file system yourself.lvextend -L +2G /dev/vgdata/lvdata xfs_growfs /data resize2fs /dev/vgdata/lvweb
Examentip
Forgetting the file system is the classic mistake: the LV is bigger but df -h shows the old size. Always use lvextend -r.
Shrink volumes and reduce a volume group
XFS cannot shrink. ext4 can, but only unmounted — lvreduce -r does the steps for you. To remove a disk from a VG, first move its data away.
- Shrink an ext4 LV (with its file system).
lvreduce -r -L 2G /dev/vgdata/lvweb - Move data off a PV and remove it from the VG.
- Remove an LV and a VG you do not need anymore.
Let op
Shrinking an LV without shrinking the file system first destroys the file system. Always use -r (or run resize2fs first). Remove the fstab line before you delete an LV, or the next boot fails.
Remember the main points
The most important facts of this chapter, in short.
- LVM has three layers: physical volumes (PV) → volume group (VG) → logical volumes (LV).
- Commands:
pvcreate,vgcreate,lvcreate; show withpvs,vgs,lvs(or thedisplayversions). - The VG is split in extents (default 4 MiB);
vgcreate -s 8Msets another extent size;lvcreate -l 100gives 100 extents,-L 500Mgives a size. - LVs appear as
/dev/VG/LVand/dev/mapper/VG-LV. - Make a VG bigger with
vgextend VG /dev/new-pv. - Grow an LV and its file system together:
lvextend -r -L +1G /dev/VG/LV(-rruns xfs_growfs or resize2fs). - Only ext4 can shrink:
lvreduce -r -L 3G …. Move data off a PV withpvmove, thenvgreduce.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| LVM | Logical Volume Manager: flexible storage on top of disks. |
| Physical volume (PV) | A disk or partition prepared for LVM. |
| Volume group (VG) | A pool of space made from one or more PVs. |
| Logical volume (LV) | A part of a VG, used like a partition. |
| Physical extent (PE) | The smallest block of space in a VG. |
| Device mapper | The kernel part that makes LVs appear as devices. |
| Snapshot | A frozen copy of an LV at one moment. |
| Thin provisioning | LVs that can be bigger than the space really used. |
Check what you learned
Think about the answer first, then open it.
1Write the commands to make LV data of 500 MiB in a new VG vgdata on /dev/vdb.
2How do you make a VG with extents of 16 MiB?
3How do you make an LV of exactly 50 extents?
4How do you grow a mounted XFS LV by 1 GiB?
5The VG is full. How do you add the new disk /dev/vdc?
pvcreate /dev/vdc and vgextend VG /dev/vdc (vgextend can also make the PV for you).
6How do you see the free space in a VG?
vgs (column VFree) or vgdisplay VG.
7How do you take a disk out of a VG that holds data?
pvmove /dev/vdb to move the data to other PVs, then vgreduce VG /dev/vdb.
8Which two names can you use in fstab for LV data in VG vgdata?
/dev/vgdata/data or /dev/mapper/vgdata-data (or its UUID).