LVM逻辑卷管理

时间:2023-03-21
本文章向大家介绍LVM逻辑卷管理,主要内容包括LVM原理、LVM优点、LVM配置流程、创建LVM案例、删除逻辑卷、使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

LVM原理

LVM是通过交换PE的方式,达到弹性变更文件系统大小的

  • 剔除原本LV中的PE,就可以减少LV的容量
  • 把其他PE添加到LV,就可以扩容LV容量
  • 一般默认PE大小是4M,LVM最多有65534个PE,所以LVM最大的VG是256G单位
  • PE是LVM最小的存存储单位,类似文件系统的block单位,因此PE大小影响VG容量
  • LV如同/dev/sd[a-z]的分区概念

LVM优点

  • 文件系统可以跨多个磁盘,大小不会受到磁盘限制
  • 可在系统运行的情况下,动态扩展文件系统大小
  • 可以增加新的磁盘到LVM的存储池中

LVM配置流程

  1. 物理分区阶段:将物理磁盘fdisk格式化修改System ID为LVM标记(8e)
  2. PV阶段:通过pvcreatepvdisplay将Linux分区处理为物理卷PV
  3. VG阶段:接下来通过vgcreatevgdisplay将创建好的物理卷PV处理为卷组VG
  4. LV阶段:通过lvcreate将卷组分成若干个逻辑卷LV
  5. 开始使用:通过mkfs对LV格式化,最后挂载LV使用

创建LVM案例

  • 选择两块硬盘,创建pv

[root@junwu_server ~]# pvcreate /dev/sdd /dev/sdf
Physical volume "/dev/sdd" successfully created.
Physical volume "/dev/sdf" successfully created.

##检查下pv,pvscan、pvdisplay

