Useful LVM Commands with Examples

TECH-NOTESTECH-NOTES
5 min read

In this article, we will explore a selection of essential LVM commands commonly encountered by Linux administrators and engineers. These commands play a pivotal role in effectively managing storage resources on Linux systems.

What is LVM?


Logical Volume Management (LVM) is a powerful tool for managing disk space on Linux systems. Whether you’re a seasoned Linux administrator or just getting started, mastering LVM commands can significantly enhance your ability to manage storage efficiently. In this guide, we’ll explore 20 essential LVM commands along with examples to help you streamline disk management tasks on your Linux system.

When we attached the new disk to our virtual machine. We can’t use disks directly from our file system because this is a block disk. We need to create volume first. In this step we are using LVM because LVM offers a versatile and scalable approach to disk management, allowing administrators to efficiently utilize and manage storage resources in complex computing environments.

If we have two disks, we attach our Linux system. We can check using the following command:

lsblk

When we check, we can see our block storage sdb, sdc or something like that.

The pvcreate command is a fundamental step in setting up Logical Volume Management (LVM) on Linux systems. It initializes physical volumes, marking them as LVM candidates. This prepares them for use in creating logical volumes within volume groups. For example, to initialize a physical volume /dev/sdb, you would execute:

  • pvcreate
pvcreate /dev/sdb

After creating physical volume, we need to create volume group using this command. A volume group can create one or more physical volumes, not only /dev/sdb but also another physical volume you have. Volume group means the group of physical volumes. We can use multiple logical volumes from the one volume group.

Example: We have two physical volumes (10 GB per volume) with /dev/sda and /dev/sdb. So, we can create two physical volumes using this command:

  • vgcreate
pvcreate /dev/sda    #10GB
pvcreate /dev/sdb    #10GB
vgcreate vg_centos /dev/sda
vgcreate vg_centos /dev/sdb   

## volume group vg_centos is total 20 GB

After creating the volume group, we need to create logical volume. When we create logical volume, we can specify the size and name of the logical volume. In this example, we will create two logical volumes (10 GB per volume) from vg_centos volume group.

  • lvcreate
lvcreate -L 10GB -n lv01 vg_centos
lvcreate -L 10GB -n lv02 vg_centos

After creating these, we can check how many pv, vg and lv are existing in our system using the following commands:

pvs    ## how many physical are exist in our system
vgs    ## how many volume group are exist in our system
lvs    ## how many logical volume are exist in our system

If you want to check specific resource details pv, vg and lv, you can use the following commands:

  • pvdisplay

  • vgdisplay

  • lvdisplay

pvdisplay /dev/sdb    ## You can see the details of physical volume /dev/sdb
vgdisplay vg_centos   ## You can see the details of volume group vg_centos
lvdisplay /dev/vg_centos/lv01    ## You can see the details of logical volume lv01

Our data size is growing day by day; at this time, we need to extend our logical volume. Before we extend our logical volume, we need to check volume group and physical volume first with the above commands. If our volume groups have the free space, we just need to extend lv. If not, we need to extend pv and vg first using the following commands:

Example:

NameStatusTotal sizeExtend
block sdaalready extend15 GB+ 5GB
block sdbalready extend15 GB+ 5 GB
/dev/sdaneed to extend10 GB+ 5 GB
/dev/sdbneed to extend10 GB+ 5 GB
vg_centosdepends on pv extend20 GB+10 GB
lv01need to extend10 GB+ 5 GB
lv02need to extend10 GB+ 5 GB

Above example, we already extend our disk sda and sdb per 5 GB. it means our disk sda and sdb are 15 GB per disk. After extending the block storage, we need to extend our physical volume using this command:

  • pvresize
pvresize /dev/sda
pvresize /dev/sdb

After extending pv, our pv are 15 GB per each. In this case, our vgs automatically recognize the additional space, so we don’t need to run vgextend again. After this, we need to extend lvs using the following commands:

  • lvextend
lvextend -L +5GB -n lv01 vg_centos    ## total 15 GB
lvextend -L +5GB -n lv02 vg_centos    ## total 15 GB

When we added another physical device sdc in our system. We need to extend this device storage to our vg we need to create pv first and then we need to extend vg using this command:

  • vgextend
vgextend /dev/sdc

If you want to remove your pvs, vgs and lvs, you can use the following commands:

  • pvremove

  • vgremove

  • lvremove

pvremove /dev/sda    ## remove pv /dev/sda
vgremove vg_centos   ## remove vg vg_centos
lvremove /dev/vg_centos/lv01    ## remove lv /dev/vg_centos/lv01

If you want to rename your vgs and lvs, you can use the following commands:

  • vgrename

  • lvrename

vgrename vg_centos vg_new
lvrename /dev/vg_centos/lv01 /dev/vg_centos/lv_new

If you want to release your vgs and lvs unused space, you can use the following commands:

  • vgreduce

  • lvreduce

vgreduce vg_centos /dev/sdc ## release /dev/sdc from volume group vg_centos
lvreduce -L 5G /dev/vg_centos/lv01 ## release unused space 5GB back to its volume group vg_centos from lv01

If you want to migrate one pv to another pv, you can use the following command:

  • pvmove
pvmove /dev/sda /dev/sdc ## migrate from /dev/sda to /dev/sdc

That’s all I know of LVM useful commands. I hope you now have a solid foundation to efficiently manage your storage resources. And you can manage your day-by-day storage issue like a Ninja.

Have a great day!..

0
Subscribe to my newsletter

Read articles from TECH-NOTES directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

TECH-NOTES
TECH-NOTES

I'm a cloud-native enthusiast and tech blogger, sharing insights on Kubernetes, AWS, CI/CD, and Linux across my blog and Facebook page. Passionate about modern infrastructure and microservices, I aim to help others understand and leverage cloud-native technologies for scalable, efficient solutions.