Logical Volume Management (LVM) in Linux allows for flexible disk management. Here’s a step-by-step guide to understand and use LVM:
Step-by-step guide on how to use LVM
Step1: Install LVM Tools
First, ensure that LVM tools are installed. On most Linux distributions, you can install them using:
sudo apt-get install lvm2 # For Debian/Ubuntu
sudo yum install lvm2 # For CentOS/RHEL
Step2: Create Physical Volumes (PVs)
Physical Volumes are the raw disks or disk partitions you will use in LVM.
- Decide which disk or partition to utilize. For example,
/dev/sdb1.
sudo pvcreate /dev/sdb1
Step3: Create a Volume Group (VG)
Several physical volumes are combined into a single storage pool using volume groups
sudo vgcreate my_vg /dev/sdb1
- `my_vg` is the name of the volume group.
Step4: Create Logical Volumes (LVs)
Logical Volumes are created from the space in the Volume Groups.
sudo lvcreate -L 10G -n my_lv my_vg
-L 10Gspecifies the size of the logical volume.-n my_lvsets the name of the logical volume.my_vgis the volume group from which space will be allocated.
Step5: Format the Logical Volume
Format the logical volume with a filesystem.
sudo mkfs.ext4 /dev/my_vg/my_lv
Step6: Mount the Logical Volume
Make a mount point, then mount the volume that makes sense.
sudo mkdir /mnt/my_mount
sudo mount /dev/my_vg/my_lv /mnt/my_mount
Step7: Configure Auto-Mounting
To ensure the logical volume mounts automatically at boot, add it to /etc/fstab.
echo ‘/dev/my_vg/my_lv /mnt/my_mount ext4 defaults 0 2’ | sudo tee -a /etc/fstab
***Common LVM Operations***
Step8: Extend a Logical Volume
First, extend the logical volume, then resize the filesystem.
sudo lvextend -L +5G /dev/my_vg/my_lv
sudo resize2fs /dev/my_vg/my_lv
-L +5Gextends the logical volume by 5 GB.
Step9: Reduce a Logical Volume
First, reduce the filesystem, then the logical volume.
sudo umount /mnt/my_mount
sudo e2fsck -f /dev/my_vg/my_lv
sudo resize2fs /dev/my_vg/my_lv 10G
sudo lvreduce -L 10G /dev/my_vg/my_lv
sudo mount /dev/my_vg/my_lv /mnt/my_mount
- Make sure to back up data before reducing to avoid data loss.
Step10: Remove a Logical Volume
sudo umount /mnt/my_mount
sudo lvremove /dev/my_vg/my_lv
Step11: Remove a Volume Group
sudo vgremove my_vg
Step12: Remove a Physical Volume
sudo pvremove /dev/sdb1
*** LVM Overview ***
- Physical Volumes (PVs): Raw disks or partitions.
- Volume Groups (VGs): Pools of storage from PVs.
- Logical Volumes (LVs): Flexible storage units created from VGs.
- Benefits: Resize volumes, snapshots, flexible storage management. *** Example Scenario ***
- Add a new disk:
/dev/sdc - Create PV:
pvcreate /dev/sdc - Extend VG:
vgextend my_vg /dev/sdc - Extend LV:
lvextend -L +10G /dev/my_vg/my_lv - Resize filesystem:
resize2fs /dev/my_vg/my_lv
“Install CyberPanel on CentOS in this 6 Easy Steps”
