LVM
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 lvm yum install -y lvm2*
LVM的创建与管理
1、创建物理分区,通过fdisk创建类型为LVM的分区
fdisk -l /dev/sdb /dev/sdc 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: 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 LVM Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 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: 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/sdc3
pvs
pvdisplay
3、创建卷组
vgcreate myvg1 /dev/sdb1 /dev/sdb2 /dev/sdc3 Physical volume "/dev/sdb2" successfully created. Volume group "myvg1" successfully created vgcreate myvg2 /dev/sdc1 /dev/sdc2 /dev/sdb3 Volume group "myvg2" successfully created
vgs
vgdisplay
4、激活卷组
vgchange -a y myvg1 vgchange -a y myvg2
vgchange -a n myvg1(停用)
vgchange -a n myvg1(停用)
5、创建逻辑卷
lvcreate -L 4G -n mylv1 myvg1 lvcreate -l 1533 -n mylv12 myvg1 lvcreate -l 4861 -n mylv2 myvg2
lvs
lvdisplay
6、格式化逻辑卷,创建文件系统
mkfs.xfs /dev/myvg1/mylv1 mkfs.xfs /dev/myvg1/mylv12 mkfs.xfs /dev/myvg2/mylv2
7、建立挂载目录,挂载卷
mkdir /mylv1 mkdir /mylv12 mkdir /mylv2 mount /dev/myvg1/mylv1 /mylv1 mount /dev/myvg1/mylv12 /mylv12 mount /dev/myvg2/mylv2 /mylv2 df -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/sdd Disk /dev/sdd: 26.8 GB, 26843545600 bytes, 52428800 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: 0xadcef81f Device Boot Start End Blocks Id System /dev/sdd1 2048 4196351 2097152 8e Linux LVM pvcreate /dev/sdd1 vgextend myvg2 /dev/sdd1 vgdisplay myvg2 | grep Free Free PE / Size 511 / <2.00 GiB
9、修改逻辑卷大小
增加逻辑卷空间 1、利用lvextend扩展逻辑卷大小 lvextend -L +2G /dev/myvg1/mylv12 2、利用resize2fs文件系统大小 resize2fs /dev/myvg1/mylv12 32G 减小逻辑卷空间 1、卸载已经挂载的逻辑卷分区 umount /dev/myvg1/mylv12 2、利用resize2fs修改文件系统大小 resize2fs /dev/myvg1/mylv12 20G 3、利用lvreduce修改逻辑卷大小 lvreduce -L -12G /dev/myvg1/mylv12 4、挂载修改后的逻辑卷分区 mount /dev/myvg1/mylv12 /mylv12
10、删除逻辑卷,物理卷,卷组
umount /dev/myvg1/mylv1 lvremove /dev/myvg1/mylv1 umount /dev/myvg1/mylv12 lvremove /dev/myvg1/mylv12
vgreduce myvg1 /dev/sdb1 vgreduce myvg1 /dev/sdb2
pvremove /dev/sdb1 pvremove /dev/sdb2 vgremove myvg1 pvremove /dev/sdc3