一、LVM介绍
在我们管理Linux磁盘的时候,通常会遇到这么一种情况。在最初规划Linux的磁盘的时候,我们给某个分区划分了一定量的磁盘空间,使用一段时间后,发现我们规划的磁盘空间不足了,这个时候怎么办?我们肯定要想办法给扩容呀,找一块磁盘分区,格式化,然后挂载,然后把数据拷贝到新加的磁盘上,然后把原有的磁盘卸载掉,通常我们是这样做的。这样一来虽然达到了扩容的目的,但是费时费力;如果第二次扩容,空间还是不够,我们又要重复上面的步骤;如果分配的磁盘空间过大,使用了很久才发现它根本用不到那么多空间,这个时候是不是又要调整呀,听着就感觉很繁琐。这时候你肯定要想有没有那种可挂载上一块盘,我们利用某种管理手段,可以在线的调整磁盘的空间,这样有节约了时间,也可实现弹性管理磁盘。对,lvm就有这样的功能。
LVM(logical volume manager)逻辑卷管理,它可以弹性的管理文件系统的容量,LVM的工作原理是将一个或多个底层块设备组织成一个逻辑设备,这个逻辑设备可以在未来新增或移除其他的分区到这个逻辑设备中,从而实现了弹性管理磁盘。

PV:物理卷(physical volume);物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如raid),是LVM的基本储存逻辑块,但和基本的物理存储介质(如分区,磁盘等)比较,却包含有与LVM相关的管理参数
VG:卷组(volume group)LVM卷组类似于非LVM系统中的物理硬盘,尤其物理卷组成,可以在卷组上创建一个或多个LVM分区(逻辑卷),LVM卷组由一个或多个物理卷组成
LV:逻辑卷(logical volume)LVM的逻辑卷类似非LVM系统中的硬盘分区,在逻辑卷之上可以创建文件系统。
LVM就是将一些零碎的磁盘分区(PV)合并成一个较大的磁盘(VG),然后在根据需要对这个较大的磁盘(VG)进行划分成不同的大小分区(LV),这些小的分区(LV)是可以动态的扩展和缩小的,这就是LVM的逻辑卷管理过程。所谓LVM的大磁盘就是将许多PV整合成的VG,那么这个大磁盘最大可达到多少容量呢?我们知道在文件系统里面数据存储的最小单位是块(block),不过在LVM当中数据储存的最小单位是PE,PE的的作用同block的作用一样,需要注意到是LVM默认使用的PE大小是4M,而LVM的VG最多能包含65534个PE,也就是说默认LVM的VG会有4×65534M/1024M=256G,所以PE的大小决定了VG的最大容量,当然我们在创建VG的时候可指定PE大小,从而来决定VG的大小。
二、LVM实现
1)前期准备几块硬盘或者分区来模拟PV
[root@test-centos7-node1 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 2G 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 46G 0 part /
sdb 8:16 0 50G 0 disk
sr0 11:0 1 792M 0 rom
[root@test-centos7-node1 ~]# 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 0x6180123f.
Command (m for help): p
Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0x6180123f
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): p
Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0x6180123f
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
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (20973568-104857599, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-104857599, default 104857599): +10G
Partition 2 of type Linux and of size 10 GiB is set
Command (m for help): t
Partition number (1,2, default 2):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (41945088-104857599, default 41945088):
Using default value 41945088
Last sector, +sectors or +size{K,M,G} (41945088-104857599, default 104857599): +10G
Partition 3 of type Linux and of size 10 GiB is set
Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0x6180123f
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
/dev/sdb2 20973568 41945087 10485760 8e Linux LVM
/dev/sdb3 41945088 62916607 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@test-centos7-node1 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 2G 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 46G 0 part /
sdb 8:16 0 50G 0 disk
├─sdb1 8:17 0 10G 0 part
├─sdb2 8:18 0 10G 0 part
└─sdb3 8:19 0 10G 0 part
sr0 11:0 1 792M 0 rom
[root@test-centos7-node1 ~]#
说明:这里是用的分区来模拟硬盘当作PV,这里需要注意一点,就是我们分区完保存后,可能存在不同不到问题,就是我们分区完用lsblk看不到分区,可用 partprobe来同步分区信息。centos6 使用partx -a /dev/sdb来增加分区同步到内存,如果是删除某个分区,就需要执行partx -d -nr # /dev/sdb 此命令表示从/dev/sdb的分区信息中删除sdb#分区信息,并同步到内存中。这里还需要注意一点的是我们分区的类型要选8e 表示Linux LVM
2)装lvm2这个包
[root@test-centos7-node1 ~]# lvs
-bash: lvs: command not found
[root@test-centos7-node1 ~]# yum info lvm2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
Name : lvm2
Arch : x86_64
Epoch : 7
Version : 2.02.185
Release : 2.el7_7.2
Size : 1.3 M
Repo : updates/7/x86_64
Summary : Userland logical volume management tools
URL : http://sources.redhat.com/lvm2
License : GPLv2
Description : LVM2 includes all of the support for handling read/write operations on
: physical volumes (hard disks, RAID-Systems, magneto optical, etc.,
: multiple devices (MD), see mdadm(8) or even loop devices, see
: losetup(8)), creating volume groups (kind of virtual disks) from one
: or more physical volumes and creating one or more logical volumes
: (kind of logical partitions) in volume groups.
[root@test-centos7-node1 ~]# yum install lvm2 -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running