[root@junwu_server ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdd lvm2 --- 20.00g 20.00g
/dev/sde lvm2 --- 20.00g 20.00g
/dev/sdf lvm2 --- 20.00g 20.00g
/dev/sdh lvm2 --- 20.00g 20.00g
[root@junwu_server ~]# pvdisplay
"/dev/sdf" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdf
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 10bnwq-NDNn-win7-Scij-D5iB-6RDs-ITuR9u

"/dev/sdh" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdh
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Zo7ZtH-SA6k-e3x9-SJ6D-y1A9-ZBWD-zXrlCc

"/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 wHefLK-0PYy-Zspy-lQS2-PzJ2-4Jrx-JWt1Xr

"/dev/sdd" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdd
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID TTs2iF-FHT2-3W9u-oYs8-3hFs-dP3i-mFXlXk

  •  创建pv后,就可以创建卷组了,格式:vgcreate 卷组名 pv pv

[root@junwu_server ~]# vgcreate first_vg /dev/sdd /dev/sdf
Volume group "first_vg" successfully created

##检查下vg,vgscan、vgdisplay

[root@junwu_server ~]# vgscan
Reading volume groups from cache.
Found volume group "first_vg" using metadata type lvm2
[root@junwu_server ~]# vgdisplay
--- Volume group ---
VG Name first_vg
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 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0
Free PE / Size 10238 / 39.99 GiB
VG UUID IUaU7A-z5F7-fSIo-ORUk-cQE9-guiY-nbLndA

  • 扩展、缩减卷组vg

##添加新的pv

[root@junwu_server ~]# pvcreate /dev/sdh
Physical volume "/dev/sdh" successfully created.

##扩展卷组

[root@junwu_server ~]# vgextend first_vg /dev/sdh
Volume group "first_vg" successfully extended

[root@junwu_server ~]# vgdisplay
--- Volume group ---
VG Name first_vg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size <59.99 GiB
PE Size 4.00 MiB
Total PE 15357
Alloc PE / Size 0 / 0
Free PE / Size 15357 / <59.99 GiB
VG UUID IUaU7A-z5F7-fSIo-ORUk-cQE9-guiY-nbLndA

##缩减卷组,移除一个物理卷pv

[root@junwu_server ~]# vgreduce first_vg /dev/sdh
Removed "/dev/sdh" from volume group "first_vg"

##移除pv物理卷

[root@junwu_server ~]# pvremove /dev/sdh
Labels on physical volume "/dev/sdh" successfully wiped.

  • 根据first_vg创建逻辑卷

[root@junwu_server ~]# lvcreate -n lv_omg -L 20G first_vg
Logical volume "lv_omg" created.
[root@junwu_server ~]# lvdisplay
--- Logical volume ---
LV Path /dev/first_vg/lv_omg
LV Name lv_omg
VG Name first_vg
LV UUID 1ziwbH-XzYb-DslH-jB1y-Qice-jMyp-FOKLzN
LV Write Access read/write
LV Creation host, time junwu_server, 2023-03-20 10:51:14 -0400
LV Status available
# open 0
LV Size 20.00 GiB
Current LE 5120
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

  • 对逻辑卷进行格式化,然后挂载使用

[root@junwu_server ~]# mkfs.xfs /dev/first_vg/lv_omg
meta-data=/dev/first_vg/lv_omg isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, 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

##挂载

[root@junwu_server ~]# mkdir /mnt/lvm_test
[root@junwu_server ~]# mount /dev/first_vg/lv_omg /mnt/lvm_test/

##检查分区使用情况

[root@junwu_server ~]# df -Th /mnt/lvm_test/
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/first_vg-lv_omg xfs 20G 33M 20G 1% /mnt/lvm_test

  • 逻辑卷LVM扩容

##检查下vg还有多少空间

[root@junwu_server ~]# vgs8
VG #PV #LV #SN Attr VSize VFree
first_vg 2 1 0 wz--n- 39.99g 19.99g

[root@junwu_server ~]# lvextend -L +5G /dev/first_vg/lv_omg
Size of logical volume first_vg/lv_omg changed from 20.00 GiB (5120 extents) to 25.00 GiB (6400 extents).
Logical volume first_vg/lv_omg successfully resized.

##查看挂载文件使用情况,发现还是20G,并没有扩容

[root@junwu_server ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 476M 0 476M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 7.7M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda3 xfs 20G 2.6G 17G 14% /
/dev/sda1 xfs 197M 110M 88M 56% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/first_vg-lv_omg xfs 20G 33M 20G 1% /mnt/lvm_test

##使用xfs_growfs扩展xfs文件系统

[root@junwu_server ~]# xfs_growfs /dev/first_vg/lv_omg
meta-data=/dev/mapper/first_vg-lv_omg isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, 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 5242880 to 6553600

删除逻辑卷

搭建LVM是先pv

      vg

      lv

删除LVM相反先lv

       vg

       lv

  • 取消挂载

[root@junwu_server ~]# umount /dev/first_vg/lv_omg

  • 删除逻辑卷lv

[root@junwu_server ~]# lvremove /dev/first_vg/lv_omg
Do you really want to remove active logical volume first_vg/lv_omg? [y/n]: yes
Logical volume "lv_omg" successfully removed

[root@junwu_server ~]# lvs
[root@junwu_server ~]# lvdisplay

  • 删除卷组vg

[root@junwu_server ~]# vgremove /dev/first_vg
Volume group "first_vg" successfully removed
[root@junwu_server ~]# vgs
[root@junwu_server ~]# vgdisplay

  • 删除物理卷pv

[root@junwu_server ~]# pvremove /dev/sd[d-h]
No PV found on device /dev/sdh.
Device /dev/sdg excluded by a filter.
Labels on physical volume "/dev/sdd" successfully wiped.
Labels on physical volume "/dev/sde" successfully wiped.
Labels on physical volume "/dev/sdf" successfully wiped.

[root@junwu_server ~]# pvs
[root@junwu_server ~]# pvdisplay

原文地址:https://www.cnblogs.com/junwured/p/17237989.html