一、LVM简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制。LVM将一个或多个磁盘分区(PV)虚拟为一个卷组(VG),相当于一个大的硬盘,我们可以在上面划分一些逻辑卷(LV)。当卷组的空间不够使用时,可以将新的磁盘分区加入进来。我们还可以从卷组剩余空间上划分一些空间给空间不够用的逻辑卷使用。
LVM模型如下图:
物理硬盘(虚拟硬盘)---新建8e格式分区---创建物理卷PV--多个PV合成一个组VG---在VG基础上创建逻辑卷LV
二、LVM添加硬盘和扩容
测试环境:CentOS7 64位(KVM虚拟机)
LVM版本:lvm2-2.02.105-14.el7.x86_64
1、使用 fdisk -l 查看到这块新盘为/dev/sda:
# fdisk /dev/sda
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): 3
First sector (62914560-211812351, default 62914560): 回车
Using default value 62914560
Last sector, +sectors or +size{K,M,G} (62914560-211812351, default 211812351): 回车
Using default value 211812351
Partition 3 of type Linux and of size 71 GiB is set
Command (m for help): t Partition number (1-3, default 3): 3 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): w The partition table has been altered!
# partprobe
使用 fdisk 命令对新盘进行分区,这里建立了一个主分区/dev/sda,大小25GB,最后使用 partprobe 命令重新读取分区表:
3、创建物理卷(PV)
使用 pvcreate 命令创建物理卷,pvdisplay 查看物理卷信息:
[root@iotApp&iagri-44 ~]# pvcreate /dev/sda4 Physical volume "/dev/sda4" successfully created [root@iotApp&iagri-44 ~]# pvscan PV /dev/sda2 VG centos lvm2 [49.80 GiB / 0 free] PV /dev/sda3 VG centos lvm2 [5.00 GiB / 5.00 GiB free] PV /dev/sda4 lvm2 [25.00 GiB] Total: 3 [79.80 GiB] / in use: 2 [54.80 GiB] / in no VG: 1 [25.00 GiB]
4、将PV加入卷组(VG)
使用 vgdisplay 查看卷组信息,下图显示卷组名为centos,空闲大小为0:
[root@iotApp&iagri-44 ~]# vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 54.80 GiB PE Size 4.00 MiB Total PE 14028 Alloc PE / Size 12749 / 49.80 GiB Free PE / Size 1279 / 5.00 GiB VG UUID mMcfc3-2mj2-cFk6-uMNY-FWQl-cXZ5-ENEEXX
使用 vgextend 命令把/dev/vdb1加入到centos:
[root@iotApp&iagri-44 ~]# vgextend centos /dev/sda4 Volume group "centos" successfully extended [root@iotApp&iagri-44 ~]# vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 5 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 3 Act PV 3 VG Size 79.79 GiB PE Size 4.00 MiB Total PE 20427 Alloc PE / Size 12749 / 49.80 GiB Free PE / Size 7678 / 29.99 GiB VG UUID mMcfc3-2mj2-cFk6-uMNY-FWQl-cXZ5-ENEEXX
5、创建逻辑卷(LV)&扩容
创建逻辑卷用lvcreate
使用 lvextend 命令进行逻辑卷扩容。我把所有剩余空间都分配给了newlv,增大到了25GB,
[root@iotApp&iagri-44 ~]# lvextend -l +7678 /dev/centos/root Size of logical volume centos/root changed from 47.94 GiB (12272 extents) to 77.93 GiB (19950 extents). Logical volume root successfully resized [root@iotApp&iagri-44 ~]# lvdisplay --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID Zep8rM-0AR5-RE0m-zj2n-Nc8S-VrL5-W2Zjzx LV Write Access read/write LV Creation host, time localhost, 2016-10-12 03:54:21 -0400 LV Status available # open 1 LV Size 77.93 GiB Current LE 19950 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
使用 xfs_growfs 命令在线调整xfs格式文件系统大小(CentOS6使用resize2fs):
[root@iotApp&iagri-44 ~]# xfs_growfs /dev/mapper/centos-root meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=3141632 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=12566528, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=6136, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 12566528 to 20428800
最后我们看到逻辑卷分区已经动态扩容了25GB:
[root@iotApp&iagri-44 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 78G 924M 78G 2% / devtmpfs 2.9G 0 2.9G 0% /dev tmpfs 2.9G 0 2.9G 0% /dev/shm tmpfs 2.9G 8.4M 2.9G 1% /run tmpfs 2.9G 0 2.9G 0% /sys/fs/cgroup /dev/sda1 197M 105M 93M 54% /boot