LVM 是 Logical Volume Manager(逻辑卷管理器)的简写。
LVM 将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续加入其它的硬盘分区,以此实现磁盘空间的动态管理,相对于普通的磁盘分区具有更大的灵活性。
与传统的磁盘与分区相比,LVM 为计算机提供了更高层次的磁盘存储。它使系统管理员可以更方便的为用户分配存储空间。在 LVM 管理下的存储卷可以按需要随时改变大小与移除(可能需对文件系统工具进行升级)。
LVM 具有分层结构,涉及物理卷 PV,卷组 VG,逻辑卷 LV 等概念:
- 物理卷 PV(Physical Volume):硬盘分区后但还未格式化为文件系统,可使用 pvcreate 命令将分区创建为 PV,其对应的 system ID 为 8e 即 LVM 格式所对应的系统标识符。
- 卷组 VG(Volume Group): 将多个 PV 组合起来,使用 vgcreate 创建卷组,这样卷组就可以包含多个 PV,相当于重新组合多个分区后所得到的磁盘。虽然 VG 是组合了多个 PV,但创建 VG 时会将 VG 所有的空间根据指定的 PE 大小划分为多个 PE,在 LVM 模式中存储都是以 PE 为单元,类似于文件系统中的 BLOCK。
- 物理块 PE(Physical Extent):PE 是 VG 的存储单元,实际的数据都是存储在 PE 中
- 逻辑卷 LV(Logical Volume):VG 相当于组合的多个硬盘,则 LV 相当于分区,只不过该分区是通过 VG 进行划分的。VG 中存在很多 PE,可以指定将多少个 PE 划分给一个 LV,也可以直接指定大小来划分。划分为 LV 后就相当于划分了分区,仅需要对 LV 进行格式化文件系统即可。
- 逻辑块 LE(Logical Extent): PE 是物理存储单元,而 LE 则是逻辑存储单元,即 LE 为 LV 中的逻辑存储单元,与 PE 大小一致。从 VG 中划分 LV,实际上就是从 VG 中划分 PE,而划分 LV 后称之为 LE,而不是 PE 了。上述概念的关系如下图所示:
在滴滴云控制台向测试 VM 添加两块云盘,登录 VM 后查看云盘状态如下,VDA 为 VM 自带磁盘,VDB,VDC 为新添加的云盘,下面使用 LVM 对新增云盘进行管理:
fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b2f10
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 41943006 20970479+ 83 Linux
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x119da33d
Device Boot Start End Blocks Id System
Disk /dev/vdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
- 在VDB,VDC 上创建分区,并改变分区类型为 8e
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x119da33d
Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 8e Linux LVM
Disk /dev/vdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xda0158ee
Device Boot Start End Blocks Id System
/dev/vdc1 2048 41943039 20970496 8e Linux LVM
- 准备物理卷 PV
[root@10-254-46-127 home]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.
[root@10-254-46-127 home]# pvcreate /dev/vdc1
Physical volume "/dev/vdc1" successfully created.
[root@10-254-46-127 home]# pvdisplay
"/dev/vdb1" is a new physical volume of "<20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vdb1
VG Name
PV Size <20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ilx3U6-jN8d-p31W-Jnwn-bO0D-gDUy-Ywy0v9
"/dev/vdc1" is a new physical volume of "<20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vdc1
VG Name
PV Size <20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 4Aiksq-NTwH-ZzPP-NPBw-ELjk-ITV7-nRDXuv
- 准备卷组 VG
[root@10-254-46-127 home]# vgcreate vg1 /dev/vdb1 /dev/vdc1
Volume group "vg1" successfully created
[root@10-254-46-127 home]# vgdisplay
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0
Free PE / Size 10238 / 39.99 GiB
VG UUID lUPgSx-CKcw-4juK-IhMP-qiRb-muVK-c9xKL9
-
创建逻辑卷 LV
向 LV 存储数据时,主要有线性模式和条带模式两种模式。
线性模式(Linear) 是先将数据存储在属于同一个 PV 的 PE,然后再向下一个 PV 中的 PE 。
条带模式(Striped) 是指将一份数据拆分为多份,分别写入该 LV 对应的每个 PV 中,因此读写性能会优于线性模式。
默认情况下创建的 LV 采用线性模式,如果有需要,可以选择条带模式。
[root@10-254-46-127 home]# lvcreate -L 30G -n lv1 vg1
WARNING: ext4 signature detected on /dev/vg1/lv1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vg1/lv1.
Logical volume "lv1" created.
[root@10-254-46-127 home]#
[root@10-254-46-127 home]# lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID TKR12d-lntX-TSl6-RX2m-7mjD-r7we-AxO0Ga
LV Write Access read/write
LV Creation host, time 10-254-46-127, 2018-12-30 21:19:11 +0800
LV Status available
# open 0
LV Size 30.00 GiB
Current LE 7680
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 252:0
- 创建逻辑卷并格式化,挂载
[root@10-254-46-127 home]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1966080 inodes, 7864320 blocks
393216 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2155872256
240 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@10-254-46-127 home]#
[root@10-254-46-127 home]# mount /dev/vg1/lv1 /lvm-mount/
[root@10-254-46-127 home]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.9G 19G 10% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 57M 440M 12% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/1000
/dev/mapper/vg1-lv1 30G 45M 28G 1% /lvm-mount
-
扩展一个 LV
调整逻辑卷大小的功能是 LVM 最有用的功能。这个部分会讨论我们怎么样扩展一个存在的逻辑卷。
下面,我们将会扩展先前创建的逻辑卷 ‘LV1’ 扩大到 200MB。注意,调整逻辑卷大小之后,也需要对文件系统调整大小进行匹配。这个额外的步骤各不相同,取决于创建文件系统的类型。
在本文中,我们使用 ‘LV1’ 创建了 Ext4 类型的文件系统,所以这里的操作是针对 Ext4 文件系统的。
[root@10-254-46-127 home]# umount /lvm-mount/
[root@10-254-46-127 home]#
[root@10-254-46-127 home]# lvresize -L 35G /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 30.00 GiB (7680 extents) to 35.00 GiB (8960 extents).
Logical volume vg1/lv1 successfully resized.
[root@10-254-46-127 home]#
[root@10-254-46-127 home]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg1/lv1: 11/1966080 files (0.0% non-contiguous), 167453/7864320 blocks
[root@10-254-46-127 home]#
[root@10-254-46-127 home]#
[root@10-254-46-127 home]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 9175040 (4k) blocks.
The filesystem on /dev/vg1/lv1 is now 9175040 blocks long.
[root@10-254-46-127 home]# lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID TKR12d-lntX-TSl6-RX2m-7mjD-r7we-AxO0Ga
LV Write Access read/write
LV Creation host, time 10-254-46-127, 2018-12-30 21:19:11 +0800
LV Status available
# open 0
LV Size 35.00 GiB
Current LE 8960
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 252:0
[root@10-254-46-127 home]# mount /dev/vg1/lv1 /lvm-mount/
[root@10-254-46-127 home]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.9G 19G 10% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 57M 440M 12% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/1000
/dev/mapper/vg1-lv1 35G 48M 33G 1% /lvm-mount