Centos7中逻辑卷(LVM)的简单管理
我们在安装linux系统或者添加新的硬盘时需要正确的评估各个分区当前所需的大小,甚至硬盘所需空间的变化趋势,以分配合适大小的硬盘空间满足生产需要。然而这个过程有时是很难准确判断的,往往需要分配比预期超出许多的空间以避免硬盘分区空间耗尽。LVM技术可以很方便的解决这个问题。
LVM是Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对硬盘分区进行管理的一种机制。LVM是建立在硬盘和分区上的逻辑层,它允许跨硬盘、分区创建逻辑卷并创建文件系统,可以很方便执行增加、扩展、删除等操作。本文就LVM的创建、扩展以及移除做个简单介绍。
1 创建物理卷
物理卷(Physical Volume,PV):就是指硬盘分区,也可以是整个硬盘或已创建的软RAID,是LVM的基本存储设备。在创建逻辑卷之前需要先创建物理卷,然后将多个物理卷组合创建卷组,最后在卷组的基础上创建逻辑卷。
将空白硬盘(或者分区)加入主机,系统识别到新的硬盘sdb后,使用pvcreate
对硬盘创建PV:
代码实现:
[root@Centos7T ~]#lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
[root@Centos7T ~]#fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.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
Building a new DOS disklabel with disk identifier 0x6200a0ac.
Command (m for help): p
Disk /dev/sdb: 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: 0x6200a0ac
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e # 修改系统ID为 Linux LVM
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 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: 0x6200a0ac
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@Centos7T ~]#pvcreate /dev/sdb1 # 创建物理卷
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@Centos7T ~]#pvdisplay
"/dev/sdb1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID xT6409-MGxH-M7Dy-TvZz-Fl55-qarO-Bb1n3U
需要注意的是,如果针对空闲分区创建PV,需要重新指定分区的系统ID。在上面的代码中Hex code (type L to list all codes): 8e
指定分区的系统ID为Linux LVM。空白为分区磁盘直接使用pvcreate /dev/sdb
命令,系统ID自动转成Linux LVM。
2 创建卷组
卷组(Volume Group,VG):是由一个或多个物理卷所组成的存储池,在卷组上能创建一个或多个逻辑卷。现在使用vgcreate
命令对上文创建的PV(单个物理卷)创建卷组。
代码实现:
[root@Centos7T ~]#vgcreate -s 10M vg0 /dev/sdb1 # 创建卷组vg0,指定物理块PE大小10M,并将pv:/dev/sdb1加入vg0.
Volume group "vg0" successfully created
[root@Centos7T ~]#vgdisplay # 查看所有卷组的信息.后面接具体卷组名,可查看指定卷组信息
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 1
Metadata Sequen