LLI Wiki — My Digital Knowledge Base

LVM cheat sheet: physical volumes, volume groups and logical volumes

Common Linux LVM commands: create, extend, reduce, mirrors, snapshots, configuration backup and recovery scenarios.

This is a quick LVM (Logical Volume Manager) command reference for Linux, condensed from this cheat sheet. It lists the common commands per object type (physical volumes, volume groups, logical volumes) and is not exhaustive: check the manual page of each command. For pvs, vgs and lvs, each added -v gives more verbose output.

Files and tools

Item Purpose
/etc/lvm/lvm.conf Main LVM configuration file
/etc/lvm/backup Automatic metadata backups
/etc/lvm/archive Automatic archives, written after a volume group change
/etc/lvm/cache Persistent filter cache
/var/lock/lvm Lock files that prevent metadata corruption
$HOME/.lvm LVM command history
lvmdump, lvmdump -d <dir> Diagnostic dump (a tarball by default)
dmsetup info, dmsetup ls, dmsetup status Device-mapper diagnostics

Physical volumes

Task Command
Display pvdisplay -v, pvs -v, pvs -a, pvs --segments
Scan (LVM and non-LVM disks) pvscan -v
Create pvcreate /dev/sdb1
Create with a given UUID (to recover a volume group) pvcreate --uuid <uuid> /dev/sdb1
Remove pvremove /dev/sdb1
Check metadata consistency pvck -v /dev/sdb1
Forbid new extent allocation on a disk (it must already be in a volume group) pvchange -x n /dev/sdb1
Move used extents to another physical volume (before removing a disk) pvmove -v /dev/sdb2 /dev/sdb3

Useful options: -M2 creates an LVM2 physical volume, --addtag adds a tag, -x allows or forbids extent allocation, -u changes the UUID. The pvs attributes include (a)llocatable and e(x)ported. pvmove does not work on mirrored volumes: first convert them back with lvconvert -m 0.

Volume groups

Task Command
Display vgdisplay -v, vgs -v, vgs -a -o +devices
Scan vgscan -v
Create vgcreate VolData00 /dev/sdb1 /dev/sdb2 /dev/sdb3 or vgcreate VolData00 /dev/sdb[123]
Create with a 32 MB extent size vgcreate VolData00 -s 32 /dev/sdb1
Extend vgextend VolData00 /dev/sdb3
Reduce vgreduce VolData00 /dev/sdb3
Remove a missing disk vgreduce --removemissing --force VolData00
Remove vgremove VolData00 (-f also removes its logical volumes)
Check metadata consistency vgck VolData00
Activate or deactivate vgchange -a y VolData00, vgchange -a n VolData00
Rename (no active logical volume) vgrename VolData00 Data_Vol_01
Convert the metadata format (for example LVM1 to LVM2) vgconvert -M2 VolData00
Merge (Old into New) vgmerge New_Vol_Group Old_Vol_Group
Split vgsplit Old_Vol_Group New_Vol_Group [physical volumes] [-n logical volume name]
Export or import vgexport VolData00, vgimport VolData00 (-a for all)
Import a cloned and renamed group vgimportclone /dev/sdb1
Recreate the directory and special files in /dev vgmknodes VolData00

Useful creation options: -l maximum number of logical volumes, -p maximum number of physical volumes, -s physical extent size (4 MB by default), -A automatic backup. An exported volume group shows an x as the third attribute in vgs. To merge, unmount the file systems and deactivate the group being merged with vgchange -a n, merge, reactivate it, run vgscan and back up the configuration.

Backup and restore of the configuration

# Backup to the default location (/etc/lvm/backup)
vgcfgbackup VolData00

# Backup to a specific file
vgcfgbackup -f /var/backup/VolData00_bkup VolData00

# Backup of all volume groups (%s is replaced by the group name)
vgcfgbackup -f /var/backup/vg_backups_%s

# Restore
vgcfgrestore -f /var/backup/VolData00_bkup VolData00

Backups are plain text files. vgcfgrestore -l lists the available backups.

Logical volumes

