Logical Volume Management (LVM) is a powerful technology that allows for flexible storage device management. It provides users with the ability to pool and abstract the physical layout of storage devices, enabling easy administration and allocation of logical units. LVM2, the current iteration of LVM, utilizes the device mapper Linux kernel framework to combine existing storage devices into groups and allocate logical volumes from the combined space as needed.
Advantages of LVM
LVM offers several advantages that make it a popular choice for storage management. One of the key advantages is increased abstraction, flexibility, and control. With LVM, logical volumes can be assigned meaningful names such as “databases” or “root-backup”. These logical volumes can also be resized dynamically as space requirements change, and can even be migrated between physical devices within the pool on a running system.
LVM also provides advanced features like snapshotting, striping, and mirroring. Snapshotting allows for the creation of point-in-time copies of logical volumes, which can be used for backups or testing purposes. Striping improves performance by spreading data across multiple physical devices, while mirroring provides data redundancy by maintaining identical copies of data on multiple devices.
LVM Architecture and Terminology
Before diving into the administrative commands and operations of LVM, it’s important to familiarize yourself with the architecture and terminology used in LVM.
LVM Storage Management Structures
LVM organizes storage devices through several layers of abstractions. The basic layers used by LVM are:
- Physical Volumes: Physical volumes (PVs) are the raw building blocks for higher levels of abstraction in LVM. They are regular storage devices or disk-like devices, such as RAID arrays, and are identified by the prefix “pv…”. LVM writes a header to these devices to allocate them for management.
- Volume Groups: Volume groups (VGs) are storage pools that combine physical volumes into a unified logical device. They abstract the characteristics of the underlying devices and are identified by the prefix “vg…”.
- Logical Volumes: Logical volumes (LVs) are the primary components that users and applications interact with. They are functionally equivalent to partitions on a physical disk but offer much more flexibility. Logical volumes are identified by the prefix “lv…” or “lvm…”.
By combining physical volumes into volume groups, administrators can unify the storage space available on a system. They can then segment the volume group into arbitrary logical volumes, which act as flexible partitions.
Understanding Extents
LVM organizes volumes into fixed-size chunks called extents. Extents represent the smallest unit of allocation by LVM and are the key to its flexibility and power. Logical extents, maintained by LVM, map to physical extents on the underlying physical volumes.
LVM allows for copying and reorganizing physical extents without interrupting users, enabling seamless expansion or shrinkage of logical volumes. Extents can be added or removed from a logical volume, allowing for dynamic resizing.
Common Use Cases
Now that we have an understanding of the terminology and structures used in LVM, let’s explore some common use cases that demonstrate the power and flexibility of LVM.
Use Case: Creating Logical Volumes from Physical Disks
In this use case, we will create four logical volumes using two physical disks. We will utilize the pvcreate
, vgcreate
, and lvcreate
commands to achieve this.
Step 1: Marking the Physical Devices as Physical Volumes
First, we need to identify the physical devices that LVM can manage. We can do this by running the lvmdiskscan
command:
sudo lvmdiskscan
The output will display all available block devices that LVM can interact with. Make sure to double-check that the devices you intend to use with LVM do not have any important data already written to them, as using these devices within LVM will overwrite their current contents.
Once you have identified the devices you want to use, mark them as physical volumes within LVM using the pvcreate
command:
sudo pvcreate /dev/sda /dev/sdb
Verify that LVM has registered the physical volumes by running pvs
:
sudo pvs
Step 2: Adding the Physical Volumes to a Volume Group
Now that we have created physical volumes from the devices, we can create a volume group. Most of the time, you will have a single volume group per system for maximum flexibility in allocation. In this example, we will create a volume group named “LVMVolGroup” and add both physical volumes to it:
sudo vgcreate LVMVolGroup /dev/sda /dev/sdb
To check the volume group information, run pvs
again:
sudo pvs
Step 3: Creating Logical Volumes from the Volume Group Pool
With the volume group created, we can now allocate logical volumes from it. In this example, we will create four separate logical volumes: “projects”, “www”, “db”, and “workspace”.
To create the logical volumes, use the lvcreate
command, specifying the volume group and the desired size for each logical volume:
sudo lvcreate -L 10G -n projects LVMVolGroup sudo lvcreate -L 5G -n www LVMVolGroup sudo lvcreate -L 20G -n db LVMVolGroup sudo lvcreate -l 100%FREE -n workspace LVMVolGroup
You can check the volume group information again to verify that the logical volumes have been created:
sudo vgs -o +lv_size,lv_name
Step 4: Formatting and Mounting the Logical Volumes
Now that we have created the logical volumes, we can format them with a file system and mount them as normal block devices.
Format each logical volume with the Ext4 file system:
sudo mkfs.ext4 /dev/mapper/LVMVolGroup-projects sudo mkfs.ext4 /dev/mapper/LVMVolGroup-www sudo mkfs.ext4 /dev/mapper/LVMVolGroup-db sudo mkfs.ext4 /dev/mapper/LVMVolGroup-workspace
Create mount points for each logical volume:
sudo mkdir -p /mnt/projects sudo mkdir -p /mnt/www sudo mkdir -p /mnt/db sudo mkdir -p /mnt/workspace
Mount the logical volumes to the appropriate mount points:
sudo mount /dev/mapper/LVMVolGroup-projects /mnt/projects sudo mount /dev/mapper/LVMVolGroup-www /mnt/www sudo mount /dev/mapper/LVMVolGroup-db /mnt/db sudo mount /dev/mapper/LVMVolGroup-workspace /mnt/workspace
To make the mounts persistent, add them to the /etc/fstab
file:
sudo nano /etc/fstab
Add the following lines to the end of the file:
/dev/mapper/LVMVolGroup-projects /mnt/projects ext4 defaults,nofail 0 0
/dev/mapper/LVMVolGroup-www /mnt/www ext4 defaults,nofail 0 0
/dev/mapper/LVMVolGroup-db /mnt/db ext4 defaults,nofail 0 0
/dev/mapper/LVMVolGroup-workspace /mnt/workspace ext4 defaults,nofail 0 0
Save and exit the file. The logical volumes will now be mounted automatically at boot.
Conclusion
Logical Volume Management (LVM) is a powerful technology that allows for flexible storage device management. By pooling and abstracting the physical layout of storage devices, LVM provides increased abstraction, flexibility, and control. It offers advanced features like snapshotting, striping, and mirroring, making it a popular choice for storage management.
In this article, we explored the architecture and terminology used in LVM, as well as some common use cases. We learned how to create logical volumes from physical disks, mark physical devices as physical volumes, add physical volumes to a volume group, and format and mount logical volumes.
LVM provides businesses with efficient, scalable, and secure cloud hosting solutions. If you’re looking for reliable and professional cloud hosting services, consider Shape.host. They offer Cloud VPS solutions that can be customized to meet your specific requirements. With Shape.host, you can trust that your data is in safe hands.