Introduction to Linux LVM
This article was last updated on: July 24, 2024 am
Logical Volume Management LVM is a versatile hard disk system tool. It is very easy to use on Linux or other similar systems. Traditional partitions use fixed-size partitions, and resize is cumbersome. However, LVM can create and manage “logical” volumes instead of using physical hard disks directly. It allows administrators to flexibly manage logical volumes that are scaled up and down with simplicity without corrupting stored data. Feel free to add new hard disks to LVM to directly extend existing logical volumes. LVM does not require a reboot for the kernel to know that partitions exist.
LVM uses a hierarchical structure, as shown in the following diagram.
- At the top of the figure, first isThe actual physical diskand its divisionpartitionand abovePhysical Volume (PV)。
- One or more physical volumes can be used to createVolume Group (VG)。
- Then based on volume groups can be createdLogical Volume (LV)。 As long as there is free space in the volume group, you can create logical volumes as you like.
- File systems are created on logical volumes and can then be mounted and accessed by the operating system.
LVM test instructions
This article will cover itHow to create and manage LVM volumes in Linux。 We will divide it into two parts.
- In the first part, we first create multiple logical volumes on a hard disk and then mount them there
/lvm-mount
Directory. Then we will resize the created volume. - In the second part, we will add additional volumes from another hard disk to LVM.
Prepare the disk partitions
By using fdisk
to create a disk partition. We need to create 3 1G partitions.
i️Note.
Partitions are not required to be of a consistent size
Similarly, partitions need to use the “8e” type to make them available for LVM.
1 |
|
1 |
|
Repeat above to create the other two partitions. Once the partition is created, we should have output similar to the following:
1 |
|
1 |
|
Prepare Physical Volume (PV)
The partition you just created is used to store physical volumes. LVM can use physical volumes of different sizes.
1 |
|
Use the following command to check the creation of a physical volume. Some of the output is intercepted below./dev/sdb2
It’s a new one 1.01 GiB
Physical volumes.
1 |
|
1 |
|
Use the following command to delete a physical volume.
1 |
|
Prepare Volume Group (VG)
The following command is used to create a named volume-group1
volume group, using /dev/sdb1
, /dev/sdb2
and /dev/sdb3
Create.
1 |
|
Use the following command to verify the volume group.
1 |
|
1 |
|
From the output, we can see the usage/total amount of the volume group. Physical volumes provide space for volume groups. As long as there is free space in this volume group, we can create logical volumes at will.
Use the following command to delete a volume group.
1 |
|
Create a logical volume (LV)
The following command creates a named 1v1
, a logical volume with a size of 100MB. We use small partitions to reduce execution time. This logical volume uses the space of the volume group created earlier.
1 |
|
Logical volumes can be used lvdisplay
Command view.
1 |
|
1 |
|
Now that the logical volume is ready, we can format and mount the logical volume just like any other ext3/4 partition!
1 |
|
Once the logical volume is mounted, we can go to the mount point /lvm-mount/
Read and write up. To create and mount additional logical volumes, we repeat this process.
Finally, use lvremove
We can delete the logical volume.
1 |
|
Extend an LVM volume
The ability to resize logical volumes is the most useful feature of LVM. This section discusses how we can extend a logical volume that exists. Next, we’ll extend the logical volume we created earlier lv1
Expand to 200MB.
⚠️Note:
After you resize a logical volume, you also need to match the file system resizing. This extra step varies, depending on the type of file system you are creating. In this article, we use:
lv1
A file system of type ext4 is created, so the operation here is for the ext4 file system. (The ext3 file system is similar.) The order in which commands are executed is important.
First, we unmount the lv1 volume
1 |
|
Then, set the size of the volume to 200M
1 |
|
Next, check for disk errors
1 |
|
After you run the following command to extend the file system, the ext4 information is updated.
1 |
|
The logical volume should now be expanded to 200MB. We check the status of the LV to verify.
1 |
|
1 |
|
Now the logical volume can be mounted again, and the same method can be used for other partitions.
Reduce one LVM volume
This section describes how to reduce the size of LVM volumes. The order of the commands is equally important. Also, the following commands are valid for ext3/4 file systems.
❌Dangerous:
If the size value of the reduced logical volume is less than the size of the stored data, the data stored in later parts will be lost.
First, unmount the volume.
1 |
|
Then, detect disk errors.
1 |
|
Next, shrink the file system and update the ext4 information.
1 |
|
When finished, reduce the logical volume size
1 |
|
Finally, verify the resizing logical volume.
1 |
|
1 |
|
Expand a volume group
This section discusses ways to extend a volume group by adding a physical volume to a volume group. Let’s assume our volume group volume-group1
Already full, needs to expand. There are no other free partitions on the hard disk (SDB) in hand, so we added another hard disk (SDC). We’ll see how to add SDC’s partitions to a volume group to expand.
Detect the current volume group status
1 |
|
1 |
|
First, we create a 2GB partition sdc1 of type LVM(8e), as described earlier in the tutorial.
1 |
|
1 |
|
Then, we create a physical volume /dev/sdc1
1 |
|
Now that the physical volume is ready, we can simply add it to the volume group that already exists volume-group1
Above.
1 |
|
use vgdisplay
to verify (you can see that the volume group size has increased).
1 |
|
1 |
|
i️Note:
Although we use a separate disk for demonstration, in fact, it is
8e
Types of disk partitions can be used to extend a volume group.
To summarize, LVM is a powerful tool for creating and managing variable-sized partitions. In this article, we have described how dynamic partitioning is created and used in LVM. We also covered ways to expand/shrink logical volumes and volume groups, and how to add a new disk to LVM.
Hope that helps.
Original link
📑 Original link
via: http://xmodulo.com/2014/05/use-lvm-linux.html
Translator:Vic___ Proofread:wxy
This article is provided by LCTT original translation,Linux China Honors launched