Task Command
Display lvdisplay -v, lvdisplay --maps (mirrors), lvs -v, lvs -a -o +devices
Stripe information lvs -a -o +stripes,stripesize
Scan lvscan -v, lvmdiskscan
Create (10 MB) lvcreate -L 10M VolData00
Create using extents lvcreate -l 10 VolData00
Create with a name lvcreate -L 10M -n web01 VolData00
Create on a specific disk lvcreate -L 10M VolData00 /dev/sdb1
Create a striped volume lvcreate -i 3 -L 24M -n lvol1 vg01
Create a mirrored volume lvcreate -L 10M -m1 -n data01 vg01
Create a mirror without a mirror log on disk lvcreate -L 10M -m1 --mirrorlog core -n data01 vg01
Extend lvextend -L 20M /dev/VolData00/vol01
Reduce or resize lvreduce -L 5M /dev/VolData00/vol01, lvresize -L 5M /dev/VolData00/vol01
Remove lvremove /dev/VolData00/vol01
Add a mirror to a non-mirrored volume lvconvert -m1 --mirrorlog core /dev/VolData00/vol01 /dev/sdb2
Remove a mirror (the disk given is the one removed) lvconvert -m0 /dev/VolData00/vol01 /dev/sdb2
Mirror a striped volume lvconvert --stripes 3 -m1 --mirrorlog core /dev/VolData00/data01 /dev/sdd1 /dev/sde1 /dev/sdf1
Activate or deactivate lvchange -a y /dev/VolData00/vol01, lvchange -a n /dev/VolData00/vol01
Rename lvrename /dev/VolData00/vol_old /dev/VolData00/vol_new
Snapshot lvcreate --size 100M --snapshot --name snap /dev/vg01/data01

Useful options: -L size (k, m, g, t suffixes), -l number of extents, -C contiguous allocation, -i stripes, -I stripe size, -m mirrors, --mirrorlog, -n name.

Sizes are rounded up to the next extent (4 MB by default) when extending or reducing. Extending a logical volume does not grow the file system: for ext2/ext3/ext4 use resize2fs or fsadm.

fsadm resize /dev/VolData01/data01
resize2fs -p /dev/VolData01/data01 [size]

-p shows a progress bar. Shrink the file system before reducing the logical volume, and back up first.

Recovery scenarios

Simulate a disk failure

dd if=/dev/zero of=/dev/sdb2 count=10

This overwrites the start of the partition: use it only on a test disk.

Repair a failed mirror (no LVM corruption)

# Check the volume, assuming /dev/sdb2 has failed
lvs -a -o +devices

# Remove the failed disk from the volume group (the volume becomes non-mirrored)
vgreduce --removemissing --force VolData00

# Replace the disk, then partition it with type 8e
fdisk /dev/sdb

# Add the new disk to LVM and to the volume group
pvcreate /dev/sdb2
vgextend VolData00 /dev/sdb2

# Mirror the volume again
lvconvert -m1 --mirrorlog core /dev/VolData00/vol02 /dev/sdb2

Corrupt LVM metadata, same drive

# Try to bring the volume group online
vgchange -a y VolData00

# Restore the LVM configuration
vgcfgrestore VolData00

# Bring the volume group online again, then check the file system
vgchange -a y VolData00
e2fsck /dev/VolData00/data01

Corrupt LVM metadata, faulty disk replaced

# Activation fails with UUID conflicts: note the UUID, then deactivate
vgchange -a y VolData00
vgchange -a n VolData00

# Sometimes only one logical volume is affected
lvchange -a y /dev/VolData00/web02
lvchange -a n /dev/VolData00/web02

# Replace the disk and partition it with type 8e
fdisk /dev/sdb

# Recreate the physical volume with the previous UUID
pvcreate --uuid <previous_uuid> /dev/sdb2

# Restore the configuration, then reactivate
vgcfgrestore VolData00
vgchange -a y VolData00
lvchange -a y /dev/VolData00/web02

# Check the file system
e2fsck /dev/VolData00/data01

The previous UUID can be read from the backup files in /etc/lvm/backup or with pvs -v.

Notes

Mastodon LLIMastodon SocialFosstodon