Understanding Logical Volume Manager
Logical Volume Manager (LVM) is a software-based storage managemnt solutions used in Linux.
LVM provides a flexible and dynamic way to manage disk storage by abstracting physical storage devices into logical volumes.
It allows us to create, resize, move, and manage storage volumes easily providng benefits such as improved disk utilization, data redundancy, and simplified disk management.
Key components and concept of LVM
- Physical Volumes (PVs): Physical volumes are the actual storage devices or partitions. such as hard drives, SSDs, or disk partitions, that LVM uses as building blocks. We can add one or more physical volumes to an LVM setup.
- Volume Groups (VG): Volume groups are created by combining one or more physical volumes. VGs act as a pool of storage that can be allocated to logical volumes. They provide a way to manage and allocate storage resources across multiple physical devices.
- Logical Volumes (LVs): Logical volumes are virtual partitions created within volume groups. They can be thought of as equivalent to traditional partitions but with the added benefit of flexibilty. LVs can be resized dynamically, making it easier to adapt to changing storage needs.
- Extents: Logical volumes are divided into extents, which are fixed-size blocks of storage. Extent sizes can be configured when creating volume groups and logical volumes.
- Striping and Monitoring: LVM supports advanced features like striping (spreading data across multiple physical volumes for improved performance) and mirroring (creating redundant copies of data on seperate PVs for fault tolerance)
Simplified LVM Workfow
- We create physical volumes by adding storage devices to the LVM setup using the
pvcreate
command
- We group one or more physical volumes into volume groups using the
vgcreate
- Within a volume group, we create logical volumes using the
lvcreate
command. These logical volumes can be formatted with filesystems and mounted like regular partitions.
- We can resize logical volumes (both grow and shrink) as needed without the need to repartition disks.
- We can implement advanced features like striping, monitoring, and snapshots to meet spesific storage requirements
LVM Use Case Example
Imagine you are a system administrator responsible for managing storage on a Linux server that hosts a database application. The server has multiple hard drives, and you want to use LVM to make storage management more flexible, enable easy expansion, and improve data redundancy.
- Initial setup
You have three hard drives:
/dev/sda
,/dev/sdb
,/dev/sdc
. You initialize these drives as physical volumes (PVs) using thepvcreate
command, so that the LVM can recognize the harddrives :
pvcreate /dev/sda /dev/sdb /dev/sdc
- Create volume groups
Now, you create a volume groups based on those PVs and name it
myvg
that encompasses those physical volumes:
vgcreate myvg /dev/sda /dev/sdb /dev/sdc
- Create logical volumes Now that you already create volume groups, you can now create multiple logical volumes on top of a volume groups. You need to allocate storage for database, application logs, and user files. You can create logical volumes for each purposes:
- 50GB logical volume for the database data:
- 20GB logical volume for the application logs:
- 100GB logical volume for the user files:
lvcreate -L 50GB -n db_storage myvg
lvcreate -L 20GB -n log_storage myvg
lvcreate -L 100GB -n user_files_storage myvg
- Formatting and Mounting LVs Format the logical volumes with appropriate filesystems (e.g., ext4) and mount them to spesific directories:
- Database Storage:
- 20GB logical volume for the application logs:
- 100GB logical volume for the user files:
mkfs.ext4 /dev/myvg/db_storage mount /dev/myvg/dbstorage /mnt/db_data
mkfs.ext4 /dev/myvg/log_storage mount /dev/myvg/log_storage /var/log/app_logs
mkfs.ext4 /dev/myvg/user_files_storage mount /dev/myvg/user_files_storage /home/user_files
- Extending LVs As the database grows, you can easily expand the database data logical volume without affecting other volumes:
lvextend -L +20G /dev/myvg/db_storage resize2fs /dev/myvg/db_storage
- Implementing Redundancy/Mirroring To ensure data availabillity, you can implement data mirroring
lvconvert --type mirror /dev/myvg/db_storage
- Snapshots Backup Before performing a major update, you should create a snapshot of the database data for backup purposes
lvcreate --size 10G --snapshot --name db_data_snapshot /dev/myvg/db_data
- Adding Another PVs to VGs
vgextend myvg /dev/sdd