Programster's Blog

Tutorials focusing on Linux, programming, and open-source

LVM Cheatsheet

Related Posts

Table of Contents

  1. Logical Volumes
    1. List Logical Volumes
    2. Create Logical Volume
    3. Create Filesystem on New LVM
    4. Extend Logical Volume
    5. Resize Filesystem
  2. Volume Groups
    1. List Volume Groups
    2. Create Volume Group
    3. Add Physical Volume To Volume Group
    4. Remove Physical Volume To Volume Group
    5. Extend Volume Group
    6. Destroy Volume Group
  3. Physical Volumes
    1. List Physical Volumes
    2. Create Physical Volume
    3. Move Extents Off Physical Volume
    4. Check Volume Extents
    5. Remove From Volume Group
  4. References

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]  

You can leave out the -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] ...  

The ... 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

This can only be done only if there no extents on it. You may need to move them first.

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

Refer here

References

Last updated: 20th June 2023
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch