Linux Disk Management Based on LVM Logical Volumes [Concise Tutorial]

1. Disadvantages of traditional disk management

Traditional disk management: Use MBR to partition the hard disk first, then format the partition's file system and finally mount the partition..

Traditional disk management is more troublesome when the partition has no space to use for expansion. The space used by the partition is full and is no longer sufficient. At this time, the partition cannot be expanded by stretching the partition. It can only be expanded by adding a new hard disk, then creating a partition on the new hard disk, and then formatting the partition. , and then copy everything from the previous partition to the new partition.
The newly added hard disk exists as an independent file system, and the original file system has not been expanded in any way.

Traditional disk management cannot perform dynamic disk management. When adding a new hard disk to copy the original content, you need to uninstall the current mount point, and when there are many original content files, it takes more time. Some real-time services also do not accept stopping and then copying and mounting content.

2. LVM disk management

LVM (logical volume manager) logical volume management.

Logical Volume Management LVM is a versatile hard disk system tool. Whether on Linux or other similar systems, it is very easy to use. Traditional partitions use fixed-size partitions, and resizing them is cumbersome. However, LVM can create and manage "logical" volumes instead of using physical hard disks directly. It allows administrators to flexibly manage the expansion and contraction of logical volumes with simple operation without damaging the stored data. New hard disks can be added to LVM at will to directly extend existing logical volumes. LVM does not require a reboot to let the kernel know about the partition's existence.

The working principle of LVM is actually very simple. It abstracts and encapsulates the underlying physical hard disk and then presents it to the upper-layer application in the form of a logical volume.

In the traditional disk management mechanism, the upper-layer application directly accesses the file system to read the underlying physical hard disk. In LVM, it encapsulates the underlying hard disk. When we operate on the underlying physical hard disk, , it no longer operates on partitions, but performs underlying disk management operations on it through something called a logical volume.

For example: when a new physical hard disk is added, the upper-layer service cannot feel it, because the last service was presented to the logical volume (the logical volume is mounted).

The biggest feature of LVM is that it can dynamically manage disks. Becausethe size of the logical volume can be dynamically adjusted, and will not lose existing data . If a new hard disk is added, it will not change the existing upper logical volume. As a dynamic disk management mechanism, logical volume technology greatly improves the flexibility of disk management! ! !

3. Basic concepts PE, PV, VG, LV and LVM principles

  1. PE (Physical Extend) physical extension
  2. PV (Physical Volume) physical volume
  3. VG (Volume Group) volume group
  4. LV (Logical Volume) Logical Volume

Insert image description here

In LVM's disk management, the hard disk is first divided into partitions, and then the partitions are formatted to store physical volumes (PV). One or more physical volumes can be used to create volume groups (VG), and then logical volumes can be created based on the volume groups (VG). LV).

During the process of formatting a physical volume, LVM divides the underlying hard disk into PE (Physical Extend) , LVM disk management The default size of PE is 4M, and PE is the most basic unit of logical volume management

As long as there is free space in the volume group, you can create logical volumes as you like. The file system is created on the logical volume, and then the operating system mounts and accesses it.

4. Experiment-Using LVM Disk Management

Experiment content: Add two hard disks to the current operating system, and then use LVM disk management

Insert image description here

/dev/sdb and /dev/sdc in the picture below are our new hard drives.

Insert image description here

(1) Disk partition

Divide /dev/sdb into three primary partitions, with space of 2GB, 3GB, and 5GB respectively.

fdisk /dev/sdb
[root@robin robin]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x7e72cf82 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-20971519,默认为 2048)
将使用默认值 2048
Last 扇区, +扇区 or +size{
    
    K,M,G} (2048-20971519,默认为 20971519):+2G
分区 1 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分区号 (2-4,默认 2):2
起始 扇区 (4196352-20971519,默认为 4196352)
将使用默认值 4196352
Last 扇区, +扇区 or +size{
    
    K,M,G} (4196352-20971519,默认为 20971519):+3G
分区 2 已设置为 Linux 类型,大小设为 3 GiB

命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
分区号 (3,4,默认 3):3
起始 扇区 (10487808-20971519,默认为 10487808)
将使用默认值 10487808
Last 扇区, +扇区 or +size{
    
    K,M,G} (10487808-20971519,默认为 20971519)
将使用默认值 20971519
分区 3 已设置为 Linux 类型,大小设为 5 GiB

命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):1
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):2
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。

Check after partitioning is completed

fdisk -l /dev/sdb # 查看sdb的磁盘分区列表信息

Insert image description here

(2) Prepare physical volume

The partition just created is used to store physical volumes. LVM can use physical volumes of different sizes.
Description of the commands that will be used next:

  1. pvcreate creates a physical volume
  2. pvremove deletes a physical volume
  3. pvdisplay View the attribute information of the physical volume
  4. pvs lists all physical volumes and their information on the system (a command line tool provided by LVM)
  5. pveextend extends the size of a physical volume

