Linux: logical volume management

July 13, 2022

Lang: cs en de es

The Linux OS is a powerful tool. You can manage partitions on the fly, even enlarge, add and move them between physical disks. All on the fly and without downtime. Logical Volume Management technology is designed to do all this and more. I'll describe what LVM can do and how to use this technology in this article.

In a previous article I described how to Linux software RAID. LVM can do RAID too, plus it can do a lot of other things and is suitable for servers, where storage space requirements may change and you may need to make changes to disks while the system is running without service interruption.

For example, you will encounter LVM if you are using proxmox. And a lot of automatic installers will use LVM for disk management by default. So it may be useful to at least know LVM so you won't be surprised when you come across this technology.

What is LVM

Features

  • change (increase) the size of the block device (disk) at runtime
  • moving a block device to another physical disk
  • RAID1
  • merging multiple disks into one (equivalent to RAID0)
  • creating block devices for Virtual Private Servers (VPS)
  • snapshots
  • atd

Disadvantages?

With options come complexities, making LVM more difficult to understand and navigate. The same is true with LVM, so you need to study and try the technology.

There is more work involved with LVM and unless you are planning major partition changes and disk purchases, it is unnecessary. Where it raises the possibility of needing to increase disk storage with minimal downtime, LVM is the ideal solution, along with a filesystem that can be expanded on the fly.

Installation

In a Linux distribution, Debian, the following command will install LVM support:

apt install lvm2

LVM usage

Some basic commands:

  • pvs view Physical Volumes
  • vgs view Volume Groups
  • lvs display Logical Volumes

Creating LVM

You can create an LVM on a disk or better a partition as follows:

  1. Creating Physical Volumes:
    pvcreate /dev/sdc1
  2. Create Volume Groups:
    vgcreate home_vg /dev/sdc1
  3. Creating Logical Volumes:
    lvcreate -l 100%FREE -n home home_vg

Format the block device and modify filesystem parameters:

mkfs.ext4 -L server-backup /dev/mapper/home_vg-home
tune2fs -c 0 -i 0 /dev/mapper/home_vg-home

Changes to LVM

Enlarge the partition so that you use all the available capacity:

pvcreate /dev/sde1
vgextend home_vg /dev/sde1
lvextend /dev/home_vg/home /dev/sde1
#OR:
lvextend -l +100%FREE /dev/home_vg/home
resize2fs /dev/home_vg/home

The following command branches the logical volume /dev/myvg/homevol to 12GB
lvextend -L12G /dev/myvg/homevol
The following command adds 1GB to the existing logical volume size /dev/myvg/homevol.
lvextend -L+1G /dev/myvg/homevol

Enlarge the XFS filesystem:

xfs_growfs /

Enlarge partition #1 on the xvda disk to use empty space. Typically if in VMWare or Proxmox you enlarge the disk that the VPS is using. (Not directly related to LVM) You need to enlarge a given partition and then the EXT filesystem also needs to be enlarged.

growpart /dev/xvda 1
resize2fs /dev/xvda1
The growpart program is included in the cloud-utils package. If growpart reports this error:
unexpected output in sfdisk --version [sfdisk from util-linux 2.36.1]
It could also be that you are not using the English language on your system, and so the command in question does not understand the sfdisk program it is using. The solution is to set your environment variable to English with the following command:
export LANG=en_US.UTF-8

Deleting the LVM metadata on a given partition will also delete the RAID information or other signature of how the partition is used.

wipefs -a /dev/sdb3

Video tutorial

For a detailed and visual demonstration of how to use LVM, see this video tutorial on how to start using LVM:

More about LVM

Debian.org LVM
LVM
LVM howto

Články na podobné téma

Backup: the Proxmox Backup Server
Linux as a router and firewall
How to upload a docker image to the Docker Registry
Linux Software RAID
Running a web application behind a proxy
Mailbox migration
Docker multistage build
Podman
Importing Windows into Proxmox virtualization
Docker and PHP mail
Proxmox virtualization
Docker and Cron
How to address poor file share performance in Docker
How to get started using Docker correctly
Raspberry PI
GNU/Linux operating system

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+