LVM Cheatsheet
Related Posts
Table of Contents
Logical Volumes
List Logical Volumes
sudo lvdisplay
Create Logical Volume
Create an LVM by adding it to a Volume Group that already exists.
lvcreate -L [Size in GB]G [Volume Group Name]
... or to just use the entire space:
lvcreate -l 100%FREE -n [LVM Name] [Volume Group Name]
-n [LVM Name]
if you wish for the system to set a name automatically.
Create Filesystem on New LVM
This creates an ext4 filesystem on the newly created LVM
sudo mkfs -t ext4 /dev/[volume group]/[Logical volume name]
Extend Logical Volume
By Amount
sudo lvextend -L+[Number of GB to add]G -n /dev/[volume group name]/[logicial volume name]
By Percentage Free
sudo lvextend -l 100%FREE /dev/$VG_NAME/$LV_NAME
To Specified Size
sudo lvextend -L [New Size In GB]G -n /dev/[volume group name]/[logicial volume name]
Resize Filesystem
If you extend your LVM then you need to resize the filesystem in order to utilize it.
resize2fs /dev/[volume group name]/[logical volume name]
Volume Groups
List Volume Groups
sudo vgdisplay
Create Volume Group
Volume groups are collections of physical volumes. To create one, you need to specify which volumes it should group/manage.
vgcreate [new volume group name] /dev/sd[x] /dev/sd[x] ...
...
signifies that you can specify any number of physical volumes. The [x]
signifies different drive letters such as a or a1.
Add Physical Volume To Volume Group
vgextend $VG_NAME /dev/sd[x]
Extend Volume Group
See "Add Physical Volume To Volume Group"
Remove Physical Volume From Volume Group
The following command would remove the /dev/sdb1
disk partition from the volume group.
vgreduce myVolumeGroupName /dev/sdb1
Destroy Volume Group
vgremove [volume group name]
Physical Volumes
List Physical Volumes
sudo pvdisplay
Create Physical Volume
Create a physical volume on a drive. This is required before you can add it to a volume group or create a volume group from it.
pvcreate /dev/sd[x]
Move Extents Off Physical Volume
If you wish to remove a disk/partition from a volume group, it needs to not be being used for storage. If there are enough physical extents available on the other physical volumes that this physical volume belongs to (how to check), then we can simply move them with the following example command:
pvmove myVolumeGroupName /dev/sdb1
Check Volume Extents
If you want to check how utilized your physical volumes are, run the following command:
pvs -o+pv_used
Example output:
PV VG Fmt Attr PSize PFree Used
/dev/sda1 myvg lvm2 a- 17.15G 12.15G 5.00G
/dev/sdb1 myvg lvm2 a- 17.15G 12.15G 5.00G
/dev/sdc1 myvg lvm2 a- 17.15G 12.15G 5.00G
/dev/sdd1 myvg lvm2 a- 17.15G 2.15G 15.00G
Remove From Volume Group
References
- datadisk.co.uk - Redhat LVM Cheatsheet
- Redhat Docs - Displaying Physical Volumes
- Redhat Docs - Removing a Disk from a Logical Volume
First published: 16th August 2018