乌市泽哥 发表于 前天 04:10

LVM(Logical Volume Manager)

一. LVM概述

1. 什么是 LVM

LVM(Logical Volume Manager,逻辑卷管理器)是 Linux 体系下的一种 存储管理 机制,能够灵活地管理磁盘分区。它提供了一种比传统分区管理(如fdisk、parted)更高级的存储管理方式,允许动态调整存储空间,方便扩展和缩减分区,而不会影响已有数据。
2. LVM 主要概念

LVM 的核心概念类似于 RAID 或虚拟存储池,其主要组成部分如下:

[*]PV(Physical Volume,物理卷)

[*]LVM 的最底层单位,可以是整个硬盘,也可以是一个分区。
[*]通过 pvcreate 将物理磁盘或分区初始化为 LVM 可用的 PV。

[*]VG(Volume Group,卷组)

[*]由多个 PV 组成的存储池,全部的存储空间都可以在 VG 内部动态分配。
[*]通过 vgcreate 创建 VG。

[*]LV(Logical Volume,逻辑卷)

[*]相称于传统的“分区”,可以格式化文件体系(如ext4、xfs),然后挂载利用。
[*]通过 lvcreate 创建 LV,大小可以随时扩展或缩小(前提是文件体系支持)。

[*]PE(Physical Extents,物理分块)

[*]LVM 内部的最小存储单元,默认大小 4MB(可调整)。
[*]VG 内的空间被划分成若干个 PE,LV 的大小由 PE 数目决定。
┌───────────────────────────────┐
│      物理磁盘(sdb, sdc)   │
│(未初始化)                     │
└───────────────────────────────┘
         ↓ pvcreate
┌───────────────────────────────┐
│      物理卷(PV)             │
│/dev/sdb   /dev/sdc          │
└───────────────────────────────┘
         ↓ vgcreate
┌───────────────────────────────┐
│      卷组(VG)               │
│my_vg = PV1 + PV2             │
│(存储池)                     │
└───────────────────────────────┘
         ↓ lvcreate
┌───────────────────────────────┐
│      逻辑卷(LV)             │
│/dev/my_vg/my_lv (50G)       │
│(类似于分区,可格式化挂载)      │
└───────────────────────────────┘
3. LVM 的优势


[*]动态扩展与缩减存储:可在不停机的情况下扩展 LV 或 VG,而无需重新分区。
[*]跨多块磁盘管理:可以将多块磁盘归并为一个存储池(VG),提高存储利用率。
[*]快照(Snapshot):支持创建数据快照,方便数据备份与恢复。
[*]条带化存储(Striping):类似 RAID 0,提高磁盘 I/O 性能。
4. LVM 基本操作

# 1. 创建物理卷
pvcreate /dev/sdb /dev/sdc

# 2. 创建卷组
vgcreate my_vg /dev/sdb /dev/sdc

# 3. 创建逻辑卷(比如创建 10GB 的逻辑卷)
lvcreate -L 10G -n my_lv my_vg

# 4. 格式化并挂载
mkfs.ext4 /dev/my_vg/my_lv
mkdir /mnt/lvm_mount
mount /dev/my_vg/my_lv /mnt/lvm_mount

# 5. 扩展逻辑卷
lvextend -L +5G /dev/my_vg/my_lv   # 增加 5GB
resize2fs /dev/my_vg/my_lv         # 扩展文件系统(ext4)二. LVM 实战操作

1. 环境准备

https://img2024.cnblogs.com/blog/3546069/202502/3546069-20250225224841317-2032894650.png
2. 对磁盘分区

2.1 利用 fdisk 对 /dev/sdc 举行分区

# 1.进入 fdisk 交互模式
# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x0313bb2e.

# 2.查看磁盘信息
Command (m for help): p
Disk /dev/sdc: 300 GiB, 322122547200 bytes, 629145600 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0313bb2e
# 发现磁盘 /dev/sdc 没有有效的分区表,默认是 dos 分区表。

# 3.创建 GPT 分区表
Command (m for help): g
Created a new GPT disklabel (GUID: F96B7C68-81FB-D14C-A17D-C686B732B937).

# 4.创建第一个 100G 分区
Command (m for help): n
Partition number (1-128, default 1):        # 分区编号(默认 1)
First sector (2048-629145566, default 2048):        # 起始扇区(默认 2048)
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-629145566, default 629145566): +100G

Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.
# 成功创建 /dev/sdc1 分区,大小 100G

# 5.创建第二个 200G 分区
Command (m for help): n
Partition number (2-128, default 2):        # 分区编号(默认 2)
First sector (209717248-629145566, default 209717248):        # 起始扇区(默认 209717248)
Last sector, +/-sectors or +/-size{K,M,G,T,P} (209717248-629145566, default 629145566):

Created a new partition 2 of type 'Linux filesystem' and of size 200 GiB.
# 成功创建 /dev/sdc2 分区,大小 200G

# 6.查看分区情况
Command (m for help): p
Disk /dev/sdc: 300 GiB, 322122547200 bytes, 629145600 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: F96B7C68-81FB-D14C-A17D-C686B732B937

Device         Start       End   SectorsSize Type
/dev/sdc1       2048 209717247 209715200100G Linux filesystem
/dev/sdc2209717248 629145566 419428319200G Linux filesystem

# 7.保存分区表并退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# 8.验证分区是否生效
# fdisk -l /dev/sdc
Disk /dev/sdc: 300 GiB, 322122547200 bytes, 629145600 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: F96B7C68-81FB-D14C-A17D-C686B732B937

Device         Start       End   SectorsSize Type
/dev/sdc1       2048 209717247 209715200100G Linux filesystem
/dev/sdc2209717248 629145566 419428319200G Linux filesystem2.2 对/dev/sde举行分区

# fdisk -l /dev/sde
Disk /dev/sde: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


# fdisk /dev/sde

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xf8cc9c96.

Command (m for help): p
Disk /dev/sde: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf8cc9c96

Command (m for help): g
Created a new GPT disklabel (GUID: 7B0A9C2B-F3B1-3A42-9665-8640DFC613DC).

Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-2147483614, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2147483614, default 2147483614): +500G

Created a new partition 1 of type 'Linux filesystem' and of size 500 GiB.

Command (m for help): n
Partition number (2-128, default 2):
First sector (1048578048-2147483614, default 1048578048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1048578048-2147483614, default 2147483614):

Created a new partition 2 of type 'Linux filesystem' and of size 524 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# fdisk -l /dev/sde
Disk /dev/sde: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 7B0A9C2B-F3B1-3A42-9665-8640DFC613DC

Device          Start      End    SectorsSize Type
/dev/sde1      2048 1048578047 1048576000500G Linux filesystem
/dev/sde21048578048 2147483614 1098905567524G Linux filesystem2.3 检查磁盘分区情况

https://img2024.cnblogs.com/blog/3546069/202502/3546069-20250225224900254-490155706.png
3. 创建pv

3.1 检察现有的pv列表

# pvsPV         VG      FmtAttr PSize   PFree/dev/sda3ubuntu-vg lvm2 a--
页: [1]
查看完整版本: LVM(Logical Volume Manager)