LVM(Logical Volume Manager)逻辑卷管理,它是Linux环境下对磁盘和分区进行管理的一种机制。
普通的磁盘分区管理方式在分区划分好之后一般无法改变其大小(有风险)
LVM就是将物pv融合成一个或几个大的vg,按照我们的需求去划分vg的空间,由于是虚拟的存储池,所以划分空间时可以自由的调整大小
LVM重点在于可以弹性的调整文件系统(filesystem)的容量,而并非在于数据的存储效率及安全**
物理卷(PV,Physical Volume):由磁盘或分区转化而成
卷组(VG,Volume Group):将多个物理卷组合在一起组成了卷组,组成同一个卷组的可以是同一个硬盘的不同分区,也可以是不同硬盘上的不同分区,我们通常把卷组理解为一块硬盘。
逻辑卷(LV,Logical Volume):把卷组理解为硬盘的话,那么我们的逻辑卷则是硬盘上的分区,逻辑卷可以进行格式化,存储数据。
物理扩展(PE,Physical Extend):PE卷组的最小存储单元,PE所在的位置是VG卷组,即硬盘上,那么我们可以把PE理解为硬盘上的扇区,默认是4MB,可自由配置。
(1)将磁盘分区,或整块磁盘不分区
(2)将磁盘上的分区或整块盘建立成物理卷(PV)
(3)将创建好的物理卷组合成卷组(VG),通过添加或者删除一块物理卷,来实现卷组大小的调整。
(4)最后将卷组划分成逻辑卷(LV),逻辑卷也是可以随意调整大小的,逻辑卷相当于真正的分区,那么要使用必须进行格式化和挂载。
使用fdisk 来进行分区,分区的系统ID需要使用t选项修改为8e00
格式:pvcreate 磁盘名/分区名
分区:
整块硬盘:
格式:
pvs 是pvscan的简写
pvdisplay
pvdisplay 指定磁盘名/分区名
[root@xxx ~]# :pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdd1 lvm2 --- 10.00g 10.00g
/dev/sde lvm2 --- 20.00g 20.00g
[root@xxx ~]# :pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID CQu05U-dzdQ-QkTT-W4OR-EaIW-f7EV-cC9sJi
"/dev/sdd1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdd1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID wvT7tu-mSmu-OzjE-oNrQ-4ILN-S38J-hscKsx
"/dev/sde" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sde
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID fF8Umg-JrkA-NAMu-QTMI-ebds-NFTD-HS1osw
格式:
pvremove 磁盘名/分区名
[root@xxx ~]# :pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdd1 lvm2 --- 10.00g 10.00g
/dev/sde lvm2 --- 20.00g 20.00g
/dev/sdf lvm2 --- 20.00g 20.00g
[root@xxx ~]# :pvremove /dev/sdf
Labels on physical volume "/dev/sdf" successfully wiped.
You have new mail in /var/spool/mail/root
[root@xxx ~]# :pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdd1 lvm2 --- 10.00g 10.00g
/dev/sde lvm2 --- 20.00g 20.00g
格式:
vgcreate 选项 卷组名 磁盘名/分区名(可以写多个,空格分开)
-s 大小:指定VG的PE大小
[root@xxx ~]# :vgcreate -s 2M vg1 /dev/sdd1 /dev/sde
Volume group "vg1" successfully created
格式:
vgs 是vgscan的简写
vgdisplay
vgdisplay 指定磁盘名/分区名
[root@xxx ~]# :vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg1 2 0 0 wz--n- <30.00g <30.00g
[root@xxx ~]# :vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <19.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID d4wUf7-yU7O-0hdk-ohdD-4UME-TIBw-qQMQyr
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size <30.00 GiB
PE Size 2.00 MiB
Total PE 15358
Alloc PE / Size 0 / 0
Free PE / Size 15358 / <30.00 GiB
VG UUID 1U8tJu-cHBt-B8TV-Ao2j-t29G-ULc3-DlHB04
格式:
vgextend 卷组名 磁盘名/分区名
[root@xxx ~]# :vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg1 1 0 0 wz--n- <10.00g <10.00g
[root@xxx ~]# :vgextend vg1 /dev/sde
Volume group "vg1" successfully extended
You have new mail in /var/spool/mail/root
[root@xxx ~]# :vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg1 2 0 0 wz--n- <30.00g <30.00g
删除需谨慎
格式:
vgreduce 卷组名 物理卷名
[root@xxx ~]# :vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg1 2 0 0 wz--n- <30.00g <30.00g
[root@xxx ~]# :vgreduce vg1 /dev/sde
Removed "/dev/sde" from volume group "vg1"
You have new mail in /var/spool/mail/root
[root@xxx ~]# :vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg1 1 0 0 wz--n- <10.00g <10.00g
格式:
vgremove 卷组名
看到VG一列,卷组被删除掉
[root@xxx ~]# :pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdd1 vg1 lvm2 --- 10.00g 10.00g
/dev/sde vg1 lvm2 --- 20.00g 20.00g
[root@xxx ~]# :vgremove vg1
Volume group "vg1" successfully removed
[root@xxx ~]# :pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdd1 lvm2 --- 10.00g 10.00g
/dev/sde lvm2 --- 20.00g 20.00g
格式:
lvcreate 参数 -n 逻辑卷名 卷组名
参数:
-L 容量:指定创建的逻辑卷大小,单位:MB,GB,TB等
-l(小) 个数:按照PE个数来指定逻辑卷大小
-n 逻辑卷名:指定逻辑卷叫什么名
[root@xxx ~]# :lvcreate -L 15G -n lv1 vg1
Logical volume "lv1" created.
注:因为逻辑卷相当于是分区,所以需要进行格式化和挂载才能使用,及自启动
[root@xxx ~]# :mkfs.xfs /dev/vg1/lv1
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=983040 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=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 log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
You have new mail in /var/spool/mail/root
[root@xxx ~]# :mkdir /disk/guazai5
[root@xxx ~]# :mount /dev/vg1/lv1 /disk/guazai5
[root@xxx ~]# :df -h | grep movie
You have new mail in /var/spool/mail/root
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1 15G 33M 15G 1% /disk/guazai5
[root@xxx ~]# :echo "/dev/vg1/lv1 /disk/guazai5 xfs defaults 0 0" >> /etc/fstab
[root@xxx ~]# :mount -a
[root@xxx ~]# :df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 2.7G 15G 16% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sdc1 20G 483M 20G 3% /disk/guazai4
/dev/sdb1 10G 33M 10G 1% /disk/guazai3
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 15G 33M 15G 1% /disk/guazai5
格式:
lvs 是lvscan的简写
lvdisplay
[root@xxx ~]# :lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv1 vg1 -wi-ao---- 15.00g
You have new mail in /var/spool/mail/root
[root@xxx ~]# :lvdisplay
--- Logical volume ---
LV Path /dev/vg1/lv1
LV Name lv1
VG Name vg1
LV UUID Oxd3vD-Q0wD-QHb0-bf6o-3TEJ-VV08-DI01U9
LV Write Access read/write
LV Creation host, time xxx, 2020-04-22 23:42:51 -0400
LV Status available
# open 1
LV Size 15.00 GiB
Current LE 7680
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID AmBZcm-EgKm-4Lx7-WSUf-KYvH-CtEO-zpIHjz
LV Write Access read/write
LV Creation host, time localhost, 2020-02-27 23:30:51 -0500
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID ZzVT9H-lHCv-lbjh-xsMU-uDM9-17Yp-ThoiF9
LV Write Access read/write
LV Creation host, time localhost, 2020-02-27 23:30:51 -0500
LV Status available
# open 1
LV Size <17.00 GiB
Current LE 4351
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
注意:CentOS6.x中修改lv及文件系统的大小,必须先将lv及文件系统卸载(umount)CentOS7.x可以不用卸载
格式:
lvextend -L +10G /dev/vg1/lv1 先增加lv的大小
C7:xfs_growfs /dev/vg1/lv1 再修改文件系统的大小
C6:resize2fs -f /dev/vg1/lv1 再修改文件系统的大小
3.3.1先卸载,增加lv容量
[root@xxx ~]# :umount /dev/mapper/vg1-lv1
You have new mail in /var/spool/mail/root
[root@xxx ~]# :df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 2.7G 15G 16% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sdc1 20G 483M 20G 3% /disk/guazai4
/dev/sdb1 10G 33M 10G 1% /disk/guazai3
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@xxx ~]# :lvextend -L +5G /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 15.00 GiB (7680 extents) to 20.00 GiB (10240 extents).
Logical volume vg1/lv1 successfully resized.
You have new mail in /var/spool/mail/root
[root@xxx ~]# :mount /dev/vg1/lv1 /disk/guazai5
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1 15G 33M 15G 1% /disk/guazai5
卸载挂载点后,我们增加lv大小后,我们挂载查看,实际容量并未增加,所以
3.3.2修改文件系统的大小
[root@xxx ~]# :xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 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 5242880
You have new mail in /var/spool/mail/root
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1 20G 33M 20G 1% /disk/guazai5
就可以查看到容量大小已经更新(15G+5G变成20G)
注意:修改lv及文件系统的大小,必须先将lv及文件系统卸载(umount)
注意:做好备份
格式:
lvreduce -L +10G /dev/vg1/lv1 指定分区减小多多大
mkfs.xfs -f /dev/vg1/lv1 再重新格式化 ,挂载,查看
注意:xfs文件系统默认只支持扩容,不支持缩小,so,无法真正做到无损…(因为需要重新格式化)
3.4.1 首先卸载挂载点,指定减小的分区大小
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1 20G 33M 20G 1% /disk/guazai5
You have new mail in /var/spool/mail/root
[root@xxx ~]# :umount /dev/mapper/vg1-lv1
You have new mail in /var/spool/mail/root
[root@xxx ~]# :lvreduce -L 5G /dev/mapper/vg1-lv1
WARNING: Reducing active logical volume to 5.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: Y
Size of logical volume vg1/lv1 changed from 20.00 GiB (10240 extents) to 5.00 GiB (2560 extents).
Logical volume vg1/lv1 successfully resized.
3.4.2 强制重新格式化,挂载,查看
[root@xxx ~]# :mkfs.xfs -f /dev/mapper/vg1-lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
You have new mail in /var/spool/mail/root
[root@xxx ~]# :mount /dev/vg1/lv1 /disk/guazai5
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1 5.0G 33M 5.0G 1% /disk/guazai5
到这里,我们查看的容量大小就变成5G了。
注意:把备份恢复