Linux Volume Management
What is LVM ?
Linux Volume Management (LVM) is a flexible way of managing disk storage. LVM allows administrators to pool storage devices, resize volumes, and efficiently manage storage allocation without the need for physical disk partitioning, which can be limiting. With LVM, logical volumes (LVM’s equivalent to partitions) can be resized dynamically, making it ideal for environments where storage requirements change frequently.
Key Concepts of LVM
Physical Volumes (PVs):
These are physical disks or disk partitions initialized for LVM use (e.g.,
/dev/sda1
,/dev/sdb
).PVs are the base units for LVM and can span multiple disks if necessary.
Volume Groups (VGs):
A volume group is a collection of physical volumes.
By combining multiple physical volumes, you can create a large storage pool.
Volume groups allow you to manage storage capacity across multiple physical devices.
Logical Volumes (LVs):
Logical volumes are created within volume groups and can be thought of as virtual partitions.
They can be resized on demand without unmounting or rebooting the system (if the filesystem supports it).
Logical volumes provide the flexibility to expand or shrink storage as needed.
Physical Extents (PEs) and Logical Extents (LEs):
Extents are fixed-size chunks of storage on PVs and are used by LVM to manage storage allocation.
Physical extents reside on PVs, while logical extents are mapped to the LVs.
Why We Need LVM ?
Here are several key reasons why LVM is often essential in Linux systems:
Flexible Resizing:
- Unlike traditional partitions, LVM allows administrators to expand or reduce the size of logical volumes on-the-fly. This flexibility is critical for applications and filesystems that need more storage over time.
Efficient Use of Space:
- With LVM, you can combine multiple disks into a single volume group, maximizing storage utilization. This makes it possible to create logical volumes that are not constrained by the size of individual disks.
Snapshots for Backup and Testing:
LVM allows the creation of snapshots, which are read-only or read-write copies of the current state of a logical volume.
Snapshots are useful for backups, testing changes, or providing a restore point.
Easier Storage Management in Large Systems:
- LVM is particularly beneficial in large environments where administrators need to manage multiple disks and frequently reconfigure storage allocations.
Disk Redundancy and Migration:
LVM can be configured to mirror logical volumes across multiple physical volumes, providing redundancy.
It also simplifies the migration of data between disks if hardware needs to be upgraded or replaced.
Common Use Cases for LVM
Growing storage for applications without downtime.
Creating and using snapshots for database backups.
Simplifying disk replacement or upgrades by allowing data migration across physical volumes in the background.
Setting up redundancy and mirroring for data protection.
Practical Implementation of the LVM
Create the EC2 instance with base image of ubuntu
Connect to the ec2 instance
Now firstly update your system using
sudo apt-get update -y
List all block devices: Running
lsblk
without any options will display a tree-like format of all block devices.lsblk
The
df
command in Linux is used to display information about disk space usage on the file system. The-h
option makes the output human-readable, showing sizes in powers of 1024 (e.g., K, M, G).df -h
Create the Volume in AWS (volume should be created in the same AZ as EC2 )
Attach the volume to that particular EC2 and now you can see the blocks
Now create the physical volume , volume groups and lastly the logical volume
Note : You should be switch to super user “sudo su“ and then you enter into the lvm using “lvm“ command.
Now we can display the details of the pv , vg and lv through “pvsdisplay“ , “vgdisplay“ and “lvsdisplay“ commands
Extending a logical volume in Linux involves increasing the size of an existing logical volume (LV) within a volume group (VG) using free space from the VG. Here are the steps to do this:
Extend the Logical Volume
Use the
lvextend
command to extend the logical volume. You can specify the additional size you want to add or the total size you want to achieve.Extend by a Specific Size
To extend the logical volume by a specific size (e.g., 10G):
sudo lvextend -L +10G /dev/VG_NAME/LV_NAME
Extend to a Specific Size
To extend the logical volume to a specific total size (e.g., 50G):
sudo lvextend -L 50G /dev/VG_NAME/LV_NAME
Replace
VG_NAME
with the name of your volume group andLV_NAME
with the name of your logical volume.Formatting a logical volume (LV) in Linux involves creating a filesystem on the LV so it can be used for data storage. Here are the steps to do this:
Identify the Logical Volume
First, identify the logical volume you want to format. You can use the
lvdisplay
command to list all logical volumes and their details:sudo lvdisplay
Format the Logical Volume
Choose a filesystem type to format the logical volume. Common filesystem types include
ext4
. Here are the commands to format a logical volume with each of these filesystems:Format with ext4
sudo mkfs.ext4 /dev/VG_NAME/LV_NAME
Replace
VG_NAME
with the name of your volume group andLV_NAME
with the name of your logical volume.Mounting a Volume
Create a Mount Point: This is the directory where the filesystem will be attached.
sudo mkdir /mnt/myvolume
Mount the Volume: Use the
mount
command to attach the filesystem to the directory.sudo mount /dev/sdX1 /mnt/myvolume
Replace
/dev/sdX1
with the appropriate device identifier for your volume (e.g.,/dev/sda1
for the first partition on the first disk).Verify the Mount: Use
df -h
orlsblk
to check that the volume is mounted.df -h
Unmounting a Volume
Unmount the Volume: Use the
umount
command to detach the filesystem from the directory.sudo umount /mnt/myvolume
Ensure no processes are using the filesystem; otherwise, you might get a "device is busy" error.
Verify the Unmount: Use
df -h
orlsblk
to confirm that the volume is unmounted.df -h
Thank you for reading this article if you like this you then drop a like and you can share it ! .
Subscribe to my newsletter
Read articles from Shravankumar Sirvi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by