Introduction to LVM
Logical Volume Manager (LVM) is a storage management solution that provides flexibility in managing disk storage. It allows dynamic resizing, combining multiple disks, and creating snapshots, among other features.
LVM Components:
- Physical Volume (PV) – A physical disk or partition initialized for LVM.
- Volume Group (VG) – A pool of storage created from one or more PVs.
- Logical Volume (LV) – A partition-like entity created from a VG.
- File System – The file system (ext4, XFS, etc.) created on an LV for data storage.
Setting Up LVM
1. Installing LVM (If Not Already Installed)
sudo apt install lvm2 # Debian/Ubuntu
sudo yum install lvm2 # RHEL/CentOS
sudo dnf install lvm2 # Fedora
2. Creating Physical Volumes (PVs)
Convert a disk or partition into an LVM Physical Volume.
sudo pvcreate /dev/sdb /dev/sdc
Check PV status:
sudo pvs
3. Creating a Volume Group (VG)
Create a Volume Group named vg_data
from two disks:
sudo vgcreate vg_data /dev/sdb /dev/sdc
Verify VG status:
sudo vgs
4. Creating a Logical Volume (LV)
Create a Logical Volume of 50GB from vg_data
:
sudo lvcreate -L 50G -n lv_storage vg_data
Check LV status:
sudo lvs
5. Formatting and Mounting the Logical Volume
Format the LV with the ext4 file system:
sudo mkfs.ext4 /dev/vg_data/lv_storage
Create a mount point and mount the LV:
sudo mkdir /mnt/storage
sudo mount /dev/vg_data/lv_storage /mnt/storage
To mount it automatically at boot, add it to /etc/fstab
:
echo "/dev/vg_data/lv_storage /mnt/storage ext4 defaults 0 2" | sudo tee -a /etc/fstab
Managing LVM
6. Expanding a Logical Volume (LV) When Running Out of Space
If lv_storage
is full, increase its size:
sudo lvextend -L +20G /dev/vg_data/lv_storage # Add 20GB
sudo resize2fs /dev/vg_data/lv_storage # Resize ext4 filesystem
For XFS file systems:
sudo xfs_growfs /mnt/storage
7. Adding a New Disk to Expand the Volume Group
If you need more space, add a new disk (/dev/sdd
) to vg_data
:
sudo pvcreate /dev/sdd
sudo vgextend vg_data /dev/sdd
Check available space:
sudo vgs
8. Reducing the Size of a Logical Volume
Unmount the LV first:
sudo umount /mnt/storage
Resize the filesystem (for ext4):
sudo e2fsck -f /dev/vg_data/lv_storage
sudo resize2fs /dev/vg_data/lv_storage 30G
Reduce LV size:
sudo lvreduce -L 30G /dev/vg_data/lv_storage
Remount the LV:
sudo mount /dev/vg_data/lv_storage /mnt/storage
9. Removing a Logical Volume
Unmount the LV:
sudo umount /mnt/storage
Remove LV:
sudo lvremove /dev/vg_data/lv_storage
10. Removing a Physical Volume from a Volume Group
To remove /dev/sdd
from vg_data
, first, move data:
sudo pvmove /dev/sdd
Then, remove it:
sudo vgreduce vg_data /dev/sdd
sudo pvremove /dev/sdd
11. Splitting a Volume Group
If you need to split vg_data
into two groups:
sudo vgsplit vg_data vg_backup /dev/sdd
12. Merging Two Volume Groups
Merge vg_backup
into vg_data
:
sudo vgmerge vg_data vg_backup
Handling Out-of-Space Situations
Scenario 1: Expanding an LV Using Free VG Space
sudo lvextend -L +10G /dev/vg_data/lv_storage
sudo resize2fs /dev/vg_data/lv_storage
Scenario 2: Adding a New Disk to VG and Expanding LV
sudo pvcreate /dev/sde
sudo vgextend vg_data /dev/sde
sudo lvextend -L +20G /dev/vg_data/lv_storage
sudo resize2fs /dev/vg_data/lv_storage
Scenario 3: Moving Data and Reconfiguring LVM
If the VG is full and you need to migrate data:
sudo pvmove /dev/sdb /dev/sdf # Move data to a new disk
sudo vgreduce vg_data /dev/sdb # Remove old disk
Conclusion
LVM provides flexible storage management, allowing resizing, adding/removing disks, and efficiently handling disk space. Regular monitoring using lvdisplay
, vgdisplay
, and pvs
helps in managing storage effectively.
Recommended Practices:
- Always backup data before resizing or removing LVs.
- Use
pvmove
before removing a disk from a VG. - Monitor disk usage with
df -h
andlvs
.
By mastering LVM, you gain powerful control over disk storage without downtime.