详解 LVM

LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现,LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。

我的新书《LangChain编程从入门到实践》 已经开售!推荐正在学习AI应用开发的朋友购买阅读!
LangChain编程从入门到实践

Linux设备命名约定

Linux启动时会给每个设备命名,然后依次提供位置和分区。

1
2
3
4
5
6
7
8
9
10
dev/fd0 - 第一个软驱。
dev/fd1 - 第二个软驱。
dev/sda - 第一个SCSI磁盘SCSI ID地址位置。
dev/sdb - 第二个SCSI磁盘地址位置,以此类推。
dev/scd0 or /dev/sr0 - 第一个SCSI CD-ROM。
dev/hda - IDE主要控制器上的主磁盘。
dev/hdb - IDE主要控制器上的从磁盘。
dev/mmcblk0 - PCMCIA上的SDHC卡。特殊设备命名。
dev/sdb - 针对SCSI仿真的USB闪存盘。但是,内核与多个驱动程序并行启动。这并不意味着您的sda或sdb驱动器是USB驱动,但USB模块将与驱动器1同时启动,并且同时发送消息。
dev/hdc和/dev/hdd - 分别为次要控制器的主磁盘和从磁盘。较新的IDE控制器具有两个通道充当两个控制器。

对主启动记录 (MBR) 驱动器进行分区时,最多可以包含 4 个主要分区或者主要分区和逻辑分区的组合,GUID分区表(GPT)驱动器也推荐这么设置。下面是我电脑上的分区,没有sda4,表示主要分区(sda1sda3)和逻辑分区(sda5sda10)的组合方式

1
2
3
4
5
6
7
8
9
10
11
sudo fdisk -l
设备 启动 Start 末尾 扇区 Size Id 类型
/dev/sda1 * 63 84985927 84985865 40.5G 7 HPFS/NTFS/exFAT
/dev/sda2 124051454 1465149167 1341097714 639.5G f W95 扩展 (LBA)
/dev/sda3 84987904 124049407 39061504 18.6G 83 Linux
/dev/sda5 167772296 601882767 434110472 207G 7 HPFS/NTFS/exFAT
/dev/sda6 601882832 1033896151 432013320 206G 7 HPFS/NTFS/exFAT
/dev/sda7 1033896216 1465149167 431252952 205.7G 7 HPFS/NTFS/exFAT
/dev/sda8 124051456 143581183 19529728 9.3G 82 Linux 交换 / Solaris
/dev/sda9 143583232 144558079 974848 476M 83 Linux
/dev/sda10 144560128 167766015 23205888 11.1G 83 Linux

LVM硬盘管理

LVM基本术语

LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。

物理卷(Physical Volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。

卷组(Volume Group):LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。

逻辑卷(Logical Volume):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。

PE(physical extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。

LE(logical extent):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。

关系

PV:是物理的磁盘分区

VG:LVM中的物理的磁盘分区,可以将VG理解为一个仓库或者是几个大的硬盘。

LV:也就是从VG中划分的逻辑分区

201208221004474075.jpg

LVM扩容

开始分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fdisk /dev/sdb
# p是打印当前磁盘所有分区
Command (m for help): p
# n是新建分区
Command (m for help): n
# 新建主分区
Command action
e extended
p primary partition (1-4)
p
# 给主分区从1开始编号
Partition number (1-4): 1
# 默认从1开始,也可以手动输入1
First cylinder (1-130541, default 1):
Using default value 1
# 默认把磁盘所有空间分配到此分区。这里支持K,M,G,如果想给此分区分配100M空间,就输入+100M;如果要分配100G那么就输入+100G
Last cylinder, +cylinders or +size{K,M,G} (1-130541, default 130541):
Using default value 130541
# w为写入分区信息并保存,然后退出
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

查看刚刚分区的信息:/dev/sdb1:fdisk -l

格式化分区:mkfs.ext4 /dev/sdb1

挂载分区:

1
2
3
mkdir /backup
mount /dev/sdb1 /backup/
df -hT

挂载磁盘(永久挂载):通过vi编辑器编辑/etc/fstab文件

回到问题

“KVM虚拟机该用LVM分区吗?”

“It depends.”
If you are on an environment that you control (vmware or kvm or whatever), and can make your own decisions about disk performance QoS, then I’d recommend not using LVM inside your VMs. It doesn’t buy you much flexibility that you couldn’t get at the hypervisor level.
Remember, the hypervisor is already effectively performing these tasks. If you want to be able to arbitrarily resize file systems (a fine idea), just create a separate virtual disk for each filesystem.
One thing you might think of as you go down this road. You don’t even necessarily need to put partitions on your virtual disks this way. For example, you can create a virtual disk for /home; it is /dev/vdc inside your vm. When creating the filesystem, just do something like mke2fs -j /dev/vdc instead of specifying a partition.
This is a fine idea, but…most tools (and other admins who come after you) will expect to see partitions on every disk. I’d recommend just putting a single partition on the disk and be done with it. It does mean one more step when resizing the filesystem, though. And don’t forget to properly align your partitions - starting the first partition at 1MB is a good rule of thumb.
All that said - Doing this all at the hypervisor level means that you probably have to reboot the VM to resize partitions. Using LVM would allow you to hot-add a virtual disk (presuming your hypervisor/OS combination allows this), and expand the filesystem without a reboot. This is definitely a plus.

参考链接

有关硬盘和设备的Ubuntu Linux术语说明

Linux文件系统详解

Should LVM partitions be used in virtual machine images?

作者

莫尔索

发布于

2020-09-30

更新于

2024-05-19

许可协议

评论