LVM

发布日期:2019-04-16

LVM(Logical Volume Manager,逻辑卷管理器)是Linux下对磁盘分区进行管理的一种机制。

基本术语:

  PV(physical volume,物理卷)指硬盘分区或者从逻辑上看和硬盘分区类似的设备

  VG(volume group,卷组)类似于非LVM系统的物理硬盘,一个LVM卷组由一个或多个物理卷组成

  LV(logical volume,逻辑卷)类似于非LVM系统的硬盘分区,LV建立在VG上,可以在LV上建立文件系统

  PE(physical extent)PV中可以分配的最小存储单元,PE的大小可以指定,默认为4MB

  LE(logical extent)LV中可以分配的最小存储单元,在同一个卷组上,LE的大小和PE是一样的,且一一对应

  

安装:  

rpm -qa | grep lvmyum install -y lvm2*

 LVM的创建与管理

1、创建物理分区,通过fdisk创建类型为LVM的分区

fdisk -l /dev/sdb /dev/sdcDisk /dev/sdb: 21.5 GB 21474836480 bytes 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x5aea1fae Device Boot Start End Blocks Id System/dev/sdb1 2048 4196351 2097152 8e Linux LVM/dev/sdb2 4196352 12584959 4194304 8e Linux LVM/dev/sdb3 12584960 41943039 14679040 8e Linux LVMDisk /dev/sdc: 10.7 GB 10737418240 bytes 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xda670c21 Device Boot Start End Blocks Id System/dev/sdc1 2048 4196351 2097152 83 Linux/dev/sdc2 4196352 10487807 3145728 8e Linux LVM/dev/sdc3 10487808 18876415 4194304 8e Linux LVM

2、创建物理卷

pvcreate /dev/sdb1 /dev/db2 /dev/sdb3 /dev/sdc1 /dev/sdc2 /dev/sdc3pvspvdisplay

3、创建卷组

vgcreate myvg1 /dev/sdb1 /dev/sdb2 /dev/sdc3 Physical volume "/dev/sdb2" successfully created. Volume group "myvg1" successfully createdvgcreate myvg2 /dev/sdc1 /dev/sdc2 /dev/sdb3 Volume group "myvg2" successfully createdvgsvgdisplay

4、激活卷组

vgchange -a y myvg1vgchange -a y myvg2vgchange -a n myvg1(停用)vgchange -a n myvg1(停用)

5、创建逻辑卷

lvcreate -L 4G -n mylv1 myvg1lvcreate -l 1533 -n mylv12 myvg1lvcreate -l 4861 -n mylv2 myvg2lvslvdisplay

6、格式化逻辑卷,创建文件系统

mkfs.xfs /dev/myvg1/mylv1mkfs.xfs /dev/myvg1/mylv12mkfs.xfs /dev/myvg2/mylv2

7、建立挂载目录,挂载卷

mkdir /mylv1mkdir /mylv12mkdir /mylv2mount /dev/myvg1/mylv1 /mylv1mount /dev/myvg1/mylv12 /mylv12mount /dev/myvg2/mylv2 /mylv2df -h | grep mylv/dev/mapper/myvg1-mylv1 4.0G 33M 4.0G 1% /mylv1/dev/mapper/myvg1-mylv12 6.0G 33M 6.0G 1% /mylv12/dev/mapper/myvg2-mylv2 19G 33M 19G 1% /mylv2

 8、添加新的物理卷到卷组

fdisk -l /dev/sddDisk /dev/sdd: 26.8 GB 26843545600 bytes 52428800 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0xadcef81f Device Boot Start End Blocks Id System/dev/sdd1 2048 4196351 2097152 8e Linux LVMpvcreate /dev/sdd1vgextend myvg2 /dev/sdd1vgdisplay myvg2 | grep Free Free PE / Size 511 / <2.00 GiB

9、修改逻辑卷大小

增加逻辑卷空间1、利用lvextend扩展逻辑卷大小lvextend -L +2G /dev/myvg1/mylv122、利用resize2fs文件系统大小resize2fs /dev/myvg1/mylv12 32G减小逻辑卷空间1、卸载已经挂载的逻辑卷分区umount /dev/myvg1/mylv122、利用resize2fs修改文件系统大小resize2fs /dev/myvg1/mylv12 20G3、利用lvreduce修改逻辑卷大小lvreduce -L -12G /dev/myvg1/mylv124、挂载修改后的逻辑卷分区mount /dev/myvg1/mylv12 /mylv12

10、删除逻辑卷,物理卷,卷组

umount /dev/myvg1/mylv1lvremove /dev/myvg1/mylv1umount /dev/myvg1/mylv12lvremove /dev/myvg1/mylv12vgreduce myvg1 /dev/sdb1vgreduce myvg1 /dev/sdb2pvremove /dev/sdb1pvremove /dev/sdb2vgremove myvg1pvremove /dev/sdc3