How to Use Logical Volume Manager (LVM) in Linux
LVM (Logical Volume Manager) in Linux is a system for managing disk storage
LVM provides flexibility and control over how storage is allocated, resized, and used.
It allows you to combine multiple physical storage devices into a single logical unit, which can be resized dynamically and managed more easily than traditional partitions.
Key Concepts of LVM
Physical Volume (PV): These are the actual storage devices or partitions (e.g.,
/dev/sda1
,/dev/sdb
) that are used in LVM. Physical volumes are initialized using thepvcreate
command.Volume Group (VG): A volume group is created by combining one or more physical volumes. The volume group acts as a storage pool from which logical volumes are created. This can be dynamically resized by adding or removing physical volumes. VGs are created using the
vgcreate
command.Logical Volume (LV): Logical volumes are created from the storage available in a volume group. These are analogous to partitions but are much more flexible. You can resize them, create snapshots, or even strip across multiple physical devices. LVs are created using the
lvcreate
command.Physical Extents (PE): Physical volumes are divided into fixed-size chunks called physical extents. The size of these extents is determined when the volume group is created. Logical volumes are composed of these extents.
Logical Extents (LE): Logical volumes are also divided into logical extents, which map one-to-one with physical extents.
Advantages of Using LVM
Flexible Resizing: You can increase or decrease the size of logical volumes dynamically without needing to unmount them.
Snapshots: LVM allows you to create snapshots of a logical volume, which is useful for backups and testing.
Pooling Storage: You can combine multiple physical disks or partitions into a single logical volume group, allowing for better utilization of disk space.
Redundancy and Striping: LVM can be combined with RAID to offer redundancy or striping for performance improvement.
Basic LVM Operations
1. Create Physical Volumes (PV)
To initialize a disk or partition as a physical volume:
pvcreate /dev/sda1 /dev/sdb
2. Create a Volume Group (VG)
Combine physical volumes into a volume group:
vgcreate my_volume_group /dev/sda1 /dev/sdb
3. Create a Logical Volume (LV)
Create a logical volume from the volume group:
lvcreate -L 20G -n my_logical_volume my_volume_group
-L 20G
specifies the size of the logical volume.-n my_logical_volume
gives the logical volume a name.
4. Format the Logical Volume
Once the logical volume is created, format it with a file system (e.g., ext4):
mkfs.ext4 /dev/my_volume_group/my_logical_volume
5. Mount the Logical Volume
Mount the logical volume to make it available for use:
mount /dev/my_volume_group/my_logical_volume /mnt/my_mount_point
6. Resize a Logical Volume
If you need to resize a logical volume (increase its size), first resize the logical volume and then resize the filesystem:
lvextend -L +5G /dev/my_volume_group/my_logical_volume
resize2fs /dev/my_volume_group/my_logical_volume
7. Create a Snapshot
Create a snapshot of an existing logical volume:
lvcreate -L 5G -s -n my_snapshot /dev/my_volume_group/my_logical_volume
8. Remove a Logical Volume
To delete a logical volume:
lvremove /dev/my_volume_group/my_logical_volume
LVM Example Workflow :
Here’s a step-by-step example of how you might use LVM in practice:
Initialize Physical Volumes:
pvcreate /dev/sda1 /dev/sdb1
Create a Volume Group:
vgcreate vg1 /dev/sda1 /dev/sdb1
Create a Logical Volume:
lvcreate -L 50G -n lv1 vg1
Format the Logical Volume:
mkfs.ext4 /dev/vg1/lv1
Mount the Logical Volume:
mount /dev/vg1/lv1 /mnt/mydata
Extend the Logical Volume:
Add a new physical volume (e.g.,
/dev/sdc1
):pvcreate /dev/sdc1 vgextend vg1 /dev/sdc1
Extend the logical volume and resize the filesystem:
lvextend -L +20G /dev/vg1/lv1 resize2fs /dev/vg1/lv1
Results :
Conclusion :
LVM in Linux offers a powerful and flexible approach to managing disk storage. It simplifies disk management by allowing dynamic resizing of logical volumes, snapshot capabilities, and more efficient use of storage resources. LVM makes it easier for system administrators to handle the growing storage needs of modern systems, enabling them to manage storage space without downtime or complex partitioning schemes.
Subscribe to my newsletter
Read articles from Aditya Gadhave directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aditya Gadhave
Aditya Gadhave
👋 Hello! I'm Aditya Gadhave, an enthusiastic Computer Engineering Undergraduate Student. My passion for technology has led me on an exciting journey where I'm honing my skills and making meaningful contributions.