August 24 2023

What is LVM and how does it work?

Exploring LVM: How dynamic disk space management has revolutionized storage interaction and efficiency on Linux systems.

The Linux world is full of tools and technologies that allow system administrators to manage hardware resources efficiently and flexibly. One such tool is LVM, or Logical Volume Manager. In this article, we'll explore what LVM is, how it works, and how it can be used to manage storage on Linux systems.

1. Introduction to LVM

LVM, short for Logical Volume Manager, represents a revolution in the way system administrators and end users interact and manage storage space on a Linux system. At the heart of its philosophy is the idea of ​​treating physical disks not as rigid and static entities, but rather as flexible resources within a unified pool, known as a "Volume Group". This pool concept allows users to dynamically allocate, resize, and free up disk space, creating what are called "logical volumes."

 

Unlike traditional partition management, where each partition has a size and physical location on the disk, LVM abstracts this concept. In practice, this means that, with LVM, you are not bound by the physical limitations of the disk. For example, if you need more space for a particular application or service, you can resize a logical volume on the fly, without needing to unmount the file system or stop the service. This flexibility also extends to the ability to move data between different disks transparently, without interruption or downtime.

Additionally, LVM offers a more granular view of storage, allowing for more efficient management of disk space. This results in greater operational efficiency, reducing the risk of wasted space while ensuring resources are available when and where they are needed.

2. Main components of LVM

LVM is based on three main components:

  • Physical Volumes (PV): These represent the physical disks or partitions that are used as storage for LVM.
  • Volume Groups (VG): A VG is a set of PVs. You can think of a VG as a large virtual disk made up of one or more physical disks.
  • Logical Volumes (LV): These are the virtual "discs" that are created inside a VG. LVs can be used as if they were traditional partitions.

3. Why use LVM?

There are many reasons a system administrator might want to use LVM:

  • Flexibility: With LVM, you can resize logical volumes on the fly, without the need to unmount the filesystem or stop the service.
  • Snapshot: LVM allows you to create snapshots of volumes, which are useful for backups or for testing changes without impacting the original volume.
  • Data migration: With LVM, you can move data from one physical disk to another without interruption.
  • Simplified management: LVM offers centralized storage management, making it easier to manage large amounts of disk space.

4. How LVM works

Now that we understand the basic concepts of LVM, let's see how it works in practice.

to. Initialization of Physical Volumes

Before you can use a disk or partition with LVM, it must be initialized as a PV. This can be done with the command pvcreate.

pvcreate /dev/sdb1

b. Creating a Volume Group

Once you have one or more PVs, you can create a VG on them. For example, to create a VG called "myvg" with a PV, you could use:

vgcreate myvg /dev/sdb1

c. Creation of Logical Volumes

With a VG ready, you can start making LVs. For example, to create a 10GB LV called "mylv" inside the "myvg" VG, you could use:

lvcreate -L 10G -n mylv myvg

d. Formatting and editing

Once the LV is created, it can be formatted with a filesystem such as ext4 and mounted like any other partition:

mkfs.ext4 /dev/myvg/mylv mount /dev/myvg/mylv /mnt/mydata

5. Advanced operations with LVM

In addition to basic operations, LVM offers a number of advanced features.

to. Resizing of Logical Volumes

LVM allows you to resize LVs on the fly. For example, to extend an LV by 5GB, you could use:

lvextend -L +5G /dev/myvg/mylv resize2fs /dev/myvg/mylv

b. Creating snapshots

Snapshots are useful for creating a "snapshot" copy of an LV:

lvcreate -L 5G -s -n mylv_snapshot /dev/myvg/mylv

c. Data migration between disks

If you want to move data from one PV to another, you can use pvmove:

pvmove /dev/sdb1 /dev/sdc1

6. LVM Commands Cheatsheet for Linux

Here is a cheatsheet of LVM (Logical Volume Manager) commands. This guide has been designed to give you a quick and clear overview of the main LVM commands, providing not only a brief description of each command's function, but also syntax examples to make them easier to use. Whether you're new to LVM or just looking for a quick reference, this cheatsheet can help you better understand the desired command and its related syntax. Use it as a starting point or as a quick reference tool during your LVM work sessions.

Commands for Physical Volume (PV)

 

Command Function Example syntax
pvscan Scan all physical devices used as PV pvscan
pvccreate Initialize partitions for PV creation pvcreate /dev/sdx1
pvdisplay Show attributes provided by PV pvdisplay /dev/sdx1
pvchange Modify the attribute gained from HP pvchange /dev/sdx1
pv About PV pvdisplay /dev/sdx1
pvck Check PV metadata pvck /dev/sdx1
pvremove Remove VP pvremove /dev/sdx1

