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:
Physical Volumes (PV): The actual physical disks or partitions
Volume Groups (VG): Groups of physical volumes
Logical Volumes (LV): Virtual partitions created from volume groups
Essential LVM Commands
Physical Volume Management
- Initialize a disk or partition as a physical volume:
pvcreate /dev/sdb
- Display physical volume information:
pvdisplay
pvs # For a condensed view
- Remove a physical volume:
pvremove /dev/sdb
Volume Group Management
- Create a volume group:
vgcreate vg_name /dev/sdb /dev/sdc
- Display volume group information:
vgdisplay
vgs # For a condensed view
- Extend a volume group:
vgextend vg_name /dev/sdd
- Reduce a volume group:
vgreduce vg_name /dev/sdd
Logical Volume Management
- 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
- Display logical volume information:
lvdisplay
lvs # For a condensed view
- 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
- 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
- Prepare the physical volumes:
pvcreate /dev/sdb /dev/sdc
- Create a volume group:
vgcreate data_vg /dev/sdb /dev/sdc
- Create a logical volume:
lvcreate -L 15G -n data_lv data_vg
- 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
- Add a new disk to the volume group:
pvcreate /dev/sdd
vgextend data_vg /dev/sdd
- Extend the logical volume:
lvextend -L +10G /dev/data_vg/data_lv
- Resize the filesystem:
# For ext4 filesystem
resize2fs /dev/data_vg/data_lv
LVM Snapshots
- Create a snapshot:
lvcreate -L 5G -s -n snap_name /dev/vg_name/lv_name
- Restore from snapshot:
lvconvert --merge /dev/vg_name/snap_name
Troubleshooting Tips
- 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.