Linux

LVM

Logical Volume Manager for flexible disk management.

#linux #disk #storage #lvm

View Current Configuration

Short Form

Quick overview of physical volumes.

pvs

Quick overview of volume groups.

vgs

Quick overview of logical volumes.

lvs

Detailed Output

Detailed information about physical volumes.

pvdisplay

Detailed information about volume groups.

vgdisplay

Detailed information about logical volumes.

lvdisplay

Physical Volumes

Create Physical Volume

Initialize a disk or partition for use with LVM.

pvcreate [device]

Remove Physical Volume

Remove a physical volume from LVM.

pvremove [device]

Volume Groups

Create Volume Group

Create a volume group with a single device.

vgcreate [vg_name] [device]

Create a volume group spanning multiple devices.

vgcreate data /dev/sdb /dev/sdc

Extend Volume Group

Add a physical volume to an existing volume group.

vgextend [vg_name] [device]

Reduce Volume Group

Remove a physical volume from a volume group.

vgreduce [vg_name] [device]

Rename Volume Group

vgrename vg0 [vg_name]

⚠️ Warning: After renaming, update /etc/fstab and regenerate GRUB configuration.


Logical Volumes

Create Logical Volume

Create a logical volume with specific size.

lvcreate -L 16G -n [lv_name] [vg_name]

Alternative syntax using --size.

lvcreate --size 1G -n swap [vg_name]

Create a logical volume using all free space.

lvcreate -l 100%FREE -n [lv_name] [vg_name]

Extend Logical Volume

Extend by specific amount.

lvextend -L +10G [vg_name]/[lv_name]

Extend to use all free space.

lvextend -l +100%FREE [vg_name]/[lv_name]

Note: After extending, resize the filesystem:

  • For ext4: resize2fs /dev/[vg_name]/[lv_name]
  • For xfs: xfs_growfs /mount/point

Reduce Logical Volume

⚠️ Warning: Always backup data before reducing! For ext4, shrink filesystem first.

e2fsck -f /dev/[vg_name]/[lv_name]
resize2fs /dev/[vg_name]/[lv_name] 10G
lvreduce -L 10G [vg_name]/[lv_name]

Remove Logical Volume

lvremove [vg_name]/[lv_name]

Rename Logical Volume

lvrename /dev/[vg_name]/lv00 [lv_name]

⚠️ Warning: After renaming, update /etc/fstab and regenerate GRUB configuration.


Advanced Features

Snapshot

Create a snapshot of a logical volume for backup purposes.

lvcreate --snapshot -n snapshot_20240109 [vg_name]/[lv_name]

Specify snapshot size.

lvcreate --snapshot -L 5G -n snapshot_backup [vg_name]/[lv_name]

Merge Snapshot

Revert logical volume to snapshot state.

lvconvert --merge [vg_name]/snapshot_name

Thin Provisioning

Create a thin pool logical volume.

lvcreate -i 1 -L 50M -T [vg_name]/thinpool_lv

Create a thin logical volume from the pool.

lvcreate -V 10G -T [vg_name]/thinpool_lv -n thin_lv

💡 Tip: Thin provisioning allows overcommitting storage - allocate more space to VMs/containers than physically available.


Mirrored Logical Volume

Create a mirrored volume for redundancy.

lvcreate --mirrors 1 -n mirror_lv -L 100M [vg_name]

Note: Requires at least 2 physical volumes in the volume group.


RAID Logical Volume

Create a RAID5 logical volume across 3 devices.

lvcreate --type raid5 -i 3 -L 100M -n raid_lv [vg_name]

Create a RAID1 (mirror) logical volume.

lvcreate --type raid1 -m 1 -L 100M -n raid1_lv [vg_name]

Check RAID status.

lvs -a -o +devices,segtype,sync_percent

Cache Logical Volume

Create a cache volume on fast storage (SSD/NVMe).

lvcreate -L 100M -n cache [vg_name] /dev/nvme0n1p3

Create cache metadata volume.

lvcreate -L 10M -n cache_meta [vg_name] /dev/nvme0n1p3

Combine cache and metadata, then attach to slow volume.

lvconvert --type cache-pool --poolmetadata [vg_name]/cache_meta [vg_name]/cache
lvconvert --type cache --cachepool [vg_name]/cache [vg_name]/[lv_name]

💡 Tip: Cache volumes improve performance of large, slow volumes by storing frequently accessed blocks on fast storage.


Post-Rename Configuration

After renaming volume groups or logical volumes, follow these steps:

1. Update /etc/fstab

Edit /etc/fstab to reflect new VG/LV names.

nano /etc/fstab

Change old paths like /dev/mapper/oldvg-oldlv to /dev/mapper/[vg_name]-[lv_name].


2. Regenerate GRUB Configuration

Mount the system and regenerate GRUB config.

mount /dev/mapper/[vg_name]-[lv_name] /mnt
mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
mount --bind /run /mnt/run
mount /dev/sda1 /mnt/boot

Chroot and regenerate GRUB.

chroot /mnt
grub-mkconfig --output=/boot/grub/grub.cfg
exit

Reboot the system.

reboot

Common Operations

Check LVM Version

lvm version

Scan for LVM Volumes

Scan for physical volumes.

pvscan

Scan for volume groups.

vgscan

Scan for logical volumes.

lvscan

Activate/Deactivate Volumes

Activate all logical volumes.

vgchange -ay

Deactivate a specific volume group.

vgchange -an [vg_name]

Activate a specific logical volume.

lvchange -ay [vg_name]/[lv_name]

Troubleshooting

Check Volume Group Space

vgs -o +vg_free

Check Physical Volume Usage

pvs -o +pv_used,pv_free

View LVM Activity

lvs -a -o +lv_layout,lv_role

Fix Inconsistent Metadata

vgck [vg_name]

Backup LVM Metadata

vgcfgbackup [vg_name]

Restore from backup.

vgcfgrestore [vg_name]

Best Practices

💡 Tips:

  • Always leave some free space in volume groups for snapshots
  • Use thin provisioning for efficient space utilization
  • Create snapshots before major system changes
  • Regularly monitor LVM space with vgs and lvs
  • Keep LVM metadata backups in a safe location
  • Use descriptive names for volume groups and logical volumes
  • Document your LVM layout and dependencies