First, create three physical volumes of different sizes based on the three primary partitions just now.

pvcreate /dev/sdb1
pvcreate /dev/sdb2
pvcreate /dev/sdb3

Usepvdisplay to look at the attribute information of the physical volume. You can see the properties of each physical volume. The name is its primary partition name.

Insert image description here

(3) Preparing the paper group

Common commands (same format as physical volumes)

  1. vgcreate creates a volume group
  2. vgremove deletes a volume group
  3. vgdisplay View volume group information
  4. vgs lists all volume groups and their information on the system (a command line tool provided by LVM)
  5. vgextend extends the physical volume in the volume group (adds a new physical volume to the volume group)

Put the three physical volumes just created into a volume group (vg_test)

# -s 选项用来指定 PE的大小, vg_test为新建的卷组名称
vgcreate -s 4M vg_test /dev/sdb1 /dev/sdb2 /dev/sdb3

Similarly, verifyCheck the volume group information and use the vgdisplay command
Insert image description here

(4) Create a logical volume

Commonly used commands:

  1. lvcreate creates a logical volume
  2. lvremove deletes a logical volume
  3. lvdisplay View logical volume information
  4. lvs lists all logical volumes and their information on the system (a command line tool provided by LVM)
  5. lvresize adjusts the size of a logical volume
  6. lvextend extends the size of a logical volume

The size of the volume group we just generated is almost 10GB. NowTo create a logical volume based on the volume group, first create a 5GB logical volume (lv_test).

# -n 指定逻辑卷的名称 
# -L 指定逻辑卷的大小
lvcreate -n lv_test -L 5G /dev/vg_test

Use the lvdisplay command to check. You can see that the volume group of lv_test is vg_test, and its size is 5G, and its path is "/dev/vg_test/lv_test" (/dev/volume group/logical volume)

Insert image description here

(5) Format-mount

To format a logical volume as a file system, use the mkfs command. Mount using the mount command.

  1. Format logical volume
mkfs.ext4 /dev/vg_test/lv_test

Insert image description here

  1. mount
mkdir /home/robin/data # 这里我存在一个robin用户,所以我将robin用户下的data目录作为挂载点使用
mount /dev/vg_test/lv_test /home/robin/data
  1. View the status after mounting (df)
df -h /home/robin/data

Insert image description here

(6) Extend logical volume

Dynamic expansion is the most useful function of LVM.
Expanding a logical volume is divided into the following steps:

  1. Unmount the logical volume currently being extended (umount)
  2. Extend logical volume size (lvresize)
  3. Check for disk errors
  4. Update file system size
  5. Remount
 umount /dev/vg_test/lv_test # 1. 卸载逻辑卷
 lvresize -L 7G /dev/vg_test/lv_test # 2. 扩展逻辑卷大小到7G
 e2fsck -f /dev/vg_test/lv_test # 3. 检查逻辑卷磁盘错误
 resize2fs /dev/vg_test/lv_test # 4.更新文件系统大小
 mount /dev/vg_test/lv_test /home/robin/data # 5.重新挂载

Use the df command again to view the disk information of the mounted directory. The capacity has changed.

Insert image description here

(7) Extend volume group

Assume that the volume group (vg_test) created by our first hard disk (/dev/sdb) is full, and the hard disk /dev/sdb has no extra space that can be used. At the same time, we also have a hard disk /dev/sdc. (10GB size) has not been used yet, we divide two G from the second hard disk to the volume group vg_test for expansion.

The essence of volume group expansion is to add physical volumes to the volume group

So extending a volume group from other hard disks can be divided into the following steps:
● Partition the new hard disk
● Create physical disks of different sizes Volume
● Add the newly created physical volume to the volume group that needs to be expanded
● Check whether the volume group has been expanded

  1. Partition /dev/sdc (just divide a 2G partition for us to expand and use)
fdisk /dev/sdc

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):1
起始 扇区 (2048-20971519,默认为 2048)
将使用默认值 2048
Last 扇区, +扇区 or +size{
    
    K,M,G} (2048-20971519,默认为 20971519):+2G
分区 1 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):8e

WARNING: If you have created or modified any DOS 6.xpartitions, please see the fdisk manual page for additionalinformation.

已将分区“FAT12”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

Insert image description here

Insert image description here

  1. Create physical volume /dev/sdc1 based on primary partition /dev/sdc1
pvcreate /dev/sdc1
  1. Add the newly created physical volume to the volume group (vg_test) that needs to be expanded
vgextend vg_test /dev/sdc1
  1. Check whether the volume group is expanded correctly
vgdisplay vg_test

Insert image description here

tips: The first hard drive here is 10G plus the second extended 2G, so it is close to 12G in size.


Guess you like

Origin blog.csdn.net/m0_63622279/article/details/134345525