Managing Logical Volumes

June 16, 2025 by Roberto Puzzanghera 0 comments

Create the partions with (c)fdisk as usual and toggle the partion that you want to dedicate to the LVM with the 43 flag. In the first disk (/dev/nvme0n1 for me) I have

Device            Start       End   Sectors   Size Type 
/dev/nvme0n1p1     2048    196607    194560    95M EFI System 
/dev/nvme0n1p2 16779264  37750783  20971520    10G Linux filesystem 
/dev/nvme0n1p3 37750784 879097855 841347072 401.2G Linux LVM 
/dev/nvme0n1p4   196608  16779263  16582656   7.9G Linux swap

p4 is the swap partition, p2 is the root / partition, while p3 is ready for the volume where I'll have to put my LXC virtual machines.

The secondi disk will be entirily dedicated to the LVM:

Device         Start       End   Sectors   Size Type 
/dev/nvme1n1p1  2048 879097855 879095808 419.2G Linux LVM

Create the volume with the /dev/nvme0n1p3 and /dev/nvme1n1p1 partions:

# pvcreate /dev/nvme0n1p3 /dev/nvme1n1p1 
 Physical volume "/dev/nvme0n1p3" successfully created. 
 Physical volume "/dev/nvme1n1p1" successfully created.

Create a virtual group named my_vg (it will hold the /usr/local mount)

# vgcreate my_vg /dev/nvme0n1p3 /dev/nvme1n1p1 
 Volume group "my_vg" successfully created

Create the Logical Volume usrlocal and allocate all the available space:

lvcreate -l 100%FREE -n usrlocal my_vg

Create the filesystem

mkfs.ext4 /dev/my_vg/usrlocal

Mount the volume in /usr/local

mount /dev/my_vg/usrlocal /usr/local

Main LVM commands

Physical Volumes (PV)

Command Description Example
pvcreate /dev/sdx Initialize a physical device for LVM pvcreate /dev/nvme0n1p3
pvs List all physical volumes pvs
pvdisplay /dev/sdx Show detailed info about a PV pvdisplay /dev/nvme1n1p1
pvremove /dev/sdx Remove LVM label from a physical volume pvremove /dev/nvme1n1p1

Volume Groups (VG)

Command Description Example
vgcreate my_vg /dev/sdx ... Create a volume group from PVs vgcreate my_vg /dev/nvme0n1p3 /dev/nvme1n1p1
vgs List volume groups vgs
vgdisplay my_vg Show detailed info about a volume group vgdisplay my_vg
vgextend my_vg /dev/sdx Add a PV to an existing volume group vgextend my_vg /dev/sdb1
vgreduce my_vg /dev/sdx Remove a PV from a volume group (must be empty) vgreduce my_vg /dev/sdb1
vgremove my_vg Remove an empty volume group vgremove my_vg

Logical Volumes (LV)

Command Description Example
lvcreate -L size -n name my_vg Create an LV with fixed size lvcreate -L 100G -n data my_vg
lvcreate -l 100%FREE -n name my_vg Create an LV using all free space lvcreate -l 100%FREE -n usrlocal my_vg
lvs List logical volumes lvs
lvdisplay /dev/my_vg/name Show detailed info about an LV lvdisplay /dev/my_vg/usrlocal
lvremove /dev/my_vg/name Remove (delete) an LV lvremove /dev/my_vg/usrlocal
lvextend -L +size /dev/my_vg/name Increase LV size lvextend -L +50G /dev/my_vg/usrlocal
lvreduce -L -size /dev/my_vg/name Reduce LV size (use with care!) lvreduce -L -10G /dev/my_vg/usrlocal

Other Useful Commands

Command Description Example
lvchange -ay /dev/my_vg/name Activate an LV lvchange -ay /dev/my_vg/usrlocal
lvchange -an /dev/my_vg/name Deactivate an LV lvchange -an /dev/my_vg/usrlocal
pvmove /dev/sdx Move data from one PV to others in VG pvmove /dev/nvme1n1p1
vgscan Scan all disks for VG headers vgscan
vgchange -ay my_vg Activate all LVs in a VG vgchange -ay my_vg
vgchange -an my_vg Deactivate all LVs in a VG vgchange -an my_vg

Add a comment

Recent comments
Recent posts

RSS feeds