Simplifying Storage in Linux with LVM
Managing storage in Linux can be a daunting task, especially for beginners. However, with the help of Logical Volume Manager (LVM), you can simplify the process of managing disk space. In this blog post, we'll explore the basics of LVM and how it can make your life easier.
What is LVM?
LVM stands for Logical Volume Manager. It is a device mapper framework that provides logical volume management for the Linux kernel. LVM allows you to create, resize, and manage disk partitions more flexibly than traditional partitioning schemes.
Benefits of Using LVM
Flexibility: Easily resize volumes as your storage needs change.
Snapshots: Create snapshots of your data for backups and restores.
Efficient Use of Space: Combine multiple physical volumes into a single logical volume.
Better Disk Management: Manage disk space more efficiently across multiple drives.
Basic LVM Commands And Steps
Partition the Disk /dev/sdb Using fdisk:
sudo fdisk /dev/sdb
Inside fdisk prompt:
n # Add a new partition
p # Primary partition
ENTER # Default partition number
ENTER # Default first sector
ENTER # Default last sector (uses all available space)
t # Change partition type
8e # Set to Linux LVM
w # Write changes and exit
Verify the Partition:
sudo fdisk -l /dev/sdb
Create Physical Volume:
To create a physical volume, use the following command:
sudo pvcreate /dev/sdb1
Verify the Physical Volume:
To verify the physical volume, use the following command:
sudo pvs
Create Volume Group of 2 GB Named vgapps:
sudo vgcreate vgapps /dev/sdb1
Verify the Volume Group:
To verify the volume group, use the following command:
sudo vgdisplay vgapps
Create Logical Volumes by the name app1-lv and app2-lv:
To create logical volumes named app1-lv and app2-lv, use the following commands:
sudo lvcreate -L 1G -n app1-lv vgapps
sudo lvcreate -L 1G -n app2-lv vgapps
Verify the Logical Volumes:
sudo lvdisplay vgapps
Apply a Filesystem and Set Mount Points:
sudo mkfs.ext4 /dev/vgapps/app1-lv
sudo mkfs.ext4 /dev/vgapps/app2-lv
sudo mkdir /app1
sudo mkdir /app2
sudo mount /dev/vgapps/app1-lv /app1
sudo mount /dev/vgapps/app2-lv /app2
df -Th
Set Permanent Mount Points in fstab:
Edit the fstab file:
sudo nano /etc/fstab
Add the following lines:
/dev/vgapps/app1-lv /app1 ext4 defaults 0 2
/dev/vgapps/app2-lv /app2 ext4 defaults 0 2
Apply the new fstab configuration:
sudo mount -av
Verify:
To verify the new configuration, use the following command:
df -Th
Extend the Disk Space Using LVM (Add 1 GB to app1-lv):
To extend the disk space using LVM and add 1 GB to app1-lv, first ensure you have free space in the volume group:
sudo vgextend vgapps /dev/sdc1
Add Space To Logical Volume:
lvextend -L +1G /dev/vgapps/app1-lv
Resize the filesystem:
resize2fs /dev/vgapps/app1-lv 2G
Verify:
To verify the new configuration, use the following command:
df -h
Conclusion
By mastering LVM, you've gained powerful tools for managing storage in Linux. With the ability to create, resize, and manage logical volumes, your system is now more flexible and efficient. Keep exploring LVM's features to optimize your storage solutions and streamline your workflow. Happy managing with LVM!
Subscribe to my newsletter
Read articles from Raja Ramees directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Raja Ramees
Raja Ramees
IT Specialist: Proficient in Bash, Python, Git, MySQL, AWS, Linux, and Ansible