Logical Volume Management (LVM) allows for flexible disk space management in Linux. Below are step-by-step instructions to configure LVM on both generic Linux and Ubuntu.
Step 1: Install LVM
LVM is included in most Linux distributions, but if it’s missing, install it using:
bashsudo apt update
sudo apt install lvm2 -y # Ubuntu/Debian
For RHEL-based systems:
bashsudo yum install lvm2 -y
sudo systemctl enable --now lvm2-lvmetad
Step 2: Identify Available Disks
Check available disks using:
bashlsblk
fdisk -l
Assume we will use /dev/sdb
and /dev/sdc
for LVM.
Step 3: Create Physical Volumes (PVs)
Convert the disks to LVM physical volumes:
bashsudo pvcreate /dev/sdb /dev/sdc
Verify:
bashsudo pvdisplay
Step 4: Create a Volume Group (VG)
Create a volume group named vg_data
:
bashsudo vgcreate vg_data /dev/sdb /dev/sdc
Verify:
bashsudo vgdisplay
Step 5: Create Logical Volumes (LV)
Create a logical volume (lv_storage
) of 10GB:
bashsudo lvcreate -L 10G -n lv_storage vg_data
Or use 50% of available space:
bashsudo lvcreate -l 50%VG -n lv_storage vg_data
Verify:
bashsudo lvdisplay
Step 6: Format the Logical Volume
Format with an ext4 filesystem:
bashsudo mkfs.ext4 /dev/vg_data/lv_storage
Step 7: Mount the Logical Volume
Create a mount point:
bashsudo mkdir /mnt/lvm_storage
Mount the volume:
bashsudo mount /dev/vg_data/lv_storage /mnt/lvm_storage
Verify:
bashdf -h
Step 8: Persistent Mounting
To mount at boot, edit /etc/fstab
:
bashsudo nano /etc/fstab
Add:
bash/dev/vg_data/lv_storage /mnt/lvm_storage ext4 defaults 0 2
Save and exit, then test:
bashsudo mount -a
Step 9: Extending a Logical Volume (Optional)
If more space is needed:
bashsudo lvextend -L +5G /dev/vg_data/lv_storage
Resize filesystem:
bashsudo resize2fs /dev/vg_data/lv_storage
Verify:
bashdf -h
Step 10: Removing LVM (Optional)
If needed, unmount and remove:
bashsudo umount /mnt/lvm_storage
sudo lvremove /dev/vg_data/lv_storage
sudo vgremove vg_data
sudo pvremove /dev/sdb /dev/sdc