- Convert disk
into Physical Volume
- Combine PVs into
Volume Group
- Create Logical
Volume from VG
- Format
and mount the LV
This layered approach gives LVM its flexibility.
Advantages of LVM in Linux
·
Easy Resizing
·
Better
Storage Utilization
·
Snapshots for
short-term Backup or recovery
·
Scalability
·
High
Flexibility
Disadvantages of LVM
1. Complexity
It can be a bit challenging to beginners.
2. Recovery
Challenges
Data recovery is more complex as compared to
standard partitions.
3. Slight Overhead
Minimal performance impact.
LVM Commands in Linux (Step-by-Step Guide)
Step 1: Install LVM
Before the
installation of lvm check it is already installed on the system, for this run
the following command.
#sudo lvm version
In case lvm is not installed on the system then
first thing to do is install lvm on the system or if previous version of lvm is
installed update it with latest one.
If you are a beginner, understanding basic Linux commands will help you follow LVM operations easily. Read our beginners friendly guide on Basic Linux Commands Guide.
For Installation or Update
Run this command to update the overall system
#sudo apt update
After successfully updating the system, now
install lvm utility with this command.
#sudo apt install lvm2 -y
To update the older version of lvm with latest
version command is
#sudo apt --only-upgrade lvm2 -y
Step 2: Create
Physical Volume
Lvm latest version is
installed on the system, now create physical volume using the following
command.
#sudo pvcreate /dev/sdb /dev/sdc
To check physical volume is create, command is
#sudo pvdisplay
Step 3: Create Volume
Group
Now create volume
group with the following command
#sudo vgcreate
my_vg /dev/sdb /dev/sdc
Check
VG:
Now check volume group is created
#sudo vgdisplay
Step 4: Create
Logical Volume
This command is used
to create logical volume. Volume size is adjustable as required. All space can
be allocated to single logical volume but it depends on the usage scenario.
Here logical volume of 5G will be created.
#sudo lvcreate -L 5G -n
my_lv my_vg
Command Explanation:
sudo
·
Run the
command with superuser (root) privileges
·
It is required
because disk operations need admin access
lvcreate
·
The lvcreate
command is used to create a logical volume
·
Dynamic
partitions within the volume groups are logical volumes
-L 5G
·
Size of the
logical volume
·
5G means
Gigabytes, you can use M (MB), T (TB) etc.
-n my_lv
·
Sets the name
of the logical volume
·
In this case
the new volume name is called my_lv
my_vg
·
This is the
volume group name
·
The logical
volume is created in this volume group
·
A volume
group is made of one or more physical disks or partitions
Summary
This command creates a
logical volume with the name of my_lv inside the volume group my_vg using root
privileges.
Step 5: Format
Logical Volume
Now format the logical
volume using this command
#sudo mkfs.ext4 /dev/my_
vg/my_lv
Command explanation:
Mkfs.ext4
·
Stands for
make file system (ext4)
·
It creates
ext4 file system on the specified device
ext4
·
One of the
most popular Linux file system
·
Reliable and
fast
·
Supports
large files and journaling
/dev/my_vg/my_lv
·
This is the
logical volume path
·
Created
earlier using lvcreate
·
Acts like a
virtual disk or partition
Note: This command will destroy
any existing data on /dev/my_vg/my_lv.
Step 6: Mount Logical
Volume
Create directory named
mydata inside /mnt and mount the previously created logical volume on this
mount point.
#sudo mkdir
/mnt/mydata
#sudo mount
/dev/my_vg/my_lv /mnt/mydata
Verify the Mount
#sudo df -h
Command explanation:
Mount
·
Mount is a
Linux command used to attach a file system to directory
/dev/my_vg/my_lv
·
This is
logical volume created earlier using LVM
·
It contains
the file system ext4 in this case
/mnt/mydata
·
The mount
point (target directory)
·
This is where
file system will be accessed
Verify the mount
·
Displays
mounted file system and their usage
·
Confirms that
logical volume is successfully mounted
Summary
·
The system
links the logical volume to /mnt/mydata
·
Files stored
in this logical volume accessed through this directory
·
It is a
directory that serves as an entry point into the volume
After setting up LVM, monitoring the system performance becomes important. You can learn how to track system resources in our guide on Linux System Performance with top and htop.
How to Add LVM Mount
Entry in /etc/fstab
To make LVM logical
volume mount automatically at boot you need to add an entry in the /etc/fstab
file.
1. Get UUID of the
Logical Volume
It is recommended to
use UUID instead of device path for reliability.
#sudo blkid
/dev/my_vg/my_lv
2. Edit /etc/fstab
#sudo vim /etc/fstab
3. Add Entry
Add the following line
at the end of the file.
4. Test the Configuration
Before rebooting test
the entry.
#sudo mount -a
·
If no errors
then configuration is correct
·
If error then
fix before reboot
5. Verify Mount Point
#sudo df -h
Step 7: Extend Logical
Volume
This command is used
to add 2GB more in the current size of the logical volume.
#sudo lvextend -L +2G
/dev/my_vg/my_lv
Command explanation
lvextend
·
This lvm
command is used to increase the size of logical volume
-L +2G
·
Specifies the
change in size and here it will add 2GB more in the current size of the Logical
volume
/dev/my_vg/my_lv
·
Logical
volume path which is created earlier
Summary
·
Lvm allocates
the additional free space from the volume group
·
Extend the
logical volume size
· The block device becomes large
#sudo resize2fs /dev/my_vg/my_lv
Step 8: Reduce
Logical Volume
The process of
reducing logical volume is bit more in length as compared to extend. First
unmount the directory before reducing the logical volume.
#sudo umount /mnt/mydata
#sudo e2fsck -f /dev/my_vg/my_lv
#sudo resize2fs /dev/my_vg/my_lv 4G
#sudo lvreduce -L 4G /dev/my_vg/my_lv
#sudo mount /dev/my_vg/my_lv /mnt/mydata
Commands explanation:
Umount
·
Ensures file
system is not in use
E2fsck
·
Checks and
repairs file system before shrinking
·
Required for
safety
Resize2fs
·
Shrink the
file system to 4GB
Mount
·
Reattach the
volume
LVM Snapshots
Explained
Snapshots enable you to take a snapshot of the
state of a volume. In this case it creates a snapshot of the logical volume
/dev/my_vg/my_lv with the size of 1GB and name is snap_lv.
Create Snapshot
#sudo lvcreate --size 1G --snapshot --name
snap_lv /dev/my_vg/my_lv
Command
explanation
lvcreate
· lvcreate is used to
create logical volume
· Here it is used to
create a snapshot volume
--size
· Specifies the size of
the snapshot
· This is not the full
size of the original volume
· It is the space used
to store changes (copy to write data)
--snapshot
· This is used to
create snapshot volume rather than a normal volume
· Captures the state of
the original volume at a specific time
· It uses copy on write
technology
--name
· Assigns the snapshot
name as snap_lv
/dev/my_vg/my_lv
· Initially original
and snapshot share the same data
· When data changed on
the original volume then old data is copied into the snapshot
· Snapshot preserves
the original state
Common Errors and Their Solutions in LVM
Even though LVM is powerful, users may encounter
errors during the configuration and management. Below are some common issues
along with their solutions.
1. Volume
Group Not Found
Error
· Volume group “my_vg”
not found
Cause
· Volume group does not
exist
· Incorrect vg name
Solution
Run “sudo vgdisplay” command to check the
following
· Verify the correct VG
name
· Use the correct name
in your command
2.
Logical Volume Already Exists
Error
· Logical volume my_lv
already exists
Cause
· A logical volume with
the same name already exists in the VG
Solution
Run the “sudo lvdisplay” command to check the
following
#sudo lvdisplay
· Check the existing
logical volumes
· Use a different name
or remove the existing logical volume
#sudo lvremove /dev/my_vg/my_lv
3. Insufficient
Free Space in Volume Group
Error
· Insufficient free
space available in volume group
Cause
· All space may be
already allocated and no space is available for new one
Solution
Run this command to check
#sudo vgdisplay
· Check available space
· Add more physical
volume
#sudo vgextend my_vg /dev/sdd
4.
File system not resized After Extending LV
Error
· LV size increased but
usable space didn’t change
Cause
· File system was not
resized
Solution
Run this command
#sudo resize2fs /dev/my_vg/my_lv
5. Device Busy Error During Unmount
Error
· Umount /mnt/mydata:
target is busy
Cause
· Directory is being
used by the process
Solution
Run this command
#lsof +D /mnt/mydata
· Identify the running
processes
· Stop them or use
#sudo umount -l /mnt/mydata
6. File
system Corruption Warning
Error
· File system needs to
be checked
Cause
· File system is
inconsistent
Solution
Run this command
#sudo e2fsck -f /dev/my_vg/my_lv
7. Mount
Fails (Wrong Files system Type)
Error
· Wrong fs type, bad
option or bad superblock
Cause
· File system is not
created or corrupted
Solution
Run this command
#sudo mkfs.ext4 /dev/my_vg/my_lv
Warning:
this
will erase data
8. Snapshot
Runs Out of Space
Error
· Snapshot becomes
invalid
Cause
· Snapshot size is too small
Solution
Run this command
#sudo lvcreate –size 2G –snapshot –name snap_lv
/dev/my_vg/my_lv
· Create snapshot of
larger size
Real-World Use Cases of LVM
1. Server
Environments
· Manage growing
storage needs dynamically
2. Cloud
Infrastructure
· Scale storage without
downtime
3. Database Systems
· Adjust storage based
on data growth
4. Backup Systems
· Use snapshots for
quick recovery
Best Practices for Using LVM
- Always keep
backups before resizing
- Use meaningful
names for VG and LV
- Monitor disk
usage regularly
- Avoid shrinking
volumes unless necessary
- Use snapshots
wisely (they consume space)
Pro Tips for Beginners
- First practice
in a virtual machine
- Understand PV,
VG and LV clearly
- Avoid risky
operations on production systems
- Always verify
commands before execution
FAQs about LVM in Linux
1. What is LVM in Linux?
LVM is a flexible disk management system that
allows dynamic storage allocation.
2. Is LVM better than
traditional partitioning?
Yes, for most modern use cases due to
flexibility and scalability.
3. Can I resize LVM
without reboot?
Yes, in most cases, especially for extending
volumes.
4. Does LVM affect
performance?
Performance
impact is minimal and usually negligible.
Post a Comment