LVM in Linux: An introduction and Installation Guide
LVM, short for Logical Volume Manager, is a virtualization technology for disk and storage management in Linux. LVM provides a dynamic and flexible solution for managing volumes and storage devices, allowing users to tailor storage space management to their specific needs. LVM is utilized to facilitate and improve the creation of logical volumes, manage disk allocation, create partitions, create virtual groups, combine hard disks in storage devices, and create software RAIDs. LVM efficiently organizes raw storage devices into logical volumes and ultimately optimizes disk usage and configuration.
LVM is a beneficial and practical solution, especially for those planning to buy Linux VPS and efficiently manage disk space based on changing storage needs. Therefore, installing LVM on a Linux server is a good practice for Linux server administrators to achieve the highest productivity from the server.
Fortunately, most modern Linux distributions such as Ubuntu, CentOS, RHEL, Fedora and etc. support LVM. If you haven’t installed LVM on your Linux system yet, you can easily do so using your distribution’s package manager. This article is a comprehensive guide to installing LVM on Linux. Additionally, this article will teach you how to create and manage partitions, physical volumes, volume groups, and logical volumes, becoming proficient in dynamic storage management on Linux systems.
Introduction to LVM
In Linux, LVM provides flexibility in managing and allocating storage space by abstracting physical storage devices such as disks and partitions into logical volumes. By providing this abstraction layer, storage management becomes more optimized and straightforward, and Various advanced features are made available for dynamic storage management and data protection, leading to a reduction in downtime. In the following, we will explain concepts such as physical volumes, volume groups, and logical volumes:
Physical Volumes (PVs): A partition or raw storage device that can be a physical hard disk, SSD, or a partition on a disk. Managing and allocating physical volumes through LVM are performed using the
Volume Groups (VGs): A logical container that allows the grouping of one or more physical volumes for collective storage management. To create a volume group and add one or more physical volumes to it, you should use the
Logical Volumes (LVs): Logical Volumes are Logical storage units formatted with a file system and function similarly to regular partitions for storing data. The
lvcreate command is used to create logical volumes and adjust their size, as well as to extend the size of the volume group. Logical volumes are used for storing files and data, and they can be mounted, unmounted, resized, and managed, much like regular partitions. After creation and formatting by file systems using tools such as
mkfs, logical volumes are usable as directories in the file hierarchy.
Dynamic management capability is a key advantage of LVM, allowing you to determine the size of logical volumes and volume groups. Additionally, LVM will enable you to make instant copies of a logical volume called snapshots and use them for backup, system recovery, or testing changes.
LVM protects data through support for mirroring and striping. Mirroring in LVM involves creating multiple copies of data across several physical volumes for redundancy. At the same time, striping is a technique for distributing data across various PVs in a volume group to enhance read and write performance.
Key features of LVM
Understanding the features and advantages of LVM helps you decide whether you need to install LVM on your Linux system. Additionally, awareness of the benefits of installing LVM on Linux provides insight into LVM features for you to leverage. Let’s become familiar with the features of LVM:
- Creates logical volumes covering one or more physical disks or partitions used for exchange space, file systems, or any other storage needs.
- Establishes logical entities called volume groups for grouping and managing multiple physical volumes.
- Manages partitions and physical storage devices, making disk replacement and expansion easy.
- By providing the ability to create thinly logical volumes, it eliminates the need for a large initial consumption of space. It initially considers minimal space and dynamically grows as data is added. This feature aids in optimal disk utilization.
- LVM allows the creation of snapshots of logical volumes for backup purposes or non-disruptive system testing.
- LVM supports Data Striping across multiple physical volumes, playing a crucial role in improving read and write performance.
- The Data Mirroring feature in LVM protects against data loss in the event of disk failure by maintaining multiple copies of data on separate physical volumes.
- LVM allows resizing logical volumes and volume groups, enabling you to add or remove storage space without deleting the file system or disrupting services.
- LVM provides flexibility and cohesiveness in storage management by seamlessly integrating with RAID (Redundant Array of Independent Disks) software, allowing for adaptable and synchronized storage management.
Prerequisites for setting up LVM partitions in Linux
Before LVM installation in Linux, pay attention to the following requirements:
- A Linux server or system running modern Linux distributions.
- Root user or sudo privileges for installing packages and configuring LVM.
- Adequate disk space and the availability of one or more hard disks, SSDs, or partitions with storage space.
- Backup your data before changing the storage configuration for data protection and recovery.
Quick Guide to Install LVM in Linux
Now that you have equipped your Linux server with the prerequisites to install LVM in Linux, you are ready to proceed with the LVM installation. You can install LVM on modern and popular Linux distributions such as Ubuntu, Debian, CentOS, Fedora, and more using your distribution-specific package manager. Here are the general steps for installing LVM on a Linux system:
1. Update Package Lists (Optional)
To install LVM packages on Linux, access the terminal. For installing the latest versions, it is recommended to update the list of packages available on your Linux system using your distribution’s package manager:
sudo apt update # For Debian/Ubuntu
sudo yum update # For CentOS/RHEL
sudo dnf update # For Fedora
2. Install LVM Packages on Linux distros
Now, using your distribution’s package manager, install the packages related to LVM. Use the appropriate command for the Linux distribution you are using to install the main LVM package, which is named “lvm2”.
To install LVM in Debian/Ubuntu-based systems, run the following command:
sudo apt install lvm2
To install LVM in CentOS/RHEL-based systems, run the following command:
sudo yum install lvm2
To install LVM in Fedora, use the following command:
sudo dnf install lvm2
sudo pacman -S lvm2
Then, press the ‘y’ key and hit Enter.
The LVM installation process on Linux may take some time, but after a short wait, it will be successfully installed on your Linux system.
3. Start and Enable LVM Service
On systemd-based systems, it may be necessary to ensure the automatic startup of the LVM service during the boot of the Linux system. To achieve this, start and enable the LVM service by running the following commands:
sudo systemctl start lvm2-lvmetad
sudo systemctl enable lvm2-lvmetad
4. Verify LVM Installation
To ensure that LVM is properly installed and running, use the
lvs (list logical volumes),
lvdisplay (display logical volume properties), or
lvm version commands:
sudo lvm version
If the previous commands provided information about your logical volumes or displayed details about the LVM version on your system, you can be confident that LVM is installed correctly.
Congratulations! LVM is now installed on your Linux system and ready for use. It’s time to start creating volume groups and logical volumes and managing your storage space using LVM commands such as lvcreate, vgcreate, etc.
How to create LVM partitions in Linux
To get started with LVM, our guide can help create volume groups, logical volumes, extend and mount a logical volume, and achieve other objectives. Therefore, follow these steps:
Identifying Your Physical Volumes (PVs)
Identifying the physical volumes (the disks and partitions) in use is a prerequisite for creating logical volumes and volume groups. Commands like lsblk or fdisk, providing a list of all partitions or disks, assist you in recognizing and selecting the desired empty physical volume to configure for LVM usage:
sudo fdisk -l
sudo parted -l
The output of the previous commands displays a list of all available disks and partitions, along with pertinent details.
/dev/sda and /dev/sdb are hard disks that you may identify from the output of the previous commands. You can choose these hard disks for integration into the LVM ecosystem or, if you prefer, create a partition on your hard disk with all available space and set the partition type to Linux LVM or 8E to use it with LVM.
Creating a Partition on a Disk
To create a partition on a hard disk, first, you need to select the disk on which you want to create the partition. Then, create a partition on your selected disk using the
fdisk command and the device name of the disk you want to partition. For example, suppose we want to create a partition on the disk /dev/sdX:
sudo fdisk /dev/sdX
Replace /dev/sdb with your actual disk’s name.
fdisk command supports various options to enhance its functionality, including ‘n‘ (To create a new partition) and ‘p‘ (To create a primary partition). You will be prompted to specify the first and last sectors for the partition, where pressing Enter allows you to allocate the entire disk for the partition and accept the default values set for the partition.
To create an empty partition table on the disk, input the letter ‘o‘ and press Enter. Then, to create a new partition, type ‘n‘ and press Enter. Press Enter to accept the default values for using the entire disk.
Next, run the command ‘t,’ press Enter, and type ‘8e‘ as the Hex code to change the partition type. Press Enter again.
To write the changes to the disk after creating the partition, execute the ‘w‘ command. Remember to perform this step to save the partition information.
Finally, to quit fdisk after writing the changes, type ‘q.’
After creating the partition, you can format it with the file system of your choice (e.g., ext4, ntfs, etc.). Apply your desired format using the following command. For example:
sudo mkfs.ext4 /dev/sdX1
Replace /dev/sdX1 with the actual partition device name created.
Congratulations, your new partition (in our example /dev/sdX1 ) is created and ready to be integrated into LVM.
Initialize Physical Volumes
After selecting or creating a partition as a physical volume, at this step, you will add your desired partition as a PV to LVM and initialize the physical volume using the pvcreate command. For example, we intend to initialize the new partition we created (/dev/sdX1):
sudo pvcreate /dev/sdX1
Replace /dev/sdX1 with the name of your desired partition or disk. As a result, you have initialized your physical volume, and it is now part of the LVM ecosystem.
Listing Physical Volumes
To view a list of available physical volumes, execute the following command:
Displaying more information about PV
If you want to gain more information about all available physical volumes or a specific physical volume, you can use the following command:
Creating a Volume Group (VG)
The best method for collective management of multiple physical volumes is to create a volume group. Therefore, create a volume group to group physical volumes after the initial setup of physical volumes using the
vgcreate command. For example, if you want to create a volume group named “myvg” using the physical volume /dev/sdX1, execute the following command:
sudo vgcreate myvg /dev/sdX1
As a result, you’ve created a unified storage repository, which is the basis of LVM operation.
Listing volume groups
To check the list of created volume groups, use the following command:
Displaying more information about VG
To get more detailed information about all volume groups or a specific volume group, use the following commands:
sudo vgdisplay myvg
Extending a Volume Group
You might want to add more physical volumes to a volume group. In such cases, to increase storage capacity, you can expand the volume group by running the following command:
sudo vgextend myvg /dev/sdX2
As a result, in this example, we added a new physical volume named /dev/sdX2 to the newly created volume group “myvg.”
Creating Logical Volumes (LVs)
After creating the volume group, you have the ability to create LVM Logical Volumes. You can create a logical volume in the volume group with a specific size using the lvcreate command. For example, if you want to create a logical volume named “mylv” with a size of 10 GB in the volume group “myvg,” use the following command:
sudo lvcreate -n mylv -L 10G myvg
‘-n‘ sets the Logical Volume name, and ‘-L‘ specifies the size.
Viewing Logical volumes
To view the created Logical Volumes in the Volume Group, use the following command:
Viewing details about LV
To view more information about a specific LV or all logical volumes created, run the following command:
sudo lvdisplay VG_NAME/LV_NAME
Resizing a logical volume
The flexibility and scalability of Logical Volume are among its most valuable advantages because it is expandable according to your variable needs. For example, if you want to increase the size of your created Logical Volume by 6 GB based on your requirements, you should run the following command:
lvresize -L +6G /dev/myvg/mylv
The “+” option in the previous command increases the size. In some cases, you can even decrease the Logical Volume size based on your needs. In that case, to reduce the size by 6 GB, you should use the “–” option (-L -6G).
Formatting the Logical Volume
After creating the Logical Volume, it’s time to format it with your desired file system. Therefore, coordinate and organize your desired Logical Volume with your chosen file system using the mkfs command. For example, to format “mylv” with the ext4 file system, execute the following command:
sudo mkfs.ext4 /dev/myvg/mylv
Instead of /dev/myvg/mylv, replace it with the path of your Logical Volume.
Note: You can define your LV path as /dev/VG_NAME/LV_NAME.
Mounting the Logical Volume
Creating new volumes alone doesn’t fulfill your needs; you must know how to use Logical Volumes in linux. To integrate Logical Volumes into the system and make use of them, you need to mount the Logical Volumes. For this purpose, first, create a mount point (a directory) and then mount your Logical Volume on it. For example, to mount “mylv” on the /mnt/mydata directory, use the following commands:
sudo mkdir /mnt/mydata
sudo mount /dev/myvg/mylv /mnt/mydata
As a result, by using these commands, you created a directory /mnt/mydata as a mount point and mounted the Logical Volume “mylv” onto it, making the data available for any purpose.
Auto-Mount at Boot
If you intend to mount the Logical Volume during Linux system boot automatically, you should open the /etc/fstab file with your preferred text editor and add the following entry to it:
/dev/myvg/mylv /mnt/mydata ext4 defaults 0 0
Managing Logical Volumes
You can use LVM commands in linux to manage Logical Volumes by resizing, creating snapshots, and others. The most common LVM commands for managing new volumes are as follows:
lvresize: Changes the size of Logical Volumes.
lvcreate –snapshot: Creates an instant snapshot to capture the state of a Logical Volume at a specific moment.
lvconvert –merge: Merges the snapshot into the original volume.
lvremove: Removes a Logical Volume.
For a better understanding, let’s consider an example. Suppose you want to create an instant snapshot named “mylv_snapshot” with a size of 3 GB based on the original Logical Volume /dev/myvg/mylv. In that case, use the following command:
lvcreate -s -n mylv_snapshot -L 3G /dev/myvg/mylv
Creating instant copies of volumes for backup, recovery, and data analysis purposes is useful.
If you no longer need a Logical Volume, you can remove it from your volume group by executing the
Replace the path of your Logical Volume with /dev/myvg/mylv.
Note: Be very cautious when using LVM commands, and before making changes to your storage infrastructure, always make sure to create backups of your important data.
Checking LVM Configuring Details
The following command helps you gain a general insight into the LVM configuration, including physical volumes, logical volumes, and volume groups:
pvs && vgs && lvs
This command provides information about physical volumes, volume groups, and logical volumes, along with a snapshot of the current state of LVM.
LVM is a dynamic and flexible technique for storage management that enables storage space management based on your needs. LVM is used to add and remove physical volumes, create logical volumes, and resize volumes. LVM has become a valuable tool for organizations and systems seeking efficient and scalable storage solutions in Linux environments, addressing their needs for server environments in virtualization platforms for storage resource management and allocation. With this article, you learned how to install LVM in Linux. LVM has a wide world, and the discovery of the world of LVM does not end in this article, this is a comprehensive guide to start working with LVM, and it has taught you the basics of LVM. Now, with advanced LVM options, you can manage your storage according to your specific needs.