Commands for Volume Group (VG)

Command Function Example syntax
vgscan Scan each PV for VG vgscan
vgcreate Create a Volume Group vgcreate myvg /dev/sdx1
vgdisplay Show the attributes available in VG vgdisplay myvg
vgchange Modify the attributes of VG vgchange myvg
vgs About VG vgs
vgck Check the VG metadata vgck myvg
vgrename Rename VG vgrename oldvgname newvgname
vgreduce Remove PV from VG to reduce its size vgreduce myvg /dev/sdx1
vgextend Add PV to Volume Group vgextend myvg /dev/sdx2
vgmerge Merge two Volume Groups vgmerge myvg1 myvg2
vgsplit Separate two Groups of Volumes vgsplit myvg1 myvg2
vgremove Remove a Volume Group vgremove myvg

Commands for Logical Volume (LV)

 

Command Function Example syntax
lvscan Scan all logical volumes for LV lvscan
lvcreate Create a LV in the Volume Group lvcreate -L 10G -n mylv myvg
lvdisplay Show the attributes of LV lvdisplay /dev/myvg/mylv
lvchange Modify the attributes of LV lvchange /dev/myvg/mylv
lv Show the attributes of LV lvdisplay /dev/myvg/mylv
lvrename Rename LV lvrename /dev/myvg/oldlvname /dev/myvg/newlvname
lvreduce Reduce the size of LV lvreduce -L -5G /dev/myvg/mylv
liveextend Increase LV size lvextend -L +5G /dev/myvg/mylv
lvresize Change the size of LV lvresize -L 15G /dev/myvg/mylv
lv remove Remove LV lvremove /dev/myvg/mylv

7. Conclusion

LVM is a powerful and flexible tool that every Linux system administrator should know. It offers simplified storage management, with the ability to resize, move, and snapshot volumes on the fly. If you're looking for a way to manage storage more efficiently and flexibly, LVM might be the solution you're looking for.

This article has provided a high-level overview of LVM and its capabilities. There are many more features and details that can be explored; therefore, I encourage you to experiment and delve further into this topic.

Here are some reference websites for LVM, offering detailed documentation, guides and tutorials:

  1. Official LVM Documentation
  2. The Linux Documentation Project (LDP)
    • URL: https://tldp.org/HOWTO/LVM-HOWTO/ LDP is one of the oldest and most respected resources for Linux documentation. Their LVM guide is detailed and covers many of the features of LVM.
  3. Arch Linux Wiki
    • URL: https://wiki.archlinux.org/title/LVM While specific to Arch Linux, this wiki is known for its detailed and technical guides. The LVM page is no exception and offers a comprehensive overview of how to use LVM on Arch Linux.
  4. LinuxConfig.org
    • URL: https://linuxconfig.org/lvm-tutorial This site offers a variety of guides and tutorials on various Linux topics, including LVM. The LVM guide is detailed and covers both basic and advanced operations.

These sites are great starting points for anyone who wants to learn more about LVM. I recommend that you explore them and use the information provided to experiment and learn further.

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION

Managed Server Srl is a leading Italian player in providing advanced GNU/Linux system solutions oriented towards high performance. With a low-cost and predictable subscription model, we ensure that our customers have access to advanced technologies in hosting, dedicated servers and cloud services. In addition to this, we offer systems consultancy on Linux systems and specialized maintenance in DBMS, IT Security, Cloud and much more. We stand out for our expertise in hosting leading Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Joomla, OpenCart and Magento, supported by a high-level support and consultancy service suitable for Public Administration, SMEs and any size.

Red Hat, Inc. owns the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. owns the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt. Oracle Corporation owns the rights to Oracle®, MySQL®, and MyRocks®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; REDIS® is a registered trademark of Redis Labs Ltd. F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB. Adobe Inc. holds the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited. Automattic Inc. owns the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert holds the rights to Drupal®. Amazon Web Services, Inc. holds the rights to AWS®; Google LLC holds the rights to Google Cloud™ and Chrome™; Microsoft Corporation holds the rights to Microsoft®, Azure®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®. Apache® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group. CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV Hetzner Online GmbH owns the rights to Hetzner®; OVHcloud is a registered trademark of OVH Groupe SAS; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Facebook, Inc. owns the rights to Facebook®. This site is not affiliated, sponsored or otherwise associated with any of the entities mentioned above and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. Any other trademarks mentioned belong to their registrants. MANAGED SERVER® is a trademark registered at European level by MANAGED SERVER SRL, Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

Back to top