Understanding LVM in Linux

ยท

3 min read

What is LVM?

LVM(Logical Volume Manager) adds a layer of abstraction between your physical storage devices and the file systems by creating a logical view of storage. This abstraction allows for:

  • Dynamic volume resizing

  • Storage pooling across multiple disks

  • Easy management of storage capacity

  • Snapshot capabilities for backup purposes

LVM Architecture

The LVM structure consists of three main components:

  1. Physical Volumes (PV): The actual physical disks or partitions

  2. Volume Groups (VG): Groups of physical volumes

  3. Logical Volumes (LV): Virtual partitions created from volume groups

Essential LVM Commands

Physical Volume Management

  1. Initialize a disk or partition as a physical volume:
pvcreate /dev/sdb
  1. Display physical volume information:
pvdisplay
pvs  # For a condensed view
  1. Remove a physical volume:
pvremove /dev/sdb

Volume Group Management

  1. Create a volume group:
vgcreate vg_name /dev/sdb /dev/sdc
  1. Display volume group information:
vgdisplay
vgs  # For a condensed view
  1. Extend a volume group:
vgextend vg_name /dev/sdd
  1. Reduce a volume group:
vgreduce vg_name /dev/sdd

Logical Volume Management

  1. Create a logical volume:
lvcreate -L 10G -n lv_name vg_name  # Create with specific size
lvcreate -l 100%FREE -n lv_name vg_name  # Use all available space
  1. Display logical volume information:
lvdisplay
lvs  # For a condensed view
  1. Extend a logical volume:
lvextend -L +5G /dev/vg_name/lv_name  # Add 5GB
lvextend -l +100%FREE /dev/vg_name/lv_name  # Use all free space
  1. Reduce a logical volume (requires unmounting):
umount /mount_point
lvreduce -L -5G /dev/vg_name/lv_name  # Reduce by 5GB

Practical Examples

Setting Up LVM from Scratch

  1. Prepare the physical volumes:
pvcreate /dev/sdb /dev/sdc
  1. Create a volume group:
vgcreate data_vg /dev/sdb /dev/sdc
  1. Create a logical volume:
lvcreate -L 15G -n data_lv data_vg
  1. Format and mount the logical volume:
mkfs.ext4 /dev/data_vg/data_lv
mkdir /mnt/data
mount /dev/data_vg/data_lv /mnt/data

Extending Storage Space

  1. Add a new disk to the volume group:
pvcreate /dev/sdd
vgextend data_vg /dev/sdd
  1. Extend the logical volume:
lvextend -L +10G /dev/data_vg/data_lv
  1. Resize the filesystem:
# For ext4 filesystem
resize2fs /dev/data_vg/data_lv

LVM Snapshots

  1. Create a snapshot:
lvcreate -L 5G -s -n snap_name /dev/vg_name/lv_name
  1. Restore from snapshot:
lvconvert --merge /dev/vg_name/snap_name

Troubleshooting Tips

  1. Missing Physical Volume:
pvscan  # Scan for all physical volumes
vgreduce --removemissing vg_name  # Remove missing PVs from VG

Conclusion

LVM provides flexible storage management capabilities in Linux systems. By understanding these basic commands and concepts, you can effectively manage your storage infrastructure. Remember to always back up your data before making significant changes to your storage configuration.

For more advanced usage, consult the man pages (man lvm) or your distribution's documentation.

ย