Today we’ll look at logical volume management tasks. LVM improves the traditional partition to disk approach, adding flexibility (extending and reducing space), snapshot capabilities and redundancy.
First, some terminology is in order:
physical volumes (PV) are the physical devices that are mapped to be used inside LVM. Their storace unit is called a physical extent (PE)
volume groups (VG) are storage pools made from PVs
logical volumes (LV) are made up of the VG space
Creating PVs
I am now going to create 2 PVs of 100 MB each. You first need to create 2 LVM partitions. If you look inside fdisk, you see the partition code for LVM is 8e:
1
8e Linux LVM
So before writing the partition in fdisk, use t to change it to Linux LVM. Then verify that the new partitions were created:
1234
lsblk | grep sdb
sdb 8:16 0 1G 0 disk
├─sdb1 8:17 0 100M 0 part
└─sdb2 8:18 0 100M 0 part
pvdisplay /dev/sdb1
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg-zero
PV Size 100.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 24
Free PE 24
Allocated PE 0
PV UUID ViTFfu-jBZE-47Ie-d6qB-WTDt-fpNf-bPC00Y
vgdisplay vg-zero
--- Volume group ---
VG Name vg-zero
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 192.00 MiB
PE Size 4.00 MiB
Total PE 48
Alloc PE / Size 0 / 0
Free PE / Size 48 / 192.00 MiB
VG UUID gUf1XI-znJt-MM2T-V721-PNbe-uxcr-m3bVTe
Creating LVs
Next, I created a LV named inventory with a size of 128 MB, from the vg-zero VG:
lvdisplay /dev/vg-zero/inventory
--- Logical volume ---
LV Path /dev/vg-zero/inventory
LV Name inventory
VG Name vg-zero
LV UUID jE4XRJ-ddlX-nM8s-NVSr-4cfK-F9O0-JcKecs
LV Write Access read/write
LV Creation host, time rhel7, 2018-01-26 15:36:04 +0200
LV Status available
# open 0
LV Size 128.00 MiB
Current LE 32
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
Before using the new LV, you need to create a filesystem on top of it. I went with XFS:
the first field represents the device, which can be specified by name, UUID, or label
the second field is the mount point of the device
the third is the filesystem type
fourth is for the mount options. The defaults option specifies the usage of default options: rw, suid, dev, exec, auto, nouser, async
the fifth is a backup flag for the dump utility. Use 1 to enable it and 0 to disable it
sixth represents the fsck automatic check at boot. Use 0 to disable it, 1 for the root filesystem, and 2 for other filesystems that might need the automatic checking
It’s a good practice to mount the devices you added to fstab before a reboot to ensure there are no errors: mount -a
Extending / reducing VGs
To add more space to a VG, you can add more PVs to it. Let’s see its current space:
lvremove /dev/vg-zero/inventory
Do you really want to remove active logical volume vg-zero/inventory? [y/n]: y
Logical volume "inventory" successfully removed
Remove the VG:
12
vgremove vg-zero
Volume group "vg-zero" successfully removed
Finally, also remove the PVs:
123
pvremove /dev/sdb1 /dev/sdb2
Labels on physical volume "/dev/sdb1" successfully wiped.
Labels on physical volume "/dev/sdb2" successfully wiped.
And don’t forget to delete any remaining fstab entries.