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.

LVM 结构

  1. At the top of the figure, first isThe actual physical diskand its divisionpartitionand abovePhysical Volume (PV)
  2. One or more physical volumes can be used to createVolume Group (VG)
  3. 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.
  4. 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.

  1. 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.
  2. In the second part, we will add additional volumes from another hard disk to LVM.

Prepare the disk partitions

By using fdiskto 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
# fdisk /dev/sdb 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Command (m for help): n ## 新建
Command action
e extended
p primary partition (1-4)
p ## 主分区

Partition number (1-4): 1 ## 分区号
First cylinder (1-1044, default 1): ## 回车用默认的1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +1G ## 大小

Command (m for help): t ## 改变类型
Selected partition 1
Hex code (type L to list codes): 8e ## LVM 的分区代码
Changed system type of partition 1 to 8e (Linux LVM)

Repeat above to create the other two partitions. Once the partition is created, we should have output similar to the following:

1
# fdisk -l 
1
2
3
4
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1 1 132 1060258+ 8e Linux LVM
/dev/sdb2 133 264 1060290 8e Linux LVM
/dev/sdb3 265 396 1060290 8e Linux LVM

Prepare Physical Volume (PV)

The partition you just created is used to store physical volumes. LVM can use physical volumes of different sizes.

1
2
3
# pvcreate /dev/sdb1
# pvcreate /dev/sdb2
# pvcreate /dev/sdb3

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
# pvdisplay 
1
2
3
4
5
6
7
8
9
10
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 1.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID jszvzz-ENA2-g5Pd-irhV-T9wi-ZfA3-0xo092

Use the following command to delete a physical volume.

1
# pvremove /dev/sdb1 

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
# vgcreate volume-group1 /dev/sdb1 /dev/sdb2 /dev/sdb3

Use the following command to verify the volume group.

1
# vgdisplay 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--- Volume group ---
VG Name volume-group1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 3.02 GiB
PE Size 4.00 MiB
Total PE 774
Alloc PE / Size 0 / 0
Free PE / Size 774 / 3.02 GiB
VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK

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
# vgremove volume-group1 

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
# lvcreate -L 100M -n lv1 volume-group1 

Logical volumes can be used lvdisplay Command view.

1
# lvdisplay 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
--- Logical volume ---
LV Name /dev/volume-group1/lv1
VG Name volume-group1
LV UUID YNQ1aa-QVt1-hEj6-ArJX-I1Q4-y1h1-OFEtlW
LV Write Access read/write
LV Status available
# open 0
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2

Now that the logical volume is ready, we can format and mount the logical volume just like any other ext3/4 partition!

1
2
3
# mkfs.ext4 /dev/volume-group1/lv1
# mkdir /lvm-mount
# mount /dev/volume-group1/lv1 /lvm-mount/

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
2
# umount /lvm-mount/ 
# lvremove /dev/volume-group1/lv1

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
# umount /lvm-mount/ 

Then, set the size of the volume to 200M

1
# lvresize -L 200M /dev/volume-group1/lv1 

Next, check for disk errors

1
# e2fsck -f /dev/volume-group1/lv1 

After you run the following command to extend the file system, the ext4 information is updated.

1
# resize2fs /dev/volume-group1/lv1 

The logical volume should now be expanded to 200MB. We check the status of the LV to verify.

1
# lvdisplay 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
--- Logical volume ---
LV Name /dev/volume-group1/lv1
VG Name volume-group1
LV UUID 9RtmMY-0RIZ-Dq40-ySjU-vmrj-f1es-7rXBwa
LV Write Access read/write
LV Status available
# open 0
LV Size 200.00 MiB
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2

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
# umount /dev/volume-group1/lv1

Then, detect disk errors.

1
# e2fsck -f /dev/volume-group1/lv1 

Next, shrink the file system and update the ext4 information.

1
# resize2fs /dev/volume-group1/lv1 100M 

When finished, reduce the logical volume size

1
2
3
# lvresize -L 100M /dev/volume-group1/lv1 
WARNING: Reducing active logical volume to 100.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv1? [y/n]: y
Reducing logical volume lv1 to 100.00 MiB Logical volume lv1 successfully resized

Finally, verify the resizing logical volume.

1
# lvdisplay 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
--- Logical volume ---
LV Name /dev/volume-group1/lv1
VG Name volume-group1
LV UUID 9RtmMY-0RIZ-Dq40-ySjU-vmrj-f1es-7rXBwa
LV Write Access read/write
LV Status available
# open 0
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2

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
# vgdisplay volume-group1 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--- Volume group ---
VG Name volume-group1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 8
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 3.02 GiB
PE Size 4.00 MiB
Total PE 774
Alloc PE / Size 25 / 100.00 MiB
Free PE / Size 749 / 2.93 GiB
VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK

First, we create a 2GB partition sdc1 of type LVM(8e), as described earlier in the tutorial.

1
# fdisk /dev/sdc 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1044, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +2G

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Then, we create a physical volume /dev/sdc1

1
# pvcreate /dev/sdc1 

Now that the physical volume is ready, we can simply add it to the volume group that already exists volume-group1 Above.

1
# vgextend volume-group1 /dev/sdc1 

use vgdisplay to verify (you can see that the volume group size has increased).

1
# vgdisplay 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--- Volume group ---
VG Name volume-group1
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 4
Act PV 4
VG Size 5.03 GiB
PE Size 4.00 MiB
Total PE 1287
Alloc PE / Size 25 / 100.00 MiB
Free PE / Size 1262 / 4.93 GiB
VG UUID bwd2pS-fkAz-lGVZ-qc7C-TaKv-fFUC-IzGNBK

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

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


Introduction to Linux LVM
https://e-whisper.com/posts/27678/
Author
east4ming
Posted on
September 18, 2021
Licensed under