LVM逻辑卷
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVM逻辑卷相关的知识,希望对你有一定的参考价值。
LVM
逻辑卷(Logical Volume)是由逻辑磁盘形成的虚拟盘,也可称为磁盘分区。
用途:解决分区空间不够用或者分区空间绰绰有余的情况。
逻辑卷的工作原理:逻辑卷(lv)是将几个磁盘分区或者块设备组织起来形成一个大的扩展分区(卷组),该扩展分区不能直接用,需要将其划分成逻辑卷(lv)才能使用,lv可以格式化成不同的文件系统,挂载后直接使用。 lv的扩展和缩减是不会影响原有数据的,但逻辑卷缩减的风险大于逻辑卷扩展的风险。逻辑卷可以支持快照功能。
创建LVM
1.首先创建两个分区/硬盘来,下例为sdb1分区和sdc1分区
[[email protected] ~]#fdisk -l /dev/sdb /dev/sdc
Disk /dev/sdb: 161.1 GB, 161061273600 bytes, 314572800 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: 0x90c3db64
Device Boot Start End Blocks Id System
/dev/sdb1 2048 41945087 20971520 8e Linux LVM
Disk /dev/sdc: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000c72ff
Device Boot Start End Blocks Id System
/dev/sdc1 2048 41945087 20971520 8e Linux LVM
2.创建PV 使用pvcreate/dev/DEVICE把两个分区创建为PV,用pvs/pvdisplay查看PV信息。
[[email protected] ~]#pvcreate /dev/sdb1 /dev/sdc1
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc1" successfully created.
[[email protected] ~]#pvdisplay
"/dev/sdc1" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID gIYSbC-QrkI-MRto-xMdP-PJoX-vq40-0JUhSK
"/dev/sdb1" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ONZUYP-4RfT-HDny-c2QD-yi8R-ecsF-n76KkI
3.创建VG vgcreate "vgname" /dev/sd 创建VG,加-s 可以指定PE大小,vgs/vgdisplay查看VG信息
[[email protected] ~]#vgcreate vg1 /dev/sdb1 /dev/sdc1
Volume group "vg1" successfully created
[[email protected] ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg1 2 0 0 wz--n- 39.99g 39.99g
4.创建LV lvcreate常见选项如下
-n 指定LV的名称
-L 指定lv的大小
[[email protected] ~]#lvcreate -n lv_mysql -L 15G vg1
Logical volume "lv_mysql" created.
[[email protected] ~]#lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv_mysql
LV Name lv_mysql
VG Name vg1
LV UUID jycUPI-cTfZ-n1Pe-aHA7-g16p-2PMH-PkAA1F
LV Write Access read/write
LV Creation host, time centos7.5.localdomain, 2018-10-23 15:36:57 +0800
LV Status available
# open 0
LV Size 15.00 GiB
Current LE 3840
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
5.设置文件系统,挂载。
[[email protected] ~]#mkfs.ext4 /dev/vg1/lv_mysql
[[email protected] ~]#mount /dev/vg1/lv_mysql /mnt
删除LV VG PV命令
删除逻辑卷
lvremove/dev/VG_NAME/LV_NAME
删除卷组
先做pvmove,再做vgremove
删除pv
pvremove/dev/DEVICE
扩展PVM
1.如果正在使用的某个lv空间快要满的时候,可以给lv增加空前,前提是VG里有空闲的空间。lvextend -l 可以指定pe大小扩展空间,可以用百分百表示,-L直接写单位。
[[email protected] ~]#lvextend -l +50%FREE /dev/vg1/lv_mysql
[[email protected] ~]#lvextend -L +1G /dev/vg1/lv_mysql
2.指定完需要增加空间的大小之后用xfs_growfs更新,etx系列的文件系统用resize2fs
[[email protected] ~]#xfs_growfs /dev/vg1/lv_mysql
meta-data=/dev/mapper/vg1-lv_mysql isize=512 agcount=4, agsize=983040 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=3932160, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 3932160 to 10483712
为卷组扩展空间
1.如果卷组的的空间不够了也可以扩展,先看下卷组空间可用空间已经为0
[[email protected] ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg1 2 1 0 wz--n- 39.99g 0
2.先找一块硬盘,把他设置为PV,pvs显示sde硬盘没有加入到任何卷组。
[[email protected] ~]#pvcreate /dev/sde
WARNING: dos signature detected on /dev/sde at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sde.
Physical volume "/dev/sde" successfully created.
[[email protected] ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 vg1 lvm2 a-- <20.00g 0
/dev/sdc1 vg1 lvm2 a-- <20.00g 0
/dev/sde lvm2 --- 50.00g 50.00g
3.把硬盘加入到指定的卷组vgextend
[[email protected] ~]#vgextend vg1 /dev/sde
Volume group "vg1" successfully extended
[[email protected] ~]#vgs
VG #PV #LV #SN Attr VSize VFree
vg1 3 1 0 wz--n- <89.99g <50.00g
4.卷组扩展完毕之后就可以再给卷组里其他逻辑卷扩展空间了
lvextend -r -L 100G /dev/vg1/lv_mysql
5.删除卷组里的pv设备 vgreduce
[[email protected] ~]#vgreduce vg1 /dev/sde
Removed "/dev/sde" from volume group "vg1"
缩减逻辑卷
只有在ext系列上才可以缩减逻辑卷,在xfs的文件系统上不支持此操作。
操作步骤
1,取消挂载
2,先缩减文件系统
3,再缩减逻辑卷大小
(1)卸载逻辑卷
(2)缩减文件系统为20Gresize2fs
[[email protected] ~]#resize2fs /dev/vg1/lv_mysql 20G
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: New size smaller than minimum (64422)
3.缩减逻辑卷lvreduce
[[email protected] ~]#lvreduce -L 20G /dev/vg1/lv_mysql
WARNING: Reducing active logical volume to 20.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv_mysql? [y/n]: y
Size of logical volume vg1/lv_mysql changed from 39.99 GiB (10238 extents) to 20.00 GiB (5120 extents).
Logical volume vg1/lv_mysql successfully resized.
4.重新挂载逻辑卷
逻辑卷命令
lvextend -r -L :扩展逻辑卷空间使用
lvremove :删除逻辑卷
resize2fs :缩减文件系统大小
lvreduce -L: 缩减逻辑卷大小
vgextend : 扩展卷组,把新的pv加入到卷组
resize2fs /dev/vg/ ext4
xfs_growfs /dev/vg/ xfs
vgreduce :删除卷组里的pv设备
vgrename : 卷组改名
pvmove :可以把pv上使用的pe迁移到相同卷组的其他pv上
pcscan : 扫描
迁移逻辑卷
[[email protected] ~]#pvmove /dev/sdc1
/dev/sdc1: Moved: 100.00%
迁移vg1卷组,查看下卷组下的pv
[[email protected] ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg1
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 2559
Allocated PE 2560
PV UUID ONZUYP-4RfT-HDny-c2QD-yi8R-ecsF-n76KkI
1.首先取消挂载
2.把卷组设置为禁用状态vgchange–an 激活为-ay
[[email protected] ~]#vgchange -an vg1
0 logical volume(s) in volume group "vg1" now active
3.把卷组下的逻辑卷设置为导出状态vgexport vg1 导入为vgimport vg
[[email protected] ~]#vgexport vg1
Volume group "vg1" successfully exported
[[email protected] ~]#lvdisplay
Volume group vg1 is exported
4.设置为导出状态之后,就可以把磁盘拔下来插入到其他服务器上面,然后再把导出状态改为导入,把禁用状态激活即可
LVM快照
快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据改动了,则原始数据会被移动到快照区,没有改动的区域则由快照区和文件系统共享。
建立快照的卷大小只需要原始逻辑卷的15%~20%就够了,也可以使用lvextend放大快照
快照只会保留原有的文件,新建的文件没有
创建一个快照
基于ext系统
也是用lvcreate; -s指明是一个逻辑卷的快照
[[email protected] lv]#lvcreate -n kuaizhao -s -L 2G /dev/vg1/lv_mysql
Logical volume "kuaizhao" created.
[email protected] lv]#lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv_mysql
LV Name lv_mysql
VG Name vg1
LV UUID jycUPI-cTfZ-n1Pe-aHA7-g16p-2PMH-PkAA1F
LV Write Access read/write
LV Creation host, time centos7.5.localdomain, 2018-10-23 15:36:57 +0800
LV snapshot status source of
kuaizhao [active]
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/vg1/kuaizhao
LV Name kuaizhao
VG Name vg1
LV UUID dk8cT0-Ykl5-p3CW-LGsc-KxIA-3oMl-iESK0J
LV Write Access read/write
LV Creation host, time centos7.5.localdomain, 2018-10-23 17:51:11 +0800
LV snapshot status active destination for lv_mysql
LV Status available
# open 0
LV Size 10.00 GiB
Current LE 2560
COW-table size 2.00 GiB
COW-table LE 512
Allocated to snapshot 0.01%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
还原快照
还原快照先取消挂载
还原快照用lvconvert--merge +快照路径命令可以把快照还原到原来的逻辑卷
[[email protected] mnt]#lvconvert --merge /dev/vg1/kuaizhao
Merging of volume vg1/kuaizhao started.
vg1/lv_mysql: Merged: 100.00%
基于xfs
xfs系统里创建好快照之后,在挂载的时候要使用mount -o nouuid不检查uuid,因为创建的快照UUID和逻辑卷的UUID是相同的
删除快照
lvremove /dev/vg1/kuaizhao
以上是关于LVM逻辑卷的主要内容,如果未能解决你的问题,请参考以下文章