Skip to content

Lvm

Tags: linux, lvm


Creating an LVM

You can create a PV directly on a disk, but for consistency you should create a partition then your PV on that.

partred /dev/sdX
mklabel gpt
mkpart
Name? Blank
Type? Default
Start? 0%
End? 100%
quitPV
Terminal window
pvcreate /dev/sdX#
pvdisplay
Terminal window
vgcreate <vg_name> /dev/sdX#
vgdisplay
Terminal window
lvcreate -L 1TiB -n <LV-Name> <VG-Name>
lvdisplay
Terminal window
mkfs.ext4 /dev/volgrp01/lv01

Modifying Volumes

Making a larger partition from new disk space

Enlarge the partition with parted

Terminal window
parted (select disk)
resizepart #
100%

LVM: we need to tell the LV subsystem about the new disk space; we do this by first extending the physical volume which occupies the partition:

Terminal window
pvresize /dev/sdX#

then the logical volume

by percentage

Terminal window
lvresize -r -l 100%PVS /dev/mapper/XXXXXXXX

by GB

Terminal window
lvresize -r -L +4GB /dev/mapper/XXXXXXXXX

If df -h still shows the wrong size then the above command didn’t expand the FS as it should. This could be due to version issues of the tools. Run resize2fs on the mapped volume.


Moving disk space around

File system check the volume to be shrunk, then shrink it by several GB more than you will be reclaiming. It will be enlarged later. Shrink the volume by your desired amount.

Assuming an end goal of 200G

Terminal window
e2fsck -f /dev/foo/roo
resize2fs /dev/foo/roo 180G
lvreduce -L 200G /dev/foo/roo

Enlarge your target voume to take all now free space

Terminal window
lvresize -r -l 100%PVS /dev/foo/boo

Fix the filesystems

Terminal window
resize2fs /dev/foo/roo
resize2fs /dev/foo/boo

Delete volumes

Terminal window
lvremove /dev/rhel/home

Shrinking XFS

You cannot shrink XFS. To get around this backup the data in your XFS volume and delete it. Do your required actions and then remake the XFS volume.